I have an OpenGL C# project, that I would like to give functionality like Unity3D game engine.
Introduction: I have Transform class that provides transformations matrix to shader. Each transform can have parent transform. Code that calculates final transformations matrix looks like this:
public Vector3 LocalPosition { get; set; }
public Quaternion LocalRotation { get; set; }
public Vector3 LocalScale { get; set; }
public Matrix GetModelMatrix() {
Matrix result;
if(HasParent)
result = Parent.GetModelMatrix();
else
result = Matrix.CreateIdentity();
ApplyLocalTransformations(result);
return result;
}
private void ApplyLocalTransform(Matrix matrix)
{
matrix.Translate(LocalPosition);
matrix.Rotate(LocalRotation);
matrix.Scale(LocalScale);
}
As you see LocalPosition, LocalScale and LocalRotation are transformations RELATIVE to parent. This code works fine.
Problem: I want to add 3 more properties (hello Unity3D):
public Vector3 AbsolutePosition { get; set; }
public Quaternion AbsoluteRotation { get; set; }
public Vector3 AbsoluteScale { get; set; }
I want to have ability to get and set absolute transformations to child transforms. While setting Absolute values Local should be updated consistently and vice versa.
Example: We have parent at position (1, 1, 1) and child with LocalPosition = (0, 0, 0), having this information we can calculate child’s AbsolutePosition = (1, 1, 1). Now we set child’s AbsolutePosition = (0, 0, 0). It’s LocalPosition will now be = (-1, -1, -1). It’s a very simple example, in real scenario we have to consider parent’s scale and rotation to calculate Position.
How to calculate Absolute and Local Position i have an idea: I can take last column from transformations matrix and it will be my AbsolutePosition. To get LocalPosition i can subtract from AbsolutePosition last column of parent transformations matrix. But mathematics behind Rotation and Scale still unclear for me.
Question: Can you help me with algorithm that will calculate Local and Absolute Position, Rotation and Scale?
P.S.: considering performance would be great.
http://stackoverflow.com/q/27551201/1204080
you can get some reputation there ![]()