Given the following coordinate system1, where positive z
goes towards the ceiling:
I have a glm::vec3
called dir
representing a (normalized) direction between two points A
and B
in 3D space2:
The two points A
and B
happen to be on the same plane, so the z
coordinate for dir
is zero. Now, given an angle α
, I would like to rotate dir
towards the ceiling by the specified amount. As an example, if α
is 45 degrees, I would like dir
to point in the same x
/y
direction, but 45 degrees towards the ceiling3:
My original idea was to calculate the "right" vector of dir
, and use that as a rotation axis. I have attempted the following:
glm::vec3 rotateVectorUpwards(const glm::vec3& input, const float aRadians)
{
const glm::vec3 up{0.0, 0.0, 1.0};
const glm::vec3 right = glm::cross(input, glm::normalize(up));
glm::mat4 rotationMatrix(1); // Identity matrix
rotationMatrix = glm::rotate(rotationMatrix, aRadians, right);
return glm::vec3(rotationMatrix * glm::vec4(input, 1.0));
}
I would expect that invoking rotateVectorUpwards(dir, glm::radians(45))
would return a vector representing my desired new direction, but it always returns a vector with a zero z
component.
I have also attempted to represent the same rotation with quaternions:
glm::quat q;
q = glm::rotate(q, aRadians, right);
return q * input;
But, again, the resulting vector always seems to have a zero z
component.
What am I doing wrong?
- Am I misunderstanding what the "axis of rotation" means?
- Is my
right
calculation incorrect?
How can I achieve my desired result?