Added LookAt to transform C# API

This commit is contained in:
antopilo
2024-11-24 04:18:55 -05:00
parent 6744a58043
commit b33bfcab57
2 changed files with 51 additions and 0 deletions

View File

@@ -362,6 +362,45 @@ namespace Nuake {
}
}
void TransformLookAt(int entityId, float x, float y, float z, float ux, float uy, float uz)
{
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
if (entity.IsValid() && entity.HasComponent<TransformComponent>())
{
auto& component = entity.GetComponent<TransformComponent>();
// Get the entity's current position
const Vector3 globalPos = component.GetGlobalTransform()[3];
const Vector3 targetPos = { x, y, z };
const Vector3 direction = glm::normalize(globalPos - targetPos);
Vector3 up = { ux, uy, uz };
if (glm::abs(glm::dot(direction, up)) > 0.999f)
{
up = { 0.0f, 0.0f, 1.0f };
}
// Calculate the right vector
const Vector3 right = glm::normalize(glm::cross(up, direction));
// Recompute the true up vector
const Vector3 adjustedUp = glm::normalize(glm::cross(direction, right));
// Construct a rotation matrix
const Matrix3 rotationMatrix =
{
right,
adjustedUp,
direction
};
// Convert rotation matrix to a quaternion
Quat orientation = glm::quat_cast(rotationMatrix);
component.SetLocalRotation(std::move(orientation));
}
}
float LightGetIntensity(int entityId)
{
Entity entity = { (entt::entity)(entityId), Engine::GetCurrentScene().get() };
@@ -614,6 +653,7 @@ namespace Nuake {
RegisterMethod("TransformComponent.GetPositionIcall", &TransformGetPosition);
RegisterMethod("TransformComponent.GetGlobalPositionIcall", &TransformGetGlobalPosition);
RegisterMethod("TransformComponent.RotateIcall", &TransformRotate);
RegisterMethod("TransformComponent.LookAtIcall", &TransformLookAt);
// Lights
RegisterMethod("LightComponent.GetLightIntensityIcall", &LightGetIntensity);

View File

@@ -54,6 +54,7 @@ namespace Nuake.Net
internal static unsafe delegate*<int, NativeArray<float>> GetGlobalPositionIcall;
internal static unsafe delegate*<int, NativeArray<float>> GetPositionIcall;
internal static unsafe delegate*<int, float, float, float, void> SetPositionIcall;
internal static unsafe delegate*<int, float, float, float, float, float, float, void> LookAtIcall;
internal static unsafe delegate*<int, float, float, float, void> RotateIcall;
public TransformComponent(int entityId)
@@ -109,6 +110,16 @@ namespace Nuake.Net
}
set { }
}
public void LookAt(Vector3 targetPosition)
{
LookAt(targetPosition, new(0, 1, 0)); // Default up direction
}
public void LookAt(Vector3 targetPosition, Vector3 up)
{
unsafe { LookAtIcall(EntityID, targetPosition.X, targetPosition.Y, targetPosition.Z, up.X, up.Y, up.Z); }
}
}
public class LightComponent : IComponent