From 28560d034e42b036c81ab50fed7f61b912e8f774 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Mon, 17 Feb 2020 17:36:44 +0100 Subject: [PATCH] Add methods for the user to retrieve Colliders, CollisionBodies and RigidBodies --- CHANGELOG.md | 2 + src/body/CollisionBody.cpp | 2 + src/collision/Collider.cpp | 10 ++++- src/collision/Collider.h | 5 ++- src/engine/PhysicsWorld.cpp | 14 +++--- src/engine/PhysicsWorld.h | 89 ++++++++++++++++++++++++++++++++++++- 6 files changed, 110 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d10a1d92..29b8b182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ - It is now possible to change the scale of a ConvexMeshShape using the ConvexMeshShape::setScale() method - It is now possible to change the scale of a ConcaveMeshShape using the ConcaveMeshShape::setScale() method - It is now possible to change the scale of a HeightFieldShape using the HeightFieldShape::setScale() method + - A method PhysicsWorld::getCollisionBody(uint index) has been added on a physics world to retrieve a given CollisionBody + - A method PhysicsWorld::getRigidBody(uint index) has been added on a physics world to retrieve a given RigidBody ### Changed diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index e9354080..fdd8b12b 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -137,6 +137,7 @@ uint CollisionBody::getNbColliders() const { // Return a const pointer to a given collider of the body /** +* @param index Index of a Collider of the body * @return The const pointer of a given collider of the body */ const Collider* CollisionBody::getCollider(uint colliderIndex) const { @@ -150,6 +151,7 @@ const Collider* CollisionBody::getCollider(uint colliderIndex) const { // Return a pointer to a given collider of the body /** +* @param index Index of a Collider of the body * @return The pointer of a given collider of the body */ Collider* CollisionBody::getCollider(uint colliderIndex) { diff --git a/src/collision/Collider.cpp b/src/collision/Collider.cpp index 14176b29..d2c24076 100644 --- a/src/collision/Collider.cpp +++ b/src/collision/Collider.cpp @@ -147,7 +147,15 @@ const AABB Collider::getWorldAABB() const { /** * @return Pointer to the internal collision shape */ -CollisionShape* Collider::getCollisionShape() const { +CollisionShape* Collider::getCollisionShape() { + return mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity); +} + +// Return a const pointer to the collision shape +/** + * @return Pointer to the internal collision shape + */ +const CollisionShape* Collider::getCollisionShape() const { return mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity); } diff --git a/src/collision/Collider.h b/src/collision/Collider.h index 50599103..66f9f818 100644 --- a/src/collision/Collider.h +++ b/src/collision/Collider.h @@ -104,7 +104,10 @@ class Collider { Entity getEntity() const; /// Return a pointer to the collision shape - CollisionShape* getCollisionShape() const; + CollisionShape* getCollisionShape(); + + /// Return a const pointer to the collision shape + const CollisionShape* getCollisionShape() const; /// Return the parent body CollisionBody* getBody() const; diff --git a/src/engine/PhysicsWorld.cpp b/src/engine/PhysicsWorld.cpp index bf41d5fb..efe1ec73 100644 --- a/src/engine/PhysicsWorld.cpp +++ b/src/engine/PhysicsWorld.cpp @@ -58,7 +58,7 @@ PhysicsWorld::PhysicsWorld(MemoryManager& memoryManager, const WorldSettings& wo mFixedJointsComponents(mMemoryManager.getHeapAllocator()), mHingeJointsComponents(mMemoryManager.getHeapAllocator()), mSliderJointsComponents(mMemoryManager.getHeapAllocator()), mCollisionDetection(this, mCollidersComponents, mTransformComponents, mCollisionBodyComponents, mRigidBodyComponents, mMemoryManager), - mBodies(mMemoryManager.getHeapAllocator()), mEventListener(nullptr), + mCollisionBodies(mMemoryManager.getHeapAllocator()), mEventListener(nullptr), mName(worldSettings.worldName), mIslands(mMemoryManager.getSingleFrameAllocator()), mContactSolverSystem(mMemoryManager, *this, mIslands, mCollisionBodyComponents, mRigidBodyComponents, mCollidersComponents, mConfig.restitutionVelocityThreshold), @@ -124,8 +124,8 @@ PhysicsWorld::~PhysicsWorld() { "Physics World: Physics world " + mName + " has been destroyed"); // Destroy all the collision bodies that have not been removed - for (int i=mBodies.size() - 1 ; i >= 0; i--) { - destroyCollisionBody(mBodies[i]); + for (int i=mCollisionBodies.size() - 1 ; i >= 0; i--) { + destroyCollisionBody(mCollisionBodies[i]); } #ifdef IS_PROFILING_ACTIVE @@ -135,7 +135,7 @@ PhysicsWorld::~PhysicsWorld() { #endif - assert(mBodies.size() == 0); + assert(mCollisionBodies.size() == 0); assert(mCollisionBodyComponents.getNbComponents() == 0); assert(mTransformComponents.getNbComponents() == 0); assert(mCollidersComponents.getNbComponents() == 0); @@ -180,7 +180,7 @@ CollisionBody* PhysicsWorld::createCollisionBody(const Transform& transform) { mCollisionBodyComponents.addComponent(entity, false, bodyComponent); // Add the collision body to the world - mBodies.add(collisionBody); + mCollisionBodies.add(collisionBody); #ifdef IS_PROFILING_ACTIVE @@ -219,7 +219,7 @@ void PhysicsWorld::destroyCollisionBody(CollisionBody* collisionBody) { collisionBody->~CollisionBody(); // Remove the collision body from the list of bodies - mBodies.remove(collisionBody); + mCollisionBodies.remove(collisionBody); // Free the object from the memory allocator mMemoryManager.release(MemoryManager::AllocationType::Pool, collisionBody, sizeof(CollisionBody)); @@ -461,7 +461,6 @@ RigidBody* PhysicsWorld::createRigidBody(const Transform& transform) { mRigidBodyComponents.setMassInverse(entity, decimal(1.0) / mRigidBodyComponents.getInitMass(entity)); // Add the rigid body to the physics world - mBodies.add(rigidBody); mRigidBodies.add(rigidBody); #ifdef IS_PROFILING_ACTIVE @@ -507,7 +506,6 @@ void PhysicsWorld::destroyRigidBody(RigidBody* rigidBody) { rigidBody->~RigidBody(); // Remove the rigid body from the list of rigid bodies - mBodies.remove(rigidBody); mRigidBodies.remove(rigidBody); // Free the object from the memory allocator diff --git a/src/engine/PhysicsWorld.h b/src/engine/PhysicsWorld.h index c70de69d..30b800e1 100644 --- a/src/engine/PhysicsWorld.h +++ b/src/engine/PhysicsWorld.h @@ -213,8 +213,8 @@ class PhysicsWorld { /// Reference to the collision detection CollisionDetectionSystem mCollisionDetection; - /// All the bodies (rigid and soft) of the world - List mBodies; + /// All the collision bodies of the world + List mCollisionBodies; /// Pointer to an event listener object EventListener* mEventListener; @@ -434,6 +434,24 @@ class PhysicsWorld { /// Set an event listener object to receive events callbacks. void setEventListener(EventListener* eventListener); + /// Return the number of CollisionBody in the physics world + uint getNbCollisionBodies() const; + + /// Return a constant pointer to a given CollisionBody of the world + const CollisionBody* getCollisionBody(uint index) const; + + /// Return a pointer to a given CollisionBody of the world + CollisionBody* getCollisionBody(uint index) ; + + /// Return the number of RigidBody in the physics world + uint getNbRigidBodies() const; + + /// Return a constant pointer to a given RigidBody of the world + const RigidBody* getRigidBody(uint index) const; + + /// Return a pointer to a given RigidBody of the world + RigidBody* getRigidBody(uint index) ; + #ifdef IS_PROFILING_ACTIVE /// Return a reference to the profiler @@ -775,6 +793,73 @@ inline void PhysicsWorld::setEventListener(EventListener* eventListener) { mEventListener = eventListener; } +// Return the number of CollisionBody in the physics world +/// Note that even if a RigidBody is also a collision body, this method does not return the rigid bodies +inline uint PhysicsWorld::getNbCollisionBodies() const { + return mCollisionBodies.size(); +} + +// Return a constant pointer to a given CollisionBody of the world +/** + * @param index Index of a CollisionBody in the world + * @return Constant pointer to a given CollisionBody + */ +inline const CollisionBody* PhysicsWorld::getCollisionBody(uint index) const { + + assert(index < mCollisionBodies.size()); + + // TODO : Report error here if index is not within bounds + + return mCollisionBodies[index]; +} + +// Return a pointer to a given CollisionBody of the world +/** + * @param index Index of a CollisionBody in the world + * @return Pointer to a given CollisionBody + */ +inline CollisionBody* PhysicsWorld::getCollisionBody(uint index) { + + assert(index < mCollisionBodies.size()); + + // TODO : Report error here if index is not within bounds + + return mCollisionBodies[index]; +} + +// Return the number of RigidBody in the physics world +inline uint PhysicsWorld::getNbRigidBodies() const { + return mRigidBodies.size(); +} + + +// Return a constant pointer to a given RigidBody of the world +/** + * @param index Index of a RigidBody in the world + * @return Constant pointer to a given RigidBody + */ +inline const RigidBody* PhysicsWorld::getRigidBody(uint index) const { + assert(index < mRigidBodies.size()); + + // TODO : Report error here if index is not within bounds + + return mRigidBodies[index]; +} + +// Return a pointer to a given RigidBody of the world +/** + * @param index Index of a RigidBody in the world + * @return Pointer to a given RigidBody + */ +inline RigidBody* PhysicsWorld::getRigidBody(uint index) { + + assert(index < mRigidBodies.size()); + + // TODO : Report error here if index is not within bounds + + return mRigidBodies[index]; +} + } #endif