mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-04 11:35:06 +03:00
Added lookat
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user