Guest User

box2d - libgdx

a guest
Feb 8th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. ///// body construction
  2.         BodyDef bodyDef = new BodyDef();
  3.         // We set our body to dynamic, for something like ground which doesnt move we would set it to StaticBody
  4.         bodyDef.type = BodyType.DynamicBody;
  5.         // Set our body's starting position in the world
  6.         bodyDef.position.set(100, 200);
  7.        
  8.         // Create our body in the world using our body definition
  9.         Body body = world.createBody(bodyDef);
  10.  
  11.         // Create a circle shape and set its radius to 6
  12.         CircleShape circle = new CircleShape();
  13.         circle.setRadius(20f);
  14.  
  15.         // Create a fixture definition to apply our shape to
  16.         FixtureDef fixtureDef = new FixtureDef();
  17.         fixtureDef.shape = circle;
  18.         fixtureDef.density = 500f;
  19.         fixtureDef.friction = 0f;
  20.         fixtureDef.restitution = 1f;
  21.        
  22.         // Create our fixture and attach it to the body
  23.         fixture = body.createFixture(fixtureDef);
  24.  
  25. //// apply force on press of keys
  26.         Vector2 impulse = new Vector2();
  27.         impulse.x = ( Gdx.input.isKeyPressed( Input.Keys.LEFT ) ? - 1000
  28.                 : ( Gdx.input.isKeyPressed( Input.Keys.RIGHT ) ? 1000 : 0 ) );
  29.         impulse.y = ( Gdx.input.isKeyPressed( Input.Keys.UP ) ? 1000
  30.                 : ( Gdx.input.isKeyPressed( Input.Keys.DOWN ) ? - 1000 : 0 ) );
  31.  
  32.  
  33. //// painting using the body pos
  34.     Actor e = (Actor) b.getUserData();
  35.    
  36.     if (e != null) {
  37.         // Update the entities/sprites position and angle
  38.         e.setPosition(b.getPosition().x, b.getPosition().y);
  39.         // We need to convert our angle from radians to degrees
  40.         e.setRotation(MathUtils.radiansToDegrees * b.getAngle());
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment