Create a new collider and add it to the body This method will return a pointer to a new collider. A collider is an object with a collision shape that is attached to a body. It is possible to attach multiple colliders to a given body. You can use the returned collider to get and set information about the corresponding collision shape for that body. * @param collisionShape A pointer to the collision
| 67 | * @return A pointer to the collider that has been created |
| 68 | */ |
| 69 | Collider* Body::addCollider(CollisionShape* collisionShape, const Transform& transform) { |
| 70 | |
| 71 | // Create a new entity for the collider |
| 72 | Entity colliderEntity = mWorld.mEntityManager.createEntity(); |
| 73 | |
| 74 | // Check that the transform is valid |
| 75 | if (!transform.isValid()) { |
| 76 | RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Error, Logger::Category::Collider, |
| 77 | "Error when adding a collider: the init transform is not valid", __FILE__, __LINE__); |
| 78 | } |
| 79 | assert(transform.isValid()); |
| 80 | |
| 81 | // Create a new collider to attach the collision shape to the body |
| 82 | Collider* collider = new (mWorld.mMemoryManager.allocate(MemoryManager::AllocationType::Pool, |
| 83 | sizeof(Collider))) Collider(colliderEntity, this, mWorld.mMemoryManager); |
| 84 | |
| 85 | // Add the collider component to the entity of the body |
| 86 | AABB shapeAABB = collisionShape->getLocalBounds(); |
| 87 | const Transform localToWorldTransform = mWorld.mTransformComponents.getTransform(mEntity) * transform; |
| 88 | Material material(mWorld.mConfig.defaultFrictionCoefficient, mWorld.mConfig.defaultBounciness); |
| 89 | ColliderComponents::ColliderComponent colliderComponent(mEntity, collider, shapeAABB, |
| 90 | transform, collisionShape, 0x0001, 0xFFFF, localToWorldTransform, material); |
| 91 | bool isActive = mWorld.mBodyComponents.getIsActive(mEntity); |
| 92 | mWorld.mCollidersComponents.addComponent(colliderEntity, !isActive, colliderComponent); |
| 93 | |
| 94 | |
| 95 | mWorld.mBodyComponents.addColliderToBody(mEntity, colliderEntity); |
| 96 | |
| 97 | // Assign the collider with the collision shape |
| 98 | collisionShape->addCollider(collider); |
| 99 | |
| 100 | #ifdef IS_RP3D_PROFILING_ENABLED |
| 101 | |
| 102 | |
| 103 | // Set the profiler |
| 104 | collider->setProfiler(mProfiler); |
| 105 | |
| 106 | #endif |
| 107 | |
| 108 | // Compute the world-space AABB of the new collision shape |
| 109 | const AABB aabb = collisionShape->computeTransformedAABB(mWorld.mTransformComponents.getTransform(mEntity) * transform); |
| 110 | |
| 111 | // Notify the collision detection about this new collision shape |
| 112 | mWorld.mCollisionDetection.addCollider(collider, aabb); |
| 113 | |
| 114 | RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Body, |
| 115 | "Body " + std::to_string(mEntity.id) + ": Collider " + std::to_string(collider->getBroadPhaseId()) + " added to body", __FILE__, __LINE__); |
| 116 | |
| 117 | RP3D_LOG(mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider, |
| 118 | "Collider " + std::to_string(collider->getBroadPhaseId()) + ": collisionShape=" + |
| 119 | collider->getCollisionShape()->to_string(), __FILE__, __LINE__); |
| 120 | |
| 121 | // Return a pointer to the collision shape |
| 122 | return collider; |
| 123 | } |
| 124 | |
| 125 | // Return the number of colliders associated with this body |
| 126 | /** |