Added apply effect functionality

This commit is contained in:
Robin Engman 2014-02-12 10:23:07 +01:00
parent 3324da528d
commit f0178cf558
3 changed files with 106 additions and 4 deletions

View File

@ -12,6 +12,8 @@ using namespace ::Utility::Value;
API_Impl API_instance; API_Impl API_instance;
API & API::Instance() API & API::Instance()
{ {
return API_instance; return API_instance;
@ -251,9 +253,72 @@ void API_Impl::ReleaseFromLimbo( const ICustomBody* objRef )
} }
void API_Impl::ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ) void API_Impl::ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect)
{ {
btRigidBody* body;
btCollisionShape* shape;
btMotionState* state;
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, NULL, NULL);
Sphere* sphere;
Box* box;
switch(collideable->type)
{
case ICollideable::Type::Type_sphere:
sphere = dynamic_cast<Sphere*>(collideable);
// Add collision shape
shape = new btSphereShape(sphere->radius);
// Add motion state
state = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f),btVector3(sphere->center.x, sphere->center.y, sphere->center.z)));
// Add rigid body
rigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(0, state, shape);
body = new btRigidBody(rigidBodyCI);
break;
case ICollideable::Type::Type_box:
box = dynamic_cast<Box*>(collideable);
// Add collision shape
shape = new btBoxShape(btVector3(box->boundingOffset.x, box->boundingOffset.y, box->boundingOffset.z));
// Add motion state
state = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f),btVector3(box->center.x, box->center.y, box->center.z)));
// Add rigid body
rigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(0, state, shape);
body = new btRigidBody(rigidBodyCI);
break;
//case ICollideable::Type::Type_cone:
//cone = dynamic_cast<Cone*>(collideable);
// Add collision shape
//shape = new btConeShape();
// Add motion state
//state = new btDefaultMotionState(btTransform(btQuaternion(),btVector3(cone->center.x, cone->center.y, cone->center.z)));
// Add rigid body
//btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, state, shape);
//body = new btRigidBody(rigidBodyCI);
//break;
default:
return;
}
ContactSensorCallback callback(*body, effect, args);
this->dynamicsWorld->contactTest(body, callback);
delete state;
state = NULL;
delete shape;
shape = NULL;
delete body;
body = NULL;
} }
namespace Oyster namespace Oyster

View File

@ -12,6 +12,42 @@ namespace Oyster
class API_Impl : public API class API_Impl : public API
{ {
public: public:
struct ContactSensorCallback : public btCollisionWorld::ContactResultCallback
{
ContactSensorCallback(btRigidBody& contactBody, EventAction_ApplyEffect effect, void* args)
: btCollisionWorld::ContactResultCallback(), body(contactBody), func(effect), args(args) {}
btRigidBody& body;
EventAction_ApplyEffect func;
void* args;
virtual bool needsCollision(btBroadphaseProxy* proxy) const
{
if(!btCollisionWorld::ContactResultCallback::needsCollision(proxy))
return false;
return body.checkCollideWithOverride(static_cast<btCollisionObject*>(proxy->m_clientObject));
}
virtual btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0, int partId0, int index0, const btCollisionObjectWrapper* colObj1, int partId1, int index1)
{
btVector3 pt;
if(colObj0->m_collisionObject == &body)
{
pt = cp.m_localPointA;
this->func((ICustomBody*)(colObj0->getCollisionObject()->getUserPointer()), this->args);
}
else
{
assert(colObj1->m_collisionObject == &body && "Body does not match either collision object");
pt = cp.m_localPointB;
this->func((ICustomBody*)(colObj1->getCollisionObject()->getUserPointer()), this->args);
}
return 0;
}
};
API_Impl(); API_Impl();
virtual ~API_Impl(); virtual ~API_Impl();
@ -33,7 +69,7 @@ namespace Oyster
void UpdateWorld(); void UpdateWorld();
void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ); void ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect);
private: private:
btBroadphaseInterface* broadphase; btBroadphaseInterface* broadphase;

View File

@ -45,6 +45,7 @@ namespace Oyster
typedef Struct::Gravity Gravity; typedef Struct::Gravity Gravity;
typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto ); typedef void (*EventAction_Destruction)( ::Utility::DynamicMemory::UniquePointer<ICustomBody> proto );
typedef void (*EventAction_ApplyEffect)(ICustomBody* collidedBody, void* args);
/** Gets the Physics instance. */ /** Gets the Physics instance. */
static API & Instance(); static API & Instance();
@ -99,7 +100,7 @@ namespace Oyster
* @param hitAction: A function that contains the effect. Parameterlist contains the custom body * @param hitAction: A function that contains the effect. Parameterlist contains the custom body
the collideable hits, and the arguments sent to the function. the collideable hits, and the arguments sent to the function.
********************************************************/ ********************************************************/
virtual void ApplyEffect( const Oyster::Collision3D::ICollideable& collideable, void* args, void(hitAction)(ICustomBody*, void*) ) = 0; virtual void ApplyEffect(Oyster::Collision3D::ICollideable* collideable, void* args, EventAction_ApplyEffect effect) = 0;
protected: protected:
virtual ~API() {} virtual ~API() {}