🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

What goes into a "Transform" structure in C/C++?

Started by
4 comments, last by PlanetExp 6 years, 1 month ago

As a beginner, I've been toying around with my own home brew game engine for the last few months to learn the ropes of game programming. In the book Game Engine Architecture by Jacob Gregory there is code snippet that looks like


void AddAxes(const Transform& xfm, ...);

In Box2D there is a simple Transform structure that seemingly holds a Rotation structure and a Position (vector or point is unbeknownst to me).

On the other spectrum, Unity has their own Transform class that holds just about anything relating to transforming something (scale, rotation, translation, child/parent transform relations and helper methods).

Being notoriously hard to google, if I'd implement my own Transform structure, What should it contain? Is it just a general transformation container abstraction or does it hold some kind of meaning in game programming?

I'm up to implementing a debug camera system so I'm wanting to use this said transform for more general purpose than just adding axes to the canvas.

 

Many thanks

Fred

Advertisement

The Transform class is usually responsible to track orientation of objects in the world and resolving parent-child spatial relationship between them. It can be implemented in a number of different ways. In my implementation I am storing:

  • Array of children transform pointers
  • parent transform pointer
  • parent inverse "rest" matrix : used to resolve parent-child relationship. It contains parent's inverse world matrix, at the time it was attached to the parent
  • "rest" translation, rotation, scale, worldmatrix : the original, unparented orientation
  • current translation, rotation, scale, worldmatrix : the calculated orientation after resolving parent information
  • previous translation, rotation, scale, worldmatrix : for generating velocity buffer (optional)

My memory is a bit hazy about this, because it was so long I did this, it can probably be simplified, optimized. I suggest you put in the time to implement this so that you can use it for anything general, even 2D GUI system. :)

 

2 hours ago, PlanetExp said:

Is it just a general transformation container abstraction or does it hold some kind of meaning in game programming?

There are no rules as to what a 'transform' might contain. You could put parent child relationships as part of a transform, or you might not. It might include for example a 4x4 matrix, and / or a position, quaternion and scale, or just a 2d offset. What it contains will depend on what you are using it for. The needs of e.g. a 3d modelling tool may be different to a physics engine or highly optimized renderer. Indeed you may end up having more than one transform structure within the same program.

Box2D has no scaling because scaling is not useful for a physics engine most of the time. Unity has scaling, because their transform is a general purpose tool to place anything anywhere with any rotate/scaling/translation.

Thank you for such incredibly helpful replies! It makes sense to have a transform in the rendering engine to describe all general transforms that applies to your game objects (and other objects too) in one neat container. In hindsight it seem quite self-explanatory. Why Jason Gregory have it in their debug code in the AddAxis method also make sense: you'd put a set of axis in the world after it's been transformed -- if the axis don't end up where you want probably means you got something wrong somewhere.

Many thanks again! Learning game programming has been an exhilarating, albeit somewhat painful experience :)

Fred

This topic is closed to new replies.

Advertisement