# Normal -- 3x3 Rotation Matrix

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

## Recommended Posts

Hi there, I have a heightmap terrain, and when i place a tank on it i want the tank to have the same slop as the terrain. Now i have already created an index called Terrain[index].normal, what i need to do is get the X Rotation & Y Rotation from that so that i can complete this 3x3 matrix X Y Z --------- 1 , 0 , 0 0 , 1 , 0 0 , 0 , 1 Correct me if im wrong but those are the axis rotation columns and the rotation are applied in order X --> Y --> Z --> Translation Now obviously the tiles are evenly spaced in our 128*128 height map world with 1 block size = 1 on both the x & y axis. So if something is in block 4 simple translate by 4. :-) --------------------------------------------------------- Now i imagine that we will first need to determine the X Rotation to be able to get the Y Rotation, and then the Z Rotation is reserved and it is the direction that the unit is facing in the game mechanics.

##### Share on other sites
I suppose you could do this with Euler angles, but for me at least that's not the easiest way to think about it. More intuitive IMO is to find the rotation that aligns the tank up vector with the terrain normal; this preserves the tank's basic orientation while aligning it with the terrain. If your tank's orientation is represented as a 3x3 matrix, this involves finding the axis-angle pair that rotates the tank up vector (probably the second or third row or column of the matrix) onto the terrain normal, and then multiplying the orientation matrix by a rotation matrix constructed from this axis-angle pair. This may not fit into your current scheme if you're using Euler angles, but I think taking a more general approach would pay off in the end.

I wasn't quite clear on your post, but FYI the rows or column of a rotation matrix are an orthonormal basis (3 unit-length and mutually perpendicular vectors), i.e. the direction vectors of your tank.

Well, that may not have been much help, but feel free to ask if you need further info.

##### Share on other sites
no that was alot of help, you described my problem in 3 lines...

Thanks now i know what to search for, infact i can go find the code on google.

How ever im not to sure what you mean, but yeah think of an RTS game the units only aim on the TopDown-Plane which happens to Span the Z Axis in my game.. IE air units's Z Position value are higher than ground units.

So i thought what if i just take the Angle on the X axis and the Y Axis and us that im not 2 sure how to do this, but all my units will have a 3x3 matrix for rendering and an Scalar Angle for their facing when viewed from the top, then each time they move/rotate ill just make the required adjustements to the matrix.

In case of move ill have 2 recalculate, in case of rotate ill just construct a matrix to rotate it on the z axis and multiply?

I dont really care how i get to the result, speed is important though, and i just plan to plug it into opengl with MatrixMult so a matrix would be best :-)

##### Share on other sites
I can't really give you a complete answer, but I'll tell how I would probably do it if I were making a 3d RTS.

First of all, the 'align with the normal' method is a good place to start, but will often result in the tank visually intersecting the terrain. So to really do it right you'd want a more sophisticated method, but we'll stick with the simple alignment for now.

So every object in the game has a matrix representing its position and orientation, which can be used for various things, including rendering (through submission to OpenGL via glMultMatrix()). Every update you position the bottom center of the tank at the exact height of the terrain directly beneath it, and then re-orient the 3x3 portion of the matrix using the aforementioned axis-angle method. Presumably the tank will never be going straight up or down, so 2d problems such as turning towards or targeting an opponent can be handled by projection onto the xy plane and performing the calculations there. This way the objects can be both freely orientable (necessary for the terrain alignment) and also have a 2d aspect for solving AI problems and so forth.

Admittedly there are quite a few details involved here, and if you don't already have some basics in place (axis-angle matrices, updating the rotation part of a 4x4 matrix, orthogonalization, and so on), it might take some work to implement. But in the end this approach will give you maximum flexibility. This is just how I'd do it though; you might be able to get similar results using other methods and therefore be able to avoid drastically changing your current code.

##### Share on other sites
What u said is exactly the way ill be doing it.

However u have cleared up that i need some strong Matrix maths functions, so i guess ill look into that.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 12
• 29
• 11
• 44
• ### Forum Statistics

• Total Topics
634856
• Total Posts
3019661
×