Add methods for the user to retrieve Colliders, CollisionBodies and RigidBodies

This commit is contained in:
Daniel Chappuis 2020-02-17 17:36:44 +01:00
parent 0ec26f5184
commit 28560d034e
6 changed files with 110 additions and 12 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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);
}

View File

@ -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;

View File

@ -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

View File

@ -213,8 +213,8 @@ class PhysicsWorld {
/// Reference to the collision detection
CollisionDetectionSystem mCollisionDetection;
/// All the bodies (rigid and soft) of the world
List<CollisionBody*> mBodies;
/// All the collision bodies of the world
List<CollisionBody*> 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