min max static function on vector3f, bound is public, adding movable into collision calculation

This commit is contained in:
Bluub
2021-05-31 17:26:54 +02:00
parent fcbbfd04d8
commit 4d0621a987
5 changed files with 47 additions and 26 deletions

View File

@@ -40,6 +40,11 @@ namespace BBM
: WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize}) : WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize})
{ } { }
Vector3f CollisionComponent::getBounds(void) const
{
return _bound;
}
float CollisionComponent::getBoundX(void) const float CollisionComponent::getBoundX(void) const
{ {
return _bound.x; return _bound.x;

View File

@@ -17,6 +17,8 @@ namespace BBM
//! @brief Bound size on all axis //! @brief Bound size on all axis
Vector3f _bound; Vector3f _bound;
public: public:
//! @brief get vector of bounds
Vector3f getBounds(void) const;
//! @brief get bound size on the X axis //! @brief get bound size on the X axis
float getBoundX(void) const; float getBoundX(void) const;
//! @brief get bound size on the Y axis //! @brief get bound size on the Y axis

View File

@@ -35,5 +35,6 @@ namespace BBM
MovableComponent &operator=(const MovableComponent &) = delete; MovableComponent &operator=(const MovableComponent &) = delete;
friend class MovableSystem; friend class MovableSystem;
friend class CollisionSystem;
}; };
} // namespace WAL } // namespace WAL

View File

@@ -158,6 +158,22 @@ namespace BBM
{ {
return RAY::Vector3(this->x, this->y, this->z); return RAY::Vector3(this->x, this->y, this->z);
} }
static Vector3<T> min(Vector3<T> a, Vector3<T> b)
{
Vector3<T> min = { std::min(a.x, b.x),
std::min(a.y, b.y),
std::min(a.z, b.z)};
return min;
}
static Vector3<T> max(Vector3<T> a, Vector3<T> b)
{
Vector3<T> max = { std::max(a.x, b.x),
std::max(a.y, b.y),
std::max(a.z, b.z)};
return max;
}
}; };
typedef Vector3<float> Vector3f; typedef Vector3<float> Vector3f;

View File

@@ -2,6 +2,7 @@
// Created by Louis Auzuret on 5/20/21 // Created by Louis Auzuret on 5/20/21
// //
#include "Component/Movable/MovableComponent.hpp"
#include "Component/Position/PositionComponent.hpp" #include "Component/Position/PositionComponent.hpp"
#include "Component/Collision/CollisionComponent.hpp" #include "Component/Collision/CollisionComponent.hpp"
#include "System/Collision/CollisionSystem.hpp" #include "System/Collision/CollisionSystem.hpp"
@@ -15,33 +16,29 @@ namespace BBM
} }
void CollisionSystem::onFixedUpdate(WAL::Entity &entity) void CollisionSystem::onFixedUpdate(WAL::Entity &entity)
{ {
try {
auto &posA = entity.getComponent<PositionComponent>(); auto &posA = entity.getComponent<PositionComponent>();
auto &col = entity.getComponent<CollisionComponent>(); auto &col = entity.getComponent<CollisionComponent>();
Vector3f minA = { std::min(posA.getX(), posA.getX() + col.getBoundX()), Vector3f position = posA.position;
std::min(posA.getY(), posA.getY() + col.getBoundY()), try {
std::min(posA.getZ(), posA.getZ() + col.getBoundZ())}; auto &movable = entity.getComponent<MovableComponent>();
Vector3f maxA = { std::max(posA.getX(), posA.getX() + col.getBoundX()), position += movable._velocity;
std::max(posA.getY(), posA.getY() + col.getBoundY()), } catch (std::exception &e) { };
std::max(posA.getZ(), posA.getZ() + col.getBoundZ())}; Vector3f minA = Vector3f::min(position, position + col.getBounds());
Vector3f maxA = Vector3f::max(position, position + col.getBounds());
for (auto &other : _wal.scene->getEntities()) { for (auto &other : _wal.scene->getEntities()) {
if (&other == &entity) if (&other == &entity)
continue; continue;
auto &colB = entity.getComponent<CollisionComponent>(); if (!other.hasComponent(typeid(CollisionComponent)) ||
auto &posB = other.getComponent<PositionComponent>(); !other.hasComponent(typeid(PositionComponent)))
Vector3f minB = { std::min(posB.getX(), posB.getX() + colB.getBoundX()), continue;
std::min(posB.getY(), posB.getY() + colB.getBoundY()), auto colB = entity.getComponent<CollisionComponent>().getBounds();
std::min(posB.getZ(), posB.getZ() + colB.getBoundZ())}; auto posB = other.getComponent<PositionComponent>().position;
Vector3f maxB = { std::max(posB.getX(), posB.getX() + colB.getBoundX()), Vector3f minB = Vector3f::min(posB, posB + colB);
std::max(posB.getY(), posB.getY() + colB.getBoundY()), Vector3f maxB = Vector3f::max(posB, posB + colB);
std::max(posB.getZ(), posB.getZ() + colB.getBoundZ())};
if ((minA.x <= maxB.x && maxA.x >= minB.x) && if ((minA.x <= maxB.x && maxA.x >= minB.x) &&
(minA.y <= maxB.y && maxA.y >= minB.y) && (minA.y <= maxB.y && maxA.y >= minB.y) &&
(minA.z <= maxB.z && maxA.z >= minB.z)) (minA.z <= maxB.z && maxA.z >= minB.z))
col.onCollide(entity, other); col.onCollide(entity, other);
} }
} catch (std::exception &e) {
return;
}
} }
} }