shooter/engine/physics/RigidBody.h

80 lines
2.7 KiB
C
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 05.02.2021.
//
#ifndef ENGINE_RIGIDBODY_H
#define ENGINE_RIGIDBODY_H
#include <vector>
#include <memory>
2021-10-03 08:38:10 +03:00
#include <functional>
2021-09-13 15:53:43 +03:00
#include "../utils/Point4D.h"
#include "../Triangle.h"
#include "Simplex.h"
2021-10-09 13:41:12 +03:00
#include "../Mesh.h"
2021-09-13 15:53:43 +03:00
struct CollisionPoint {
Point4D a; // Furthest point of a into b
Point4D b; // Furthest point of b into a
Point4D normal; // b a normalized
double depth; // Length of b a
bool hasCollision;
};
class RigidBody : public Mesh {
private:
2021-09-13 15:53:43 +03:00
Point4D _findFurthestPoint(const Point4D& direction);
Point4D _support(std::shared_ptr<RigidBody> obj, const Point4D& direction);
2021-09-13 15:53:43 +03:00
std::function<void(const std::string&, std::shared_ptr<RigidBody>)> _collisionCallBack;
2021-09-13 15:53:43 +03:00
static bool _nextSimplex(Simplex& points, Point4D& direction);
static bool _line(Simplex& points, Point4D& direction);
static bool _triangle(Simplex& points, Point4D& direction);
static bool _tetrahedron(Simplex& points, Point4D& direction);
2021-10-03 07:47:05 +03:00
static std::pair<std::vector<Point4D>, size_t> _getFaceNormals(const std::vector<Point4D>& polytope, const std::vector<size_t>& faces);
static void _addIfUniqueEdge(std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b);
2021-09-13 15:53:43 +03:00
protected:
Point4D _velocity;
Point4D _acceleration;
bool _collision = false;
bool _isCollider = true;
bool _inCollision = false;
Point4D _collisionNormal;
2021-09-13 15:53:43 +03:00
public:
RigidBody() = default;
explicit RigidBody(const Mesh& mesh);
2021-09-13 15:53:43 +03:00
std::pair<bool, Simplex> checkGJKCollision(std::shared_ptr<RigidBody> obj);
CollisionPoint EPA(const Simplex& simplex, std::shared_ptr<RigidBody> obj);
2021-09-13 15:53:43 +03:00
[[nodiscard]] Point4D collisionNormal() const { return _collisionNormal; }
2021-09-13 15:53:43 +03:00
[[nodiscard]] bool isCollision() const { return _collision; }
[[nodiscard]] bool inCollision() const {return _inCollision; }
[[nodiscard]] bool isCollider() const {return _isCollider; }
void setInCollision(bool c) { _inCollision = c; }
void setCollision(bool c) { _collision= c; }
void setCollider(bool c) { _isCollider = c; }
void updatePhysicsState();
void setVelocity(const Point4D& velocity);
void addVelocity(const Point4D& velocity);
void setAcceleration(const Point4D& acceleration);
[[nodiscard]] Point4D velocity() const { return _velocity; }
[[nodiscard]] Point4D acceleration() const { return _acceleration; }
[[nodiscard]] const std::function<void(const std::string&, std::shared_ptr<RigidBody>)>& collisionCallBack() const { return _collisionCallBack; }
void setCollisionCallBack(const std::function<void(const std::string&, std::shared_ptr<RigidBody>)>& f) { _collisionCallBack = f; }
2021-09-13 15:53:43 +03:00
};
#endif //INC_3DZAVR_RIGIDBODY_H