shooter/engine/Object.cpp

204 lines
5.9 KiB
C++
Raw Normal View History

2021-10-02 20:36:07 +03:00
//
// Created by Иван Ильин on 15.03.2021.
//
2021-10-31 13:00:38 +03:00
#include <stdexcept>
2021-10-02 20:36:07 +03:00
#include "Object.h"
#include "Matrix4x4.h"
2021-10-02 20:36:07 +03:00
2021-11-09 22:54:20 +03:00
bool ObjectNameTag::contains(const ObjectNameTag &nameTag) const {
if(_name.find(nameTag.str()) != std::string::npos) {
return true;
}
return false;
}
2021-10-31 11:39:08 +03:00
void Object::transform(const Matrix4x4 &t) {
_transformMatrix = t * _transformMatrix;
2021-10-02 20:36:07 +03:00
2021-10-31 11:39:08 +03:00
for (auto &[attachedName, attachedObject] : _attachedObjects) {
if (!attachedObject.expired()) {
attachedObject.lock()->transformRelativePoint(position(), t);
2021-10-28 16:58:02 +03:00
}
}
2021-10-02 20:36:07 +03:00
}
2021-10-31 11:39:08 +03:00
void Object::transformRelativePoint(const Vec3D &point, const Matrix4x4 &transform) {
2021-10-02 20:36:07 +03:00
// translate object in new coordinate system (connected with point)
_transformMatrix = Matrix4x4::Translation(position() - point) * _transformMatrix;
// transform object in the new coordinate system
2021-10-31 11:39:08 +03:00
_transformMatrix = transform * _transformMatrix;
// translate object back in self connected coordinate system
_position = _transformMatrix.w() + point;
_transformMatrix = Matrix4x4::Translation(-_transformMatrix.w()) * _transformMatrix;
2021-10-29 23:44:37 +03:00
2021-10-31 11:39:08 +03:00
for (auto &[attachedName, attachedObject] : _attachedObjects) {
if (!attachedObject.expired()) {
attachedObject.lock()->transformRelativePoint(point, transform);
2021-10-28 16:58:02 +03:00
}
}
2021-10-02 20:36:07 +03:00
}
void Object::translate(const Vec3D &dv) {
2021-10-02 20:36:07 +03:00
_position = _position + dv;
2021-10-02 20:36:07 +03:00
2021-10-31 11:39:08 +03:00
for (auto &[attachedName, attachedObject] : _attachedObjects) {
if (!attachedObject.expired()) {
attachedObject.lock()->translate(dv);
2021-10-28 16:58:02 +03:00
}
}
2021-10-02 20:36:07 +03:00
}
void Object::scale(const Vec3D &s) {
transform(Matrix4x4::Scale(s));
2021-10-02 20:36:07 +03:00
}
void Object::rotate(const Vec3D &r) {
2021-10-28 16:58:02 +03:00
_angle = _angle + r;
2021-10-02 20:36:07 +03:00
2022-02-23 07:51:53 +03:00
Matrix4x4 rotationMatrix = Matrix4x4::RotationX(r.x()) * Matrix4x4::RotationY(r.y()) * Matrix4x4::RotationZ(r.z());
transform(rotationMatrix);
}
2021-10-02 20:36:07 +03:00
void Object::rotate(const Vec3D &v, double rv) {
transform(Matrix4x4::Rotation(v, rv));
}
2021-10-02 20:36:07 +03:00
void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) {
2021-10-31 11:47:00 +03:00
_angle = _angle + r;
transformRelativePoint(s, Matrix4x4::Rotation(r));
2021-10-02 20:36:07 +03:00
}
void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) {
transformRelativePoint(s, Matrix4x4::Rotation(v, r));
2021-10-02 20:36:07 +03:00
}
void Object::rotateLeft(double rl) {
2021-10-28 16:58:02 +03:00
_angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x() + rl,
_angleLeftUpLookAt.y(),
_angleLeftUpLookAt.z()};
2021-10-02 20:36:07 +03:00
2021-10-29 23:44:37 +03:00
rotate(left(), rl);
2021-10-02 20:36:07 +03:00
}
void Object::rotateUp(double ru) {
2021-10-28 16:58:02 +03:00
_angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(),
_angleLeftUpLookAt.y() + ru,
_angleLeftUpLookAt.z()};
2021-10-02 20:36:07 +03:00
2021-10-29 23:44:37 +03:00
rotate(up(), ru);
2021-10-02 20:36:07 +03:00
}
void Object::rotateLookAt(double rlAt) {
2021-10-28 16:58:02 +03:00
_angleLeftUpLookAt = Vec3D{_angleLeftUpLookAt.x(),
_angleLeftUpLookAt.y(),
_angleLeftUpLookAt.z() + rlAt};
2021-10-29 23:44:37 +03:00
rotate(lookAt(), rlAt);
2021-10-02 20:36:07 +03:00
}
void Object::translateToPoint(const Vec3D &point) {
2021-10-29 23:44:37 +03:00
translate(point - position());
2021-10-02 20:36:07 +03:00
}
2021-10-31 22:11:04 +03:00
void Object::attractToPoint(const Vec3D &point, double value) {
Vec3D v = (point - position()).normalized();
translate(v*value);
}
void Object::rotateToAngle(const Vec3D &v) {
2021-10-28 16:58:02 +03:00
rotate(v - _angle);
2021-10-02 20:36:07 +03:00
}
2021-10-31 11:39:08 +03:00
std::shared_ptr<Object> Object::attached(const ObjectNameTag &tag) {
if (_attachedObjects.count(tag) == 0 || _attachedObjects.find(tag)->second.expired()) {
return nullptr;
2021-10-28 16:58:02 +03:00
}
return _attachedObjects.find(tag)->second.lock();
2021-10-02 20:36:07 +03:00
}
2021-10-28 16:58:02 +03:00
bool Object::checkIfAttached(Object *obj) {
2021-10-31 11:39:08 +03:00
for (const auto&[nameTag, attachedObject] : _attachedObjects) {
2021-10-28 16:58:02 +03:00
if (obj == attachedObject.lock().get() || attachedObject.lock()->checkIfAttached(obj)) {
return true;
}
}
return false;
}
void Object::attach(std::shared_ptr<Object> object) {
2021-10-31 11:39:08 +03:00
if (this != object.get()) {
if (!object->checkIfAttached(this)) {
2021-10-28 16:58:02 +03:00
_attachedObjects.emplace(object->name(), object);
} else {
throw std::invalid_argument{"Object::attach: You tried to create infinite recursive call chains"};
}
} else {
throw std::invalid_argument{"Object::attach: You cannot attach object to itself"};
2021-10-28 16:58:02 +03:00
}
2021-10-02 20:36:07 +03:00
}
2021-10-31 11:39:08 +03:00
void Object::unattach(const ObjectNameTag &tag) {
2021-10-17 10:21:10 +03:00
_attachedObjects.erase(tag);
2021-10-02 20:36:07 +03:00
}
2021-10-16 20:22:55 +03:00
2021-10-29 23:44:37 +03:00
// OpenGL function
GLfloat *Object::glInvModel() const {
2021-11-04 04:32:15 +03:00
auto *v = new GLfloat[4 * 4];
2021-10-29 23:44:37 +03:00
2021-10-31 13:00:38 +03:00
v[0] = -static_cast<GLfloat>(left().x());
v[4] = -static_cast<GLfloat>(left().y());
v[8] = -static_cast<GLfloat>(left().z());
v[12] = static_cast<GLfloat>(position().dot(left()));
2021-10-29 23:44:37 +03:00
2021-10-31 13:00:38 +03:00
v[1] = static_cast<GLfloat>(up().x());
v[5] = static_cast<GLfloat>(up().y());
v[9] = static_cast<GLfloat>(up().z());
v[13] = -static_cast<GLfloat>(position().dot(up()));
2021-10-29 23:44:37 +03:00
2021-10-31 13:00:38 +03:00
v[2] = -static_cast<GLfloat>(lookAt().x());
v[6] = -static_cast<GLfloat>(lookAt().y());
v[10] = -static_cast<GLfloat>(lookAt().z());
v[14] = static_cast<GLfloat>(position().dot(lookAt()));
2021-10-29 23:44:37 +03:00
2021-10-31 13:00:38 +03:00
v[3] = static_cast<GLfloat>(0.0f);
v[7] = static_cast<GLfloat>(0.0f);
v[11] = static_cast<GLfloat>(0.0f);
v[15] = static_cast<GLfloat>(1.0f);
2021-10-29 23:44:37 +03:00
return v;
}
2021-10-31 11:39:08 +03:00
GLfloat *Object::glModel() const {
2021-11-04 04:32:15 +03:00
auto *m = new GLfloat[4 * 4];
2021-10-29 23:44:37 +03:00
2021-10-31 13:00:38 +03:00
m[0] = static_cast<GLfloat>(left().x());
m[4] = static_cast<GLfloat>(up().x());
m[8] = static_cast<GLfloat>(lookAt().x());
m[12] = static_cast<GLfloat>(position().x());
m[1] = static_cast<GLfloat>(left().y());
m[5] = static_cast<GLfloat>(up().y());
m[9] = static_cast<GLfloat>(lookAt().y());
m[13] = static_cast<GLfloat>(position().y());
m[2] = static_cast<GLfloat>(left().z());
m[6] = static_cast<GLfloat>(up().z());
m[10] = static_cast<GLfloat>(lookAt().z());
m[14] = static_cast<GLfloat>(position().z());
m[3] = static_cast<GLfloat>(0.0f);
m[7] = static_cast<GLfloat>(0.0f);
m[11] = static_cast<GLfloat>(0.0f);
m[15] = static_cast<GLfloat>(1.0f);
2021-10-29 23:44:37 +03:00
return m;
}
2021-10-16 20:22:55 +03:00
Object::~Object() {
_attachedObjects.clear();
}