# Matrix Question - Handling translation in world space.

This topic is 990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there.  X)

I've been learning matrices, and they're very interesting.  There is one thing I can't seem to wrap my head around, and I haven't had much luck finding any examples or explanations.  I was hoping someone could maybe point me in the right direction here.

I get that you can multiply in particular orders to combine translation, rotation, and scaling matrices and then apply this transformation to a vector.  However, what has me scratching my head is how this gets implemented in an actual game.  Especially regarding translation.

Up until now I've been working with particles and shapes, as code objects, where I store their position as X and Y properties.  This doesn't work out so well with a matrix rotation.  Some articles mention that you must translate back to the origin before doing a rotation.  It seems it might get expensive to keep doing this.  It's also not too clear on how this is achieved in code when dealing with game objects.  The assumption I thought was that the transformation is supposed to provide you the results, and not so much that the results will repeatedly be used to construct the next transformation matrix.

I got to thinking and playing around with having the matrices behave as a local coordinate system for the object, assuming it's center or centroid is the origin.  Then using it's X and Y properties as a global/world point for the rendering to use as an offset.  So my translation ends up actually just the same as before I began using matrices, a method that adds and subtracts from the X and Y position of the object.  It begins to feel kind of pointless to use matrices at all if I'm resorting to this.

Even using the matrices as local transformations, it seems after you do the first translation, the rotation is going to never again rotate around it's center point.  At least until you move it back, then move it back again to where it was.  Translation in this sense to me seems to be something that simply moves the pivot point of a vector.

I'm having a hard time seeing how these get applied in a way where you're not doing a lot of extra operations, or creating something that reduces code and increases readability.

What are the general practices of implementing transformation matrices in games when it comes to translating locally and in a world coordinate system?

I don't know if that was framed concisely enough, but if anyone has a suggestion or a resource, it would be much appreciated.  ^_^

##### Share on other sites

Generally, for each game object you can store translation (vector3), scale (vector3, or maybe just a single float), and rotation (quaternion) separately.

Then, each frame (or whenever the object moves/rotates/changes size) combine them (scale, rotation, then translation) to get the final world matrix. Whatever framework you're using presumably has functions to do this.

##### Share on other sites

Generally, for each game object you can store translation (vector3), scale (vector3, or maybe just a single float), and rotation (quaternion) separately.

Then, each frame (or whenever the object moves/rotates/changes size) combine them (scale, rotation, then translation) to get the final world matrix. Whatever framework you're using presumably has functions to do this.

Thanks for the reply.  What would the combined matrix be applied to get the world matrix?  Are there typically coordinates for local and world space stored in every object as well?

##### Share on other sites
Wrap your transformation code inside a class that exposes a simple interface. You can change your internal implementation without effecting outside code.

class Transformation
{
public:
Transformation(const Vector3& position, const Quaternion& rotation);
Transformation(const Vector3& position, const Vector3& rollPitchYaw);

// transform a point from local space to world space
Vector3 TransformPoint(const Vector3& point) const;
// transform a direction from local space to world space
Vector3 TransformDirection(const Vector3& point) const;

// transform a point from world space to local space
Vector3 InverseTransformPoint(const Vector3& point) const;
// transform a direction from world space to local space
Vector3 InverseTransformDirection(const Vector3& point) const;

// combine two transforms into a single transform.
Transformation Concat(const Transformation& other);
};

The above class could be implemented with matrices or with an origin/rotation pair without changing the external interface. You could also add uniform scaling pretty easily to the transform. If you want to scale x and y independently you will need to allow your transform to skew objects, assuming you want to chain transforms. If that is the case, you will definitely want to go with matrices.

##### Share on other sites

What would the combined matrix be applied to get the world matrix?

Nothing (or the identity matrix). It is the world matrix.

Are there typically coordinates for local and world space stored in every object as well?

I'm not sure I understand your question. The object mesh has vertices in "local space". The world matrix is used (for example, by the vertex shader) to transform these into world space.

##### Share on other sites

Are there typically coordinates for local and world space stored in every object as well?

The local position/orientation/scale of an object are typically defined to be:

Position = 0,0,0
Scale = 1,1,1
Rotation = 0

These values give the identity matrix. Since Matrix A * Identity = Matrix A, we just don't store the local position/orientation/rotation anywhere and start off directly with the world matrix.

##### Share on other sites

I'm not sure I understand your question. The object mesh has vertices in "local space". The world matrix is used (for example, by the vertex shader) to transform these into world space.

Well I'm not doing anything with shaders yet.  I've just started learning, so I'm making objects that have an array of vertices, and rendering them with lines.

But I think I have a better understanding.  Every object would hold a matrix for it's translation coordinates into world space, and anything in the game loop will update that translation matrix.  So later in update/render it will get combined into a new matrix and all the points will be transformed, as if it were from a point at 0,0.

Is that the concept?

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013595
×