I took a look at joints, and after some trial and error, settled on a modified b2MouseJoint to work from.
I created a b2ImpulseJoint which takes and anchor, a direction instead of the target of a mouse joint.
Tried to upload the sources here... but it says .cpp extensions are not allowed.
I'm not familiar with this type of joint. What is the purpose? Do you just need a function on b2Body to apply an impulse? I'm adding that now.
Well... consider an ameoba like cell thing... in a water like environment it's mobility is best described with a direction probably applied at a specific location (which can cause torque)...
It's not really attached to anything else...
Or yesterday I was thinking it's kinda like a rocket engine... if I made a long elongated shape and put a rocket engine at the end it has a constant direction relative to the body...
Okay, you shouldn't simulate things like a rocket with joints. Just use a force. Lets say the force is always along some vector uLocal fixed in the body and it is applied at some point pLocal in the body (relative to the center of mass).
First compute the world values of the vector and point:
b2Vec2 uWorld = body->m_R.Mul(uLocal);
b2Vec2 pWorld = body->m_position + body->m_R.Mul(pLocal);
Now compute the force vector.
b2Vec2 force = forceMagnitude * uWorld;
Compute the lever arm of the force.
b2Vec2 leverArm = pWorld - body->m_position; // == body->m_R.Mul(pLocal)
Now add the force and resulting torque:
body->force += force;
body->torque += b2Cross(leverArm, force);
I'm making an API to simplify these calculations.
You might consider also adding utilities functions from and to body space, e.g.
pBody->TransformVectorToLocal( Vec2& out, const Vec2& v );
pBody->TransformPointToLocal( Vec2& out, const Vec2& p );#
And vice versa. This is self documenting and avoids error imo...
Cheers,
-Dirk
My view is that force fields are application specific. Box2D provides generic functions to apply forces/torques/impulses. There is also an AABB query in b2World that you can use to simulate force fields in a region.
Just updated from source.. you moved lots of stuff 🙂 Broke my impulse joint - which basically implements the force methods you specified, and allows defining the thing on the position of the body... anyhow, had to hack that back in 🙂 Lost the DrawShape and DrawJoint things
Read the documentation...
Have you added said utility functions to make application of these forces easier than defining a joint?