I'm trying to improve my matrices calculations, and I'm having trouble with figuring how to deal with quaternions.
I have an Orientation class which has Vector3s Position/Center/Scale, and a Quaternion for Rotation.
I've read everywhere of how quaternions are faster than matrix, and how I should hide the euler angles and use quaternions for rotations internally, and only use euler angles if I have to expose or if I have an interface, and that's why I implemented them, but I'm having trouble actually doing this.
This Orientation class is inherited to my SceneObject class, which is inherited by all "useable" objects in code.
I'm coding an engine not just for myself, but for other programmers as well, so I'm trying to get the classes they'll use as easy and fast as possible.
So I'd like them to be able to:
Image* someImage = Resources.Load<Image>("imageName");
someImage->position.x = 10.0f;
someImage->rotation.x = 45.0f; //This line is the problem
I was using code to add rotations to the quaternion:
void Orientation::SetRotation(float axisX, float axisY, float axisZ, float angleDegrees)
{
float angleRadians = angleDegrees * PI / 180;
float cAngle = cosf(angleRadians/2);
float sAngle = sinf(angleRadians/2);
_rotation.w = cAngle;
_rotation.x = axisX * sAngle;
_rotation.y = axisY * sAngle;
_rotation.z = axisZ * sAngle;
//I have a similar function to this that ADDS the rotation to the current one
}
But I recently changed to something easier because I'm trying to use this Quaternion/Orientation class for my Camera Matrix:
Quaternion(const Vector3& eulerAngles)
{
Vector3 c = Vector3(std::cos(eulerAngles.x * 0.5), std::cos(eulerAngles.y * 0.5), std::cos(eulerAngles.z * 0.5));
Vector3 s = Vector3(std::sin(eulerAngles.x * 0.5), std::sin(eulerAngles.y * 0.5), std::sin(eulerAngles.z * 0.5));
w = c.x * c.y * c.z + s.x * s.y * s.z;
x = s.x * c.y * c.z - c.x * s.y * s.z;
y = c.x * s.y * c.z + s.x * c.y * s.z;
z = c.x * c.y * s.z - s.x * s.y * c.z;
}
I wasn't able to get the proper values from the Quaternion to set my Camera Matrix, and these are the two main problems I'm having, I don't know to set up a Camera Matrix, and how to avoid using the euler angles and just stick to quaternion math.
For now I'm using the GLM's lookAt function, and I have a Quaternion for the camera rotation, and when I call Camera.RotateX() or Camera.RotateY(), I multiply the Vector3 cameraTarget and this rotation quaternion, but I think this could be better.
I'm calculating the rotation first, to get the vector, and then do the camera matrix calculation with the vector.
Isn't there a way I can keep track of the camera's values and generate/calculate everything only once?
And is there a way I can keep the rotation values exposed (it doesn't need to be euler angles) from the quaternion class and be able to calculate it only once?
Currently if I keep the Quaternion's SetRotation() function, I calculate a new quaternion and multiply it everytime it is called. The value could change due to various factors, and not straightly at once, in the same update tick, ex. changes once because of physics, another because of player input, and one more time because of an object's event. Instead of calculating 3 times, is there a way to do the calculation only once?