Page 1 of 1

Restrict b2MouseJoint movement to x axis

Posted: Mon Jun 02, 2014 7:21 am
by kyle.bong2@gmail.com
I have set up an object that falls from the top of the screen using box2d gravity. I am now implementing the ability to move the object using a b2MouseJoint as set out in the following article

http://www.raywenderlich.com/28604/how- ... ial-part-1

My question is how can i just effect the x axis movement with the mousejoint and leave gravity to take care of the y axis. Box2D v2.3.1

I have tried using the code in the article to limit the movement but that just stops the object from falling at all.


Any help would be great awesome!

Re: Restrict b2MouseJoint movement to x axis

Posted: Mon Jun 02, 2014 10:22 am
by iforce2d
In b2MouseJoint::SolveVelocityConstraints you could set the y component of 'impulse' to zero:

Code: Select all

// Cdot = v + cross(w, r)
b2Vec2 Cdot = vB + b2Cross(wB, m_rB);
b2Vec2 impulse = b2Mul(m_mass, -(Cdot + m_C + m_gamma * m_impulse));
impulse.y = 0; // <-- add this
This seems to work ok if the mouse joint is being applied at the center of mass of the dragged body, but if it's not, the body will spin (of course) and the spin does not look natural.

Re: Restrict b2MouseJoint movement to x axis

Posted: Mon Jun 02, 2014 4:03 pm
by kyle.bong2@gmail.com
EDIT - nevermind I saw your tut https://www.iforce2d.net/b2dtut/constant-speed


Hi Iforce2d, thanks for the quick reply! Just wondering is there a better way to achieve same effect not using b2MouseJoint?

Thanks!

Re: Restrict b2MouseJoint movement to x axis

Posted: Tue Jun 03, 2014 3:15 am
by iforce2d
I guess you could just apply forces to move the body yourself... this might be useful: http://www.iforce2d.net/b2dtut/constant-speed
If you don't want a constant speed you could vary the desired speed depending on how close the body is to the touch or something... basically you would be making a mouse joint yourself :)