Sign in to follow this  
Followers 0
Medo Mex

Update Quaternion Slowly

11 posts in this topic

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:
[CODE]
// 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);
}
[/CODE]

How can I do that?
0

Share this post


Link to post
Share on other sites
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 [url="http://www.gamedev.net/topic/634097-incremental-rotation-towards-target-3d/page__p__4999829#entry4999829"]here[/url].
1

Share this post


Link to post
Share on other sites
@Á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:
[CODE]x += speed * elapsedTime; // Frame independent[/CODE]
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:[list]
[*]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.
[/list]
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?
1

Share this post


Link to post
Share on other sites
@Á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?
0

Share this post


Link to post
Share on other sites
[quote name='Medo3337' timestamp='1354431411' post='5006224']
@Á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?
[/quote]

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

Share this post


Link to post
Share on other sites
I want to know when the rotation animation is completed, here is what I'm doing:
[CODE]
// 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;
}
}
[/CODE] Edited by Medo3337
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
I don't use Bullet, but looking at the documentation, I think this should work:
[code]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;
[/code]

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.
0

Share this post


Link to post
Share on other sites
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:
[CODE]
// Every call will rotate the cannon according to 'rotationSpeed'
bool lookAt(float x, float y, float z, float rotationSpeed, float elapsedTime);
[/CODE]


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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0