# JBox2D moving dynamic object

**URL:** https://discourse.processing.org/t/jbox2d-moving-dynamic-object/14001
**Category:** Libraries
**Created:** [September 15, 2019, 3:21pm UTC](https://discourse.processing.org/t/jbox2d-moving-dynamic-object/14001 "2019-09-15T15:21:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![parmex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/parmex/32/6447_2.png) [@parmex](https://discourse.processing.org/u/parmex)
#### Post date: [September 15, 2019, 3:21pm UTC](https://discourse.processing.org/t/jbox2d-moving-dynamic-object/14001/1 "2019-09-15T15:21:39Z")

</div>

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?  
 ![sdSL94mf0T](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e986ee44556dcef0281652166ea6879d8cee4495.gif)  
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:

```auto
  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;

    pushMatrix();
    translate(pos.x+speed,pos.y);
    //rotate(-a);
    fill(127);
    stroke(0);
    strokeWeight(2);    
    rect(0,0,w,h);
    popMatrix();

```

---

<div class="post-metadata">

### Author: ![Xelo](https://avatars.discourse-cdn.com/v4/letter/x/9fc348/32.png) [@Xelo](https://discourse.processing.org/u/Xelo)
#### Post date: [September 18, 2019, 5:44am UTC](https://discourse.processing.org/t/jbox2d-moving-dynamic-object/14001/2 "2019-09-18T05:44:24Z")

</div>

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:

```auto
translate(pos.x+speed,pos.y);

```

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.

---

<div class="post-metadata">

### Author: ![parmex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/parmex/32/6447_2.png) [@parmex](https://discourse.processing.org/u/parmex)
#### Post date: [September 18, 2019, 8:02am UTC](https://discourse.processing.org/t/jbox2d-moving-dynamic-object/14001/3 "2019-09-18T08:02:25Z")

</div>

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!
