I'm just getting started with Box2d and I'm trying to apply an impulse to a circle but nothing really happens.
First I define ball
:
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2);
ball = world.createBody(bodyDef);
CircleShape dynamicCircle = new CircleShape();
dynamicCircle.setRadius(50f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f;
ball.createFixture(fixtureDef);
dynamicCircle.dispose();
then I call this line from inside my inputprocessor
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Gdx.app.log("input", "touchDown");
ball.applyLinearImpulse(new Vector2(0f, 5000f), ball.getPosition(), true);
return true;
}
Here's the render method:
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(1 / 60f, 6, 2);
}
I tried logging this and can confirm the touchDown
event is triggered, but the impulse never applies.
Am I forgetting some call to make this work? What might be going wrong?
world.step
called? \$\endgroup\$50f
-meter radius of thedynamicCircle
just in case the applied force is getting discarded by an approximation. \$\endgroup\$