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})
{ }
Vector3f CollisionComponent::getBounds(void) const
{
return _bound;
}
float CollisionComponent::getBoundX(void) const
{
return _bound.x;

View File

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

View File

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

View File

@@ -158,6 +158,22 @@ namespace BBM
{
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;

View File

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