123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #ifndef __CCPHYSICS_CONTACT_H__
- #define __CCPHYSICS_CONTACT_H__
- #include "base/ccConfig.h"
- #if CC_USE_PHYSICS
- #include "base/CCRef.h"
- #include "math/CCGeometry.h"
- #include "base/CCEventListenerCustom.h"
- #include "base/CCEvent.h"
- #include "base/CCEventCustom.h"
- NS_CC_BEGIN
- class PhysicsShape;
- class PhysicsBody;
- class PhysicsWorld;
- typedef struct CC_DLL PhysicsContactData
- {
- static const int POINT_MAX = 4;
- Vec2 points[POINT_MAX];
- int count;
- Vec2 normal;
-
- PhysicsContactData()
- : count(0)
- {}
- }PhysicsContactData;
- class CC_DLL PhysicsContact : public EventCustom
- {
- public:
-
- enum class EventCode
- {
- NONE,
- BEGIN,
- PRESOLVE,
- POSTSOLVE,
- SEPARATE
- };
-
-
- PhysicsShape* getShapeA() const { return _shapeA; }
-
-
- PhysicsShape* getShapeB() const { return _shapeB; }
-
-
- const PhysicsContactData* getContactData() const { return _contactData; }
-
-
- const PhysicsContactData* getPreContactData() const { return _preContactData; }
-
-
- void* getData() const { return _data; }
-
-
- void setData(void* data) { _data = data; }
-
- EventCode getEventCode() const { return _eventCode; };
- private:
- static PhysicsContact* construct(PhysicsShape* a, PhysicsShape* b);
- bool init(PhysicsShape* a, PhysicsShape* b);
-
- void setEventCode(EventCode eventCode) { _eventCode = eventCode; };
- bool isNotificationEnabled() const { return _notificationEnable; }
- void setNotificationEnable(bool enable) { _notificationEnable = enable; }
- PhysicsWorld* getWorld() const { return _world; }
- void setWorld(PhysicsWorld* world) { _world = world; }
- void setResult(bool result) { _result = result; }
- bool resetResult() { bool ret = _result; _result = true; return ret; }
-
- void generateContactData();
- private:
- PhysicsContact();
- ~PhysicsContact();
-
- private:
- PhysicsWorld* _world;
- PhysicsShape* _shapeA;
- PhysicsShape* _shapeB;
- EventCode _eventCode;
- bool _notificationEnable;
- bool _result;
-
- void* _data;
- void* _contactInfo;
- PhysicsContactData* _contactData;
- PhysicsContactData* _preContactData;
-
- friend class EventListenerPhysicsContact;
- friend class PhysicsWorldCallback;
- friend class PhysicsWorld;
- };
- class CC_DLL PhysicsContactPreSolve
- {
- public:
-
- float getRestitution() const;
-
- float getFriction() const;
-
- Vec2 getSurfaceVelocity() const;
-
- void setRestitution(float restitution);
-
- void setFriction(float friction);
-
- void setSurfaceVelocity(const Vec2& velocity);
-
- void ignore();
-
- private:
- PhysicsContactPreSolve(void* contactInfo);
- ~PhysicsContactPreSolve();
-
- private:
- void* _contactInfo;
-
- friend class EventListenerPhysicsContact;
- };
- class CC_DLL PhysicsContactPostSolve
- {
- public:
-
- float getRestitution() const;
-
- float getFriction() const;
-
- Vec2 getSurfaceVelocity() const;
-
- private:
- PhysicsContactPostSolve(void* contactInfo);
- ~PhysicsContactPostSolve();
-
- private:
- void* _contactInfo;
-
- friend class EventListenerPhysicsContact;
- };
- class CC_DLL EventListenerPhysicsContact : public EventListenerCustom
- {
- public:
-
- static EventListenerPhysicsContact* create();
-
-
- virtual bool checkAvailable() override;
-
-
- virtual EventListenerPhysicsContact* clone() override;
-
- protected:
-
- virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB);
-
- public:
-
- std::function<bool(PhysicsContact& contact)> onContactBegin;
-
- std::function<bool(PhysicsContact& contact, PhysicsContactPreSolve& solve)> onContactPreSolve;
-
- std::function<void(PhysicsContact& contact, const PhysicsContactPostSolve& solve)> onContactPostSolve;
-
- std::function<void(PhysicsContact& contact)> onContactSeparate;
-
- protected:
- bool init();
- void onEvent(EventCustom* event);
-
- protected:
- EventListenerPhysicsContact();
- virtual ~EventListenerPhysicsContact();
-
- friend class PhysicsWorld;
- };
- class CC_DLL EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
- {
- public:
-
- static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB);
-
- virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
- virtual EventListenerPhysicsContactWithBodies* clone() override;
-
- protected:
- PhysicsBody* _a;
- PhysicsBody* _b;
-
- protected:
- EventListenerPhysicsContactWithBodies();
- virtual ~EventListenerPhysicsContactWithBodies();
- };
- class CC_DLL EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
- {
- public:
-
- static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
-
- virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
- virtual EventListenerPhysicsContactWithShapes* clone() override;
-
- protected:
- PhysicsShape* _a;
- PhysicsShape* _b;
-
- protected:
- EventListenerPhysicsContactWithShapes();
- virtual ~EventListenerPhysicsContactWithShapes();
- };
- class CC_DLL EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
- {
- public:
-
- static EventListenerPhysicsContactWithGroup* create(int group);
-
- virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
- virtual EventListenerPhysicsContactWithGroup* clone() override;
-
- protected:
- int _group;
-
- protected:
- EventListenerPhysicsContactWithGroup();
- virtual ~EventListenerPhysicsContactWithGroup();
- };
- NS_CC_END
- #endif
- #endif
|