Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Collider and ColliderShape #666

Merged
merged 9 commits into from
Oct 5, 2023
114 changes: 114 additions & 0 deletions core/include/cubos/core/geom/aabb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/// @file
/// @brief Component @ref cubos::core::geom::AABB.
/// @ingroup core-geom

#pragma once

#include <glm/glm.hpp>
luishfonseca marked this conversation as resolved.
Show resolved Hide resolved

#include <cubos/core/geom/box.hpp>

namespace cubos::core::geom
{
/// @brief Represents an axis-aligned bounding box.
/// @ingroup core-geom
struct AABB
{
/// @brief Diagonal of the AABB.
glm::vec3 diag[2] = {glm::vec3{-INFINITY}, glm::vec3{INFINITY}};

/// @brief Minimum point of the AABB.
/// @return Minimum point of the AABB.
glm::vec3 min() const
{
return diag[0];
}

/// @brief Maximum point of the AABB.
/// @return Maximum point of the AABB.
glm::vec3 max() const
{
return diag[1];
}

/// @brief Sets the minimum point of the AABB.
/// @param min Minimum point of the AABB.
void min(const glm::vec3& min)
{
diag[0] = min;
}

/// @brief Sets the maximum point of the AABB.
/// @param max Maximum point of the AABB.
void max(const glm::vec3& max)
{
diag[1] = max;
}

/// @brief Gets a @ref Box representation of the AABB.
luishfonseca marked this conversation as resolved.
Show resolved Hide resolved
/// @return @ref Box representation.
Box box() const
{
auto size = max() - min();
return Box{size / 2.0F};
}

/// @brief Gets the center of the AABB.
/// @return Center of the AABB.
glm::vec3 center() const
{
return (min() + max()) / 2.0F;
}

/// @brief Checks if the AABB overlaps with another AABB on the X axis.
/// @param other Other AABB.
/// @return Whether the AABBs overlap.
bool overlapsX(const AABB& other) const
{
return min().x <= other.max().x && max().x >= other.min().x;
}

/// @brief Checks if the AABB overlaps with another AABB on the Y axis.
/// @param other Other AABB.
/// @return Whether the AABBs overlap.
bool overlapsY(const AABB& other) const
{
return min().y <= other.max().y && max().y >= other.min().y;
}

/// @brief Checks if the AABB overlaps with another AABB on the Z axis.
/// @param other Other AABB.
/// @return Whether the AABBs overlap.
bool overlapsZ(const AABB& other) const
{
return min().z <= other.max().z && max().z >= other.min().z;
}

/// @brief Checks if the AABB overlaps with another AABB.
/// @param other Other AABB.
/// @return Whether the AABBs overlap.
bool overlaps(const AABB& other) const
{
return overlapsX(other) && overlapsY(other) && overlapsZ(other);
}
};
} // namespace cubos::core::geom

namespace cubos::core::data::old
{
inline void serialize(Serializer& ser, const core::geom::AABB& aabb, const char* name)
{
ser.beginObject(name);
ser.write(aabb.min(), "min");
ser.write(aabb.max(), "max");
ser.endObject();
}

inline void deserialize(Deserializer& des, core::geom::AABB& aabb)
{
des.beginObject();
des.read(aabb.diag[0]);
des.read(aabb.diag[1]);
des.endObject();
}
} // namespace cubos::core::data::old
11 changes: 11 additions & 0 deletions core/include/cubos/core/geom/capsule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cubos/core/data/old/deserializer.hpp>
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/geom/aabb.hpp>

namespace cubos::core::geom
{
Expand All @@ -30,6 +31,16 @@ namespace cubos::core::geom
{
return length + 2.0F * radius;
}

/// @brief Computes the local AABB of the capsule.
/// @return Local AABB of the capsule.
AABB aabb() const
{
AABB aabb;
aabb.min({-radius, -radius, -radius});
aabb.max({radius, radius + length, radius});
return aabb;
}
};
} // namespace cubos::core::geom

Expand Down
92 changes: 0 additions & 92 deletions engine/include/cubos/engine/collisions/aabb.hpp
luishfonseca marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

30 changes: 30 additions & 0 deletions engine/include/cubos/engine/collisions/collider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// @file
/// @brief Component @ref cubos::engine::Collider.
/// @ingroup collisions-plugin

#pragma once

#include <glm/mat4x4.hpp>

#include <cubos/core/geom/aabb.hpp>

namespace cubos::engine
{
/// @brief Component which adds a collider to an entity.
/// @ingroup collisions-plugin
struct [[cubos::component("cubos/collider", VecStorage)]] Collider
{
glm::mat4 transform{1.0F}; ///< Transform of the collider.

core::geom::AABB localAABB{}; ///< Local space AABB of the collider.
core::geom::AABB worldAABB{}; ///< World space AABB of the collider.

/// @brief Margin of the collider.
///
/// When the collider shape has sharp edges, a margin is needed.
/// The plugin will set it based on the shape associated with the collider.
float margin;
luishfonseca marked this conversation as resolved.
Show resolved Hide resolved

bool fresh = true; ///< Whether the collider is fresh. This is an hack and should be done in ECS.
};
} // namespace cubos::engine
31 changes: 0 additions & 31 deletions engine/include/cubos/engine/collisions/colliders/box.hpp

This file was deleted.

25 changes: 0 additions & 25 deletions engine/include/cubos/engine/collisions/colliders/capsule.hpp

This file was deleted.

17 changes: 17 additions & 0 deletions engine/include/cubos/engine/collisions/shapes/box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// @file
/// @brief Component @ref cubos::engine::BoxCollisionShape.
/// @ingroup collisions-plugin

#pragma once

#include <cubos/core/geom/box.hpp>

namespace cubos::engine
{
/// @brief Component which adds a box collision shape to an entity, used with a @ref Collider component.
/// @ingroup collisions-plugin
struct [[cubos::component("cubos/box_collision_shape", VecStorage)]] BoxCollisionShape
{
cubos::core::geom::Box box; ///< Box shape.
};
} // namespace cubos::engine
17 changes: 17 additions & 0 deletions engine/include/cubos/engine/collisions/shapes/capsule.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// @file
/// @brief Component @ref cubos::engine::CapsuleCollisionShape.
/// @ingroup collisions-plugin

#pragma once

#include <cubos/core/geom/capsule.hpp>

namespace cubos::engine
{
/// @brief Component which adds a capsule collision shape to an entity, used with a @ref Collider component.
/// @ingroup collisions-plugin
struct [[cubos::component("cubos/capsule_collision_shape", VecStorage)]] CapsuleCollisionShape
{
cubos::core::geom::Capsule capsule; ///< Capsule shape.
};
} // namespace cubos::engine
Loading
Loading