-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add bullet3 collision system #24
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
71fbd3a
feat: add collision system
LoayAhmed304 363559c
chore: add collision system executable in CMakeList
LoayAhmed304 f31dcdd
refactor: use CollisionLayer enum instead of string comparisons
LoayAhmed304 d6d3962
fix: wrongfully assigning fps overlay color
LoayAhmed304 e6dd364
perf: avoid entities removal in the middle of iterating
LoayAhmed304 d98c655
docs: remove useless comments in play state onDestroy
LoayAhmed304 f04393f
perf: cahce and reuse shapes and correctly delete when not needed
LoayAhmed304 f674e89
fix: correctly calculate the local normal vector for collision
LoayAhmed304 689d8a5
feat: make account for the entity scale when creating and caching object
LoayAhmed304 6cf2e88
docs: update data member comment to correctly define it
LoayAhmed304 a6cd99e
refactor: update headers in cpp and hpp files
LoayAhmed304 68471c8
fix: pass the correct entity when calculating the local normal
LoayAhmed304 42cc35e
docs: fix english mistakes
LoayAhmed304 fd15b58
Merge branch 'main' into feat/initial-collision
LoayAhmed304 8a95480
refactor: rename json-configs into playgrounds
LoayAhmed304 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| #include "collision-system.hpp" | ||
|
|
||
| #include <btBulletCollisionCommon.h> | ||
|
|
||
| #include <components/collider.hpp> | ||
| #include <ecs/entity.hpp> | ||
| #include <glm/glm.hpp> | ||
| #include <glm/gtc/quaternion.hpp> | ||
|
|
||
| // Helper functions for GLM-Bullet conversions | ||
| inline btVector3 glmToBtVec3(const glm::vec3& v) { | ||
| return btVector3(v.x, v.y, v.z); | ||
| } | ||
| inline glm::vec3 btToGlmVec3(const btVector3& v) { | ||
| return glm::vec3(v.getX(), v.getY(), v.getZ()); | ||
| } | ||
| static btTransform entityToBtTransform(our::Entity* entity) { | ||
| glm::mat4 m = entity->getLocalToWorldMatrix(); | ||
|
|
||
| // Extract position from column 3 | ||
| glm::vec3 pos = glm::vec3(m[3]); | ||
|
|
||
| // Extract rotation by stripping scale from the 3x3 submatrix. | ||
| // Each column of the 3x3 has length = scale along that axis. | ||
| // Normalizing each column removes the scale, leaving pure rotation. | ||
| glm::mat3 rotMat; | ||
| rotMat[0] = glm::normalize(glm::vec3(m[0])); | ||
| rotMat[1] = glm::normalize(glm::vec3(m[1])); | ||
| rotMat[2] = glm::normalize(glm::vec3(m[2])); | ||
| glm::quat rot = glm::quat_cast(rotMat); | ||
|
|
||
| btTransform t; | ||
| t.setOrigin(btVector3(pos.x, pos.y, pos.z)); | ||
| t.setRotation(btQuaternion(rot.x, rot.y, rot.z, rot.w)); | ||
| return t; | ||
| } | ||
|
|
||
| namespace gameplay { | ||
|
|
||
| inline short getMaskForLayer(short group) { | ||
| switch (group) { | ||
| case LAYER_PLAYER: | ||
| return LAYER_ENEMY | LAYER_ENVIRONMENT | LAYER_TRIGGER; | ||
| case LAYER_ENEMY: | ||
| return LAYER_PLAYER | LAYER_PROJECTILE | LAYER_ENVIRONMENT; | ||
| case LAYER_ENVIRONMENT: | ||
| return LAYER_PLAYER | LAYER_ENEMY | LAYER_PROJECTILE; | ||
| case LAYER_PROJECTILE: | ||
| return LAYER_ENEMY | LAYER_ENVIRONMENT; | ||
| case LAYER_TRIGGER: | ||
| return LAYER_PLAYER; | ||
| default: | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| void CollisionSystem::initialize() { | ||
| // collision configuration contains default setup for memory, collision setup. | ||
| collisionConfiguration = new btDefaultCollisionConfiguration(); | ||
|
|
||
| // use the default collision dispatcher | ||
| dispatcher = new btCollisionDispatcher(collisionConfiguration); | ||
|
|
||
| broadphase = new btDbvtBroadphase(); | ||
|
|
||
| // the default constraint solver | ||
| collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfiguration); | ||
| } | ||
|
|
||
| void CollisionSystem::destroy() { | ||
| // remove collision objects | ||
| for (auto& [entity, obj] : entityToBullet) { | ||
| collisionWorld->removeCollisionObject(obj); | ||
| delete obj; | ||
| } | ||
| entityToBullet.clear(); | ||
|
|
||
| // remove shapes | ||
| for (auto shape : ownedShapes) { | ||
| delete shape; | ||
| } | ||
| ownedShapes.clear(); | ||
|
|
||
| // delete bullet internals | ||
| delete collisionWorld; | ||
| delete broadphase; | ||
| delete dispatcher; | ||
| delete collisionConfiguration; | ||
| collisionWorld = nullptr; | ||
| broadphase = nullptr; | ||
| dispatcher = nullptr; | ||
| collisionConfiguration = nullptr; | ||
|
|
||
| frameCollisions.clear(); | ||
| } | ||
|
|
||
| void CollisionSystem::update(our::World* world) { | ||
| // clear previous frame's collisions | ||
| frameCollisions.clear(); | ||
|
|
||
| // Sync entities with colliders to Bullet | ||
| for (auto entity : world->getEntities()) { | ||
| ColliderComponent* collider = entity->getComponent<ColliderComponent>(); | ||
|
|
||
| if (!collider) continue; | ||
|
|
||
| if (entityToBullet.find(entity) == entityToBullet.end()) { | ||
| addEntity(entity); | ||
| } else { | ||
| syncTransform(entity); | ||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| // Remove bullet objects for entities that no longer exists | ||
| std::vector<our::Entity*> toRemove; | ||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| for (const auto& [entity, obj] : entityToBullet) { | ||
| if (world->getEntities().find(entity) == world->getEntities().end()) { | ||
| removeEntity(entity); | ||
|
LoayAhmed304 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // Run collision detection | ||
| collisionWorld->performDiscreteCollisionDetection(); | ||
|
|
||
| // Read collision events | ||
| int numManifolds = dispatcher->getNumManifolds(); | ||
| for (int i = 0; i < numManifolds; i++) { | ||
| // any potential collision will have a manifold, even if it has no contact points | ||
| btPersistentManifold* manifold = dispatcher->getManifoldByIndexInternal(i); | ||
| const btCollisionObject* objA = manifold->getBody0(); | ||
| const btCollisionObject* objB = manifold->getBody1(); | ||
| our::Entity* entityA = static_cast<our::Entity*>(objA->getUserPointer()); | ||
| our::Entity* entityB = static_cast<our::Entity*>(objB->getUserPointer()); | ||
|
|
||
| // check if they collide (if they have contact points) | ||
| // find deepest contact point (the one with the largest penetration depth) | ||
| int numContacts = manifold->getNumContacts(); | ||
| if (numContacts == 0) continue; | ||
| float deepest = 0.0f; | ||
| int deepestIndex = -1; | ||
| for (int j = 0; j < numContacts; j++) { | ||
| // negative distance means penetration, and positive distance means separation. So we want the most | ||
| // negative distance. | ||
| float d = manifold->getContactPoint(j).getDistance(); | ||
| if (d < deepest) { | ||
| deepest = d; | ||
| deepestIndex = j; | ||
| } | ||
| } | ||
|
|
||
| if (deepestIndex != -1) { | ||
| // we have a collision! | ||
| btManifoldPoint& contactPoint = manifold->getContactPoint(deepestIndex); | ||
| CollisionEvent event; | ||
| event.entityA = entityA; | ||
| event.entityB = entityB; | ||
| event.point = btToGlmVec3(contactPoint.getPositionWorldOnB()); | ||
| event.normal = btToGlmVec3(contactPoint.m_normalWorldOnB); | ||
| event.penetrationDepth = -contactPoint.getDistance(); // convert back to positive penetration depth | ||
| frameCollisions.push_back(event); | ||
| } | ||
| } | ||
| // apply pushback for non-trigger collisions | ||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| for (const CollisionEvent& event : frameCollisions) { | ||
| // discard the trigger collisions since they don't need pushback | ||
| auto* colliderA = event.entityA->getComponent<ColliderComponent>(); | ||
| auto* colliderB = event.entityB->getComponent<ColliderComponent>(); | ||
|
|
||
| if (!colliderA || !colliderB) continue; | ||
| if (colliderA->isTrigger || colliderB->isTrigger) continue; | ||
|
|
||
| // push back logic (don't push environments) | ||
| if (colliderA->layer == CollisionLayer::LAYER_ENVIRONMENT) { | ||
| event.entityB->localTransform.position -= event.normal * event.penetrationDepth; | ||
|
LoayAhmed304 marked this conversation as resolved.
Outdated
|
||
| } else if (colliderB->layer == CollisionLayer::LAYER_ENVIRONMENT) { | ||
| event.entityA->localTransform.position += event.normal * event.penetrationDepth; | ||
| } else { // this may be edited or removed later | ||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| event.entityA->localTransform.position -= event.normal * (event.penetrationDepth / 2.0f); | ||
| event.entityB->localTransform.position += event.normal * (event.penetrationDepth / 2.0f); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // On-demand function | ||
| HitInfo CollisionSystem::raycast(const Ray& ray, float maxDistance, const short targetLayer) const { | ||
| HitInfo hitInfo; | ||
|
|
||
| if (!collisionWorld) return hitInfo; | ||
| btVector3 from = glmToBtVec3(ray.origin); | ||
| btVector3 to = glmToBtVec3(ray.origin + ray.direction * maxDistance); | ||
|
|
||
| btCollisionWorld::ClosestRayResultCallback callback(from, to); | ||
|
|
||
| callback.m_collisionFilterGroup = btBroadphaseProxy::AllFilter; // check against all layers | ||
| callback.m_collisionFilterMask = targetLayer; // only collide with the target layer | ||
|
|
||
| collisionWorld->rayTest(from, to, callback); | ||
|
|
||
| if (callback.hasHit()) { | ||
| hitInfo.hit = true; | ||
| hitInfo.entity = static_cast<our::Entity*>(callback.m_collisionObject->getUserPointer()); | ||
| hitInfo.point = btToGlmVec3(callback.m_hitPointWorld); | ||
| hitInfo.normal = btToGlmVec3(callback.m_hitNormalWorld); | ||
| hitInfo.distance = callback.m_closestHitFraction * maxDistance; | ||
| } | ||
|
|
||
| return hitInfo; | ||
| } | ||
|
|
||
| // On-demand function | ||
| std::vector<our::Entity*> CollisionSystem::overlapSphere(const glm::vec3& center, float radius, short targetLayer) { | ||
| std::vector<our::Entity*> results; | ||
| if (!collisionWorld) return results; | ||
|
|
||
| for (const auto& [entity, obj] : entityToBullet) { | ||
| short objGroup = obj->getBroadphaseHandle()->m_collisionFilterGroup; | ||
| if ((objGroup & targetLayer) == 0) continue; | ||
|
|
||
| glm::vec3 entityPos = btToGlmVec3(obj->getWorldTransform().getOrigin()); | ||
|
|
||
| ColliderComponent* collider = entity->getComponent<ColliderComponent>(); | ||
| float entityRadius = collider ? collider->radius : 0.0f; | ||
|
|
||
| // sphere vs sphere hit check | ||
| float dist = glm::length(entityPos - center); | ||
| if (dist <= radius + entityRadius) { | ||
| results.push_back(entity); | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| void CollisionSystem::addEntity(our::Entity* entity) { | ||
| auto* collider = entity->getComponent<ColliderComponent>(); | ||
| if (!collider) return; | ||
|
|
||
| // Create the collision shape based on component data | ||
| btCollisionShape* shape = nullptr; | ||
| switch (collider->shape) { | ||
| case ColliderShape::Sphere: | ||
| shape = new btSphereShape(collider->radius); | ||
| break; | ||
| case ColliderShape::Capsule: { | ||
| // convert from total height to spine | ||
| float spine = collider->height - 2.0f * collider->radius; | ||
| if (spine < 0.0f) spine = 0.0f; | ||
| shape = new btCapsuleShape(collider->radius, spine); | ||
| break; | ||
| } | ||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| } | ||
| ownedShapes.push_back(shape); | ||
|
|
||
| // Create the collision object | ||
| btCollisionObject* obj = new btCollisionObject(); | ||
| obj->setCollisionShape(shape); | ||
| obj->setWorldTransform(entityToBtTransform(entity)); | ||
| obj->setUserPointer(entity); // so we can go back to the entity | ||
|
|
||
| // Mark non-environment objects as KINEMATIC. | ||
| if (collider->layer != CollisionLayer::LAYER_ENVIRONMENT) { | ||
| obj->setCollisionFlags(obj->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); | ||
| } | ||
|
|
||
| // Add to Bullet world with layer filtering | ||
| short group = collider->layer; | ||
| short mask = getMaskForLayer(group); | ||
| collisionWorld->addCollisionObject(obj, group, mask); | ||
| entityToBullet[entity] = obj; | ||
| } | ||
|
|
||
| void CollisionSystem::removeEntity(our::Entity* entity) { | ||
| if (entityToBullet.find(entity) == entityToBullet.end()) return; | ||
|
|
||
|
LoayAhmed304 marked this conversation as resolved.
|
||
| btCollisionObject* obj = entityToBullet[entity]; | ||
| collisionWorld->removeCollisionObject(obj); | ||
| delete obj; | ||
| entityToBullet.erase(entity); | ||
| } | ||
|
|
||
| void CollisionSystem::syncTransform(our::Entity* entity) { | ||
| btCollisionObject* obj = entityToBullet[entity]; | ||
|
|
||
| if (!obj) return; | ||
|
|
||
| obj->setWorldTransform(entityToBtTransform(entity)); | ||
|
|
||
| // Update the broadphase AABB so Bullet uses the new position for collision detection | ||
| collisionWorld->updateSingleAabb(obj); | ||
| } | ||
| const std::vector<CollisionEvent>& CollisionSystem::getCollisions() const { | ||
| return frameCollisions; | ||
| } | ||
|
|
||
| } // namespace gameplay | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.