XNA Game Development Forums
2012/05/18 06:36:53 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Question about getting rotation angles from Quaternions...  (Read 3132 times)
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« on: 2007/03/17 08:23:53 »

Hi Guys

This is probably a question for Mike  Smiley but if anyone can help would be appreciated muchly.

I'm having a small issue getting my head around something for my racing game.

Basically the situation is like this:

My racing game uses the Y plane as the up vector.  I have a hovercar class that uses a vector for it's position, and a quaternion for it's rotation, which is constrained to always point "up" each frame (with a little bit of wobble allowed) by doing the following:

Quaternion Q = new Quaternion(0, RotationY, 0, Rotation.W);
Slerp(Q, 0.05f);

This basically means that as the car leans over when going round corners, it will correct itself so that it points "up" again.

I also have my own functions for turning the car left and right, which takes into account turning acceleration, drift, etc.

It's all working great, and I'm more than happy with the physics engine I've got working for my hovercar.

Now, my problem is this.

I want to use the same class that I use for *my* hovercar, for the other racers in the game, to make it "fair".

Therefore, when I want the other cars in the game to turn left or right, I want to use the same functions that I call to turn *my* ship left or right.

This in itself is not a problem, however the issue is deciding whether or not the car should turn left OR right to face it's new destination (marker vectors around the track).

So essentially my question I suppose is this.

Two objects in space, constrained on the Y axis (used as up) with a vector for position, and a quaternion for rotation.  If I want one of the objects to turn to face the other one, taking the SHORTEST distance (either left or right), how do I do it?  Bearing in mind all I want back from this equation is whether or not the object should TURN left or right, not to actually do it, as I already have my own functions I will then call, to make the cars behave the same as my human-controlled racer.

Does that make sense?

Help!!  Smiley








Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #1 on: 2007/03/17 14:44:58 »

Did you look at the lookat function from the camera class? It does that Wink You'd have to do it twice and check the shortest though in that method, or check the angles with a dot product before hand to see which is closer.
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #2 on: 2007/03/19 02:16:25 »

Hi Mike

I'm actually using your LookAt function in my camera class.

It's knowing how to check the angles after I've called the Lookat function to update the quaternion by a small amount that I'm struggling with figuring out.

Can you give me an example of how to check / get the angles out of the quaternion based on the Y vector being the up plane?

So If I've got two quaternions, the original heading of the object, and another which represents it's new heading after calling the Lookat Function, how do I actually establish what the angles of the quaternion are?

Thanks
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #3 on: 2007/03/19 07:38:48 »

In case it's of any use in helping to figure this out, I found this on the net:

Euler angles can be extracted from a quaternion as:

tan(yaw)   =  2(q1q2+q4q3)/ (q42 + q12 - q22- q32)      (5a)
sin(pitch) = -2(q1q3-q4q2)                                              (5b)
tan(roll)  =  2(q4q1+q2q3)/ (q42 - q12 - q22+ q32)        (5c)

However, how you go about converting this into C# code I have NO idea....
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #4 on: 2007/03/19 08:29:41 »

Ok, had a go at doing this quickly, does this look right? (it's in VB for now)

 Dim q1 As Single = 0 'X
        Dim q2 As Single = 1 'Y
        Dim q3 As Single = 0 'Z
        Dim q4 As Single = 1 'W

        Dim eX As Single = Math.Atan(2 * ((q1 * q2) + (q4 * q3)) / ((q4 * q4) + (q1 * q1) - (q2 * q2) - (q3 * q3)))
        eX = 180 / Math.PI * eX

        Dim eY As Single = Math.Sin(-2 * ((q1 * q3) - (q4 * q2)))
        eY = 180 / Math.PI * eY

        Dim eZ As Single = Math.Atan(2 * ((q4 * q1) + (q2 * q3)) / ((q4 * q4) - (q1 * q1) - (q2 * q2) + (q3 * q3)))
        eZ = 180 / Math.PI * eZ
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #5 on: 2007/03/19 16:30:42 »

Keep in mind that in most notations on the net, the w value is actually q1 (or q0) but in XNA it is always q4. If they refer to the scalar anywhere in the text, that is the w value for our quaternions.
Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #6 on: 2007/03/19 17:54:53 »

Daphyd, i'll have the function done by tonight (my time) for you, so in a couple of hours. Wink If I cannot get this done nicely i'll go back to the method I was looking at yesterday, i've figured out the pattern of values etc in the Quaternion.
[Edit] I wanna have a play around with some things, but your VB code there should be about right, just convert it over to C# code and try it out. Wink Won't hurt.
« Last Edit: 2007/03/20 00:13:33 by Chr0n1x » Logged

DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #7 on: 2007/03/20 07:27:20 »

Ok guys,

Just to let you know I've cracked this myself, with a bit of ingenuity and luck!

I'll write up my findings properly and post them here as a mini-tut, if that's OK.

It's not "perfect" (sometimes, for some reason, the car's will just fly off in a random direction, so I'm correcting that using a function I wrote called "IsFacing", which determines whether an object is pointing in the general direction of another object, and a little bit of SLERP) but generally it plays really well now, and it feels like there's other "people" driving the other cars!  They smash in walls if they corner to fast, wobble about all over the place...

Still need to put some model collision in place so you can ram the other cars around, and then I might put up a compiled demo for download so people can let me know what they think.

Got it working with a USB XBox controller yesterday too - it's starting to feel like a REAL game!!



Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Question about Getting 2D screen coordinates General Discussion DaphydTheBard 6 2462 Last post 2007/02/27 03:23:36
by DaphydTheBard
Question: Texturing Terrain General Discussion Ashe 1 2337 Last post 2007/03/17 10:28:36
by DaphydTheBard
Array Question General Discussion LucianX 11 3350 Last post 2007/04/13 04:51:19
by Nemo Krad
Tutorial 3 help... new question i believe Hazy Mind XNA Engine hansonc 9 3126 Last post 2007/05/15 01:32:01
by BackwardsBoxers
Question about loading textures General Discussion nicknz 2 1957 Last post 2007/04/23 05:36:17
by EclipsE
tutorial 3 sprite question Hazy Mind 3D Engine precious roy 2 2728 Last post 2009/10/18 08:53:11
by Wekbaite73
FPS question General Discussion XNASorcerer 2 1468 Last post 2007/04/28 15:23:19
by XNASorcerer
Thanks And A question Hazy Mind XNA Engine zachaller 3 1567 Last post 2007/06/19 03:36:22
by Nemo Krad
Camera rotation issues (z-axis drift solved!) Hazy Mind XNA Engine ppardee 10 4266 Last post 2008/11/19 11:22:49
by inbreed
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.268 seconds with 19 queries.