# strange behavior in Box2D+LibGDX when applying impulse

I have been playing around with Box2D and LibGDX and have been using a sample code from DecisionTreeGames as the testing ground. Now I have a screen with four walls and a rectangle shape, lets call it a brick. When I use applyLinearImpulse to the brick, it starts bouncing right and left without any pattern and won't stop!

I tried adding friction and increasing the density, but the behavior still remains the same. Here are some of the code that might be useful:

method for applying the impulse:

center = brick.getWorldCenter();
brick.applyLinearImpulse(20, 0, center.x, center.y);


Defining the brick:

brick_bodyDef.type = BodyType.DynamicBody;
brick_bodyDef.position.set(pos); // brick is initially on the ground
brick_bodyDef.angle = 0;
brick_body = world.createBody(brick_bodyDef);
brick_body.setBullet(true);
brick_bodyShape.setAsBox(w,h);
brick_fixtureDef.density = 0.9f;
brick_fixtureDef.restitution = 1;
brick_fixtureDef.shape = brick_bodyShape;
brick_fixtureDef.friction=1;
brick_body.createFixture(fixtureDef);


Walls are defined the same only their bullet value is set to false

I would really appreciate it if you could help me have a change this code to have a realistic behavior (i.e. when I apply impulse to the brick it should trip a few times and then stop completely).

Further info:

1. At first, the brick is standing still.

2. With an impulse to its right side, it slowly leans rightward.

3. the moment it touches the ground wall, it jumps back with a high speed. It is like the ground is a trampoline!!

4. from here on, it bounces very quickly.

5. In these jumps it rarely touches other walls.

First, it doesn't seem like your "brick" needs to be treated like a bullet, so setBullet(false). Now, where in your code are you applying the impulse? Take a close look at your game loop and make sure you're not applying different forces somewhere else. Also, turn on debugging so you can better see what's going on. Maybe your "brick" is bouncing off of other invisible objects making it appear to be moving randomly. Make sure to set all debug flags to true to look at everything.

• I changed it to setBullet(false) and checked the loop, it is in the update function and is the only place that I apply any kind of force. By turning on the debugging, did you mean Box2DDebugRenderer or is there another way? – Arash Sep 3 '12 at 18:28
• I check it with Box2DDebugRenderer, there are no other objects. To elaborate more on its behavior: 1. at first, the brick is standing still. 2. with an impulse from its right side, it slowly leans rightward. 3. the moment it touches the ground wall, it jumps back with a high speed. 4. from here on, it bounces very quickly. 5. In these jumps it rarely touches other walls. It is like the ground is a trampoline!! – Arash Sep 3 '12 at 19:18

It seems that restitution was at fault. When I set it to 0.5 everything worked fine.

• Yes, that would have been my guess also after reading your comment on my question. You should accept your own answer. – Amplify91 Sep 4 '12 at 23:16