3d movement

This commit is contained in:
Asuro
2025-02-13 01:58:03 +01:00
parent 137d8fa539
commit 6e82678ade
7 changed files with 429 additions and 468 deletions

View File

@@ -1,5 +1,6 @@
#include "Global.h"
#include "bx/constants.h"
#include "bx/bx.h"
#include "bx/float4x4_t.h"
#include "bx/math.h"
#include <cassert>
@@ -9,89 +10,77 @@ namespace
Game::GameInstance* GameInst = nullptr;
} // namespace
void Vec2::Normalize()
{
float len = bx::sqrt(X * X + Y * Y);
X /= len;
Y /= len;
}
void Vec3::Normalize()
{
float len = bx::sqrt(X * X + Y * Y + Z * Z);
X /= len;
Y /= len;
Z /= len;
}
Quat Quat::FromEuler(float x, float y, float z)
{
x *= bx::kPi / 180.0f;
y *= bx::kPi / 180.0f;
z *= bx::kPi / 180.0f;
float cX = bx::cos(x);
float cY = bx::cos(y);
float cZ = bx::cos(z);
float sX = bx::sin(x);
float sY = bx::sin(y);
float sZ = bx::sin(z);
return Quat{cX * cY * cZ - sX * sY * sZ,
sY * cX * cZ - sX * sZ * cY,
sX * sY * cZ + sZ * cX * cY,
sX * cY * cZ + sY * sZ * cX};
}
Quat Quat::FromEuler(Vec3 rot)
{
return Quat::FromEuler(rot.X, rot.Y, rot.Z);
}
void Mat4::CreateTransform(float* out, Vec3 pos, Quat rot, Vec3 scale)
void Transform::CreateTransform(float* out, bx::Vec3 pos, bx::Quaternion rot, bx::Vec3 scale)
{
if (out == nullptr) return;
float rMat[16]{0};
float tMat[16]{0};
float sMat[16]{0};
bx::mtxFromQuaternion(rMat, bx::Quaternion{rot.X, rot.Y, rot.Z, rot.W});
bx::mtxTranslate(tMat, pos.X, pos.Y, pos.Z);
bx::mtxScale(sMat, scale.X, scale.Y, scale.Z);
bx::mtxFromQuaternion(rMat, bx::Quaternion{rot.x, rot.y, rot.z, rot.w});
bx::mtxTranslate(tMat, pos.x, pos.y, pos.z);
bx::mtxScale(sMat, scale.x, scale.y, scale.z);
float buf[16]{0};
bx::mtxMul(buf, rMat, sMat);
bx::mtxMul(out, buf, tMat);
}
void Mat4::TranslateLocal(Vec3 offset)
void Transform::Translate(bx::Vec3 offset)
{
M[12] += offset.X;
M[13] += offset.Y;
M[14] += offset.Z;
Position = bx::add(Position, offset);
}
void Mat4::Rotate(Vec3 rotation)
void Transform::TranslateLocal(bx::Vec3 offset)
{
UpdateMatrix();
float offsetPtr[4]{offset.x, offset.y, offset.z, 0.0f};
float localOffset[4]{0.0f};
bx::vec4MulMtx(localOffset, offsetPtr, MI.M);
Position = bx::add(Position, {localOffset[0], localOffset[1], localOffset[2]});
}
void Transform::Rotate(bx::Vec3 rotation)
{
float rot[16]{0};
bx::mtxRotateXYZ(rot, rotation.X, rotation.Y, rotation.Z);
bx::mtxRotateXYZ(rot, rotation.x, rotation.y, rotation.z);
float temp[16]{0};
bx::mtxMul(temp, M, rot);
for (int32_t i = 0; i < 16; ++i)
M[i] = temp[i];
bx::mtxMul(temp, rot, Rotation.M);
bx::memCopy(Rotation.M, temp, sizeof(temp));
}
Vec3 Mat4::Right()
void Transform::RotateLocal(bx::Vec3 rotation)
{
return {M[0], M[1], M[2]};
float rot[16]{0};
bx::mtxRotateXYZ(rot, rotation.x, rotation.y, rotation.z);
float temp[16]{0};
bx::mtxMul(temp, Rotation.M, rot);
bx::memCopy(Rotation.M, temp, sizeof(temp));
}
Vec3 Mat4::Up()
bx::Vec3 Transform::Right() const
{
return {M[4], M[5], M[6]};
return {M.M[0], M.M[1], M.M[2]};
}
Vec3 Mat4::Forward()
bx::Vec3 Transform::Up() const
{
return {M[8], M[9], M[10]};
return {M.M[4], M.M[5], M.M[6]};
}
bx::Vec3 Transform::Forward() const
{
return {M.M[8], M.M[9], M.M[10]};
}
void Transform::UpdateMatrix()
{
Mat4 pos;
Mat4 scale;
bx::mtxTranslate(pos.M, Position.x, Position.y, Position.z);
bx::mtxScale(scale.M, Scale.x, Scale.y, Scale.z);
Mat4 temp;
bx::mtxMul(temp.M, scale.M, Rotation.M);
bx::mtxMul(M.M, pos.M, temp.M);
bx::mtxInverse(MI.M, M.M);
}
namespace Game