# Box2D collision with mouse position

**URL:** <https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332>\
**Category:** Libraries\
**Created:** [November 10, 2020, 10:25pm UTC](https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332 "2020-11-10T22:25:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chriz96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chriz96/32/18189_2.png) [@Chriz96](https://discourse.processing.org/u/Chriz96)\
**Post date:** [November 10, 2020, 10:25pm UTC](https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332/1 "2020-11-10T22:25:19Z")

</div>

Hello, I was trying to modify the Box2D library example called Collisions and Control, in order to remove the spring behavior and manipulate the box position only with the mouseX and mouseY values without having it bounce all around the screen.

Later I’m planing on using this behavior so i can do similar collisions using simpleOpenNI and a Kinect so users can hit the falling particles displayed on a screen using their hands.

But I haven’t been able to figure it out. When I remove the spring and get the box to use mouse position it loses its collision properties and particles just fall thru it.

Any ideas or suggestions?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 10, 2020, 10:41pm UTC](https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332/2 "2020-11-10T22:41:22Z")

</div>

Hi,

I found that you need to change the update line to update the cube to the mouse position :

```auto
if (mousePressed) {
  spring.update(mouseX,mouseY);
  spring.display();
} else {
  spring.update(mouseX,mouseY); // here
}

```

At first I tried to remove the random movement by commenting out the `FixtureDef` section in the `Box` class but it’s actually the code that add physics to the object. (the particles were falling through it)

---

<div class="post-metadata">

**Author:** ![Chriz96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chriz96/32/18189_2.png) [@Chriz96](https://discourse.processing.org/u/Chriz96)\
**Post date:** [November 10, 2020, 10:44pm UTC](https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332/3 "2020-11-10T22:44:31Z")

</div>

thanks josephh, I’ll test it as soon as I can and I’ll let you know. Thanks for the reply!

---

<div class="post-metadata">

**Author:** ![Chriz96](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chriz96/32/18189_2.png) [@Chriz96](https://discourse.processing.org/u/Chriz96)\
**Post date:** [November 10, 2020, 10:47pm UTC](https://discourse.processing.org/t/box2d-collision-with-mouse-position/25332/4 "2020-11-10T22:47:50Z")

</div>

It worked! haha so simple but is just what I needed, thanks pal.
