Added lookat

This commit is contained in:
Antoine Pilote
2023-07-12 16:10:20 -04:00
parent a466065ac6
commit 8a8db47f6c
4 changed files with 69 additions and 1 deletions

View File

@@ -44,6 +44,7 @@ class Scene {
foreign static GetTranslation_(e)
foreign static SetTranslation_(e, x, y, z)
foreign static SetRotation_(e, x, y, z)
foreign static SetLookAt_(e, x, y, z)
foreign static GetRotation_(e)
//foreign static SetScale_(e, x, y, z)
@@ -111,6 +112,9 @@ class TransformComponent {
Scene.SetRotation_(_entityId, t.x, t.y, t.z)
}
SetLookAt(t) {
Scene.SetLookAt_(_entityId, t.x, t.y, t.z)
}
}
class Light {

View File

@@ -2,9 +2,55 @@
using namespace Nuake;
Quat Nuake::LookAt(Vector3 sourcePoint, Vector3 destPoint)
{
Vector3 forwardVector = glm::normalize(destPoint - sourcePoint);
float dot = glm::dot(Vector3(0, 0, 1), forwardVector);
if (glm::abs(dot - (-1.0f)) < 0.000001f)
{
return Quat(0, 1, 0, 3.1415926535897932f);
}
if (glm::abs(dot - (1.0f)) < 0.000001f)
{
return Quat(); // identity
}
float rotAngle = acos(dot);
Vector3 rotAxis = glm::cross(Vector3(0, 0, 1), forwardVector);
rotAxis = glm::normalize(rotAxis);
return CreateFromAxisAngle(rotAxis, rotAngle);
}
// just in case you need that function also
Quat Nuake::CreateFromAxisAngle(Vector3 axis, float angle)
{
float halfAngle = angle * .5f;
float s = sin(halfAngle);
Quat q;
q.x = axis.x * s;
q.y = axis.y * s;
q.z = axis.z * s;
q.w = cos(halfAngle);
return q;
}
Quat Nuake::QuatFromEuler(float x, float y, float z)
{
return Quat(Vector3(Rad(z), Rad(y), Rad(x)));
Quat q;
float cr = cos(x * 0.5);
float sr = sin(x * 0.5);
float cp = cos(y * 0.5);
float sp = sin(y * 0.5);
float cy = cos(z * 0.5);
float sy = sin(z * 0.5);
q.w = cr * cp * cy + sr * sp * sy;
q.x = sr * cp * cy - cr * sp * sy;
q.y = cr * sp * cy + sr * cp * sy;
q.z = cr * cp * sy - sr * sp * cy;
return q;
}
Vector3 Nuake::QuatToDirection(const Quat& quat)

View File

@@ -20,6 +20,8 @@ namespace Nuake
using Matrix3 = glm::mat3;
#define Rad(degrees) glm::radians(degrees)
Quat LookAt(Vector3 sourcePoint, Vector3 destPoint);
Quat CreateFromAxisAngle(Vector3 axis, float angle);
Quat QuatFromEuler(float x, float y, float z);
Vector3 QuatToDirection(const Quat& quat);
}

View File

@@ -40,6 +40,8 @@ namespace Nuake {
RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation);
RegisterMethod("GetRotation_(_)", (void*)GetRotation);
RegisterMethod("SetRotation_(_,_,_,_)", (void*)SetRotation);
RegisterMethod("SetLookAt_(_,_,_,_)", (void*)SetLookAt);
RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity);
RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity);
@@ -263,6 +265,20 @@ namespace Nuake {
transform.SetLocalRotation(QuatFromEuler(x, y, z));
}
static void SetLookAt(WrenVM* vm)
{
double handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
float x = (float)wrenGetSlotDouble(vm, 2);
float y = (float)wrenGetSlotDouble(vm, 3);
float z = (float)wrenGetSlotDouble(vm, 4);
Vector3 targetPosition = Vector3(x, y, z);
Quat rotation = LookAt(transform.GetGlobalPosition(), transform.GetGlobalPosition() + targetPosition);
transform.SetLocalRotation(rotation);
}
static void SetTranslation(WrenVM* vm)
{
double handle = wrenGetSlotDouble(vm, 1);