Hi!
I'm trying to extract the rotation of an object (to determine in what direction it's pointing in) but I'm experiencing some very strange problems.
When I rotate the object (with btSetAngularVelocity) btGetOrientation it behave like this:
0 degrees -> 0.0
90 degrees -> 0.69
135 degrees -> 0.93
180 degrees -> 0.999
-135 degrees -> 0.93
-90 degrees -> -0.69
-0 degrees -> 0.0
The wierdest thing is that after 180 degrees it start to go down again until after -135 degrees when it suddenly change sign.
Why does it behave like that? In what format are the numbers? (In other words, how can I convert the numbers into degrees?)
Thanks in advance!
Edit: I want to take out the y rotation, so i use rot[1], where rot=btGetOrientation(...).
rot[3] looks like it's rot[1] inverse.
Edit again: If I use rot.getAngle() I get correct results for 0<=x<=195, a big improvement. But how do I make it work for the other angles?
Strange values from getOrientation
-
- Posts: 6
- Joined: Wed Jul 23, 2008 9:21 pm
Re: Strange values from getOrientation
I found that getAngle() return the angle in radians.
I still can't find a way to fix it for 180>=x>=360 though.
I still can't find a way to fix it for 180>=x>=360 though.
-
- Posts: 6
- Joined: Wed Jul 23, 2008 9:21 pm
Re: Strange values from getOrientation
I've found a temporary, ugly solution:
angle=rot.getAngle(),
if((abs(rot[1]+rot[3]<1.0) && rot[1]<0.0 && -1.0*rot[1]<rot[3]) || abs(rot[1]+rot[3])<0.0)
angle*=-1.0;
This is so damn ugly I don't think I'll be able to sleep tonight, but it allow me to continue programming my game.
I really would apreciate if someone could tell me what's wrong =)
Thanks
angle=rot.getAngle(),
if((abs(rot[1]+rot[3]<1.0) && rot[1]<0.0 && -1.0*rot[1]<rot[3]) || abs(rot[1]+rot[3])<0.0)
angle*=-1.0;
This is so damn ugly I don't think I'll be able to sleep tonight, but it allow me to continue programming my game.
I really would apreciate if someone could tell me what's wrong =)
Thanks
-
- Posts: 31
- Joined: Tue Mar 04, 2008 1:41 pm
Re: Strange values from getOrientation
getOrientation return a quaternion (btQuaternion). A Quaternion is a 4-D vector that represent (if it is unitary) a rotation around one axis.
If the unitary rotation axis is v, and angle is alpha, the quaternion (q) components are:
q.x=v.x*sin(alpha)
q.y=v.y*sin(alpha)
q.z=v.z*sin(alpha)
q.w=cos(alpha)
when you call q.getAngle() you get then rotation angle around the axis btVector3(q.x, q.y, q.z).Normalize()
I hope this help you to sleep.
Nacho
EDIT: Ups, sorry. The quaternion components were:
q.x=v.x*sin(alpha/2)
q.y=v.y*sin(alpha/2)
q.z=v.z*sin(alpha/2)
q.w=cos(alpha/2)
If the unitary rotation axis is v, and angle is alpha, the quaternion (q) components are:
q.x=v.x*sin(alpha)
q.y=v.y*sin(alpha)
q.z=v.z*sin(alpha)
q.w=cos(alpha)
when you call q.getAngle() you get then rotation angle around the axis btVector3(q.x, q.y, q.z).Normalize()
I hope this help you to sleep.
Nacho
EDIT: Ups, sorry. The quaternion components were:
q.x=v.x*sin(alpha/2)
q.y=v.y*sin(alpha/2)
q.z=v.z*sin(alpha/2)
q.w=cos(alpha/2)
-
- Posts: 6
- Joined: Wed Jul 23, 2008 9:21 pm
Re: Strange values from getOrientation
Thanks a lot Nacho!
-
- Posts: 1
- Joined: Wed Feb 16, 2011 12:35 pm
Re: Strange values from getOrientation
How can I get direction vector from quaternions? I tried to follow this tutorial: http://www.gpwiki.org/index.php/OpenGL: ... t_rotation but I just fail in any possible way.
javax.vecmath is missing vector-quaternion multiplication - I wrote a function based on this tutorial and it just doesn't work.
Does anyone have some neat method of quaternion*vector?
javax.vecmath is missing vector-quaternion multiplication - I wrote a function based on this tutorial and it just doesn't work.
Does anyone have some neat method of quaternion*vector?
-
- Posts: 47
- Joined: Mon Aug 16, 2010 10:43 am
Re: Strange values from getOrientation
I use DirectX9, but maybe it'll help you. This function converts btQuaternion into D3DXVECTOR3 (Vector3 in my code) and returns it:
Code example of it's usage:
Code: Select all
Vector3 QuatToEuler(btQuaternion* quat){
double sqw = quat->w() * quat->w();
double sqx = quat->x() * quat->x();
double sqy = quat->y() * quat->y();
double sqz = quat->z() * quat->z();
return Vector3(
(float)atan2l(2.0 * ( quat->y() * quat->z() + quat->x() * quat->w() ) , ( -sqx - sqy + sqz + sqw )),
(float)asinl(-2.0 * ( quat->x() * quat->z() - quat->y() * quat->w() )),
(float)atan2l(2.0 * ( quat->x() * quat->y() + quat->z() * quat->w() ) , ( sqx - sqy - sqz + sqw )));
}
Code: Select all
Vector3 rotation = QuatToEuler(&body->getWorldTransform().getRotation());
model->SetRotation(rotation);
void SetRotation(Vector3 &val){
updateMatrix = true;
rotation = val;
D3DXMatrixRotationX(&rotationX, rotation.x);
D3DXMatrixRotationY(&rotationY, rotation.y);
D3DXMatrixRotationZ(&rotationZ, rotation.z);
}