Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Alright, so this is how I am doing it:

float xrot = 0;
           float yrot = 0;
           float zrot = 0;

           Quaternion q = new Quaternion().fromRotationMatrix(player.model.getRotation());
           if(q.getW() > 1){
            q.normalizeLocal();
           }

           float angle = (float) (2 * Math.acos(q.getW()));
           double s = Math.sqrt(1-q.getW()*q.getW());
           if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
            // if s close to zero then direction of axis not important
            xrot = q.getXf(); // if it is important that axis is normalised then replace with x=1; y=z=0;
            yrot = q.getYf();
            zrot = q.getZf();
           // z = q.getZ();
            } else {
             xrot = (float) (q.getXf() / s); // normalise axis
             yrot = (float) (q.getYf() / s);
             zrot = (float) (q.getZf() / s);
            }

But it doesn't seem to work when I try to put it into use:

player.model.addTranslation(zrot * player.speed, 0, xrot * player.speed);

AddTranslation takes 3 numbers to move my model by than many spaces (x, y, z), but hen I give it the numbers above it doesn't move the model in the direction it has been rotated (on the XZ plane)

share|improve this question
    
What are you trying to accomplish? Could you edit your question and tell us? –  Ryan Budney Dec 14 '10 at 1:41
    
I'm trying to move a model in the direction that it's facing. –  CyanPrime Dec 14 '10 at 1:44
    
I don't understand what it is you're looking for. –  Ryan Budney Dec 14 '10 at 1:55
    
I wanna make the quaternion's y into a degree (0 to 360) –  CyanPrime Dec 14 '10 at 2:19
    
If you are trying to move the character in the direction that the character is facing, there is no need to convert to a quaternion, you can use out vector of the rotation matrix. I would ask the question "How do I move translate a character in the facing direction?" here: gamedev.stackexchange.com . If no one answers the question there, I will. –  Jonathan Fischoff Dec 14 '10 at 2:23

1 Answer 1

You first need to determine which local (i.e. body-fixed) axis points out of the front of the front of your model. It is likely the positive X or positive Y axis (depending on your convention). You then take the first column if it is the X or the second column if it is the Y. These will give you a unit vector pointing in the direction that you want to travel. You can then update your position in time with

P_{k+1} = P_k + U*speed*dt

where P is your position and U is the unit vector.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.