diff --git a/examples/fallingcubes/CMakeLists.txt b/examples/cubes/CMakeLists.txt similarity index 100% rename from examples/fallingcubes/CMakeLists.txt rename to examples/cubes/CMakeLists.txt diff --git a/examples/fallingcubes/FallingCubes.cpp b/examples/cubes/Cubes.cpp similarity index 100% rename from examples/fallingcubes/FallingCubes.cpp rename to examples/cubes/Cubes.cpp diff --git a/examples/fallingcubes/Scene.cpp b/examples/cubes/Scene.cpp similarity index 100% rename from examples/fallingcubes/Scene.cpp rename to examples/cubes/Scene.cpp diff --git a/examples/fallingcubes/Scene.h b/examples/cubes/Scene.h similarity index 100% rename from examples/fallingcubes/Scene.h rename to examples/cubes/Scene.h diff --git a/src/engine/CollisionWorld.cpp b/src/engine/CollisionWorld.cpp index 8674a289..87dfa76b 100644 --- a/src/engine/CollisionWorld.cpp +++ b/src/engine/CollisionWorld.cpp @@ -34,6 +34,7 @@ using namespace std; // Constructor CollisionWorld::CollisionWorld() : mCollisionDetection(this, mMemoryAllocator), mCurrentBodyID(0) { + } // Destructor diff --git a/src/engine/CollisionWorld.h b/src/engine/CollisionWorld.h index 8678ea01..4d297466 100644 --- a/src/engine/CollisionWorld.h +++ b/src/engine/CollisionWorld.h @@ -39,6 +39,7 @@ #include "../constraint/Joint.h" #include "../constraint/ContactPoint.h" #include "../memory/MemoryAllocator.h" +#include "EventListener.h" /// Namespace reactphysics3d namespace reactphysics3d { @@ -91,7 +92,8 @@ class CollisionWorld { virtual void notifyRemovedOverlappingPair(const BroadPhasePair* removedPair); /// Notify the world about a new narrow-phase contact - virtual void notifyNewContact(const BroadPhasePair* pair, const ContactPointInfo* contactInfo); + virtual void notifyNewContact(const BroadPhasePair* pair, + const ContactPointInfo* contactInfo); /// Update the overlapping pair virtual void updateOverlappingPair(const BroadPhasePair* pair); diff --git a/src/engine/DynamicsWorld.cpp b/src/engine/DynamicsWorld.cpp index 190a9228..68073562 100644 --- a/src/engine/DynamicsWorld.cpp +++ b/src/engine/DynamicsWorld.cpp @@ -48,7 +48,7 @@ DynamicsWorld::DynamicsWorld(const Vector3 &gravity, decimal timeStep = DEFAULT_ mIslands(NULL), mNbBodiesCapacity(0), mSleepLinearVelocity(DEFAULT_SLEEP_LINEAR_VELOCITY), mSleepAngularVelocity(DEFAULT_SLEEP_ANGULAR_VELOCITY), - mTimeBeforeSleep(DEFAULT_TIME_BEFORE_SLEEP) { + mTimeBeforeSleep(DEFAULT_TIME_BEFORE_SLEEP), mEventListener(NULL) { } @@ -147,11 +147,11 @@ void DynamicsWorld::update() { // Update the AABBs of the bodies updateRigidBodiesAABB(); - - // Reset the external force and torque applied to the bodies - resetBodiesForceAndTorque(); } + // Reset the external force and torque applied to the bodies + resetBodiesForceAndTorque(); + // Compute and set the interpolation factor to all the bodies setInterpolationFactorToAllBodies(); } @@ -968,6 +968,13 @@ void DynamicsWorld::notifyNewContact(const BroadPhasePair* broadPhasePair, OverlappingPair* overlappingPair = mOverlappingPairs.find(indexPair)->second; assert(overlappingPair != NULL); + // If it is the first contact since the pair are overlapping + if (overlappingPair->getNbContactPoints() == 0) { + + // Trigger a callback event + if (mEventListener != NULL) mEventListener->beginContact(*contactInfo); + } + // Add the contact to the contact cache of the corresponding overlapping pair overlappingPair->addContact(contact); @@ -978,6 +985,9 @@ void DynamicsWorld::notifyNewContact(const BroadPhasePair* broadPhasePair, // of the two bodies involved in the contact addContactManifoldToBody(overlappingPair->getContactManifold(), overlappingPair->mBody1, overlappingPair->mBody2); + + // Trigger a callback event for the new contact + if (mEventListener != NULL) mEventListener->newContact(*contactInfo); } // Enable/Disable the sleeping technique diff --git a/src/engine/DynamicsWorld.h b/src/engine/DynamicsWorld.h index 93fd5d7d..36f6ab23 100644 --- a/src/engine/DynamicsWorld.h +++ b/src/engine/DynamicsWorld.h @@ -130,6 +130,9 @@ class DynamicsWorld : public CollisionWorld { /// becomes smaller than the sleep velocity. decimal mTimeBeforeSleep; + /// Pointer to an event listener object + EventListener* mEventListener; + // -------------------- Methods -------------------- // /// Private copy-constructor @@ -305,6 +308,9 @@ public : /// Set the time a body is required to stay still before sleeping void setTimeBeforeSleep(decimal timeBeforeSleep); + /// Set an event listener object to receive events callbacks. + void setEventListener(EventListener* eventListener); + // TODO : REMOVE THIS Island** getIslands() { return mIslands;} @@ -490,6 +496,13 @@ inline void DynamicsWorld::setTimeBeforeSleep(decimal timeBeforeSleep) { mTimeBeforeSleep = timeBeforeSleep; } +// Set an event listener object to receive events callbacks. +/// If you use NULL as an argument, the events callbacks will be disabled. +inline void DynamicsWorld::setEventListener(EventListener* eventListener) { + mEventListener = eventListener; +} + + } #endif diff --git a/src/engine/EventListener.h b/src/engine/EventListener.h new file mode 100644 index 00000000..97d7c347 --- /dev/null +++ b/src/engine/EventListener.h @@ -0,0 +1,62 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * +* Copyright (c) 2010-2013 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +#ifndef REACTPHYSICS3D_EVENT_LISTENER_H +#define REACTPHYSICS3D_EVENT_LISTENER_H + +// Libraries +#include "../constraint/ContactPoint.h" + +namespace reactphysics3d { + +// Class EventListener +/** + * This class can be used to receive event callbacks from the physics engine. + * In order to receive callbacks, you need to create a new class that inherits from + * this one and you must override the methods you need. Then, you need to register your + * new event listener class to the physics world using the DynamicsWorld::setEventListener() + * method. + */ +class EventListener { + + public : + + /// Constructor + EventListener() {} + + /// Destructor + virtual ~EventListener() {} + + /// Called when a new contact point is found between two bodies that were separated before + virtual void beginContact(const ContactPointInfo& contact) {} + + /// Called when a new contact point is found between two bodies + virtual void newContact(const ContactPointInfo& contact) {} + +}; + +} + +#endif diff --git a/src/reactphysics3d.h b/src/reactphysics3d.h index ababb9bc..cf85da5e 100644 --- a/src/reactphysics3d.h +++ b/src/reactphysics3d.h @@ -42,6 +42,7 @@ #include "engine/DynamicsWorld.h" #include "engine/CollisionWorld.h" #include "engine/Material.h" +#include "engine/EventListener.h" #include "collision/shapes/CollisionShape.h" #include "collision/shapes/BoxShape.h" #include "collision/shapes/SphereShape.h"