Orientating an object to its movement vector?

I have been pulling out my hair! i am sure that it is a simple problem and i cant see the solution :frowning:

I have a vector describing the movement of an object in my world. ( lets say a fish)

how would i go about using this vector to obtain angles describing the rotation about the axes to orient the fish so it is facing the direction of travel?

To make things simpler(i think…) is that there will be no rotation about the Z-axis. (i.e the fish will stay upright, however can change pitch and direction)

I have tried using the angle between two vectors (where the vectors i am comparing with the movement vector of the fish are the axis, i.e. (0,1,0) and (1,0,0).

But no luck.

Any help would be appreciated!

I’m assuming you want to use your movement vector to set a transform somewhere. So you need to convert it into a matrix containing the rotation. If so, you don’t need find any angles. You can create the matrix by hand.

The upper 3x3 part of the matrix contains the rotation. It is defined as 3 vectors (right, up, forward) wich are all perpendicular to each other. Here is how to calculte the vectors:
forward vector - normalize your movement vector
right vector - the cross product between forward and a temporary up vector (0, 1, 0)
up vector - the cross product between the forward and right vector.

Now you plug the 3 vectors into a matrix and use it to set a transform. I don’t remember in wich order or direction (row, column) of the vectors in the matrix so you better look that up.

Btw. Here is the code to convert a vector to euler angles, in case you do need it:


      /** Gets the angle of the vector in euler given by the vector (xp, yp, zp).
       * (0, 0, 1) is angle (0,0,0).
       * The input do not have to be normalized.
       * 
       * The returned x will be in the range [-PI/2, PI/2]
       * The returned y will be in the range [0, 2PI]
       * @return the out object.
       */ 
      public static final Vector3f getAngleFromVector(double xp, double yp, double zp, Vector3f out) {
            // rotation around up axis
            double eulery = (Math.PI*2)-getAngleFromVector(zp, xp);
            // rotation around rigth axis
            double xzLength = Math.sqrt(xp*xp+zp*zp);
            double eulerx = getAngleFromVector(xzLength, yp);
            if (yp < 0)      eulerx = (eulerx-Math.PI*2);
            out.set((float)eulerx, (float)eulery, 0);
            return out;
      }


      /** Gets the angle of the vector given by (xp, yp)
       */
      public static final double getAngleFromVector(double xp, double yp) {
            if ((xp!=0)&&(yp!=0)) {
                  //Calculate angle of vector
                  double angle=0;
                  if (xp!=0) {
                        if ((xp>=0)&&(yp>=0))
                              angle=Math.atan(yp/xp);
                        if ((xp<0)&&(yp>=0))
                              angle=Math.PI-Math.atan(yp/-xp);
                        if ((xp<0)&&(yp<0))
                              angle=Math.PI+Math.atan(-yp/-xp);
                        if ((xp>=0)&&(yp<0))
                              angle=Math.PI*2-Math.atan(-yp/xp);
                  } else {
                        if (yp>=0)
                              angle=Math.PI/2;
                        else
                              angle=Math.PI+Math.PI/2;
                  }
                  return angle;
            } else {
                  if (xp==0 && yp==0)      return 0;
                  if (xp==0 && yp>0) return Math.PI*0.5;
                  if (xp==0 && yp<0) return -Math.PI*0.5;
                  if (yp==0 && xp>0) return 0;
                  else return Math.PI;
            }
      }

Thanks, i will try the code you have suggested.

I assume i need the angles however i will try the making a the matrix first.

Cheers

worked for me.
i was retrieveing nor;mal from a surface, so i had to negate angles, but it worked.

Interesting topic. Am wrestling with this problem, too.
I found an interesting article on the topic Rotation Matrix. I’ve quoted it here:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=xith3d;action=display;num=1081451295

… together with a question (because I’ve to negate the direction vector which I read from the matrix, and I don’t know why).
Maybe my article is also interesting to you?