Advertisement
Blagojee

PlSim

Apr 14th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import com.badlogic.gdx.ApplicationAdapter;
  8. import com.badlogic.gdx.Gdx;
  9. import com.badlogic.gdx.Input;
  10. import com.badlogic.gdx.graphics.Color;
  11. import com.badlogic.gdx.graphics.OrthographicCamera;
  12. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  13. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  14. import com.badlogic.gdx.math.Vector3;
  15.  
  16. public class Entity extends ApplicationAdapter{
  17.     private ShapeRenderer shapeRenderer;
  18.     private OrthographicCamera camera;
  19.     private Controller c;
  20.     private Vector3 mouse;
  21.    
  22.     private int step = 1;
  23.    
  24.     public Entity(Controller c){
  25.         shapeRenderer = new ShapeRenderer();
  26.         camera = new OrthographicCamera(800, 600);
  27.         camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  28.         /*camera.position.x = Gdx.graphics.getWidth()/2;
  29.         camera.position.y = Gdx.graphics.getHeight()/2;*/
  30.         mouse = new Vector3(0, 0, 0);
  31.         this.c = c;
  32.         System.out.println("[ + ] Entity Spawned");
  33.     }
  34.    
  35.     private boolean collision(double x1, double y1, double r1, double x2, double y2, double r2){
  36.         double dx = x1* c.scale - x2* c.scale;
  37.         double dy = y1* c.scale - y2* c.scale;
  38.         double dist = Math.sqrt(dx * dx + dy * dy);
  39.         if(dist < r1/2 + r2/2){
  40.             return true;
  41.         } else return false;
  42.     }
  43.    
  44.     private double normalize(double x){
  45.         return (((x - -16000)) * 800) / 32000;
  46.     }
  47.    
  48.     public void render(){
  49.         double timestep = 24 * 3600;
  50.         step++;
  51.        
  52.         shapeRenderer.setProjectionMatrix(camera.combined);
  53.         mouse.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  54.         camera.unproject(mouse);
  55.         camera.update();
  56.        
  57.         // Spawning objects on click, left for light body, right for the body with the mass of the sun
  58.         if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
  59.             Body x = new Body();
  60.             x.name = "Temp";
  61.             x.mass = 1.302 * Math.pow(10, 13);
  62.             x.size = 5;
  63.             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
  64.             x.py = normalize((mouse.y + Gdx.graphics.getHeight()/2 - camera.viewportHeight/2) * c.AU);
  65.             x.color = Color.GRAY;
  66.             x.created = true;
  67.         }
  68.         if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
  69.             Body x = new Body();
  70.             x.name = "Temp";
  71.             x.mass = 1.98892 * Math.pow(10, 30);
  72.             x.size = 2;
  73.             x.px = normalize((mouse.x - Gdx.graphics.getWidth()/2 + camera.viewportWidth/2) * c.AU);
  74.             x.py = normalize((mouse.y + Gdx.graphics.getHeight()/2 - camera.viewportHeight/2) * c.AU);
  75.             c.bodies.add(x);
  76.         }
  77.        
  78.         // Camera movement
  79.         if(Gdx.input.isKeyPressed((Input.Keys.D))){ camera.translate(3, 0);  camera.update(); }
  80.         if(Gdx.input.isKeyPressed((Input.Keys.A))){ camera.translate(-3, 0); camera.update(); }
  81.         if(Gdx.input.isKeyPressed((Input.Keys.W))){ camera.translate(0, 3);  camera.update(); }
  82.         if(Gdx.input.isKeyPressed((Input.Keys.S))){ camera.translate(0, -3); camera.update(); }
  83.         if(Gdx.input.isKeyPressed((Input.Keys.SHIFT_LEFT))){ if(camera.zoom > 0.1){ camera.zoom -= 0.03f; camera.update(); }}
  84.         if(Gdx.input.isKeyPressed((Input.Keys.SPACE))){ camera.zoom += 0.05f; camera.update(); }
  85.        
  86.         List<BodyForce> force = new ArrayList<BodyForce>();
  87.         for(Body body : c.bodies){
  88.             double totalFx = 0.0;
  89.             double totalFy = 0.0;
  90.             for(Body other : c.bodies){
  91.                 if(body != other){
  92.                     double[] fxy = body.attraction(other);
  93.                     double fx = fxy[0];
  94.                     double fy = fxy[1];
  95.                     totalFx += fx;
  96.                     totalFy += fy;
  97.                 }
  98.             }
  99.             force.add(new BodyForce(body, totalFx, totalFy));
  100.         }
  101.        
  102.         List<Body> temp = new ArrayList<Body>();
  103.         for(Body body : c.bodies){
  104.             double fx = 0;
  105.             double fy = 0;
  106.             for(BodyForce f : force){
  107.                 if(body == f.id){
  108.                     fx = f.fx;
  109.                     fy = f.fy;
  110.                 }
  111.             }
  112.             body.vx += fx / body.mass * timestep;
  113.             body.vy += fy / body.mass * timestep;
  114.             body.px += body.vx * timestep;
  115.             body.py += body.vy * timestep;
  116.            
  117.             // Collision detection
  118.             for(Body other : c.bodies){
  119.                 if(body != other){
  120.                     if(collision(body.px, body.py, body.size, other.px, other.py, other.size)){
  121.                         if(body.size <= 10 && other.size == 1){
  122.                             body.size++;
  123.                             body.size += other.size;
  124.                             temp.add(other);
  125.                         } else if(body.size >= 3 || other.size >= 3){
  126.                             if(Math.abs(body.mass * (body.vx + body.vy)) > Math.abs(other.mass * (other.vx + other.vy))){
  127.                                 temp.add(other);
  128.                             } else if(Math.abs(body.mass * (body.vx + body.vy)) < Math.abs(other.mass * (other.vx + other.vy))){
  129.                                 temp.add(body);
  130.                             }
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.            
  136.             shapeRenderer.begin(ShapeType.Filled);
  137.             shapeRenderer.setColor(body.color);
  138.             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);
  139.             shapeRenderer.end();
  140.         }
  141.        
  142.         // Deleting all bodies that where destroyed in collision
  143.         c.bodies.removeAll(temp);
  144.     }
  145.  
  146.     private void printInfo() {
  147.         System.out.println("Step #" + step);
  148.         for(Body body : c.bodies){
  149.             System.out.println(body.name + ": Pos.= " + body.px/c.AU+ ", " + body.py/c.AU + " Vel.= " + body.vx + ", " + body.vy);
  150.         } System.out.println("---------------------------");
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement