Advertisement
dermetfan

intergrated draw a ChainShape in Box2D for GreenLittleApple

Nov 26th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.me.game.screens;
  2.  
  3. import com.badlogic.gdx.Game;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.InputAdapter;
  6. import com.badlogic.gdx.InputMultiplexer;
  7. import com.badlogic.gdx.Screen;
  8. import com.badlogic.gdx.Input.Keys;
  9. import com.badlogic.gdx.graphics.GL20;
  10. import com.badlogic.gdx.graphics.OrthographicCamera;
  11. import com.badlogic.gdx.graphics.g2d.Sprite;
  12. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  13. import com.badlogic.gdx.math.MathUtils;
  14. import com.badlogic.gdx.math.Vector2;
  15. import com.badlogic.gdx.math.Vector3;
  16. import com.badlogic.gdx.physics.box2d.Body;
  17. import com.badlogic.gdx.physics.box2d.BodyDef;
  18. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  19. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  20. import com.badlogic.gdx.physics.box2d.ChainShape;
  21. import com.badlogic.gdx.physics.box2d.FixtureDef;
  22. import com.badlogic.gdx.physics.box2d.World;
  23. import com.badlogic.gdx.utils.Array;
  24. import com.badlogic.gdx.utils.FloatArray;
  25. import com.me.game.entities.Car;
  26.  
  27. public class Play implements Screen {
  28.  
  29.     private World world;
  30.     private Box2DDebugRenderer debugRenderer;
  31.     private OrthographicCamera camera;
  32.     private SpriteBatch batch;
  33.     private final float timeStep = 1 / 60f;
  34.     private final int velocityIterations = 8, positionIterations = 3;
  35.     private Body ground;
  36.     private Car car;
  37.     private Array<Body> tmpBodies = new Array<Body>();
  38.     private boolean drawing;
  39.     private FloatArray coords = new FloatArray();
  40.     private Vector3 tmp = new Vector3();
  41.  
  42.     @Override
  43.     public void render(float delta) {
  44.         Gdx.gl.glClearColor(0, 0, 0, 1);
  45.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  46.  
  47.         world.step(timeStep, velocityIterations, positionIterations);
  48.         //      camera.position.set(box.getPosition().x, box.getPosition().y, 0);
  49.  
  50.         camera.update();
  51.  
  52.         batch.setProjectionMatrix(camera.combined);
  53.         batch.begin();
  54.         world.getBodies(tmpBodies);
  55.         for(Body body : tmpBodies)
  56.             if(body.getUserData() != null && body.getUserData() instanceof Sprite) {
  57.                 Sprite sprite = (Sprite) body.getUserData();
  58.                 sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
  59.                 sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
  60.                 sprite.draw(batch);
  61.             }
  62.         batch.end();
  63.         debugRenderer.render(world, camera.combined);
  64.     }
  65.  
  66.     @Override
  67.     public void resize(int width, int height) {
  68.         camera.viewportWidth = width / 50;
  69.         camera.viewportHeight = height / 50;
  70.         camera.update();
  71.     }
  72.  
  73.     @Override
  74.     public void show() {
  75.         batch = new SpriteBatch();
  76.         world = new World(new Vector2(0, -9.81f), true);
  77.         debugRenderer = new Box2DDebugRenderer();
  78.         camera = new OrthographicCamera();
  79.  
  80.         final BodyDef bodyDef = new BodyDef();
  81.         final FixtureDef fixtureDef = new FixtureDef(), wheelFixtureDef = new FixtureDef();
  82.  
  83.         // CAR
  84.         fixtureDef.density = 5;
  85.         fixtureDef.friction = .4f;
  86.         fixtureDef.restitution = .3f;
  87.  
  88.         wheelFixtureDef.density = fixtureDef.density * 8;
  89.         wheelFixtureDef.friction = 5000;
  90.         wheelFixtureDef.restitution = .4f;
  91.         car = new Car(world, fixtureDef, wheelFixtureDef, 0, 3, 3, 1);
  92.         Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {
  93.             @Override
  94.             public boolean keyDown(int keycode) {
  95.                 switch(keycode) {
  96.                 case Keys.ESCAPE:
  97.                     ((Game) Gdx.app.getApplicationListener()).setScreen(new Levels());
  98.                     break;
  99.                 }
  100.                 return false;
  101.             }
  102.  
  103.             @Override
  104.             public boolean touchDragged(int screenX, int screenY, int pointer) {
  105.                 if(drawing) {
  106.                     tmp.x = screenX;
  107.                     tmp.y = screenY;
  108.                     camera.unproject(tmp);
  109.                     coords.add(tmp.x);
  110.                     coords.add(tmp.y);
  111.                     return true;
  112.                 }
  113.                 return false;
  114.             }
  115.  
  116.             @Override
  117.             public boolean scrolled(int amount) {
  118.                 camera.zoom += amount / 15f;
  119.                 return true;
  120.             }
  121.  
  122.             @Override
  123.             public boolean keyTyped(char character) {
  124.                 switch(character) {
  125.                 case 'c':
  126.                     if(drawing) // drawing was enabled, so this will disable it and create the ChainShape
  127.                         createChain();
  128.                     drawing = !drawing; // toggle
  129.                     return true;
  130.                 default:
  131.                     return false;
  132.                 }
  133.             }
  134.  
  135.             public Body createChain() {
  136.                 ChainShape shape = new ChainShape();
  137.                 coords.shrink(); // reduce inner array to make sure it's not longer than needed
  138.                 shape.createChain(coords.items);
  139.  
  140.                 fixtureDef.shape = shape;
  141.                 // SETUP THE OTHER FIXTURE PROPERTIES HERE
  142.                
  143.                 Body body = world.createBody(bodyDef);
  144.                 body.createFixture(fixtureDef);
  145.                 return body;
  146.             }
  147.  
  148.         }, car));
  149.         // GROUND
  150.         bodyDef.type = BodyType.StaticBody;
  151.         bodyDef.position.set(0, 0);
  152.  
  153.         // ground shape
  154.         ChainShape groundShape = new ChainShape();
  155.         groundShape.createChain(new Vector2[] {new Vector2(-50000, 0), new Vector2(50000, 0)});
  156.  
  157.         // fixture definition
  158.         fixtureDef.shape = groundShape;
  159.         fixtureDef.friction = 10;
  160.         fixtureDef.restitution = 0f;
  161.  
  162.         ground = world.createBody(bodyDef);
  163.         ground.createFixture(fixtureDef);
  164.         groundShape.dispose();
  165.  
  166.     }
  167.  
  168.     @Override
  169.     public void hide() {
  170.         dispose();
  171.     }
  172.  
  173.     @Override
  174.     public void pause() {
  175.     }
  176.  
  177.     @Override
  178.     public void resume() {
  179.     }
  180.  
  181.     @Override
  182.     public void dispose() {
  183.         world.dispose();
  184.         debugRenderer.dispose();
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement