Biel Simon
Biel Simon

Reputation: 173

Rotating a vector by angle and axis in java

I know there are lots of questions and answers about this topic or related but i've beenn trying for 2 hours and still haven't benn able to figure it.

I would like to get a function that looks like this:

public static Vector rotateVector(Vector v, Vector axis, double angle){

}

Where the axis is a unit vector that defines the plane of rotation (the vector v rotates towards the vector axis if angle is positive)

I have already taken a look at rotation matrices but haven't been able to implement those to the above function

Upvotes: 2

Views: 10133

Answers (4)

A Boss
A Boss

Reputation: 11

This is the correct way to rotate a vector.

private Vector rotateZ(Vector vector,double angle) { // angle in radians

  //normalize(vector); // No  need to normalize, vector is already ok...

  float x1 = (float)(vector.x * Math.cos(angle) - vector.y * Math.sin(angle));

  float y1 = (float)(vector.x * Math.sin(angle) + vector.y * Math.cos(angle)) ;

  return new Vector(x1, y1);

}

Upvotes: 1

Biel Simon
Biel Simon

Reputation: 173

Got it, thanks @Chris K. Here is the java function:

public static Vector rotateVectorCC(Vector vec, Vector axis, double theta){
    double x, y, z;
    double u, v, w;
    x=vec.getX();y=vec.getY();z=vec.getZ();
    u=axis.getX();v=axis.getY();w=axis.getZ();
    double xPrime = u*(u*x + v*y + w*z)*(1d - Math.cos(theta)) 
            + x*Math.cos(theta)
            + (-w*y + v*z)*Math.sin(theta);
    double yPrime = v*(u*x + v*y + w*z)*(1d - Math.cos(theta))
            + y*Math.cos(theta)
            + (w*x - u*z)*Math.sin(theta);
    double zPrime = w*(u*x + v*y + w*z)*(1d - Math.cos(theta))
            + z*Math.cos(theta)
            + (-v*x + u*y)*Math.sin(theta);
    return new Vector(xPrime, yPrime, zPrime);
}

However, I will keep the check on Chris' answer.

Upvotes: 3

Chris K
Chris K

Reputation: 1723

Rotating (x, y, z) counter clockwise around unit vector (u, v, w) by angle theta produces a vector (xPrime, yPrime, zPrime):

double xPrime = u*(u*x + v*y + w*z)*(1d - Math.cos(theta)) 
                + x*Math.cos(theta)
                + (-w*y + v*z)*Math.sin(theta);
double yPrime = v*(u*x + v*y + w*z)*(1d - Math.cos(theta))
                + y*Math.cos(theta)
                + (w*x - u*z)*Math.sin(theta);
double zPrime = w*(u*x + v*y + w*z)*(1d - Math.cos(theta))
                + z*Math.cos(theta)
                + (-v*x + u*y)*Math.sin(theta);

Source here.

Upvotes: 4

crbah
crbah

Reputation: 336

If you want the rotation for x,y and z axis then you should use rotation matrices all at once.

NewVector = [Rotation_X][Rotation_Y][Rotation_Z]*OldVector

Here Rotation_X,Rotation_Y and Rotation_Z are 3x3 matrices. (You can see http://mathworld.wolfram.com/RotationMatrix.html)

The order of multiplication depends on the problem but i guess you want only one-axis rotation (i.e. the other 2 matrices become identity matrices)

So just putting an if-block you can set the correct matrix, and leave the rest as identity matrices.

Hope this helps.

Upvotes: 0

Related Questions