Skip to main content
gamedev.net
🔒 Locked

World transform. Someone HELP!

Started by SoaringTortoise 7 replies 1.2k views
Original Post
SoaringTortoise
SoaringTortoise
I am struggling to get my rotation matrices set up correctly. This has been the bane of my life for a while now, and I keep on dropping it and moving on to other things, but I''m running out of other things to do so... it''s time to sort this out. I''m sure I''m missing something really simple. I have a model that I want to make aligned to the slope of the ground that it sits on, AND I want to rotate the model to face in a given direction. Think of a tank driving along the ground. The basic thing should work like this: D3dxmatrixrotationx tmprot, Anglex MatRot = TmpRot D3dxmatrixrotationy tmprot, Angley d3dxmatrixmultiply MatRot, TmpRot, MatRot D3dxmatrixrotationz tmprot, Anglez d3dxmatrixmultiply MatRot, TmpRot, MatRot Then I multiply the MatRot by the transform and scale and apply the result to the world transform. That SHOULD work, as far as I am concerned. If I only supply a single angle (e.g. AngleX, AngleY OR AngleZ), then the rotation is perfect. As soon as I start to apply two or more angles, the rotation goes haywire. (Only supplying an x and z rotation, for example, seems to occassionally result in y rotation). To prove that I haven''t been idle in this, here''s what I''ve tried: 1) Changing the order that the rotations get applied (x-y-z, z-x-y, x-z-y, etc.) 2) Calculating the x and z angles using calculus based on the y rotation (instead of using only the ground-normal to get the x/z angles) 3) D3dmatrixrotationyawpitchroll... same problem (leads me to believe that it''s the calculation of the angles, not the order they are applied, that are the problem) 4) MatrixLookAtLH (to get an UP vector which is the ground normal, and then rotate around the Y only... this doesn''t do anything. I suspect because the lookat doesn''t apply the world transform). 5) Rotate around the x/z axis to get the right tilt, and then use the D3DxMatrixRotationAxis function to rotate around the Y (I thought that this would achieve what the LookatLH should have done... but nope). This really has me stuck. To my mind, getting the x/z angles by checking along the models major axis after a rotation around the Y and then just sticking the 3 rotations into the matrix should work, but... I MUST be missing something truly fundamental. Lastly, I am certain that the angles are in Radians. Sorry for the rant... just incredibly frustrated. (by getting the x/z angles by checking along the major axis after rotation, I mean: If the model is facing 0 degrees, then I would get the x/z angles by checking along two lines that form a cross, like this "+". If the model was looking at 45-degrees, I would check a cross that looks like this, "X". Makes sense?).
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
Nypyren
Nypyren
Heh, messing around with 3 rotations at once pisses me off, too.

Luckily, it seems like you want to use the matrix function that "rotates on an arbitrary axis" (I haven't done 3D in a while so I can't remember the name)


so the "arbitrary axis" is the surface normal (perpendicular vector to the plane) of the ground that the tank is sitting on, and the angle rotated around that axis is how much the tank has turned.



[edit] LOL! I said parralel... it's way too late at night

[edited by - Nypyren on May 31, 2003 5:57:06 AM]

[edited by - Nypyren on May 31, 2003 5:57:29 AM]
SoaringTortoise
SoaringTortoise
That''s that matrixrotationaxis function... problem is that you end up with the object rotated around that axis, not TO that axis. So, if the axis is at 90 degrees and you rotate around it by 45-degrees, it''s the exact same thing as rotating around the Y-axis by 45 degrees.

I think my problem is simpler than this even, but I don''t know what the problem is.... although I suspect it''s in the calculation of the angles.

Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
SoaringTortoise
SoaringTortoise
No, but seriously. What am I missing?
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
labris
labris
How do you calculate your angles, you do take the correct differences?
SoaringTortoise
SoaringTortoise
I have a function that looks like this:


Public Function GetGroundMeshRot(x As Single, y As Single, Optional Angle As Integer = 0, Optional SampleSize As Single = 1) As D3DVECTOR2
Dim H1 As Single
Dim H2 As Single
H1 = GetHeight(x + (GetCos(Angle) * SampleSize), y + (GetSin(Angle) * SampleSize))
H2 = GetHeight(x - (GetCos(Angle) * SampleSize), y - (GetSin(Angle) * SampleSize))
GetGroundMeshRot.y = Atn((H1 - H2) / SampleSize * 2) * DEG

H1 = GetHeight(x + (GetSin(Angle) * SampleSize), y + (GetCos(Angle) * SampleSize))
H2 = GetHeight(x - (GetSin(Angle) * SampleSize), y - (GetCos(Angle) * SampleSize))
GetGroundMeshRot.x = Atn((H2 - H1) / SampleSize * 2) * DEG

End Function


The angle passed into the function is the Y rotation. I convert everything to degrees because I'm more comfortable working in degrees than radians, but in the render I convert the angles back into radians before applying them to the matrices. The SampleSize is basically the size of the object that I want to orient. The GetHeight function is accurate. GetSin and GetCos refer to lookup tables with pre-computed sin and cos values.

Edit: Removed the Angle=0 from the code. I had that in there for testing.

[edited by - soaringtortoise on June 1, 2003 5:42:07 PM]
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
Sonicmage
Sonicmage
You sound like you have encountered gimbal lock, my friend. I don''t feel like explaining this in detail right here because I have too many times before. However, basically I can tell you that it means you are losing a degree of freedom which is why you are encountering such strange results. This is caused by the fact you are strictly using matrices to define your rotations. The solution is to use other means to define your rotations like quaternions or axis angle representations. Gratefully, DirectX has provided structures and functions for handling quaternions so you should study about how to use these (I must warn you in advance, it is heavy math). Good luck.
SoaringTortoise
SoaringTortoise
That''s a neat term. I''m off to research "gimbal lock"
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...
SoaringTortoise
SoaringTortoise
Hmmm.. OK. The quaternion thing fixes my rotation around 2 axes (x & y), but my z rotation is still snafu''d.

I am getting yaw and pitch absolutely perfect, but the roll is just not functioning. If I switch to just Yaw and Roll or just pitch and roll, it is still broken (the extent that on the p & r, I''m getting something that looks very much like a yaw).

Perhaps I''m just visualising this thing wrong. I am seeing the angles that need to be submitted as sequentially applied, with the perspective always looking down the long axis of the model. So, the yaw is applied first, then the pitch, then the roll. So I do the Yaw first and calculate the pitch from that Yaw, then the roll from the yaw + pitch.

Any suggestions?
Always prey on the weak, the timid and the stupid. Otherwise you'll just get your butt kicked
For a tortoise, this is extremely hard to do, but when you get it right... the expression on their faces ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.