Skip to content

Commit

Permalink
feat: add collision management between entities with velocity reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Nov 17, 2024
1 parent 7a49da9 commit 986bd67
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ static bool SphereBoxCollisions(const Components::_3D::Transform &pos1, const Co
float distance = std::sqrt(dx * dx + dy * dy + dz * dz);
return distance < col1._radius;
}

static Math::Vector3f reflectVelocity(const Math::Vector3f &velocity, const Math::Vector3f &normal)
{
Math::Vector3f reflected;
float dot = 2 * (velocity.vec.x * normal.vec.x + velocity.vec.y * normal.vec.y + velocity.vec.z * normal.vec.z);
reflected.vec.x = velocity.vec.x - dot * normal.vec.x;
reflected.vec.y = velocity.vec.y - dot * normal.vec.y;
reflected.vec.z = velocity.vec.z - dot * normal.vec.z;
return reflected;
}

static bool outOfSkybox(float maxRangeX, float maxRangeY, float maxRangeZ, const Components::_3D::Transform &pos)
{
return pos._position.vec.x < -maxRangeX || pos._position.vec.x > maxRangeX || pos._position.vec.y < -maxRangeY ||
Expand Down Expand Up @@ -300,7 +311,7 @@ void handle_collisions(Registry &r)
continue;

if ((tag1->tag == "Player" && tag2->tag == "Enemy") ||
(tag2->tag == "Player" && tag1->tag == "Enemy") && scol1.has_value() && scol2.has_value())
(tag2->tag == "Player" && tag1->tag == "Enemy") && (scol1.has_value() && scol2.has_value()))
{
if (SphereCollisions(pos1.value(), scol1.value(), pos2.value(), scol2.value()))
{
Expand All @@ -312,9 +323,17 @@ void handle_collisions(Registry &r)
pos2->_position.vec.x -= normal.vec.x;
pos2->_position.vec.y -= normal.vec.y;
pos2->_position.vec.z -= normal.vec.z;

auto &vel1 = r.getComponents<Components::_3D::Movable>()[i];
auto &vel2 = r.getComponents<Components::_3D::Movable>()[j];
if (vel1.has_value() && vel2.has_value())
{
vel1->_velocity = reflectVelocity(vel1->_velocity, normal);
vel2->_velocity = reflectVelocity(vel2->_velocity, normal);
}
}
}
else if ((tag2->tag == "Bullet" && tag1->tag == "Enemy") && scol1.has_value() && bcol2.has_value())
else if (tag2->tag == "Bullet" && tag1->tag == "Enemy" && scol1.has_value() && bcol2.has_value())
{
if (r.isRegistered<Components::Common::Health>(i) &&
SphereBoxCollisions(pos1.value(), scol1.value(), pos2.value(), bcol2.value()))
Expand Down
7 changes: 7 additions & 0 deletions Flakkari/Engine/EntityComponentSystem/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ void apply_movable(Registry &r, float deltaTime);
*/
void spawn_random_within_skybox(Registry &r);

/**
* @brief Handles collisions between entities.
*
* @param r The registry containing the entities to update.
*/
void handle_collisions(Registry &r);

} // namespace Flakkari::Engine::ECS::Systems::_3D

#endif /* !FLAKKARI_SYSTEMS_HPP_ */
3 changes: 3 additions & 0 deletions Flakkari/Server/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ void Game::loadSystems(Engine::ECS::Registry &registry, const std::string &name)
else if (name == "spawn_random_within_skybox")
registry.add_system(
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::spawn_random_within_skybox(r); });

else if (name == "handle_collisions")
registry.add_system([this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::handle_collisions(r); });
}

void Game::loadComponents(Engine::ECS::Registry &registry, const nl_component &components,
Expand Down

0 comments on commit 986bd67

Please sign in to comment.