Custom C++ Quaternion Rotation Not Working Correctly -
in code, have quaternion used rotation camera player. rotation seems work fine, directional vectors moving , rotate on 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 have camera rotated 90 degrees around y-axis , print out values of right vector, x 0.000796229, y 0 , z -1. in case, x should 0 , z should positive 1.
i have been browsing google , others' code past few days trying find doing wrong, cannot find wrong.
update:
i ended deciding incorporate glm math classes , after changing of things, works should.
in vector rotation replace first line by
quaternion rotated = rotation * quaternion(this->x, this->y, this->z, 0) * rotation.conjugate();
explanation: quaternion multiplications not commutative, means q * v not equal v * q, v being quaternion w=0.
Comments
Post a Comment