dermetfan

xeoshow: rope hanging from 2 boxes with ball falling on rope

Jul 30th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import net.dermetfan.libgdx.graphics.FixtureSprite;
  4. import net.dermetfan.someLibgdxTests.Assets;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Screen;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.OrthographicCamera;
  10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  11. import com.badlogic.gdx.math.Vector2;
  12. import com.badlogic.gdx.physics.box2d.Body;
  13. import com.badlogic.gdx.physics.box2d.BodyDef;
  14. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  15. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  16. import com.badlogic.gdx.physics.box2d.CircleShape;
  17. import com.badlogic.gdx.physics.box2d.FixtureDef;
  18. import com.badlogic.gdx.physics.box2d.PolygonShape;
  19. import com.badlogic.gdx.physics.box2d.World;
  20. import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
  21.  
  22. public class Box2DRopeTest implements Screen {
  23.  
  24.     private SpriteBatch batch;
  25.     private Box2DDebugRenderer debugRenderer;
  26.     private OrthographicCamera camera;
  27.     private World world;
  28.  
  29.     @Override
  30.     public void render(float delta) {
  31.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  32.  
  33.         world.step(1 / 60f, 8, 20);
  34.  
  35.         camera.update();
  36.         batch.setProjectionMatrix(camera.combined);
  37.         batch.begin();
  38.         FixtureSprite.draw(batch, world);
  39.         batch.end();
  40.  
  41.         debugRenderer.render(world, camera.combined);
  42.     }
  43.  
  44.     @Override
  45.     public void resize(int width, int height) {
  46.         camera.viewportWidth = width / 25;
  47.         camera.viewportHeight = height / 25;
  48.     }
  49.  
  50.     @Override
  51.     public void show() {
  52.         batch = new SpriteBatch();
  53.         debugRenderer = new Box2DDebugRenderer();
  54.         camera = new OrthographicCamera();
  55.         world = new World(new Vector2(0, -9.81f), true);
  56.  
  57.         BodyDef bodyDef = new BodyDef();
  58.         FixtureDef fixtureDef = new FixtureDef();
  59.  
  60.         // box (the rope will hang from this)
  61.         bodyDef.type = BodyType.StaticBody;
  62.         bodyDef.position.y = 7;
  63.         PolygonShape boxShape = new PolygonShape();
  64.         boxShape.setAsBox(.5f, .5f);
  65.         Body box = world.createBody(bodyDef);
  66.         box.createFixture(boxShape, 0);
  67.  
  68.         // box (the rope will hang from this as well)
  69.         bodyDef.type = BodyType.StaticBody;
  70.         bodyDef.position.set(4, 8);
  71.         PolygonShape boxShape2 = new PolygonShape();
  72.         boxShape2.setAsBox(.1f, .1f);
  73.         Body box2 = world.createBody(bodyDef);
  74.         box2.createFixture(boxShape2, 0);
  75.         boxShape2.dispose();
  76.  
  77.         // rope
  78.         short segments = 20;
  79.         float segWidth = .1f, segHeight = .3f;
  80.  
  81.         // segment definitions
  82.         bodyDef.type = BodyType.DynamicBody;
  83.         bodyDef.position.y = 6;
  84.         PolygonShape segmentShape = new PolygonShape();
  85.         segmentShape.setAsBox(segWidth / 2, segHeight / 2);
  86.         fixtureDef.shape = segmentShape;
  87.         fixtureDef.density = 1;
  88.         fixtureDef.friction = .3f;
  89.         fixtureDef.restitution = .05f;
  90.  
  91.         // revolute joint definitions
  92.         RevoluteJointDef ropeDef = new RevoluteJointDef();
  93.         ropeDef.localAnchorA.set(0, -segHeight / 2); // top
  94.         ropeDef.localAnchorB.set(0, segHeight / 2); // bottom
  95.  
  96.         FixtureSprite segmentSprite = new FixtureSprite(Assets.japanischeFlagge);
  97.  
  98.         // first segment
  99.         Body lastSegment = world.createBody(bodyDef);
  100.         lastSegment.createFixture(fixtureDef).setUserData(segmentSprite);
  101.         ropeDef.bodyA = box;
  102.         ropeDef.bodyB = lastSegment;
  103.         float oldLocalAnchorAY = ropeDef.localAnchorA.y;
  104.         ropeDef.localAnchorA.y = -.5f; // set to the bottom of the box
  105.         world.createJoint(ropeDef);
  106.         ropeDef.localAnchorA.y = oldLocalAnchorAY;
  107.  
  108.         // other segments
  109.         for(int s = 1; s < segments; s++) {
  110.             bodyDef.position.y -= segHeight; // position the segments one under another
  111.             ropeDef.bodyA = lastSegment;
  112.             ropeDef.bodyB = world.createBody(bodyDef);
  113.             ropeDef.bodyB.createFixture(fixtureDef).setUserData(segmentSprite);
  114.             world.createJoint(ropeDef);
  115.             lastSegment = ropeDef.bodyB;
  116.         }
  117.  
  118.         segmentShape.dispose();
  119.  
  120.         // last joint
  121.         ropeDef.bodyA = lastSegment;
  122.         ropeDef.bodyB = box2;
  123.         ropeDef.localAnchorB.y = -.1f;
  124.         world.createJoint(ropeDef);
  125.  
  126.         // ball (will fall on the rope)
  127.         bodyDef.type = BodyType.DynamicBody;
  128.         bodyDef.position.set(1.5f, 10);
  129.         CircleShape ballShape = new CircleShape();
  130.         ballShape.setRadius(.25f);
  131.         fixtureDef.shape = ballShape;
  132.         fixtureDef.restitution = .5f;
  133.         fixtureDef.density = 3;
  134.         fixtureDef.friction = .3f;
  135.         world.createBody(bodyDef).createFixture(fixtureDef).setUserData(new FixtureSprite(Assets.ball));
  136.         ballShape.dispose();
  137.     }
  138.  
  139.     @Override
  140.     public void hide() {
  141.         dispose();
  142.     }
  143.  
  144.     @Override
  145.     public void pause() {
  146.     }
  147.  
  148.     @Override
  149.     public void resume() {
  150.     }
  151.  
  152.     @Override
  153.     public void dispose() {
  154.         batch.dispose();
  155.         debugRenderer.dispose();
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment