View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Efficient Interpolation and Matrices 4x4

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Danicco  Members

Posted 17 February 2014 - 06:33 AM

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?

### #2Buckeye  GDNet+

Posted 17 February 2014 - 08:08 AM

I don't how practical it would be to change your code, but, because you store the rotation as a vector, you do a calc on the vector, create a quaternion, and then create a matrix from the quaternion. If you store the rotation initially as a quat, you can SLERP (rather than LERP) the previous and next quats, and make a matrix from the result. That may provide a little more efficiency. Storing rotations as quats is a common approach as your Orientation class simulates a decomposed matrix. A matrix decomposition normally results in a (rotation) quaternion and two (scale & translate) vectors, anyway.

With regard to the number of calculations you're doing, i.e., doing an interpolation of each node in the hierarchy - that's necessary as you've found.

I'm not sure exactly sure what your comment about the negative translation is about, but do you have to do that for each child? Wouldn't translating the entire hierarchy by translating just the root frame work? That may also be the source of the child rotation problem.

When you multiply the child interpolated matrix by the parent interpolated matrix, are you sure the parent was interpolated first? You can ensure of the proper order by starting at the root frame and recursively move up the hierarchy.

function CalcInterpolation( object, parentMatrix )
{
object.ObjectMatrix = Lerp( object ) * parentMatrix;
for each child in object:
{
CalcInterpolation( child, object.ObjectMatrix );
}
}


Then calculate the hierarchy with:

CalcInterpolation( rootObject, worldMatrix ); // can world do the negative translation?

EDIT: I don't know how your Matrix constructor works, but it appears you're using row-vector matrices (vs column vector). If that's true, you should be doing SRT rather than TRS. (SRT = Scale-Rotate-Translate) That may be the child rotation problem.

Edited by Buckeye, 17 February 2014 - 08:22 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #3Danicco  Members

Posted 17 February 2014 - 09:20 AM

Oh the rotation problem was that I was multiplying in the wrong order:

Matrix4x4 objectMatrix = thisObject.GetMatrix() * parentObject.GetMatrix();

//Should be

Matrix4x4 objectMatrix = parentObject.GetMatrix() * thisObject.GetMatrix();


Hmm currently I don't keep track on what object has childs or not, they're all in a flat vector and I simply iterate and draw them (checking if they should appear or not).

If one object has a parent (pointer to it's parent orientation), I'd multiply it as well and that's it.

But I guess it would be better to separate child/parents in the scene so I can ensure I'd interpolate the parent then recursively go through it's children.

Thank you for the suggestion!

### #4Buckeye  GDNet+

Posted 17 February 2014 - 10:11 AM

I'd interpolate the parent then recursively go through it's children.

Yep. No need to interpolate for every child. Once is enough, provided it's done before the child needs it. Otherwise, you'd do the parent calc for every child? No.

You may want to take a look at your algorithm. If each parent knows its children, does a child really need to know its parent? Rhetorical question - just something to consider.

Edited by Buckeye, 17 February 2014 - 10:13 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #5BornToCode  Members

Posted 17 February 2014 - 03:42 PM

I'd interpolate the parent then recursively go through it's children.

Yep. No need to interpolate for every child. Once is enough, provided it's done before the child needs it. Otherwise, you'd do the parent calc for every child? No.

You may want to take a look at your algorithm. If each parent knows its children, does a child really need to know its parent? Rhetorical question - just something to consider.

There is nothing wrong with the the children knowing his parents. Because then i t becomes alot faster to figure out which children belongs to which parents. No need to interate over each parent looking for that specific child. One of the thing you can do instead of doing rotation linearly, you should consider using Spherical linear interpolation instead as it will give you more accurate rotation.

Edited by BornToCode, 17 February 2014 - 03:44 PM.

### #6ferrous  Members

Posted 17 February 2014 - 05:07 PM

Is your rotation a set of Euler angles?  If so, I'd definitely heed the advice of others, and move towards quaternions and interpolating them instead.

### #7Danicco  Members

Posted 18 February 2014 - 03:43 PM

About the Euler angles, I have them so it's easier to program the game. I'm not making a game at the moment, only the engine, and to make it easier I left a "Vector3 Rotation" so the game programmer can just change it's XYZ values and rotate stuff.

By leaving as quaternions I'd have to calculate everytime a value has changed and that opens room for errors... I do have a quaternion variable to keep track the rotation and I use it directly when it's something automatically computed such as a Rotate function.

I'm still balancing how much easier I want to leave the game programming at the cost of faster/easier functions.

You may want to take a look at your algorithm. If each parent knows its children, does a child really need to know its parent? Rhetorical question - just something to consider.

I was just changing the algorithm now and I remembered why I did it this way, if I have the parent to keep track of their children, I'd need to have an array/vector for all objects since they *might* have children.

But if the objects only keep track of their parents, I only need a single pointer for each object, since objects can't have more than one (direct) parent.

I'm not sure how effective this is (I'm guessing close to null) but it seemed a really good idea at first...

### #8Buckeye  GDNet+

Posted 18 February 2014 - 03:56 PM

I'd need to have an array/vector for all objects since they *might* have children.

Memory's cheap; performance is expensive. Just sayin'. Certainly for you to decide.

struct TEST
{
std::vector<int> children;
};

size_t testSize = sizeof(TEST); // 16 bytes


Edited by Buckeye, 18 February 2014 - 03:58 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.