Rotation question

20 views
Skip to first unread message

cryptognome

unread,
Oct 19, 2008, 6:50:24 PM10/19/08
to ode-users
Hello everyone,

I need to apply a correcting torque to an object to cause it to align
with its direction of travel, similar to (say) an arrow. Think of a
character sitting on a ball. I'd like to keep him facing the direction
he's going, with temporary deflections in response to collisions.
(Note: it's actually a 2D simulation and interfering with the sphere's
rotation is not an issue.)

I assume the process would be essentially:

1. Get the body's velocity vector.

2. Get the body's rotation vector, quaternion, or matrix.

3. New rotation = velocity vector * current rotation * restoration
constant

What would be the best way to go about this? I'm kinda a nube here, so
more info is better. "Use the normal and multiply by the inverse of
the quaternion..." is not gonna be much help. A code snippet would be
great.



Many thanks!

Stephen Sinclair

unread,
Oct 20, 2008, 9:38:51 AM10/20/08
to ode-...@googlegroups.com

Hi, I've done a sort of "quaternion spring" by using dQMultiple2().
It works great!

i.e., (assuming target_q contains the desired rotation)

memcpy(current_q, dBodyGetQuaternion(body), sizeof(dReal)*4);
dQMultiply2(q_delta, current_q, target_q);

// Torque is embedded in the delta quaternion
angvel = dBodyGetAngularVel(body);
torque[0] = q_delta[1] * t_stiffness - angvel[0] * t_damping;
torque[1] = q_delta[2] * t_stiffness - angvel[1] * t_damping;
torque[2] = q_delta[3] * t_stiffness - angvel[2] * t_damping;

dBodyAddTorque(body, torque[0], torque[1], torque[2]);


In your case you'll have to calculate target_q according to the velocity vector.
Should also mention, it's useful to make sure the quaternion doesn't
"flip" by taking the dot product:

double dot = current_q[0]*target_q[0] + current_q[1]*target_q[1]
+ current_q[2]*target_q[2] + current_q[3]*target_q[3];

// ensure delta follows the shortest path
if (dot > 0) {
target_q[0] *= -1;
target_q[1] *= -1;
target_q[2] *= -1;
target_q[3] *= -1;
}


I'm using this code to make the object follow a haptic device's end
effector position, ODE does haptics!

Steve

Reply all
Reply to author
Forward
0 new messages