setting bound as public and add velocity with a if instead of a try/catch

This commit is contained in:
Bluub
2021-05-31 17:36:48 +02:00
parent 4d0621a987
commit e1b1f0a5a4
5 changed files with 23 additions and 74 deletions
@@ -17,61 +17,26 @@ namespace BBM
}
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> callback, Vector3f bound)
: WAL::Component(entity), onCollide(callback), _bound(bound)
: WAL::Component(entity), onCollide(callback), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> callback, float boundSize)
: WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize})
: WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize})
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> callback, Vector3f bound)
: WAL::Component(entity), onCollide(callback), _bound(bound)
: WAL::Component(entity), onCollide(callback), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> callback, float boundSize)
: WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize})
: WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize})
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound)
: WAL::Component(entity), onCollide(), _bound(bound)
: WAL::Component(entity), onCollide(), bound(bound)
{ }
CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize)
: 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
{
return _bound.x;
}
float CollisionComponent::getBoundY(void) const
{
return _bound.y;
}
float CollisionComponent::getBoundZ(void) const
{
return _bound.z;
}
void CollisionComponent::setBoundX(float value)
{
_bound.x = value;
}
void CollisionComponent::setBoundY(float value)
{
_bound.y = value;
}
void CollisionComponent::setBoundZ(float value)
{
_bound.z = value;
}
}
@@ -14,23 +14,9 @@ namespace BBM
class CollisionComponent : public WAL::Component
{
private:
//! @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
float getBoundY(void) const;
//! @brief get bound size on the Z axis
float getBoundZ(void) const;
//! @brief set bound size on the X axis
void setBoundX(float);
//! @brief set bound size on the Y axis
void setBoundY(float);
//! @brief set bound size on the Z axis
void setBoundZ(float);
//! @brief Bound size on all axis
Vector3f bound;
//! @brief onCollide functions to be called
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide;
//! @inherit
+5 -7
View File
@@ -19,19 +19,17 @@ namespace BBM
auto &posA = entity.getComponent<PositionComponent>();
auto &col = entity.getComponent<CollisionComponent>();
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());
if (entity.hasComponent(typeid(MovableComponent)))
position += entity.getComponent<MovableComponent>()._velocity;
Vector3f minA = Vector3f::min(position, position + col.bound);
Vector3f maxA = Vector3f::max(position, position + col.bound);
for (auto &other : _wal.scene->getEntities()) {
if (&other == &entity)
continue;
if (!other.hasComponent(typeid(CollisionComponent)) ||
!other.hasComponent(typeid(PositionComponent)))
continue;
auto colB = entity.getComponent<CollisionComponent>().getBounds();
auto colB = entity.getComponent<CollisionComponent>().bound;
auto posB = other.getComponent<PositionComponent>().position;
Vector3f minB = Vector3f::min(posB, posB + colB);
Vector3f maxB = Vector3f::max(posB, posB + colB);
+7 -7
View File
@@ -11,15 +11,15 @@
namespace BBM
{
//! @brief A system to handle collisions.
class CollisionSystem : public WAL::System
{
//! @brief A system to handle collisions.
class CollisionSystem : public WAL::System
{
private:
//! @brief reference to the ECS engine to get other entities
WAL::Wal &_wal;
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief A default constructor
CollisionSystem(WAL::Wal &wal);
@@ -29,5 +29,5 @@ namespace BBM
~CollisionSystem() override = default;
//! @brief A Collision system is assignable.
CollisionSystem &operator=(const CollisionSystem &) = default;
};
};
}
+3 -3
View File
@@ -33,9 +33,9 @@ TEST_CASE("Collsion test", "[Component][System]")
Entity &entity = wal.scene->getEntities()[0];
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
entity.getComponent<CollisionComponent>()._bound.x = 5;
entity.getComponent<CollisionComponent>()._bound.y = 5;
entity.getComponent<CollisionComponent>()._bound.z = 5;
entity.getComponent<CollisionComponent>().bound.x = 5;
entity.getComponent<CollisionComponent>().bound.y = 5;
entity.getComponent<CollisionComponent>().bound.z = 5;
collision.onUpdate(entity, std::chrono::nanoseconds(1));
collision.onFixedUpdate(entity);