Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import java.util.ArrayList;
- import java.util.List;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
- import com.badlogic.gdx.math.Vector3;
- public class Entity extends ApplicationAdapter{
- private ShapeRenderer shapeRenderer;
- private OrthographicCamera camera;
- private Controller c;
- private Vector3 mouse;
- private int step = 1;
- public Entity(Controller c){
- shapeRenderer = new ShapeRenderer();
- camera = new OrthographicCamera(800, 600);
- camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- /*camera.position.x = Gdx.graphics.getWidth()/2;
- camera.position.y = Gdx.graphics.getHeight()/2;*/
- mouse = new Vector3(0, 0, 0);
- this.c = c;
- System.out.println("[ + ] Entity Spawned");
- }
- private boolean collision(double x1, double y1, double r1, double x2, double y2, double r2){
- double dx = x1* c.scale - x2* c.scale;
- double dy = y1* c.scale - y2* c.scale;
- double dist = Math.sqrt(dx * dx + dy * dy);
- if(dist < r1/2 + r2/2){
- return true;
- } else return false;
- }
- private double normalize(double x){
- return (((x - -16000)) * 800) / 32000;
- }
- public void render(){
- double timestep = 24 * 3600;
- step++;
- shapeRenderer.setProjectionMatrix(camera.combined);
- mouse.set(Gdx.input.getX(), Gdx.input.getY(), 0);
- camera.unproject(mouse);
- camera.update();
- // Spawning objects on click, left for light body, right for the body with the mass of the sun
- if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
- Body x = new Body();
- x.name = "Temp";
- x.mass = 1.302 * Math.pow(10, 13);
- x.size = 5;
- x.px = normalize((mouse.x - Gdx.graphics.getWidth()/2 + camera.viewportWidth/2) * c.AU); // Alg. that is used to determine spawning x and y coordinates
- x.py = normalize((mouse.y + Gdx.graphics.getHeight()/2 - camera.viewportHeight/2) * c.AU);
- x.color = Color.GRAY;
- x.created = true;
- }
- if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
- Body x = new Body();
- x.name = "Temp";
- x.mass = 1.98892 * Math.pow(10, 30);
- x.size = 2;
- x.px = normalize((mouse.x - Gdx.graphics.getWidth()/2 + camera.viewportWidth/2) * c.AU);
- x.py = normalize((mouse.y + Gdx.graphics.getHeight()/2 - camera.viewportHeight/2) * c.AU);
- c.bodies.add(x);
- }
- // Camera movement
- if(Gdx.input.isKeyPressed((Input.Keys.D))){ camera.translate(3, 0); camera.update(); }
- if(Gdx.input.isKeyPressed((Input.Keys.A))){ camera.translate(-3, 0); camera.update(); }
- if(Gdx.input.isKeyPressed((Input.Keys.W))){ camera.translate(0, 3); camera.update(); }
- if(Gdx.input.isKeyPressed((Input.Keys.S))){ camera.translate(0, -3); camera.update(); }
- if(Gdx.input.isKeyPressed((Input.Keys.SHIFT_LEFT))){ if(camera.zoom > 0.1){ camera.zoom -= 0.03f; camera.update(); }}
- if(Gdx.input.isKeyPressed((Input.Keys.SPACE))){ camera.zoom += 0.05f; camera.update(); }
- List<BodyForce> force = new ArrayList<BodyForce>();
- for(Body body : c.bodies){
- double totalFx = 0.0;
- double totalFy = 0.0;
- for(Body other : c.bodies){
- if(body != other){
- double[] fxy = body.attraction(other);
- double fx = fxy[0];
- double fy = fxy[1];
- totalFx += fx;
- totalFy += fy;
- }
- }
- force.add(new BodyForce(body, totalFx, totalFy));
- }
- List<Body> temp = new ArrayList<Body>();
- for(Body body : c.bodies){
- double fx = 0;
- double fy = 0;
- for(BodyForce f : force){
- if(body == f.id){
- fx = f.fx;
- fy = f.fy;
- }
- }
- body.vx += fx / body.mass * timestep;
- body.vy += fy / body.mass * timestep;
- body.px += body.vx * timestep;
- body.py += body.vy * timestep;
- // Collision detection
- for(Body other : c.bodies){
- if(body != other){
- if(collision(body.px, body.py, body.size, other.px, other.py, other.size)){
- if(body.size <= 10 && other.size == 1){
- body.size++;
- body.size += other.size;
- temp.add(other);
- } else if(body.size >= 3 || other.size >= 3){
- if(Math.abs(body.mass * (body.vx + body.vy)) > Math.abs(other.mass * (other.vx + other.vy))){
- temp.add(other);
- } else if(Math.abs(body.mass * (body.vx + body.vy)) < Math.abs(other.mass * (other.vx + other.vy))){
- temp.add(body);
- }
- }
- }
- }
- }
- shapeRenderer.begin(ShapeType.Filled);
- shapeRenderer.setColor(body.color);
- shapeRenderer.ellipse((float)(body.px/50*c.AU*c.scale-camera.viewportWidth*c.scale) - body.size/2, (float)(body.py/50*c.AU*c.scale-camera.viewportHeight*c.scale) - body.size/2, body.size, body.size);
- shapeRenderer.end();
- }
- // Deleting all bodies that where destroyed in collision
- c.bodies.removeAll(temp);
- }
- private void printInfo() {
- System.out.println("Step #" + step);
- for(Body body : c.bodies){
- System.out.println(body.name + ": Pos.= " + body.px/c.AU+ ", " + body.py/c.AU + " Vel.= " + body.vx + ", " + body.vy);
- } System.out.println("---------------------------");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement