aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-04 20:42:53 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-04 20:42:53 +0100
commit24246e115c3b7829d2981a2f60ac77da657f2ed5 (patch)
tree2040a2a4ce8cd9fe3ffadb8b92d54b14890bb1fa /src
parentd76ab0bf77d0a61712dc25bbe1760995be4c4782 (diff)
added collision layers
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Rigidbody.h10
-rw-r--r--src/crepe/system/CollisionSystem.cpp12
-rw-r--r--src/crepe/system/CollisionSystem.h16
3 files changed, 38 insertions, 0 deletions
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index 756cc28..5c9f6df 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -130,6 +130,16 @@ public:
*
*/
vec2 offset;
+
+ /**
+ * \brief Defines the collision layers of a GameObject.
+ *
+ * The `collision_layers` vector specifies the layers that the GameObject will collide with.
+ * Each element in the vector represents a layer ID, and the GameObject will only detect
+ * collisions with other GameObjects that belong to these layers.
+ */
+ std::vector<int> collision_layers;
+
};
public:
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
index 34cd125..3c103e9 100644
--- a/src/crepe/system/CollisionSystem.cpp
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -359,6 +359,7 @@ CollisionSystem::gather_collisions(std::vector<CollisionInternal> & colliders) {
for (size_t i = 0; i < colliders.size(); ++i) {
for (size_t j = i + 1; j < colliders.size(); ++j) {
if (colliders[i].id == colliders[j].id) continue;
+ if(!have_common_layer(colliders[i].rigidbody.data.collision_layers,colliders[j].rigidbody.data.collision_layers)) continue;
CollisionInternalType type
= get_collider_type(colliders[i].collider, colliders[j].collider);
if (!get_collision(
@@ -381,6 +382,17 @@ CollisionSystem::gather_collisions(std::vector<CollisionInternal> & colliders) {
return collisions_ret;
}
+bool CollisionSystem::have_common_layer(const std::vector<int>& layers1, const std::vector<int>& layers2) {
+ // Iterate through each layer in the first vector
+ for (int layer : layers1) {
+ // Check if the current layer is present in the second vector
+ if (std::find(layers2.begin(), layers2.end(), layer) != layers2.end()) {
+ return true; // Common layer found
+ }
+ }
+ return false; // No common layers found
+}
+
CollisionSystem::CollisionInternalType
CollisionSystem::get_collider_type(const collider_variant & collider1,
const collider_variant & collider2) const {
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
index 6b5a4bb..a22baf2 100644
--- a/src/crepe/system/CollisionSystem.h
+++ b/src/crepe/system/CollisionSystem.h
@@ -215,6 +215,22 @@ private:
gather_collisions(std::vector<CollisionInternal> & colliders);
/**
+ * \brief Checks if two collision layers have at least one common layer.
+ *
+ * This function checks if there is any overlapping layer between the two input
+ * collision layer vectors. It compares each layer from the first vector to see
+ * if it exists in the second vector. If at least one common layer is found,
+ * the function returns true, indicating that the two colliders share a common
+ * collision layer.
+ *
+ * \param layers1 A vector of collision layers for the first collider.
+ * \param layers2 A vector of collision layers for the second collider.
+ * \return Returns true if there is at least one common layer, false otherwise.
+ */
+
+ bool have_common_layer(const std::vector<int>& layers1, const std::vector<int>& layers2);
+
+ /**
* \brief Checks for collision between two colliders.
*
* Calls the appropriate collision detection function based on the collider types.