2013-09-16 17:22:22 +04:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCPhysicsSetting.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
|
|
|
#ifndef __CCPHYSICS_CONTACT_H__
|
|
|
|
#define __CCPHYSICS_CONTACT_H__
|
|
|
|
|
2013-10-14 10:01:00 +04:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.h"
|
2013-10-30 11:53:58 +04:00
|
|
|
#include "CCEventListener.h"
|
2013-09-16 17:22:22 +04:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class PhysicsShape;
|
|
|
|
class PhysicsWorld;
|
|
|
|
|
|
|
|
class PhysicsContactInfo;
|
|
|
|
|
2013-10-25 06:31:22 +04:00
|
|
|
|
|
|
|
typedef struct PhysicsContactData
|
|
|
|
{
|
|
|
|
Point points[PHYSICS_CONTACT_POINT_MAX];
|
|
|
|
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.
|
|
|
|
*/
|
2013-09-16 17:22:22 +04:00
|
|
|
class PhysicsContact
|
|
|
|
{
|
|
|
|
public:
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief get contact shape A.
|
|
|
|
*/
|
2013-10-18 11:34:13 +04:00
|
|
|
inline PhysicsShape* getShapeA() const { return _shapeA; }
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief get contact shape B.
|
|
|
|
*/
|
2013-10-18 11:34:13 +04:00
|
|
|
inline PhysicsShape* getShapeB() const { return _shapeB; }
|
2013-10-25 06:31:22 +04:00
|
|
|
inline const PhysicsContactData* getContactData() const { return _contactData; }
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief get data.
|
|
|
|
*/
|
2013-09-16 17:22:22 +04:00
|
|
|
inline void* getData() { return _data; }
|
2013-09-17 12:27:43 +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 end.
|
|
|
|
*/
|
|
|
|
inline void setData(void* data) { _data = data; }
|
2013-09-16 17:22:22 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
static PhysicsContact* create(PhysicsShape* a, PhysicsShape* b);
|
|
|
|
bool init(PhysicsShape* a, PhysicsShape* b);
|
|
|
|
|
2013-10-18 11:34:13 +04:00
|
|
|
inline bool getNotify() { return _notify; }
|
|
|
|
inline void setNotify(bool notify) { _notify = notify; }
|
|
|
|
|
2013-10-25 06:31:22 +04:00
|
|
|
void generateContactData();
|
|
|
|
|
2013-09-16 17:22:22 +04:00
|
|
|
private:
|
|
|
|
PhysicsContact();
|
|
|
|
~PhysicsContact();
|
|
|
|
|
|
|
|
private:
|
|
|
|
PhysicsShape* _shapeA;
|
|
|
|
PhysicsShape* _shapeB;
|
|
|
|
PhysicsContactInfo* _info;
|
2013-10-18 11:34:13 +04:00
|
|
|
bool _notify;
|
2013-10-25 06:31:22 +04:00
|
|
|
bool _begin;
|
|
|
|
|
|
|
|
void* _data;
|
|
|
|
void* _contactInfo;
|
|
|
|
PhysicsContactData* _contactData;
|
2013-09-16 17:22:22 +04:00
|
|
|
|
|
|
|
friend class PhysicsWorld;
|
2013-10-21 19:16:21 +04:00
|
|
|
friend class PhysicsWorldCallback;
|
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:
|
|
|
|
// getter/setter
|
|
|
|
float getElasticity();
|
|
|
|
float getFriciton();
|
|
|
|
Point getSurfaceVelocity();
|
|
|
|
void setElasticity(float elasticity);
|
|
|
|
void setFriction(float friction);
|
|
|
|
void setSurfaceVelocity(Point surfaceVelocity);
|
|
|
|
|
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-10-25 06:31:22 +04:00
|
|
|
friend class PhysicsWorld;
|
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:
|
|
|
|
// getter
|
|
|
|
float getElasticity();
|
|
|
|
float getFriciton();
|
|
|
|
Point getSurfaceVelocity();
|
|
|
|
|
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-10-25 06:31:22 +04:00
|
|
|
friend class PhysicsWorld;
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
2013-09-17 12:27:43 +04:00
|
|
|
/*
|
|
|
|
* @brief contact listener.
|
|
|
|
*/
|
2013-10-30 11:53:58 +04:00
|
|
|
class PhysicsContactListener : public EventListener
|
2013-09-16 17:22:22 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-10-30 11:53:58 +04:00
|
|
|
static PhysicsContactListener* create();
|
|
|
|
|
|
|
|
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
2013-10-30 13:27:09 +04:00
|
|
|
virtual bool checkAvailable();
|
2013-10-30 11:53:58 +04:00
|
|
|
virtual EventListener* clone();
|
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.
|
|
|
|
*/
|
2013-10-21 19:16:21 +04:00
|
|
|
std::function<bool(PhysicsWorld& world, const PhysicsContact& contact)> onContactBegin;
|
2013-09-17 12:27:43 +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, elasticity, or surface velocity values.
|
|
|
|
*/
|
2013-10-21 19:16:21 +04:00
|
|
|
std::function<bool(PhysicsWorld& world, const PhysicsContact& contact, const 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
|
|
|
|
*/
|
2013-10-21 19:16:21 +04:00
|
|
|
std::function<void(PhysicsWorld& world, const 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.
|
|
|
|
* onContactBegin and onContactEnd will called in pairs.
|
|
|
|
*/
|
2013-10-21 19:16:21 +04:00
|
|
|
std::function<void(PhysicsWorld& world, const PhysicsContact& contact)> onContactEnd;
|
2013-10-30 11:53:58 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsContactListener();
|
|
|
|
virtual ~PhysicsContactListener();
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsContactWithBodysListener : public PhysicsContactListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsContactWithBodysListener* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
|
|
|
|
|
|
|
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
|
|
|
virtual EventListener* clone();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShape* _a;
|
|
|
|
PhysicsShape* _b;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsContactWithBodysListener();
|
|
|
|
virtual ~PhysicsContactWithBodysListener();
|
2013-09-16 17:22:22 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
#endif //__CCPHYSICS_CONTACT_H__
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|