In my code, I have a quaternion that is used for rotation for a camera for the player. Rotation itself seems to work fine, but my directional vectors for moving and to rotate on do not rotate correctly.
Quaternion Multiplication:
Quaternion Quaternion::operator*(Vector3 other) const
{
float x_ = w * other.x + y * other.z - z * other.y;
float y_ = w * other.y + z * other.x - x * other.z;
float z_ = w * other.z + x * other.y - y * other.x;
float w_ = -x * other.x - y * other.y - z * other.z;
return Quaternion(x_, y_, z_, w_);
}
Quaternion Quaternion::operator*(Quaternion other) const
{
Vector4 r = other.getValues();
float x_ = x * r.w + w * r.x + y * r.z - z * r.y;
float y_ = y * r.w + w * r.y + z * r.x - x * r.z;
float z_ = z * r.w + w * r.z + x * r.y - y * r.x;
float w_ = w * r.w - x * r.x - y * r.y - z * r.z;
return Quaternion(x_, y_, z_, w_);
}
Conjugate Function
Quaternion Quaternion::conjugate() const
{
return Quaternion(-x, -y, -z, w);
}
Vector Rotation:
void Vector3::rotate(Quaternion rotation)
{
Quaternion rotated = rotation * *this * rotation.conjugate();
x = rotated.getValues().x;
y = rotated.getValues().y;
z = rotated.getValues().z;
}
Sample Directional Vector:
Vector3 Quaternion::getRight() const
{
Vector3 right(1.0f, 0.0f, 0.0f);
right.rotate(*this);
return right;
}
If I have the camera rotated exactly 90 degrees around the y-axis and I print out the values of the right vector, x is 0.000796229, y is 0 and z is -1. In this case, x should be 0 and z should be a positive 1.
I have been browsing Google and others' code for the past few days trying to find what I am doing wrong, but I cannot find anything wrong.
UPDATE:
I ended up deciding to just incorporate GLM into my math classes and after some changing of things, everything works as it should.
I ended up deciding to just incorporate GLM into my math classes and after some changing of things, everything works as it should.