0
votes

As far as I understand it, this should cause the body to move. I have an update method in the class but I'm not sure what else to add. I am using box2d as part of libgdx. Everywhere I've looked it just says to set the linear velocity, but it isn't working.

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(new Vector2(pos.x, pos.y));
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.linearDamping = 0.0f;
    bodyDef.angularDamping = 0.0f;
    unitBoxBody = world.createBody(bodyDef);

    PolygonShape player = new PolygonShape();
    player.setAsBox(unit * xSize, unit * ySize); 
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = player;

    fixtureDef.density = 0.0f;
    fixtureDef.friction = 0.0f; 
    fixtureDef.restitution = 0.0f;
    fixture = unitBoxBody.createFixture(fixtureDef);
    player.dispose();
    unitBoxBody.setLinearVelocity(10, 0);

I have used numerous large and small values for the velocity with no change.

1
Why fixtureDef.density = 0.0f, this seems kind of odd since if you apply force to a massless object, it would have infinite acceleration. Not too sure how that's handled by Box2D. Also are you calling world.step() in your main loop?XiaoChuan Yu
Add the render(float delta) method or your principal loop methodAlex Sifuentes
@XiaoChuanYu code's just copied from another object I'm using where that doesn't really matter. I'll change it and see if it helps. Yes I am calling step. I have no problems with literally every other object in the program. Edit: changing density does nothing.Dreamtime
@AlexandroSifuentesDíaz this isn't the issue. Every thing else works fine and I can control other types of bodies with no issues.Dreamtime
Have you tried copying only the above code into another blank project and see if the body moves?XiaoChuan Yu

1 Answers

0
votes

You have to make your world step:

Put this in your render() method: world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);

and define These variables:

private static final float TIMESTEP = 1.0f / 60.0f; 
private static final int VELOCITY_ITERATIONS = 8; 
private static final int POSITION_ITERATIONS = 3;