Naming, comments and local to world update
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
using Nerfed.Runtime.Components;
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Util
|
||||
@ -21,5 +23,36 @@ namespace Nerfed.Runtime.Util
|
||||
Matrix4x4.CreateFromQuaternion(localTransform.rotation) *
|
||||
Matrix4x4.CreateTranslation(localTransform.position);
|
||||
}
|
||||
|
||||
// Force update the transform data of an entity (and children).
|
||||
// Useful for when you need precise up to date transform data.
|
||||
public static void ForceUpdateLocalToWorld(in World world, in Entity entity)
|
||||
{
|
||||
Matrix4x4 parentLocalToWorldMatrix = Matrix4x4.Identity;
|
||||
|
||||
if (world.Has<Parent>(entity))
|
||||
{
|
||||
Entity parent = world.Get<Parent>(entity).parentEntity;
|
||||
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
||||
}
|
||||
|
||||
ForceUpdateLocalToWorld(world, entity, parentLocalToWorldMatrix);
|
||||
}
|
||||
|
||||
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, in Matrix4x4 parentLocalToWorldMatrix)
|
||||
{
|
||||
LocalTransform localTransform = world.Get<LocalTransform>(entity);
|
||||
Matrix4x4 localToWorldMatrix = Matrix4x4.Multiply(parentLocalToWorldMatrix, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
world.Set(entity, localToWorld);
|
||||
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = world.OutRelations<ChildRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
ForceUpdateLocalToWorld(world, childEntity, parentLocalToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user