Results 1 to 4 of 4

Thread: rotations in 3d space

  1. #1

    rotations in 3d space

    Hi, I'm trying to write my own rotation code to handle rotation of my own vector structure. I got the mathematical transformations on the internet, and came up with these three functions:

    void rot_x(vec_p a, GLfloat r)
    a->y = a->y*cos(r) - a->z*sin(r);
    a->z = a->y*sin(r) + a->z*cos(r);
    a->x = a->x;

    void rot_y(vec_p a, GLfloat r)
    a->z = a->z*cos(r) - a->x*sin(r);
    a->x = a->z*sin(r) + a->x*cos(r);
    a->y = a->y;

    void rot_z(vec_p a, GLfloat r)
    a->x = a->x*cos(r) - a->y*sin(r);
    a->y = a->x*sin(r) + a->y*cos(r);
    a->z = a->z;

    where vec is structure of {GLfloat x, y, z}.

    These seem to work, the problem is, after many rotations the vectors seem to "lose their shape"...they become shorter and shorter until they no longer have any magnitude. It's almost as if they "lose" a bit of vector on every rotation, cumulatively adding up to a decrease in size. Does anyone know what this is caused by? I thought it might be innaccuracy caused by using GLfloat instead of doubles, or something in the sine/cosine functions. Any fix would be greatly appreciated.

  2. #2

    Re: rotations in 3d space

    I don't know why the functions don't work, they look ok to me (I have not checked the Euler rotation matrix so there might be a mathematical error). Try multiplying your vectors (vertices) by a single orthogonal rotation matrix. This way you will only need one function for every possible rotation.

    You could also try using polar coordinate vectors for your rotations. These preserve the length of a vector very well (it is always constant) but you will need functions for polar-rectangular and vice-versa conversions.

    My ultimate advice would be to use quaternions. I have had many problems with Euler methods and switching to quaternion methods made my life 10 times better.

    Hope this helps...


  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000

    Re: rotations in 3d space

    You're making a classic mistake. Take rot_x as an example. To calculate a->z, you need a->y, but you're modifying a->y on the line above. a->y is no longer the same value you pass to the function. Solution: make a copy of the vector you pass to the function and use that instead when calculating new values.

  4. #4

    Re: rotations in 3d space

    right...thanks Bob that was the problem.

    Thanks a lot


Similar Threads

  1. Camera space shadow mapping clip space issues
    By fredvam in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 09-27-2015, 04:58 PM
  2. Replies: 3
    Last Post: 09-16-2014, 08:23 AM
  3. Computing radiance in world space, object space or view space?
    By shapeare in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-25-2013, 06:22 PM
  4. what is the fastest algorithm for mapping texture space to file space?
    By wildeyedboyfromfreecloud in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 03-06-2002, 08:07 PM
  5. Object Rotations in space
    By Cardinals33 in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 03-31-2001, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean