Hallo zusammen,
ich experimentiere gerade etwas mit Box2d herum und möchte den Körper rotieren. Mit den auskommentierten Zeilen funktioniert es, mit applyTorque() nicht. Was mache ich falsch?
Zur Vervollständigung noch die render Methode:
ich experimentiere gerade etwas mit Box2d herum und möchte den Körper rotieren. Mit den auskommentierten Zeilen funktioniert es, mit applyTorque() nicht. Was mache ich falsch?
Java:
public class PhysicsModel
{
private final World world = new World(new Vector2(0,0), true);
private Body body;
private final KeyboardController keyboardController;
private float torque = 0;
public PhysicsModel(final KeyboardController controller)
{
this.keyboardController = controller;
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(0,0);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2,1);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
body.createFixture(shape, 0.0f);
shape.dispose();
}
public World getWorld()
{
return world;
}
public void step(float delta)
{
if(keyboardController.isLeft())
{
torque += 0.1f;
//body.setTransform(body.getPosition(), body.getAngle() + 0.01f);
}
if(keyboardController.isRight())
{
torque -= 0.1f;
//body.setTransform(body.getPosition(), body.getAngle() - 0.01f);
}
if(keyboardController.isUp())
{
Vector2 vec = new Vector2((float)Math.cos(body.getAngle()), (float)Math.sin(body.getAngle()));
body.applyForceToCenter(vec, true);
}
body.applyTorque(torque, true);
world.step(delta, 3, 3);
}
@Override
protected void finalize() throws Throwable
{
world.dispose();
super.finalize();
}
}
Java:
public class PhysicsModel
{
private final World world = new World(new Vector2(0,0), true);
private Body body;
private final KeyboardController keyboardController;
private float torque = 0;
public PhysicsModel(final KeyboardController controller)
{
this.keyboardController = controller;
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(0,0);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2,1);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
body.createFixture(shape, 0.0f);
shape.dispose();
}
public World getWorld()
{
return world;
}
public void step(float delta)
{
if(keyboardController.isLeft())
{
torque += 0.1f;
//body.setTransform(body.getPosition(), body.getAngle() + 0.01f);
}
if(keyboardController.isRight())
{
torque -= 0.1f;
//body.setTransform(body.getPosition(), body.getAngle() - 0.01f);
}
if(keyboardController.isUp())
{
Vector2 vec = new Vector2((float)Math.cos(body.getAngle()), (float)Math.sin(body.getAngle()));
body.applyForceToCenter(vec, true);
}
body.applyTorque(torque, true);
world.step(delta, 3, 3);
}
@Override
protected void finalize() throws Throwable
{
world.dispose();
super.finalize();
}
}
Zur Vervollständigung noch die render Methode:
Java:
@Override
public void render ()
{
physics.step(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(physics.getWorld(), camera.combined);
}