Move Solver to RigidBody.
parent
6856f48054
commit
06d0a63a75
|
@ -77,7 +77,7 @@ void World::checkCollision(const std::string& body) {
|
||||||
if (gjk.first) {
|
if (gjk.first) {
|
||||||
if (obj->isCollider()) {
|
if (obj->isCollider()) {
|
||||||
CollisionPoint epa = _objects[body]->EPA(gjk.second, obj);
|
CollisionPoint epa = _objects[body]->EPA(gjk.second, obj);
|
||||||
Solver::solveCollision(_objects[body], obj, epa);
|
_objects[body]->solveCollision(epa);
|
||||||
}
|
}
|
||||||
if (_objects[body]->collisionCallBack() != nullptr)
|
if (_objects[body]->collisionCallBack() != nullptr)
|
||||||
_objects[body]->collisionCallBack()(name, obj);
|
_objects[body]->collisionCallBack()(name, obj);
|
||||||
|
|
|
@ -286,6 +286,17 @@ std::vector<std::pair<size_t, size_t>> RigidBody::_addIfUniqueEdge(const std::ve
|
||||||
return newEdges;
|
return newEdges;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RigidBody::solveCollision(const CollisionPoint& collision) {
|
||||||
|
|
||||||
|
Vec3D velocity_parallel = collision.normal * velocity().dot(collision.normal);
|
||||||
|
Vec3D velocity_perpendicular = velocity() - velocity_parallel;
|
||||||
|
|
||||||
|
if(velocity().dot(collision.normal) > 0)
|
||||||
|
setVelocity(velocity_perpendicular);
|
||||||
|
|
||||||
|
translate(-collision.normal * collision.depth);
|
||||||
|
}
|
||||||
|
|
||||||
void RigidBody::updatePhysicsState() {
|
void RigidBody::updatePhysicsState() {
|
||||||
translate(*_velocity * Time::deltaTime());
|
translate(*_velocity * Time::deltaTime());
|
||||||
_velocity = std::make_unique<Vec3D>(*_velocity + *_acceleration * Time::deltaTime());
|
_velocity = std::make_unique<Vec3D>(*_velocity + *_acceleration * Time::deltaTime());
|
||||||
|
|
|
@ -44,7 +44,6 @@ private:
|
||||||
static std::vector<std::pair<size_t, size_t>> _addIfUniqueEdge(const std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b);
|
static std::vector<std::pair<size_t, size_t>> _addIfUniqueEdge(const std::vector<std::pair<size_t, size_t>>& edges, const std::vector<size_t>& faces, size_t a, size_t b);
|
||||||
|
|
||||||
static void makeLogObjPolytope(const std::vector<Vec3D>& polytope, const std::vector<size_t>& faces);
|
static void makeLogObjPolytope(const std::vector<Vec3D>& polytope, const std::vector<size_t>& faces);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::unique_ptr<Vec3D> _velocity = std::make_unique<Vec3D>(Vec3D{0, 0, 0});;
|
std::unique_ptr<Vec3D> _velocity = std::make_unique<Vec3D>(Vec3D{0, 0, 0});;
|
||||||
std::unique_ptr<Vec3D> _acceleration = std::make_unique<Vec3D>(Vec3D{0, 0, 0});;
|
std::unique_ptr<Vec3D> _acceleration = std::make_unique<Vec3D>(Vec3D{0, 0, 0});;
|
||||||
|
@ -61,6 +60,7 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] std::pair<bool, Simplex> checkGJKCollision(std::shared_ptr<RigidBody> obj);
|
[[nodiscard]] std::pair<bool, Simplex> checkGJKCollision(std::shared_ptr<RigidBody> obj);
|
||||||
[[nodiscard]] CollisionPoint EPA(const Simplex& simplex, std::shared_ptr<RigidBody> obj);
|
[[nodiscard]] CollisionPoint EPA(const Simplex& simplex, std::shared_ptr<RigidBody> obj);
|
||||||
|
void solveCollision(const CollisionPoint& collision);
|
||||||
|
|
||||||
[[nodiscard]] Vec3D collisionNormal() const { return *_collisionNormal; }
|
[[nodiscard]] Vec3D collisionNormal() const { return *_collisionNormal; }
|
||||||
|
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
//
|
|
||||||
// Created by Иван Ильин on 10.03.2021.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "Solver.h"
|
|
||||||
#include "../utils/Log.h"
|
|
||||||
|
|
||||||
void Solver::solveCollision(std::shared_ptr<RigidBody> obj1, std::shared_ptr<RigidBody> obj2, const CollisionPoint& collision) {
|
|
||||||
|
|
||||||
Vec3D obj1_velocity_parallel = collision.normal * obj1->velocity().dot(collision.normal);
|
|
||||||
Vec3D obj1_velocity_perpendicular = obj1->velocity() - obj1_velocity_parallel;
|
|
||||||
|
|
||||||
Vec3D obj2_velocity_parallel = collision.normal * obj2->velocity().dot(collision.normal);
|
|
||||||
Vec3D obj2_velocity_perpendicular = obj2->velocity() - obj2_velocity_parallel;
|
|
||||||
|
|
||||||
if(obj1->isCollision()) {
|
|
||||||
if(obj1->velocity().dot(collision.normal) > 0) {
|
|
||||||
obj1->setVelocity(obj1_velocity_perpendicular);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(obj2->isCollision()) {
|
|
||||||
obj2->setVelocity(obj2_velocity_perpendicular - obj2_velocity_parallel * 0.8);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(obj1->isCollision() && obj2->isCollision()) {
|
|
||||||
obj1->translate(-collision.normal * collision.depth/2.0);
|
|
||||||
obj2->translate(collision.normal * collision.depth/2.0);
|
|
||||||
} else if(obj1->isCollision()) {
|
|
||||||
obj1->translate(-collision.normal * collision.depth);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
obj2->translate(collision.normal * collision.depth);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
//
|
|
||||||
// Created by Иван Ильин on 10.03.2021.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef ENGINE_SOLVER_H
|
|
||||||
#define ENGINE_SOLVER_H
|
|
||||||
|
|
||||||
#include "RigidBody.h"
|
|
||||||
|
|
||||||
class Solver final {
|
|
||||||
public:
|
|
||||||
static void solveCollision(std::shared_ptr<RigidBody> obj1, std::shared_ptr<RigidBody> obj2, const CollisionPoint& collision);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //INC_3DZAVR_SOLVER_H
|
|
Loading…
Reference in New Issue