Does this do what I want it to do?

I'm trying to make a first person asteroid shooter game and I'm running into some difficulties with the math. I discovered that in order to turn a ship relative to itself, I need to be able to revolve a vector around another vector. This is above my current math skills so I just went to this website:

http://blog.kjeldby.dk/2008/10/rotation-around-an-vector-in-cxna/

and basically plugged it right into my code:

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
void Vector3D::revolveAround (const Vector3D &b, double degrees)
{
    // convert the degrees to radians
    double angle = degrees * M_PI / 180;

    // Set up the rotational matrix
    // a1 a2 a3
    // b1 b2 b3
    // c1 c2 c3
    double a1 = ((1 - cos (angle)) * b.x * b.x) + cos (angle);
    double a2 = ((1 - cos (angle)) * b.x * b.y) - (sin (angle) * z);
    double a3 = ((1 - cos (angle)) * b.x * b.z) + (sin (angle) * y);

    double b1 = ((1 - cos (angle)) * b.x * b.y) + (sin (angle) * z);
    double b2 = ((1 - cos (angle)) * b.y * b.y) + cos (angle);
    double b3 = ((1 - cos (angle)) * b.y * b.z) - (sin (angle) * x);

    double c1 = ((1 - cos (angle)) * b.x * b.z) - (sin (angle) * y);
    double c2 = ((1 - cos (angle)) * b.y * b.z) + (sin (angle) * x);
    double c3 = ((1 - cos (angle)) * b.z * b.z) + cos (angle);

    // The final vector
    Vector3D temp (*this);

    temp.x = a1 * x + a2 * y + a3 * z;
    temp.y = b1 * x + b2 * y + b3 * z;
    temp.z = c1 * x + c2 * y + c3 * z;

    *this = temp;
    
    return;
}


but I'm worried, because if this doesn't work, I'm not going to have a clue how to fix it. Is the math sound?
Note that I haven't troughly gone through the link in your post but the idea is pretty basic here.

Pretty much you have a matrix that will describe your position.

For example, matrix below represents a set of points (A, B) that form a line with slope 1. Now if I wanted to reflect this matrix about the Y-axis, I would want to multiply it by a value (a matrix) such that a point is now [-1, 1]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
What I'm giving:
    A   B
X  [1   0]
Y  [1   0]

What I'm multiplying by:
    A   B
X  [1   0] [-1 0]
Y  [1   0] [0  1]

Reflected (what I want):
    A   B
X  [1   0] [-1 0]   =  [-1 0]
Y  [1   0] [0  1]   =  [1  0]


I just simple points put inside a matrix to illustrate the basic point but you can do this with vectors as well. The notion I've shown is very common.
Last edited on
Does this do what I want it to do?

No idea. Did you try it for a few known vectors? I like how the author explained matrix multiplication but didn't explain how to actually derive the matrix.

You may want to look into quaternions. Check out the following link and pay special attention to the "Rotating vectors" section:

http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation#Rotating_vectors
Topic archived. No new replies allowed.