# libGDX Box2d ApplyTorque() funktioniert nicht



## temi (2. Nov 2017)

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?

```
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();
    }
}
```


```
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:

```
@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);
    }
```


----------



## temi (2. Nov 2017)

Ok, ich habe den Fehler gefunden. Der Code war teilweise von einem Tutorial herauskopiert und ist hier (zumindest für meinen Fall) fehlerhaft:

```
FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 1f; // Masse

        body.createFixture(shape, 0.0f); // verwendet die fixtureDef nicht und überschreibt die Masse mit Null.
```
Ohne Masse kein Drehmoment.


----------

