rotation matrix does not work

Hello, I am playing with the code to learn the rotation matrix, trying to rotate a single point in the vector, yet without correct results, and cannot understand what is wrong. I know that if we rotate the point (0, 3, 0) around the z-axis by an angle of T = 60° we should get (-2.6, 1.5, 0) yet I am getting (0.91, -3.1, 0). I double checked everything but it is clear that I am missing some things here in general.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  int main(int argc, char **argv) {
    vector<float> x_coords {0.0f};
    vector<float> y_coords {3.0f};
    vector<float> z_coords {0.0f};

    rotateZ(60, x_coords,y_coords);
    rotateY(0, x_coords,z_coords);
    rotateX(0, y_coords,z_coords);


    for (size_t i{0}; i < x_coords.size(); i++)
        cout << x_coords[i] << " ";
    cout << endl;

    for (size_t i{0}; i < y_coords.size(); i++)
        cout << y_coords[i] << " ";
    cout << endl;

    for (size_t i{0}; i < z_coords.size(); i++)
        cout << z_coords[i] << " ";
    cout << endl;

}

void rotateZ (float angle, vector<float> &x_coords, vector<float>&y_coords){
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<x_coords.size(); i++){
        x_coords[i] = x_coords[i] * cosTheta - y_coords[i] * sinTheta;
        y_coords[i] = y_coords[i] * cosTheta + x_coords[i] * sinTheta;
    }
}

void rotateY (float angle, vector<float> &x_coords, vector<float>&z_coords){
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<x_coords.size(); i++){
        x_coords[i] = x_coords[i] * cosTheta - z_coords[i] * sinTheta;
        z_coords[i] = z_coords[i] * cosTheta + x_coords[i] * sinTheta;
    }
}

void rotateX (float angle, vector<float> &y_coords, vector<float>&z_coords){
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<z_coords.size(); i++){
        z_coords[i] = z_coords[i] * cosTheta + y_coords[i] * sinTheta;
        y_coords[i] = y_coords[i] * cosTheta - z_coords[i] * sinTheta;
    }
}
Problem 1:
In all (decent) computer languages, sine and cosine work in radians - you are using degrees only.



Problem 2:
Consider
1
2
        x_coords[i] = x_coords[i] * cosTheta - y_coords[i] * sinTheta;
        y_coords[i] = y_coords[i] * cosTheta + x_coords[i] * sinTheta;

The first line will change x_coords[i]. But you wanted the previous value for the second line ... but you've already overwritten it.

Keep copies and use those on the RHS.



Problem 3:
You haven't given complete, compileable code.
Last edited on
Thank you. I fixed problem 1 & 2, but strangely only rotation around X axis gives the intended results, while rotation around Z axis gives nothing. I checked the rotation matrix several times, can't see any mistakes there. So just to repeat , I have point (0,3,0), rotating it 60 degrees around X axis should give and gives (0, 1.5, 2.6), while rotating it 60 degrees around Z axis should give (-2.6, 1.5, 0), but gives (0, 3, 0). Full code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include<vector>
#include<cctype>
#include<cmath>

using namespace std;

void rotateX (float, vector<float>&, vector<float>&, vector<float>&, vector<float>&);
void rotateY (float, vector<float>&, vector<float>&, vector<float>&, vector<float>&);
void rotateZ (float, vector<float>&, vector<float>&, vector<float>&, vector<float>&);

int main(int argc, char **argv) {
    vector<float> x_coords {0.0f};
    vector<float> y_coords {3.0f};
    vector<float> z_coords {0.0f};
    vector<float> rot_x_coords {0.0f};
    vector<float> rot_y_coords {0.0f};
    vector<float> rot_z_coords {0.0f};

    rotateZ(60, x_coords,y_coords, rot_x_coords, rot_y_coords);
    rotateY(0, x_coords,z_coords, rot_x_coords,rot_z_coords);
    rotateX(0, y_coords,z_coords, rot_y_coords, rot_z_coords);


    for (size_t i{0}; i < rot_x_coords.size(); i++)
        cout << rot_x_coords[i] << " ";
    cout << endl;

    for (size_t i{0}; i < rot_y_coords.size(); i++)
        cout << rot_y_coords[i] << " ";
    cout << endl;

    for (size_t i{0}; i < rot_z_coords.size(); i++)
        cout << rot_z_coords[i] << " ";
    cout << endl;

}

void rotateZ (float angle, vector<float> &x_coords, vector<float>&y_coords,
               vector<float> &rot_x_coords, vector<float>&rot_y_coords){
    angle = angle * M_PI / 180.0;
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<x_coords.size(); i++){
        rot_x_coords[i] = x_coords[i] * cosTheta - y_coords[i] * sinTheta;
        rot_y_coords[i] = y_coords[i] * cosTheta + x_coords[i] * sinTheta;
    }
}

void rotateY (float angle, vector<float> &x_coords, vector<float>&z_coords,
              vector<float> &rot_x_coords, vector<float>&rot_z_coords){
    angle = angle * M_PI / 180.0;
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<x_coords.size(); i++){
        rot_x_coords[i] = x_coords[i] * cosTheta + z_coords[i] * sinTheta;
        rot_z_coords[i] = z_coords[i] * cosTheta - x_coords[i] * sinTheta;
    }
}

void rotateX (float angle, vector<float> &y_coords, vector<float>&z_coords,
              vector<float> &rot_y_coords, vector<float>&rot_z_coords){
    angle = angle * M_PI / 180.0;
    float sinTheta = sin (angle);
    float cosTheta = cos (angle);
    for (size_t i{0}; i<z_coords.size(); i++){
        rot_y_coords[i] = y_coords[i] * cosTheta - z_coords[i] * sinTheta;
        rot_z_coords[i] = z_coords[i] * cosTheta + y_coords[i] * sinTheta;
    }
}

}
If you only want to rotate about the Z axis ... then comment out the other two rotations (lines 21 and 22)!


You don't need "before" and "after" coordinates in the procedure arguments.
Within the loop ... store the old coordinates ... use those stored values to update the current coordinates. It would also be more natural just to pass one set of X, Y, Z at a time to the functions, not a whole vector of them.

You are making the problem far too difficult.
Just learning in a hard way... thank you very much, I still don't have any programming skills so missing obvious stuff,

A.
Topic archived. No new replies allowed.