Advertisement
dermetfan

episode 19 test preview

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