Box2D collision with mouse position

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?


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

if (mousePressed) {
} 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)

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

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

1 Like