1

I'm representing rotations for actors in my rendering engine using a vec4 with axis-angle notation. The first 3 components (x, y, z) represent the (normalized) axis of rotation, and the last component (w) represents the angle (in radians) we have rotated about this axis.

For example,

  • With axis (0, 1, 0) and angle 0, up is (0, 1, 0) and forward is (0, 0, -1).
  • With axis (0, 0, 1) and angle 180, up is (0, 0, 1) and forward is (0, -1, 0).

My current solution (which doesn't work), looks like this:

// glm::vec4 Movable::getOrientation();
// glm::vec3 FORWARD(0.0f, 0.0f, -1.0f);
glm::vec3 Movable::getForward() {
   return glm::vec3(glm::rotate(
    this->getOrientation().w, glm::vec3(this->getOrientation())) *
    glm::vec4(FORWARD, 1.0f));
}

I've defined the up direction to be the same as the rotational axis, but I'm having trouble calculating the forward directional vector for an arbitrary axis. What is the easiest way to do this? I'd like to take advantage of glm functions wherever possible.

chpatton013
  • 363
  • 1
  • 5
  • 11

1 Answers1

1

One thing to keep in mind about axis-angle is that "up" should mean the same thing for all rotations with an angle of 0, as that represents no rotation no matter which direction the axis is pointed ... you can't just say up is in the direction of the axis. The proper way to calculate forward and up is to start with two vectors which represent them, say (1,0,0) for forward and (0,1,0) for up, and then apply the rotation to both those vectors to obtain the new forward and up.

Edward Doolittle
  • 4,002
  • 2
  • 14
  • 27
  • I've just updated my post with my current implementation. I thought I was taking what you suggest into account by rotating a global FORWARD vector, but I'm not getting correct results. – chpatton013 Apr 21 '15 at 01:43
  • You need to do the same with a global UP vector. – Edward Doolittle Apr 21 '15 at 01:51
  • Why is that necessary? Isn't the axis of rotation the same thing? – chpatton013 Apr 21 '15 at 02:50
  • No, the axis of rotation can't be up. Say object starts with up as the y-axis. A rotation about the z-axis with angle of rotation epsilon (very small) will displace the "up" vector by a small amount (of order of magnitude epsilon). But the z-axis and y-axis are very far apart. In other words, your scheme will introduce massive, weird discontinuities near angle 0. – Edward Doolittle Apr 21 '15 at 03:26
  • I'm not sure I follow. I should be able to rotate around my up-vector without any trouble. In fact, this is the only use case where my errant code seems to work correctly. – chpatton013 Apr 21 '15 at 03:57
  • Continuity is one test you can apply to a mathematical scheme to see if it makes sense. I applied it to your scheme, and found that it's not continuous when angle = 0. You might be able to make it work, somehow, but I think it's not worth it. Just try rotating UP as well as FORWARD and see what happens. – Edward Doolittle Apr 21 '15 at 04:29
  • After thinking over your explanation, I realized that I had a fundamental misunderstanding of how rotations... worked, i guess. Anyway, I switched to using quaternions to represent my rotations and everything is working beautifully now. Thanks for your help! – chpatton013 Apr 21 '15 at 19:11