• Create Account

## Update Quaternion Slowly

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Medo3337  Members

891
Like
0Likes
Like

Posted 30 November 2012 - 07:57 AM

Okay, guys I know I'm asking alot of questions, but I have alot alot of work to do to finish the 3D Game Engine I'm working on.

I have a RigidBody, I can rotate it by supplying a new Quaternion value, but here is the problem:

I want to rotate it SLOWLY, something like the following:
// We might need to call this method 500 times from rendering to make the RigidBody rotation is equal to FinalQuaternion

void RotateSlowly(btQuaternion FinalQuaternion)
{
btMatrix3x3 orn = body->getWorldTransform().getBasis();
// Code here to change 'orn' according to FinalQuaternion to keep rotating with certain speed until orn rotation is EQUAL to FinalQuaternion
body->getWorldTransform().setBasis(orn);
}


How can I do that?

### #2Álvaro  Members

20266
Like
1Likes
Like

Posted 30 November 2012 - 08:13 AM

You can compute the quaternion that would turn orn into FinalQuaternion, with a division. If you want to limit the angle, you force the real part of the quaternion to be at least cos(max_angle/2) and rescale the imaginary part of the quaternion to make it unit-length. I posted some code to do that here.

### #3Medo3337  Members

891
Like
0Likes
Like

Posted 30 November 2012 - 09:17 AM

@Álvaro: I'm not sure exactly how to implement that to use it with Bullet, I thought about converting the quaternion to euler and supplying the updated value to setYPR but I don't know how should I do it with frame independent, same like the following example:
x += speed * elapsedTime; // Frame independent

### #4Álvaro  Members

20266
Like
0Likes
Like

Posted 30 November 2012 - 09:44 AM

What part of what I describe don't you understand? The code I provided uses Boost quaternions, but if you understand what it does, you should be able to write the code to work with any quaternion implementation.

### #5Medo3337  Members

891
Like
0Likes
Like

Posted 30 November 2012 - 09:58 AM

Some parts I don't understand like:
q.real();
q.unreal();

I'm dealing with btQuaternion which doesn't have those methods, I would prefer using Euler but having a little problem with as I mentioned above.

### #6Álvaro  Members

20266
Like
1Likes
Like

Posted 30 November 2012 - 01:03 PM

q.real() returns the real part of q (duh ;) ). So if q = w + xi + yj + zk, q.real() returns w.
q.unreal() returns a quaternion where the real part has been zeroed out. So if q = w + xi + yj + zk, q.unreal() returns 0 + xi + yj + zk.

In any case, the procedure is this:
• Start with q = w + xi + yj + zk
• If w is negative, flip the sign of all four components
• if w is less than cos(alpha/2), set w to cos(alpha/2) and rescale the vector (x,y,z) so that w^2 + x^2 + y^2 + z^2 = 1.
The last part is the trickiest. You have to multiply x, y and z by a constant factor that is computed as sqrt((1-w^2)/(x^2+y^2+z^2)).

Is that clear?

### #7Medo3337  Members

891
Like
0Likes
Like

Posted 02 December 2012 - 12:56 AM

@Álvaro: I found out that I can do that easily by using quaternion slerp to animate the rotation, how can I know that the rotation is completed when using btQuaternion::slerp?

### #8Álvaro  Members

20266
Like
0Likes
Like

Posted 02 December 2012 - 01:04 AM

@Álvaro: I found out that I can do that easily by using quaternion slerp to animate the rotation, how can I know that the rotation is completed when using btQuaternion::slerp?

Well, the rotation is completed when you pass a 1 to slerp. How exactly are you calling it?

### #9Medo3337  Members

891
Like
0Likes
Like

Posted 02 December 2012 - 02:58 AM

I want to know when the rotation animation is completed, here is what I'm doing:
// This method should return true when the rotation is completed, otherwise, return false
bool lookAt(float x, float y, float z, float elapsedTime)
{
btQuaternion FinalQuat = GetLookAtRotation(x, y, z);
btQuaternion currentQuat;
orn.getRotation(currentQuat);
float speed = 0.001f;

// Instead of just setting FinalQuat to the rigidbody, I want to animate it
btQuaternion newQuat = currentQuat.slerp(FinalQuat, elapsedTime * speed);
if (???) // If slerp() have done animating the rotation
{
return true; // Animation completed
} else {
return false;
}
}


Edited by Medo3337, 02 December 2012 - 03:13 AM.

### #10Álvaro  Members

20266
Like
0Likes
Like

Posted 02 December 2012 - 04:13 AM

You are using slerp in a funny way. The second argument is supposed to be a number between 0 and 1 indicating at what point between currentQuat and FinalQuat you want to be. But once you have gone through the code once, the value of currentQuat has changed, so you are moving from one to the other in a non-linear fashion. Anyway, whenever you pass a 1 as the second argument, the result will be FinalQuat.

Why did you give up on the other solution I proposed?

### #11Álvaro  Members

20266
Like
0Likes
Like

Posted 02 December 2012 - 04:28 AM

I don't use Bullet, but looking at the documentation, I think this should work:
btQuaternion diffQuat = (currentQuat.inverse() * FinalQuat).nearest(btQuaternion(1.0, 0.0, 0.0, 0.0));
if (diffQuat.getAngle() > max_angle))
diffQuat.setRotation(diffQuat.getAxis(),max_angle);
btQuaternion newQuat = currentQuat * diffQuat;


That code uses a couple of trigonometric functions more than strictly necessary, but given the interface of btQuaternion, it seems to be the easiest way to do it.

Let me know if that works.

### #12Medo3337  Members

891
Like
0Likes
Like

Posted 02 December 2012 - 06:07 AM

It's not working, basically I want to animate the rotation of a tank cannon, the tank cannon should rotate with certain speed, so the method can look like that:
// Every call will rotate the cannon according to 'rotationSpeed'
bool lookAt(float x, float y, float z, float rotationSpeed, float elapsedTime);


Here is the idea:
1. The tank see the enemy
2. The tank rotate it's cannon towards the enemy lookAt(enemy.x, enemy.y, enemy.z, 0.1f, elapsedTime);
3. Keep calling lookAt() each frame until lookAt() return true, when lookAt() return true, fire a missile towards the enemy.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.