1

I have a function in my program which rotates a point (x_p, y_p, z_p) around another point (x_m, y_m, z_m) by the angles w_nx and w_ny.

The new coordinates are stored in global variables x_n, y_n, and z_n. Rotation around the y-axis (so changing value of w_nx - so that the y - values are not harmed) is working correctly, but as soon as I do a rotation around the x- or z- axis (changing the value of w_ny) the coordinates aren't accurate any more. I commented on the line I think my fault is in, but I can't figure out what's wrong with that code.

void rotate(float x_m, float y_m, float z_m, float x_p, float y_p, float z_p, float w_nx ,float w_ny)
    {
        float z_b = z_p - z_m;
        float x_b = x_p - x_m;
        float y_b = y_p - y_m;
        float length_ = sqrt((z_b*z_b)+(x_b*x_b)+(y_b*y_b));
        float w_bx = asin(z_b/sqrt((x_b*x_b)+(z_b*z_b))) + w_nx;
        float w_by = asin(x_b/sqrt((x_b*x_b)+(y_b*y_b))) + w_ny; //<- there must be that fault
        x_n = cos(w_bx)*sin(w_by)*length_+x_m;
        z_n = sin(w_bx)*sin(w_by)*length_+z_m;
        y_n = cos(w_by)*length_+y_m;
    }
4
  • 1
    If your using opengl i'm sure a graphics library would have a matrix and vectors implantation you can use. rotating a point via a matrix will simplify your algorithm.
    – andre
    Nov 7, 2012 at 18:37
  • 2
    @ahenderson Did you mean "implementation"? ;)
    – leemes
    Nov 7, 2012 at 18:53
  • If you are not already familiar with the term, you should read up on "Gimbal Lock". I can't immediately tell if your code could suffer from it, but it is a common flaw when you are doing this sort of rotation using "Euler Angles".
    – Rook
    Nov 7, 2012 at 19:15
  • i tried to use glrotatef, but it only turns the drawing, not the coordinates of the vertexes, and i need to rotate the coordinates in order to check for collisions!
    – Andi Faust
    Nov 7, 2012 at 19:26

4 Answers 4

5

What the code almost does:

  • compute difference vector
  • convert vector into spherical coordinates
  • add w_nx and wn_y to the inclination and azimuth angle (see link for terminology)
  • convert modified spherical coordinates back into Cartesian coordinates

There are two problems:

  • the conversion is not correct, the computation you do is for two inclination vectors (one along the x axis, the other along the y axis)
  • even if computation were correct, transformation in spherical coordinates is not the same as rotating around two axis

Therefore in this case using matrix and vector math will help:

b = p - m
b = RotationMatrixAroundX(wn_x) * b
b = RotationMatrixAroundY(wn_y) * b
n = m + b

basic rotation matrices.

4

Try to use vector math. Decide in which order you rotate, first along x, then along y perhaps.

If you rotate along z-axis, [z' = z]

x' = x*cos a - y*sin a;
y' = x*sin a + y*cos a;  

The same repeated for y-axis: [y'' = y']

x'' = x'*cos b - z' * sin b;
z'' = x'*sin b + z' * cos b;  

Again rotating along x-axis: [x''' = x'']

y''' = y'' * cos c - z'' * sin c
z''' = y'' * sin c + z'' * cos c

And finally the question of rotating around some specific "point":

First, subtract the point from the coordinates, then apply the rotations and finally add the point back to the result.

The problem, as far as I see, is a close relative to "gimbal lock". The angle w_ny can't be measured relative to the fixed xyz -coordinate system, but to the coordinate system that is rotated by applying the angle w_nx.

As kakTuZ observed, your code converts point to spherical coordinates. There's nothing inherently wrong with that -- with longitude and latitude, one can reach all the places on Earth. And if one doesn't care about tilting the Earth's equatorial plane relative to its trajectory around the Sun, it's ok with me.

The result of not rotating the next reference axis along the first w_ny is that two points that are 1 km a part of each other at the equator, move closer to each other at the poles and at the latitude of 90 degrees, they touch. Even though the apparent purpose is to keep them 1 km apart where ever they are rotated.

8
  • 1
    why do you use 3 angles? 2 are enough, and from where do i get y''?
    – Andi Faust
    Nov 7, 2012 at 18:33
  • 1
    @AndiFaust There are z'=z, y''=y', x'''=x'' missing. The three computations are matrix multiplications with "rotation matrices".
    – leemes
    Nov 7, 2012 at 18:54
  • If you rotate along y-axis, y doesn't change etc. Nov 7, 2012 at 18:58
  • that all seems fine, but i've made it work so far - can anyone just please tell me where the fault in my code is? please!
    – Andi Faust
    Nov 7, 2012 at 19:24
  • 1
    @AkiSuihkonen I'm not sure, but I think this solution is wrong. You have to correct the following lines: z'' = z'*sin b + x' * cos b; -> z'' = x'*sin b + z' * cos b; and z''' = z'' * sin c + y'' * cos c -> z''' = y'' * sin c + z'' * cos c. Does this make sense?
    – nevrome
    Jan 24, 2017 at 16:27
0

if you want to transform coordinate systems rather than only points you need 3 angles. But you are right - for transforming points 2 angles are enough. For details ask Wikipedia ...

But when you work with opengl you really should use opengl functions like glRotatef. These functions will be calculated on the GPU - not on the CPU as your function. The doc is here.

2
  • i tried to use glrotatef, but it only turns the drawing, not the coordinates, and i need to rotate the coordinates in order to check for collisions
    – Andi Faust
    Nov 7, 2012 at 19:14
  • Oh, you want to do manual collusion checking ... I've implemented one when I studied and it was a pain and slow. Perhaps you should consider using a physic engine
    – Andreas
    Nov 7, 2012 at 20:04
0

Like many others have said, you should use glRotatef to rotate it for rendering. For collision handling, you can obtain its world-space position by multiplying its position vector by the OpenGL ModelView matrix on top of the stack at the point of its rendering. Obtain that matrix with glGetFloatv, and then multiply it with either your own vector-matrix multiplication function, or use one of the many ones you can obtain easily online.

But, that would be a pain! Instead, look into using the GL feedback buffer. This buffer will simply store the points where the primitive would have been drawn instead of actually drawing the primitive, and then you can access them from there.
This is a good starting point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.