Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Order of Matrix Multiplications in Transformations #4

Open
leAdmin666 opened this issue Jun 6, 2024 · 1 comment
Open
Assignees
Labels
Bug Something isn't working Enhancement New feature or request

Comments

@leAdmin666
Copy link
Contributor

Background

In 3D graphics, transforming objects involves applying a series of operations: scaling, rotation, and translation. These operations are represented by matrices and are applied in a specific order to achieve the desired transformation. The correct order of applying these transformations is crucial for ensuring that the object behaves as expected.

Problem

The current implementation of the Transform class in our project applies transformations in the order of translation, rotation, and then scaling:

void UpdateModelMatrix()
{
    Matrix4x4 scaleMatrix = Matrix4x4.CreateScale(_scale);
    Matrix4x4 rotationMatrix = _rotation.ToRotationMatrix();
    Matrix4x4 translationMatrix = Matrix4x4.CreateTranslate(_position);

    modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
}

This order can lead to incorrect transformations, where objects do not appear in the right position, orientation, or size in the 3D space.

Explanation

The correct order of transformations should be:

  1. Rotation
  2. Scaling
  3. Translation

Here's why:

  1. Rotation: When we rotate an object, we want it to rotate around its local origin. If the object has already been translated, rotating it will cause it to orbit around the origin of the world space, rather than rotating in place. Applying rotation first ensures the object rotates around its own center.

  2. Scaling: Scaling should typically occur around the object's local origin. If scaling is applied after translation, the object will scale in the direction of the translation, which can lead to undesired stretching. Scaling after rotation ensures that the object scales correctly along its local axes.

  3. Translation: Translation is the last step because it moves the object to its final position in the world space. By applying translation last, we ensure that the object is positioned correctly after it has been rotated and scaled around its local origin.

Solution

To achieve the correct transformation, the matrices should be multiplied in the following order:

void UpdateModelMatrix()
{
    Matrix4x4 scaleMatrix = Matrix4x4.CreateScale(_scale);
    Matrix4x4 rotationMatrix = _rotation.ToRotationMatrix();
    Matrix4x4 translationMatrix = Matrix4x4.CreateTranslate(_position);

    modelMatrix =  rotationMatrix * scaleMatrix * translationMatrix;
}

By making this change, we ensure that all transformations are applied correctly, resulting in the expected positioning, orientation, and scaling of objects within the 3D space.

@leAdmin666 leAdmin666 added Bug Something isn't working Enhancement New feature or request labels Jun 6, 2024
@realQuartzi
Copy link
Contributor

This makes absolute sense to me and I believe you are right. The logic is there and I will test this with the limited amount of testing we can do at this time :3

realQuartzi added a commit that referenced this issue Jun 7, 2024
As showcased in issue #4 the order of multiplication for the model matrix was the root cause of the false positioning of transforms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants