2013-09-16 17:22:22 +04:00
|
|
|
/****************************************************************************
|
2014-01-07 07:47:11 +04:00
|
|
|
Copyright (c) 2013 Chukong Technologies Inc.
|
2013-09-16 17:22:22 +04:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCPHYSICS_CONTACT_H__
|
|
|
|
#define __CCPHYSICS_CONTACT_H__
|
|
|
|
|
2013-12-04 10:36:32 +04:00
|
|
|
#include "ccConfig.h"
|
2013-12-26 19:55:05 +04:00
|
|
|
#if CC_USE_PHYSICS
|
2013-11-06 10:51:56 +04:00
|
|
|
|
2014-02-20 06:53:49 +04:00
|
|
|
#include "CCRef.h"
|
2013-10-14 10:01:00 +04:00
|
|
|
#include "CCGeometry.h"
|
2013-11-01 10:50:06 +04:00
|
|
|
#include "CCEventListenerCustom.h"
|
|
|
|
#include "CCEvent.h"
|
2014-01-14 15:21:09 +04:00
|
|
|
#include "CCEventCustom.h"
|
2013-09-16 17:22:22 +04:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class PhysicsShape;
|
2013-11-01 10:50:06 +04:00
|
|
|
class PhysicsBody;
|
2013-09-16 17:22:22 +04:00
|
|
|
class PhysicsWorld;
|
|
|
|
|
|
|
|
class PhysicsContactInfo;
|
|
|
|
|
2013-11-26 13:29:09 +04:00
|
|
|
typedef Point Vect;
|
2013-10-25 06:31:22 +04:00
|
|
|
|
|
|
|
typedef struct PhysicsContactData
|
|
|
|
{
|
2013-12-05 13:19:01 +04:00
|
|
|
static const int POINT_MAX = 4;
|
2013-11-26 14:22:52 +04:00
|
|
|
Point points[POINT_MAX];
|
2013-10-25 06:31:22 +04:00
|
|
|
int count;
|
|
|
|
Point normal;
|
|
|
|
|
|
|
|
PhysicsContactData()
|
|
|
|
: count(0)
|
|
|
|
{}
|
|
|
|
}PhysicsContactData;
|
|
|
|
|
2013-09-17 12:27:43 +04:00
|
|
|
/**
|
|
|
|
* @brief Contact infomation. it will created automatically when two shape contact with each other. and it will destoried automatically when two shape separated.
|
|
|
|
*/
|
2014-01-14 15:21:09 +04:00
|
|
|
class PhysicsContact : public EventCustom
|
2013-09-16 17:22:22 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-01 10:50:06 +04:00
|
|
|
|
|
|
|
enum class EventCode
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
BEGIN,
|
|
|
|
PRESOLVE,
|
|
|
|
POSTSOLVE,
|
|
|
|
SEPERATE
|
|
|
|
};
|
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get contact shape A. */
|
2013-10-18 11:34:13 +04:00
|
|
|
inline PhysicsShape* getShapeA() const { return _shapeA; }
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get contact shape B. */
|
2013-10-18 11:34:13 +04:00
|
|
|
inline PhysicsShape* getShapeB() const { return _shapeB; }
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get contact data */
|
2013-10-25 06:31:22 +04:00
|
|
|
inline const PhysicsContactData* getContactData() const { return _contactData; }
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get data. */
|
2013-11-01 12:26:03 +04:00
|
|
|
inline void* getData() const { return _data; }
|
2013-11-12 14:13:44 +04:00
|
|
|
/**
|
|
|
|
* @brief set data to contact. you must manage the memory yourself, Generally you can set data at contact begin, and distory it at contact seperate.
|
2013-09-17 12:27:43 +04:00
|
|
|
*/
|
|
|
|
inline void setData(void* data) { _data = data; }
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get the event code */
|
2013-11-01 12:26:03 +04:00
|
|
|
EventCode getEventCode() const { return _eventCode; };
|
2014-01-14 15:21:09 +04:00
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
private:
|
2013-11-18 17:06:05 +04:00
|
|
|
static PhysicsContact* construct(PhysicsShape* a, PhysicsShape* b);
|
2013-09-16 17:22:22 +04:00
|
|
|
bool init(PhysicsShape* a, PhysicsShape* b);
|
|
|
|
|
2013-11-01 10:50:06 +04:00
|
|
|
void setEventCode(EventCode eventCode) { _eventCode = eventCode; };
|
2013-11-06 11:43:29 +04:00
|
|
|
inline bool isNotificationEnabled() const { return _notificationEnable; }
|
|
|
|
inline void setNotificationEnable(bool enable) { _notificationEnable = enable; }
|
2013-11-01 12:26:03 +04:00
|
|
|
inline PhysicsWorld* getWorld() const { return _world; }
|
2013-11-01 10:50:06 +04:00
|
|
|
inline void setWorld(PhysicsWorld* world) { _world = world; }
|
|
|
|
inline void setResult(bool result) { _result = result; }
|
|
|
|
inline bool resetResult() { bool ret = _result; _result = true; return ret; }
|
2013-10-18 11:34:13 +04:00
|
|
|
|
2013-10-25 06:31:22 +04:00
|
|
|
void generateContactData();
|
2014-01-14 15:21:09 +04:00
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
private:
|
|
|
|
PhysicsContact();
|
|
|
|
~PhysicsContact();
|
|
|
|
|
|
|
|
private:
|
2013-11-01 10:50:06 +04:00
|
|
|
PhysicsWorld* _world;
|
2013-09-16 17:22:22 +04:00
|
|
|
PhysicsShape* _shapeA;
|
|
|
|
PhysicsShape* _shapeB;
|
2013-11-01 10:50:06 +04:00
|
|
|
EventCode _eventCode;
|
2013-09-16 17:22:22 +04:00
|
|
|
PhysicsContactInfo* _info;
|
2013-11-06 11:43:29 +04:00
|
|
|
bool _notificationEnable;
|
2013-10-25 06:31:22 +04:00
|
|
|
bool _begin;
|
2013-11-01 10:50:06 +04:00
|
|
|
bool _result;
|
2013-10-25 06:31:22 +04:00
|
|
|
|
|
|
|
void* _data;
|
|
|
|
void* _contactInfo;
|
|
|
|
PhysicsContactData* _contactData;
|
2013-09-16 17:22:22 +04:00
|
|
|
|
2013-11-01 10:50:06 +04:00
|
|
|
friend class EventListenerPhysicsContact;
|
2013-10-21 19:16:21 +04:00
|
|
|
friend class PhysicsWorldCallback;
|
2013-11-01 10:50:06 +04:00
|
|
|
friend class PhysicsWorld;
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief presolve value generated when onContactPreSolve called.
|
|
|
|
*/
|
2013-09-16 17:22:22 +04:00
|
|
|
class PhysicsContactPreSolve
|
|
|
|
{
|
2013-10-25 06:31:22 +04:00
|
|
|
public:
|
2014-02-25 11:58:19 +04:00
|
|
|
/** get restitution between two bodies*/
|
|
|
|
float getRestitution() const;
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get friction between two bodies*/
|
|
|
|
float getFriction() const;
|
|
|
|
/** get surface velocity between two bodies*/
|
2013-11-01 12:26:03 +04:00
|
|
|
Point getSurfaceVelocity() const;
|
2014-02-25 11:58:19 +04:00
|
|
|
/** set the restitution*/
|
|
|
|
void setRestitution(float restitution);
|
2013-11-12 14:13:44 +04:00
|
|
|
/** set the friction*/
|
2013-10-25 06:31:22 +04:00
|
|
|
void setFriction(float friction);
|
2013-11-12 14:13:44 +04:00
|
|
|
/** set the surface velocity*/
|
2013-11-05 16:02:58 +04:00
|
|
|
void setSurfaceVelocity(const Vect& velocity);
|
2013-11-12 14:13:44 +04:00
|
|
|
/** ignore the rest of the contact presolve and postsolve callbacks */
|
2013-11-05 11:54:33 +04:00
|
|
|
void ignore();
|
2013-10-25 06:31:22 +04:00
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
private:
|
2013-10-25 06:31:22 +04:00
|
|
|
PhysicsContactPreSolve(PhysicsContactData* data, void* contactInfo);
|
2013-09-16 17:22:22 +04:00
|
|
|
~PhysicsContactPreSolve();
|
|
|
|
|
2013-10-25 06:31:22 +04:00
|
|
|
private:
|
|
|
|
float _elasticity;
|
|
|
|
float _friction;
|
|
|
|
Point _surfaceVelocity;
|
|
|
|
PhysicsContactData* _preContactData;
|
|
|
|
void* _contactInfo;
|
2013-09-16 17:22:22 +04:00
|
|
|
|
2013-11-01 10:50:06 +04:00
|
|
|
friend class EventListenerPhysicsContact;
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief postsolve value generated when onContactPostSolve called.
|
|
|
|
*/
|
2013-09-16 17:22:22 +04:00
|
|
|
class PhysicsContactPostSolve
|
|
|
|
{
|
2013-10-25 06:31:22 +04:00
|
|
|
public:
|
2014-02-25 11:58:19 +04:00
|
|
|
/** get restitution between two bodies*/
|
|
|
|
float getRestitution() const;
|
2013-11-12 14:13:44 +04:00
|
|
|
/** get friction between two bodies*/
|
|
|
|
float getFriction() const;
|
|
|
|
/** get surface velocity between two bodies*/
|
2013-11-01 12:26:03 +04:00
|
|
|
Point getSurfaceVelocity() const;
|
2013-10-25 06:31:22 +04:00
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
private:
|
2013-10-25 06:31:22 +04:00
|
|
|
PhysicsContactPostSolve(void* contactInfo);
|
2013-09-16 17:22:22 +04:00
|
|
|
~PhysicsContactPostSolve();
|
|
|
|
|
2013-10-25 06:31:22 +04:00
|
|
|
private:
|
|
|
|
void* _contactInfo;
|
2013-09-16 17:22:22 +04:00
|
|
|
|
2013-11-01 10:50:06 +04:00
|
|
|
friend class EventListenerPhysicsContact;
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
/* contact listener. it will recive all the contact callbacks. */
|
2013-11-01 10:50:06 +04:00
|
|
|
class EventListenerPhysicsContact : public EventListenerCustom
|
2013-09-16 17:22:22 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-12 14:13:44 +04:00
|
|
|
/** create the listener */
|
2013-11-01 10:50:06 +04:00
|
|
|
static EventListenerPhysicsContact* create();
|
|
|
|
virtual bool checkAvailable() override;
|
|
|
|
virtual EventListenerPhysicsContact* clone() override;
|
2013-09-16 17:22:22 +04:00
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* it will be call when two body have contact.
|
|
|
|
* if return false, it will not invoke callbacks
|
|
|
|
*/
|
2013-11-17 23:10:13 +04:00
|
|
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
2013-11-12 14:13:44 +04:00
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
public:
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief it will called at two shapes start to contact, and only call it once.
|
|
|
|
*/
|
2014-01-28 10:59:34 +04:00
|
|
|
std::function<bool(PhysicsContact& contact)> onContactBegin;
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
2014-02-25 11:58:19 +04:00
|
|
|
* @brief Two shapes are touching during this step. Return false from the callback to make world ignore the collision this step or true to process it normally. Additionally, you may override collision values, restitution, or surface velocity values.
|
2013-09-17 12:27:43 +04:00
|
|
|
*/
|
2014-01-28 10:59:34 +04:00
|
|
|
std::function<bool(PhysicsContact& contact, PhysicsContactPreSolve& solve)> onContactPreSolve;
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief Two shapes are touching and their collision response has been processed. You can retrieve the collision impulse or kinetic energy at this time if you want to use it to calculate sound volumes or damage amounts. See cpArbiter for more info
|
|
|
|
*/
|
2014-01-28 10:59:34 +04:00
|
|
|
std::function<void(PhysicsContact& contact, const PhysicsContactPostSolve& solve)> onContactPostSolve;
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief it will called at two shapes separated, and only call it once.
|
2013-11-01 10:50:06 +04:00
|
|
|
* onContactBegin and onContactSeperate will called in pairs.
|
2013-09-17 12:27:43 +04:00
|
|
|
*/
|
2014-01-28 10:59:34 +04:00
|
|
|
std::function<void(PhysicsContact& contact)> onContactSeperate;
|
2013-11-01 10:50:06 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init();
|
|
|
|
void onEvent(EventCustom* event);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
EventListenerPhysicsContact();
|
|
|
|
virtual ~EventListenerPhysicsContact();
|
2013-11-12 14:13:44 +04:00
|
|
|
|
|
|
|
friend class PhysicsWorld;
|
2013-11-01 10:50:06 +04:00
|
|
|
};
|
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
/** this event listener only be called when bodyA and bodyB have contacts */
|
2013-11-01 10:50:06 +04:00
|
|
|
class EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB);
|
|
|
|
|
2013-11-17 23:10:13 +04:00
|
|
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
2013-11-01 10:50:06 +04:00
|
|
|
virtual EventListenerPhysicsContactWithBodies* clone() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsBody* _a;
|
|
|
|
PhysicsBody* _b;
|
2013-10-30 11:53:58 +04:00
|
|
|
|
|
|
|
protected:
|
2013-11-01 10:50:06 +04:00
|
|
|
EventListenerPhysicsContactWithBodies();
|
|
|
|
virtual ~EventListenerPhysicsContactWithBodies();
|
2013-10-30 11:53:58 +04:00
|
|
|
};
|
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
/** this event listener only be called when shapeA and shapeB have contacts */
|
2013-11-01 10:50:06 +04:00
|
|
|
class EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
|
2013-10-30 11:53:58 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-01 10:50:06 +04:00
|
|
|
static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
2013-10-30 11:53:58 +04:00
|
|
|
|
2013-11-17 23:10:13 +04:00
|
|
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
2013-11-01 10:50:06 +04:00
|
|
|
virtual EventListenerPhysicsContactWithShapes* clone() override;
|
2013-10-30 11:53:58 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShape* _a;
|
|
|
|
PhysicsShape* _b;
|
|
|
|
|
|
|
|
protected:
|
2013-11-01 10:50:06 +04:00
|
|
|
EventListenerPhysicsContactWithShapes();
|
|
|
|
virtual ~EventListenerPhysicsContactWithShapes();
|
|
|
|
};
|
|
|
|
|
2013-11-12 14:13:44 +04:00
|
|
|
/** this event listener only be called when shapeA or shapeB is in the group your specified */
|
2013-11-01 10:50:06 +04:00
|
|
|
class EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static EventListenerPhysicsContactWithGroup* create(int group);
|
|
|
|
|
2013-11-17 23:10:13 +04:00
|
|
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
2013-11-01 10:50:06 +04:00
|
|
|
virtual EventListenerPhysicsContactWithGroup* clone() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int _group;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
EventListenerPhysicsContactWithGroup();
|
|
|
|
virtual ~EventListenerPhysicsContactWithGroup();
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|
2013-11-06 10:51:56 +04:00
|
|
|
#endif //__CCPHYSICS_CONTACT_H__
|