Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. package org.leverman;
  2.  
  3. import java.io.File;
  4.  
  5. import org.jbox2d.collision.AABB;
  6. import org.jbox2d.collision.shapes.PolygonShape;
  7. import org.jbox2d.collision.shapes.Shape;
  8. import org.jbox2d.collision.shapes.ShapeType;
  9. import org.jbox2d.common.Transform;
  10. import org.jbox2d.common.Vec2;
  11. import org.jbox2d.dynamics.Body;
  12. import org.jbox2d.dynamics.BodyDef;
  13. import org.jbox2d.dynamics.BodyType;
  14. import org.jbox2d.dynamics.Fixture;
  15. import org.jbox2d.dynamics.FixtureDef;
  16. import org.jbox2d.dynamics.World;
  17. import org.lwjgl.LWJGLException;
  18. import org.lwjgl.opengl.Display;
  19. import org.lwjgl.opengl.DisplayMode;
  20. import org.lwjgl.opengl.GL11;
  21. import org.lwjgl.util.glu.GLU;
  22.  
  23. public class Box2D_LWJGL {
  24.     private final String OS = System.getProperty("os.name").toLowerCase();
  25.    
  26.     final float rad2deg = 180f / (float)Math.PI;
  27.     final float deg2rad = (float)Math.PI / 180f;
  28.    
  29.     // Window
  30.     int w;
  31.     int h;
  32.    
  33.     // Delta calculation stuff
  34.     final float framerate = 60.0f;
  35.     final float dt = 1.0f / framerate;
  36.     final float nanoToSecs = 1.0f / 1000000000.0f;
  37.     long last;
  38.     float accum;
  39.  
  40.     // Fps calculation stuff
  41.     long lastFpsTime;
  42.     int frames;
  43.     double fps;
  44.    
  45.     // Box2d
  46.     World world;
  47.    
  48.     public Box2D_LWJGL() {
  49.         w = 800;
  50.         h = 600;
  51.         last = 0;
  52.         accum = 0.0f;
  53.         lastFpsTime = 0;
  54.         frames = 0;
  55.         fps = 0.0;
  56.     }
  57.    
  58.     public String getNativeName() throws LWJGLException {
  59.         if (OS.indexOf("win") > -1) {
  60.             return "windows";
  61.         } else if (OS.indexOf("linux") > -1) {
  62.             return "linux";
  63.         }
  64.         throw new LWJGLException("Platform '" + OS + "' is not supported!");
  65.     }
  66.    
  67.     private Body createBox(float x, float y, float angle, float hw, float hh, BodyType type) {
  68.         BodyDef def = new BodyDef();
  69.         def.position.set(x, y);
  70.         def.angle = angle * deg2rad;
  71.         def.type = type;
  72.        
  73.         PolygonShape shape = new PolygonShape();
  74.         shape.setAsBox(hw, hh);
  75.        
  76.         FixtureDef fixDef = new FixtureDef();
  77.         fixDef.friction = 1f;
  78.         fixDef.restitution = 0f;
  79.         fixDef.density = 1f;
  80.         fixDef.shape = shape;
  81.        
  82.         Body body = world.createBody(def);
  83.         body.createFixture(fixDef);
  84.         return body;
  85.     }
  86.    
  87.     private void init() {
  88.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  89.         GL11.glClearColor(0f, 0f, 0.2f, 1f);
  90.        
  91.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  92.        
  93.         world = new World(new Vec2(0f, -10f));
  94.         world.setAllowSleep(true);
  95.         world.setContinuousPhysics(true);
  96.        
  97.         final float thickness = 0.025f;
  98.         float bottom = -0.5f + thickness;
  99.        
  100.         createBox(0f, -0.5f, 0f, 1f, thickness, BodyType.STATIC);
  101.         createBox(-0.85f, 0f, 90f, 0.75f, thickness, BodyType.STATIC);
  102.         createBox(0.85f, 0f, 90f, 0.75f, thickness, BodyType.STATIC);
  103.        
  104.         final float rad = 0.05f;
  105.         final float jr = rad * 0.5f;
  106.         final int count = 100;
  107.         float x = 0f, y = bottom + rad * 2f * 1.1f;
  108.         for (int i = 0; i < count; i++) {
  109.             x = -jr + (float)Math.random() * jr * 2f;
  110.             createBox(x, y, 0f, rad, rad, BodyType.DYNAMIC);
  111.             y += rad * 2f * 1.5f;
  112.         }
  113.     }
  114.    
  115.     private void update(float dt) {
  116.         world.step(dt, 6, 3);
  117.     }
  118.    
  119.     private void drawAABB(AABB aabb) {
  120.         float hw = (aabb.upperBound.x - aabb.lowerBound.x) * 0.5f;
  121.         float hh = (aabb.upperBound.y - aabb.lowerBound.y) * 0.5f;
  122.         GL11.glEnable(GL11.GL_BLEND);
  123.         GL11.glColor4f(0f, 1f, 0f, 0.25f);
  124.         GL11.glBegin(GL11.GL_LINE_LOOP);
  125.         GL11.glVertex2f(hw, hh);
  126.         GL11.glVertex2f(-hw, hh);
  127.         GL11.glVertex2f(-hw, -hh);
  128.         GL11.glVertex2f(hw, -hh);
  129.         GL11.glEnd();
  130.         GL11.glDisable(GL11.GL_BLEND);
  131.     }
  132.    
  133.     private void drawShape(Shape shape) {
  134.         if (shape.getType().equals(ShapeType.POLYGON)) {
  135.             PolygonShape polyShape = (PolygonShape) shape;
  136.             int vertexCount = polyShape.getVertexCount();
  137.             Vec2[] vertices = polyShape.getVertices();
  138.             GL11.glColor3f(1f, 1f, 1f);
  139.             GL11.glBegin(GL11.GL_LINE_LOOP);
  140.             for (int i = 0; i < vertexCount; i++) {
  141.                 GL11.glVertex2f(vertices[i].x, vertices[i].y);
  142.             }
  143.             GL11.glEnd();
  144.         }
  145.     }
  146.  
  147.     private void draw(){
  148.         GL11.glLoadIdentity();
  149.         GLU.gluPerspective(45f, (float)Display.getWidth()/Display.getHeight(), 0.1f, 100f);
  150.         GL11.glTranslatef(0f, 0f, -2f);
  151.  
  152.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  153.  
  154.         // Bodies
  155.         Body body = world.getBodyList();
  156.         while (body != null) {
  157.             Transform transform = body.getTransform();
  158.             GL11.glPushMatrix();
  159.             GL11.glTranslatef(transform.p.x, transform.p.y, 0f);
  160.             GL11.glRotatef(transform.q.getAngle() * rad2deg, 0f, 0f, 1f);
  161.             Fixture fixture = body.getFixtureList();
  162.             while (fixture != null) {
  163.                 drawShape(fixture.getShape());
  164.                 fixture = fixture.m_next;
  165.             }
  166.             GL11.glPopMatrix();
  167.             body = body.m_next;
  168.         }
  169.        
  170.         // AABBs
  171.         body = world.getBodyList();
  172.         while (body != null) {
  173.             Transform transform = body.getTransform();
  174.             GL11.glPushMatrix();
  175.             GL11.glTranslatef(transform.p.x, transform.p.y, 0f);
  176.             Fixture fixture = body.getFixtureList();
  177.             while (fixture != null) {
  178.                 AABB aabb = fixture.getAABB(0);
  179.                 drawAABB(aabb);
  180.                 fixture = fixture.m_next;
  181.             }
  182.             GL11.glPopMatrix();
  183.             body = body.m_next;
  184.         }
  185.     }
  186.    
  187.     private void tick() {
  188.         // Fps
  189.         long curFpsTime = System.currentTimeMillis();
  190.         frames++;
  191.         if (curFpsTime > lastFpsTime + 1000) {
  192.             fps = frames * 1000 / (curFpsTime - lastFpsTime);
  193.             Display.setTitle("Game - " + String.format("%3.2f fps", fps));
  194.             lastFpsTime = curFpsTime;
  195.             frames = 0;
  196.         }
  197.  
  198.         // Delta time
  199.         long now = System.nanoTime();
  200.         float delta = (now - last) * nanoToSecs;
  201.         if (delta > 0.25f) {
  202.             delta = 0.25f;
  203.         }
  204.         last = now;
  205.  
  206.         // Accumulate delta time
  207.         accum += dt;
  208.         while (!Display.isCloseRequested() && accum >= dt) {
  209.             update(dt);
  210.             accum -= dt;
  211.         }
  212.  
  213.         // Draw frame
  214.         draw();
  215.     }
  216.  
  217.     public void run() throws LWJGLException {
  218.         System.setProperty("org.lwjgl.librarypath", new File("lib/lwjgl-2.9.1/native/" + getNativeName()).getAbsolutePath());
  219.  
  220.         try {
  221.             Display.setDisplayMode(new DisplayMode(w,h));
  222.             Display.create();
  223.         } catch (LWJGLException e) {
  224.             throw e;
  225.         }
  226.        
  227.         init();
  228.        
  229.         while (!Display.isCloseRequested()) {
  230.             tick();
  231.             Display.update();
  232.             Display.sync(60);
  233.         }
  234.        
  235.         Display.destroy();     
  236.     }
  237.    
  238.     public static void main(String[] args) throws Exception {
  239.         Box2D_LWJGL app = new Box2D_LWJGL();
  240.         app.run();
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement