SHOW:
|
|
- or go back to the newest paste.
| 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 | fixture.getBody().applyLinearImpulse(impulse, fixture.getBody().getWorldCenter()); | |
| 33 | ||
| 34 | //// painting using the body pos | |
| 35 | Actor e = (Actor) b.getUserData(); | |
| 36 | ||
| 37 | if (e != null) {
| |
| 38 | // Update the entities/sprites position and angle | |
| 39 | e.setPosition(b.getPosition().x, b.getPosition().y); | |
| 40 | // We need to convert our angle from radians to degrees | |
| 41 | e.setRotation(MathUtils.radiansToDegrees * b.getAngle()); | |
| 42 | } |