JBox2D moving dynamic object

Hi, i am using the JBox2D library, I have a problem.
I want to use a little square that is a dynamic object and when i press A or D it moves on left and right; but if i am over the platform it fall like a normal dynamic object. How can I do this?
The movement works but when I fall from the platform the square remain at the same height.
The code I used to move the square is this:

  void Display() {  
    float a= body.getAngle();
    Vec2 pos=box2d.getBodyPixelCoord(body);
    if(keyPressed && key=='d') b.speed+=1;
    if(keyPressed && key=='a') b.speed-=1;


are you actually using jbox2d to set the speed or is speed just a variable ‘added on’ to the jbox 2d coordinates? JBox2d requires you to use functions such as body.setLinearVelocity() and body.applyForce() to interface with the ‘speed’ of the jbox 2d box itself.

if you are just adding the speed to the translate instead of using jbox2d:


you are essentially saying: please draw the box ‘speed’ pixels horizontally from where it should be, hence itll never drop off the platform visually, nor even move horizontally according to jbox2d’s coordinates.


Thank you for the reply and the explanation! it worked!
I used getLInearvelocity and then I set the x component to the speed and works!

1 Like