Files
Seele/src/Engine/Math/Transform.cpp
T

116 lines
4.4 KiB
C++
Raw Normal View History

2020-04-15 02:03:56 +02:00
#include "Transform.h"
2024-04-05 10:41:59 +02:00
#define GLM_ENABLE_EXPERIMENTAL
2023-11-30 11:51:53 +01:00
#include "Transform.h"
2024-06-09 12:20:04 +02:00
#include <glm/gtx/quaternion.hpp>
2020-04-15 02:03:56 +02:00
using namespace Seele;
using namespace Seele::Math;
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
Transform::Transform() : position(Vector4(0, 0, 0, 0)), rotation(Quaternion(1, 0, 0, 0)), scale(Vector4(1, 1, 1, 0)) {}
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
Transform::Transform(Transform&& other) : position(other.position), rotation(other.rotation), scale(other.scale) {
2020-05-05 01:51:13 +02:00
other.position = Vector4(0, 0, 0, 0);
2022-03-19 22:45:30 +01:00
other.rotation = Quaternion(1, 0, 0, 0);
2020-05-05 01:51:13 +02:00
other.scale = Vector4(0, 0, 0, 0);
}
2024-06-09 12:20:04 +02:00
Transform::Transform(const Transform& other) : position(other.position), rotation(other.rotation), scale(other.scale) {}
2020-05-05 01:51:13 +02:00
2024-06-09 12:20:04 +02:00
Transform::Transform(Vector position) : position(Vector4(position, 0)), rotation(Quaternion(1, 0, 0, 0)), scale(Vector4(1, 1, 1, 0)) {}
2020-04-15 02:03:56 +02:00
Transform::Transform(Vector position, Quaternion rotation)
2024-06-09 12:20:04 +02:00
: position(Vector4(position, 0)), rotation(rotation), scale(Vector4(1, 1, 1, 0)) {}
2020-04-15 02:03:56 +02:00
Transform::Transform(Vector position, Quaternion rotation, Vector scale)
2024-06-09 12:20:04 +02:00
: position(Vector4(position, 0)), rotation(rotation), scale(Vector4(scale, 0)) {}
Transform::Transform(Quaternion rotation, Vector scale) : position(Vector4(0, 0, 0, 0)), rotation(rotation), scale(Vector4(scale, 0)) {}
Transform::~Transform() {}
Matrix4 Transform::toMatrix() const {
2023-11-10 19:18:09 +01:00
// TODO: actual calculations, SIMD
Matrix4 result = Matrix4(1);
result = glm::scale(result, Vector(scale));
result = glm::toMat4(rotation) * result;
result[3][0] = position.x;
result[3][1] = position.y;
result[3][2] = position.z;
return result;
2020-05-05 01:51:13 +02:00
}
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
Vector Transform::transformPosition(const Vector& v) const { return Vector(glm::toMat4(rotation) * Vector4(v, 1)); }
2021-01-19 15:30:00 +01:00
2024-06-09 12:20:04 +02:00
Vector Transform::getPosition() const { return Vector(position); }
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
Quaternion Transform::getRotation() const { return rotation; }
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
Vector Transform::getScale() const { return Vector(scale); }
2020-04-15 02:03:56 +02:00
2024-06-09 12:20:04 +02:00
void Transform::setPosition(Vector pos) { position = Vector4(pos, 0); }
2022-11-29 16:34:42 +01:00
2024-06-09 12:20:04 +02:00
void Transform::setRotation(Quaternion quat) { rotation = quat; }
2022-11-29 16:34:42 +01:00
2024-06-09 12:20:04 +02:00
void Transform::setScale(Vector s) { scale = Vector4(s, 0); }
2022-11-29 16:34:42 +01:00
2024-06-09 12:20:04 +02:00
Vector Transform::getForward() const { return glm::normalize(Vector(0, 0, 1) * rotation); }
2022-11-29 16:34:42 +01:00
2024-06-09 12:20:04 +02:00
Vector Transform::getRight() const { return glm::normalize(Vector(1, 0, 0) * rotation); }
2022-11-29 16:34:42 +01:00
2024-06-09 12:20:04 +02:00
Vector Transform::getUp() const { return glm::normalize(Vector(0, 1, 0) * rotation); }
2022-03-19 22:45:30 +01:00
2024-06-09 12:20:04 +02:00
bool Transform::equals(const Transform& other, float tolerance) {
if (abs(position - other.position).length() > tolerance) {
2020-05-05 01:51:13 +02:00
return false;
}
2024-06-09 12:20:04 +02:00
if ((abs(rotation.x - other.rotation.x) <= tolerance && abs(rotation.y - other.rotation.y) <= tolerance &&
abs(rotation.z - other.rotation.z) <= tolerance && abs(rotation.w - other.rotation.w) <= tolerance) ||
(abs(rotation.x + other.rotation.x) <= tolerance && abs(rotation.y + other.rotation.y) <= tolerance &&
abs(rotation.z + other.rotation.z) <= tolerance && abs(rotation.w + other.rotation.w) <= tolerance)) {
2020-05-05 01:51:13 +02:00
return false;
}
2024-06-09 12:20:04 +02:00
if (abs(scale - other.scale).length() > tolerance) {
2020-05-05 01:51:13 +02:00
return false;
}
return true;
}
2024-06-09 12:20:04 +02:00
void Transform::multiply(Transform* outTransform, const Transform* a, const Transform* b) {
2020-04-15 02:03:56 +02:00
outTransform->rotation = b->rotation * a->rotation;
2021-01-19 15:30:00 +01:00
outTransform->position = b->rotation * (b->scale * a->position) + b->position;
2020-04-15 02:03:56 +02:00
outTransform->scale = b->scale * a->scale;
}
2024-06-09 12:20:04 +02:00
void Transform::add(Transform* outTransform, const Transform* a, const Transform* b) {
2022-03-19 22:45:30 +01:00
outTransform->position = a->position + b->position;
outTransform->rotation = a->rotation + b->rotation;
outTransform->scale = a->scale + b->scale;
}
2024-06-09 12:20:04 +02:00
Transform& Transform::operator=(const Transform& other) {
if (&other != this) {
position = other.position;
rotation = other.rotation;
scale = other.scale;
}
2020-05-05 01:51:13 +02:00
return *this;
}
2024-06-09 12:20:04 +02:00
Transform& Transform::operator=(Transform&& other) {
if (&other != this) {
position = other.position;
rotation = other.rotation;
scale = other.scale;
other.position = Vector4(0, 0, 0, 0);
other.rotation = Quaternion(1, 0, 0, 0);
other.scale = Vector4(0, 0, 0, 0);
}
2020-05-05 01:51:13 +02:00
return *this;
}
2024-06-09 12:20:04 +02:00
Transform Transform::operator+(const Transform& other) const {
2022-03-19 22:45:30 +01:00
Transform outTransform;
add(&outTransform, this, &other);
return outTransform;
}
2024-06-09 12:20:04 +02:00
Transform Transform::operator*(const Transform& other) const {
2020-05-05 01:51:13 +02:00
Transform outTransform;
multiply(&outTransform, this, &other);
2021-10-16 12:59:11 +02:00
return outTransform;
2020-05-05 01:51:13 +02:00
}