Add the EventListener class

This commit is contained in:
Daniel Chappuis 2013-09-10 21:33:52 +02:00
parent d4c7eee175
commit 07df001e8b
10 changed files with 94 additions and 5 deletions

View File

@ -34,6 +34,7 @@ using namespace std;
// Constructor
CollisionWorld::CollisionWorld()
: mCollisionDetection(this, mMemoryAllocator), mCurrentBodyID(0) {
}
// Destructor

View File

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

View File

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

View File

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

View File

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

View File

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