0
$\begingroup$

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)

  • 0
    What are you trying to accomplish? Could you edit your question and tell us?2010-12-14
  • 0
    I'm trying to move a model in the direction that it's facing.2010-12-14
  • 0
    I don't understand what it is you're looking for.2010-12-14
  • 0
    I wanna make the quaternion's y into a degree (0 to 360)2010-12-14
  • 0
    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: http://gamedev.stackexchange.com/ . If no one answers the question there, I will.2010-12-14
  • 0
    What relationship do you want there to be between the quaternion and the degree? If you're simply looking a the associated rotation matrix to the quaternion (given by conjugation), the angle $\theta$ is just $2|q|$ where $|q|$ is the length of the quaternion $q$.2010-12-14

1 Answers 1

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.