View Full Version : vectors and rotations

David

11-20-2000, 03:16 AM

Hello all,

Imagine I have a car pointing in a direction (in 3 space), and I have a vector that I want to point the car to (so the car is pointing in the direction of the vector). How do I do this? The obvious way I thought would be to use quarternions but I dont know how to use them.

Any ideas?

David,

You don't need to use quaternions to do what you want, particularly if the car will only be rotating in a plane.

Vector v = car's current orientation

Vector d = car's desired orientation

Taking v cross d will give you a third vector, call it axis, that you will want to rotate the car around.

Recall that the dot product of two vectors is: v dot d = |v| * |d| * cos(theta) where theta is the angle between the two vectors. So, to get your angle, take v dot d, divide by the product of their lengths, and then take the acos of this number.

Finally, to rotate the car, you can build a matrix yourself or use glRotatef(theta, axis.x, axis.y, axis.z). Don't forget to normalize your axis vector and convert theta to degrees.

I hope this is what you meant and I'm not over simplifying your problem http://www.opengl.org/discussion_boards/ubb/smile.gif

Zeno

RandyU

11-22-2000, 01:29 PM

Try gluLookAt(0,0,0,upx,upy,upz,vx,vy,vz) where upx,upy,upz - (1,0,0)

vx,vy,vz - vector you want to point car to.

It may not work.

The question is: how did you get there?

There are actually infinitely many

orientations of your car that points the

"front" to the vector of your choice. You

also need an up vector to sufficiently

define your car's orientation.

Minimum, you need a "roll" factor in

addition to "turn" and "pitch" (which the

"front" vector will give you). Then, assuming

your physics have gravity down and your car

turns like normal cars, you can do something

like this:

1) I'm assuming X/Z is the "movement" plane,

with Y being "up".

2) Project the model's "front" to the XZ

plane.

3) Project the intended vector to the XZ

plane.

4) The angle between these vectors is your

rotation around the Y axis. You may wish to

just store this as an angle, rather than

store the vectors, to reduce the math. Save

this as rotY.

5) Figure out the roll around the "front"

vector from terrain or whatnot. Apply it to

the model.

6) Figure out the pitch, again from terrain

or stored values. Apply it to the model.

7) Last, apply the actual rotY around the Y

axis that you figured out in 4).

Your car is now pointed in the "front"

direction, properly aligned to terrain. For

some physics, it may be easier to switch

steps 5 & 6. Also, the "according to terrain"

steps may require projecting the terrain

that touches the wheels backwards through

the appropriate rotation angles.

I guess what I'm saying is that it's easier

to store the rotation angles and project the

model for each frame, than trying to keep a

cumulative transformation matrix.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.