Restrict b2MouseJoint movement to x axis

General discussion about Box2D tutorials
Post Reply
kyle.bong2@gmail.com
Posts: 41
Joined: Thu May 29, 2014 5:01 am

Restrict b2MouseJoint movement to x axis

Post 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!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Restrict b2MouseJoint movement to x axis

Post 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.
kyle.bong2@gmail.com
Posts: 41
Joined: Thu May 29, 2014 5:01 am

Re: Restrict b2MouseJoint movement to x axis

Post 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!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Restrict b2MouseJoint movement to x axis

Post 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 :)
Post Reply