# Applying Forces to Box2D Bodies

In full disclosure, I am VERY new to box2D let alone the Java version of it.

I have a box (here of type Hero) that I am trying to move. I did this by trying to apply a force as follows...

private void itterate() {
Vec2 vec = hero.getPosition();
hero.applyForce(new Vec2(0, 100), hero.getPosition());
Vec2 vec2 = hero.getPosition();
String test = "TEst";
}


The problem I am seeing is this doesn't change the poistion so vec == vec2. I also tried this....

private void itterate() {
Vec2 vec = hero.getPosition();
hero.applyForce(new Vec2(0, 100), hero.getWorldCenter());
Vec2 vec2 = hero.getPosition();
String test = "TEst";
}


Neither seemed to do what I am wanting. How can I "push" the object toward the bottom of the screen (Increase y)?

UPDATE I tried this as well...

private void itterate(int count) {
Vec2 vec = hero.getPosition();
Vec2 vec1 = hero.getLinearVelocity();

int velocityIterations = 6;
int positionIterations = 2;

world.setContinuousPhysics(true);

hero.applyForce(new Vec2(0, 10000), hero.getWorldCenter());
world.step(timeStep, velocityIterations, positionIterations);
Vec2 vec2 = hero.getPosition();
String test = "TEst";
}


What you have here is a misunderstanding of how physics engines work.

When a force is applied, it is merely set as some state indicating that the force was applied. The actual movement does not yet happen. The physics engine needs to integrate the physics with a time step, do collision checks, and then resolve those collisions (which can apply more impulses to the objects, causing them to move in other ways), and it does this a few to try to get the physics simulation to stabilize as best as possible.

Short version: physics engines don't apply forces immediately, they integrate them in whole when the simulation is advanced.

In Box2D, this is done with the Step method on the World object. (I'm assuming the class and methods names in jbox2d are identical or very similar to those in the main C++ Box2D.)

http://www.box2d.org/manual.html#_Toc258082976

• Thanks, but, I actually tried integrating the step and that didn't help either, but maybe I did it wrong. See update... – Jackie Dec 30 '12 at 13:24
• In your sample, you're applying the force after stepping the simulation. So you get the position with no force applied, step (which will have no effect, as no force is applied), apply a force, then get the position (which will not yet be affected by the force), so both positions will be equal to each other. – Sean Middleditch Dec 31 '12 at 1:17
• Ok I added it afterwards and I still get the same response... Updating update :-) Also I included the intervals in case those are off. I was wondering if it has anything todo with the way I am getting the position or something. – Jackie Dec 31 '12 at 3:08
• Also this is what I am loosely basing this on.... code.google.com/p/jbox2d/source/browse/trunk/jbox2d-testbed/src/… – Jackie Dec 31 '12 at 3:27

Using the previous answer here is what I came up with to get it to work (In Java)....

...
Vec2 gravity = new Vec2(0, 0f);
world = new World(gravity, true);
world.setContactListener(this);
groundBody = world.createBody(new BodyDef());
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(10f, 20f);
bodyDef.angle = 0;
test = world.createBody(bodyDef);
PolygonShape dynamicBox = new PolygonShape();
dynamicBox.setAsBox(5,  5);
FixtureDef fd = new FixtureDef();
fd.shape = dynamicBox;
fd.density = 20f;
fd.restitution = 0;
return test.createFixture(fd);

...
Inside Loop
...

float timeStep = 1.0f / 60.0f;
int velocityIterations = 6;
int positionIterations = 2;
test..setLinearVelocity(new Vec2(0, 100));
world.step(timeStep, velocityIterations, positionIterations);


This seemed to work well for me