Hi,
I have this class:
class Orientation
{
public:
Vector3 Position;
Vector3 Center;
Vector3 Rotation;
Matrix4x4 GetMatrix();
};
And this class is a regular screen object:
class Image
{
public:
Orientation currentOrientation;
Orientation previousOrientation;
};
When I draw, I interpolate them with this:
inline Matrix4x4 Lerp(Orientation& currentOrientation, Orientation& previousOrientation, float& interpolation)
{
Matrix4x4 result;
float positionX = previousOrientation.position.x + ((currentOrientation.position.x - previousOrientation.position.x) * interpolation);
float positionY = previousOrientation.position.y + ((currentOrientation.position.y - previousOrientation.position.y) * interpolation);
float positionZ = previousOrientation.position.z + ((currentOrientation.position.z - previousOrientation.position.z) * interpolation);
float scaleX = previousOrientation.scale.x + ((currentOrientation.scale.x - previousOrientation.scale.x) * interpolation);
float scaleY = previousOrientation.scale.y + ((currentOrientation.scale.y - previousOrientation.scale.y) * interpolation);
float scaleZ = previousOrientation.scale.z + ((currentOrientation.scale.z - previousOrientation.scale.z) * interpolation);
float rotationX = previousOrientation.rotation.x + ((currentOrientation.rotation.x - previousOrientation.rotation.x) * interpolation);
float rotationY = previousOrientation.rotation.y + ((currentOrientation.rotation.y - previousOrientation.rotation.y) * interpolation);
float rotationZ = previousOrientation.rotation.z + ((currentOrientation.rotation.z - previousOrientation.rotation.z) * interpolation);
//Calculate the new Matrix4x4 with these values
Quaternion rotation = Quaternion(Vector3(rotationX, rotationY, rotationZ));
result = Matrix4x4(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
positionX, positionY, positionZ, 1.0f) *
rotation.GetMatrix() *
Matrix4x4(scaleX, 0.0f, 0.0f, 0.0f,
0.0f, scaleY, 0.0f, 0.0f,
0.0f, 0.0f, scaleZ, 0.0f,
-currentOrientation.center.x, -currentOrientation.center.y, -currentOrientation.center.z, 1.0f);
//The center is negative because of my screen orientation (X,Y 0,0 at top left) so nvm this
return result;
}
This works fine, but now I'm adding object hierarchy and child objects multiply their Matrix4x4 for their parent's;
if(parentObject != 0) //parentObject is an Orientation object pointer
{
Matrix4x4 objectMatrix = Lerp(currentOrientation, previousOrientation, interpolation) * parentObject->GetMatrix();
}
But this way I'm not computing the interpolation for the parent object, so I lose interpolation in the child objects.
If I calculate two Lerps(), one for the object and another for the parent, it works, but I get a massive drop in FPS for a single object. I've read that I shouldn't take FPS as measurement of performance but I couldn't help but worry if I'm doing something bad for interpolation.
Is there a better way to handle this?
And how can I efficiently interpolate positions when I have an object hierarchy?
Also, when a parent object rotates, the child object rotates around itself, but it should rotate around the parent's XYZ... just multiplying the matrices doesn't work for this, how can I do this properly?