dfkeenan / StrideToolkit

Stride Toolkit is a .NET Standard library for use with the Stride Game Engine.
MIT License
30 stars 5 forks source link

All TransformComponent extension methods should allow you to use them on TransformComponents that have UseTRS set to false. #4

Open dfkeenan opened 6 years ago

dfkeenan commented 6 years ago

My first attempt to do these methods that worked with UseTRS set to true or false didn't seem to work correctly when updating the matrices directly. So temporarily not supporting TransformComponents with UserTRS set to false.

Need some help with people who know there math better than I do.

stefnotch commented 6 years ago

This seems to work:

public static void Translate(this TransformComponent transform, ref Vector3 translation, Space relativeTo = Space.Self)
{
    if (transform == null)
    {
        throw new ArgumentNullException(nameof(transform));
    }

    var localTranslation = translation;
        if (relativeTo == Space.Self) {
            Vector3.TransformNormal(ref translation, ref transform.WorldMatrix, out localTranslation);
        }
    if (transform.Parent != null)
    {
        Matrix.Invert(ref transform.Parent.WorldMatrix, out var inverseParent);
        Vector3.TransformNormal(ref localTranslation, ref inverseParent, out localTranslation);
    }

    if (transform.UseTRS) {
        transform.Position += localTranslation;
    } else {
        transform.LocalMatrix.TranslationVector += localTranslation;
    }

    transform.UpdateWorldMatrix();
}