# Rotations!

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to do some 3D rotations and this is the first time I ever have tried to so I'm haveing a lot of troubles. What I so far have done is implementing a quaternion class and that's pretty much it. I don't understand it and I don't know much about rotation matrices. My game is like the following: A hoovercraft is travelling over a heightmap. I have connected a spring node to a scaled interpolated normal of the terrain surface and when the hoover is to be drawn i want to to be rotated accordingly to the normal between it's position and that flying spring node. So basically I just want to find out some angles, a matrix or how it's done and rotate the ship. Also I need to find the forward vector of the hoovercraft so I can use it to add force coming from the engines and fire off some rockets etc etc :D I think I need a detailed explanation for this and probably some source code for how the matrices are added multiplied in OpenGL, else I won't be getting anywhere with this!

##### Share on other sites
I didn't follow all the details of your post, so this isn't a complete answer, but rather some suggestions.

First of all, if you're not comfortable with rotations in general, I would defer quaternions for the time being and work with matrices instead. They can perform the same functions and are a bit more intuitive (initially at least). Once you understand rotation matrices, quaternions will make more sense.

As for the forward vector, the direction vectors of an object can be extracted directly from its orientation matrix. The details depend on choice of conventions.

For aligning an object with a normal, create an axis-angle rotation that will rotate the object's up axis onto the normal, and apply it to the object orientation matrix. You can interpolate through the angle for a smoother effect. (This is a place where quaternions can be an improvement.)

If you don't have a math library of your own and are working with OpenGL, this can all be accomplished indirectly through glRotatef(), glGetFloatv(), and so on.

There are quite a few details involved here, so the above explanation may not be something that you can just plug into your code. But feel free to ask if you have any follow-up questions.

##### Share on other sites
---
For aligning an object with a normal, create an axis-angle rotation that will rotate the object's up axis onto the normal, and apply it to the object orientation matrix.
---

But exacly _how_ do I do that? ;) Thats my big problem!

##### Share on other sites
Quote:
 But exacly _how_ do I do that? ;) Thats my big problem!
There are two issues here: how to construct and concatentate rotation matrices in general, and how to construct an axis-angle rotation to rotate one vector onto another. The first is a larger topic, but the second is fairly straightforward.

Given two vectors from and to of arbitrary length, the axis of rotation is:

axis = from.Cross(to);

The angle of rotation is:

angle = atan2(length(axis), from.Dot(to));

(Don't forget to convert to degrees if necessary.)

I believe glRotate*() will normalize the axis for you, but in general you'll need to normalize it yourself. There are a few cases which will result in the axis being near zero-length: one or both of the input vectors is near zero-length, the vectors are negatively aligned, or the vectors are positively aligned. I think for vehicle alignment you can safely ignore all but the last case, which you'll need to detect and handle by loading the identity matrix.

I realize this is still kind of complicated, especially if the underlying concepts are unfamiliar. There might be a simpler solution you could hack together, but I'm not really clear enough on the details of what you're trying to do to make further suggestions.

##### Share on other sites
Perfect! Thanks a lot! :D Now I just have to figure out how I do some badass hoover physics with all these rotations, vectors, slopes and obstacles! I will have work to do for a year ;)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013247
×