• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Efficient Interpolation and Matrices 4x4

7 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 


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
0

Share this post


Link to post
Share on other sites

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.  

0

Share this post


Link to post
Share on other sites

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...

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0