views:

94

answers:

1

Hi there! I'm a real noob who just started learning 3d programming and i have a really hard time learning about rotation in 3D space. My problem is that I can't seem to figure out how to rotate an object using it's local coordinates.

I have a basic class for 3d objects and, for starters, i want to implement functions that will rotate the object on a certain axis with x degrees. So far i have the following:

 public void RollDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation, 
            Matrix4.CreateRotationX(MyMath.Conversions.DegToRad(angle)));
    }

    public void PitchDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation, 
             Matrix4.CreateRotationY(MyMath.Conversions.DegToRad(angle)));
    }

    public void YawDeg(float angle)
    {
        this.rotation = Matrix4.Mult(rotation,
             Matrix4.CreateRotationZ(MyMath.Conversions.DegToRad(angle)));
    }

'rotation' is a 4x4 matrix which starts as the identity matrix. Each time i want to roll/pitch/yaw the object, i call one of the functions above.

for drawing, i use another function that pushes a matrix onto the ModelView stack, multiplies it with the translation, rotation and scale matrices of the object (in this order) and begins drawing the vertices. ofcourse, finally i pop the matrix off the stack.

the problem is that the functions above rotate the object on the GLOBAL axis, not on the LOCAL ones, even if, from my understanding, every time you rotate an object, the local system changes it's axis and then, when a new rotation is applyied on top of the others, the local axis are used for the new one.

i read different tutorials about the math behind it and how to rotate objects, but i couldn't find one the could help me.

if anyone has the time, i would really appreciate if he could help me understand HOW to rotate around local axis and, maybe even more important, what i did wrong on my current implementation.

+1  A: 

If you want to perform your transformations in this order : translation -> rotation -> scale (which makes perfectly sense, it's what's wanted usually), you have to multiply your matrices in the reverse order.

In a right-handed coordinate system (i.e. the one openGL uses), matrix multiplication must be performed from right to left. This is why :

ModelViewTransform = Transform * View * Model // <- you begin by the model, right ? so it's this way

Note that in directX they use a left-handed coordinate system. It has his shortcomings, but it's more intuitive.

Calvin1602
oook, so: there is average, there is stupid, there is very stupid, there is mindblowing stupid and then there's me: i had to see the right-handed coordinate system for the thousandth time (thanks calvin) to think about reversing the order of the rotation matrices multiplication. the problem was i multiplied "old*new" instead of "new*old". and i had this pesky problem with RTS transformations and solved it, even with x/y/z order and solved it. i just can't stop to amaze myself :|thanks again!
cantrem