1

I have this method for rotating points in 3D using quaternions, but it seems not to work properly:

    public static ArrayList<Float> rotation3D(ArrayList<Float> points, double angle, int xi, int yi, int zi)
{
    ArrayList<Float> newPoints = new ArrayList<>();

    for (int i=0;i<points.size();i+=3)
    {
        float x_old = points.get(i);
        float y_old = points.get(i+1);
        float z_old = points.get(i+2);

        double w = Math.cos(angle/2.0);
        double x = xi*Math.sin(angle/2.0);
        double y = yi*Math.sin(angle/2.0);
        double z = zi*Math.sin(angle/2.0);

        float x_new = (float) ((1 - 2*y*y -2*z*z)*x_old + (2*x*y + 2*w*z)*y_old + (2*x*z-2*w*y)*z_old);
        float y_new = (float) ((2*x*y - 2*w*z)*x_old + (1 - 2*x*x - 2*z*z)*y_old + (2*y*z + 2*w*x)*z_old);
        float z_new = (float) ((2*x*z + 2*w*y)*x_old + (2*y*z - 2*w*x)*y_old + (1 - 2*x*x - 2*y*y)*z_old);

        newPoints.add(x_new);
        newPoints.add(y_new);
        newPoints.add(z_new);
    }

    return newPoints;
}

If i make this call rotation3D(list, Math.toRadians(90), 0, 1, 0); where points is (0,0,10), the output is (-10.0, 0.0, 2.220446E-15), but it should be (-10,0,0), right? Could someone take a look at my code and tell me if is there somethig wrong?

Here are 4 screens that represent the initial position of my object, and 3 rotations with -90 degrees (the object is not properly painted, that's a GL issue, that i will work on later):

initial position first rotation second rotation third rotation

4
  • 1
    Since 2.220446E-15 is such a small number, it seems like it is just a rounding issue within the system. Commented Mar 27, 2012 at 15:28
  • 1
    Just a comment: The cos and sin are computed on constant values. Get them out of the loop. Commented Mar 27, 2012 at 15:42
  • Indeed, i could optimise the code a bit. Thanks! Commented Mar 27, 2012 at 17:34
  • Is the method require so many parameters? I understand the float[], which represents the original coordinate of the object. I understand x,y,z , which are the values of quarternion. But why still need angle? Ans quarternion x,y,z values can create angle, although using complex calculation. Commented May 26, 2015 at 15:32

1 Answer 1

1

I haven't studied the code but what you get from it is correct: Assuming a left-handed coordinate system, when you rotate the point (0,0,10) 90 degrees around the y-axis (i.e. (0,1,0)) you end up with (-10,0,0).

If your coordinate system is right-handed I think you have to reverse the sign of the angle.

Sign up to request clarification or add additional context in comments.

8 Comments

my bad, i ment (-10,0,0), but the output is (-10.0, 0.0, 2.220446E-15), so not really accurate...
It's off by only 2*10^-15, a very small number. I'd call that pretty accurate.
I've used this method to rotate the points of a cube in 3D and it's not so accurate as it moves the object on the oZ axis with 10 units.
Then the result is wrong, the cube ends up in the wrong side of the coordinate axis? The result of the rotation does depend on the handedness of the coordinate system, see edit.
My system has oY going up, oZ facing the user, and oX perpendicular on oZ so, right-handed. With a negative angle, the cube is translated on the oX axis to the right (on the minus part of axis), but is also rotated. I'll add 2 screens so you can understand easily(and is a parallelepiped, not really a cube).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.