2013-09-09 10:29:02 +08: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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCPHYSICS_JOINT_H__
|
|
|
|
#define __CCPHYSICS_JOINT_H__
|
|
|
|
|
2013-11-06 14:51:56 +08:00
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.h"
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class PhysicsBody;
|
2013-11-07 14:17:57 +08:00
|
|
|
class PhysicsWorld;
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsJointInfo;
|
|
|
|
class PhysicsBodyInfo;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief An PhysicsJoint object connects two physics bodies together.
|
|
|
|
*/
|
2013-10-28 11:08:41 +08:00
|
|
|
class PhysicsJoint
|
2013-09-09 10:29:02 +08:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
PhysicsJoint();
|
2013-09-16 21:22:22 +08:00
|
|
|
virtual ~PhysicsJoint() = 0;
|
|
|
|
|
|
|
|
public:
|
2013-11-07 14:17:57 +08:00
|
|
|
inline PhysicsBody* getBodyA() const { return _bodyA; }
|
|
|
|
inline PhysicsBody* getBodyB() const { return _bodyB; }
|
|
|
|
inline PhysicsWorld* getWorld() const { return _world; }
|
2013-11-01 16:26:03 +08:00
|
|
|
inline int getTag() const { return _tag; }
|
2013-09-29 09:39:20 +08:00
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
2013-11-06 15:43:29 +08:00
|
|
|
inline bool isEnabled() const { return _enable; }
|
2013-09-29 09:39:20 +08:00
|
|
|
void setEnable(bool enable);
|
2013-11-06 15:43:29 +08:00
|
|
|
inline bool isCollisionEnabled() const { return _collisionEnable; }
|
2013-10-18 15:34:13 +08:00
|
|
|
void setCollisionEnable(bool enable);
|
2013-11-07 14:17:57 +08:00
|
|
|
void removeFormWorld();
|
|
|
|
static void destroy(PhysicsJoint* joint);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
void setMaxForce(float force);
|
|
|
|
float getMaxForce() const;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
PhysicsBodyInfo* getBodyInfo(PhysicsBody* body) const;
|
|
|
|
Node* getBodyNode(PhysicsBody* body) const;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-09-09 10:29:02 +08:00
|
|
|
PhysicsBody* _bodyA;
|
|
|
|
PhysicsBody* _bodyB;
|
2013-11-07 14:17:57 +08:00
|
|
|
PhysicsWorld* _world;
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsJointInfo* _info;
|
2013-09-29 09:39:20 +08:00
|
|
|
bool _enable;
|
2013-10-18 15:34:13 +08:00
|
|
|
bool _collisionEnable;
|
2013-11-07 14:17:57 +08:00
|
|
|
bool _destoryMark;
|
2013-09-29 09:39:20 +08:00
|
|
|
int _tag;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
friend class PhysicsBody;
|
2013-09-29 09:39:20 +08:00
|
|
|
friend class PhysicsWorld;
|
2013-11-08 14:25:03 +08:00
|
|
|
friend class PhysicsDebugDraw;
|
2013-09-09 10:29:02 +08:00
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief A fixed joint fuses the two bodies together at a reference point. Fixed joints are useful for creating complex shapes that can be broken apart later.
|
|
|
|
*/
|
2013-09-09 10:29:02 +08:00
|
|
|
class PhysicsJointFixed : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2013-11-07 17:46:05 +08:00
|
|
|
static PhysicsJointFixed* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-09-16 21:22:22 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsJointFixed() {}
|
|
|
|
virtual ~PhysicsJointFixed() {}
|
2013-09-09 10:29:02 +08:00
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope.
|
|
|
|
*/
|
2013-09-09 10:29:02 +08:00
|
|
|
class PhysicsJointLimit : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2013-11-18 02:16:20 +08:00
|
|
|
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
Point getAnchr1() const;
|
|
|
|
void setAnchr1(const Point& anchr1);
|
|
|
|
Point getAnchr2() const;
|
|
|
|
void setAnchr2(const Point& anchr2);
|
2013-11-01 16:26:03 +08:00
|
|
|
float getMin() const;
|
2013-10-18 15:34:13 +08:00
|
|
|
void setMin(float min);
|
2013-11-01 16:26:03 +08:00
|
|
|
float getMax() const;
|
2013-10-18 15:34:13 +08:00
|
|
|
void setMax(float max);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-18 15:34:13 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsJointLimit() {}
|
|
|
|
virtual ~PhysicsJointLimit() {}
|
2013-09-09 10:29:02 +08:00
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together.
|
|
|
|
*/
|
2013-09-09 10:29:02 +08:00
|
|
|
class PhysicsJointPin : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2013-11-07 17:46:05 +08:00
|
|
|
static PhysicsJointPin* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-18 15:34:13 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsJointPin() {}
|
|
|
|
virtual ~PhysicsJointPin() {}
|
2013-09-09 10:29:02 +08:00
|
|
|
};
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
class PhysicsJointDistance : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2013-11-07 17:46:05 +08:00
|
|
|
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
2013-10-29 17:31:35 +08:00
|
|
|
|
2013-12-02 18:29:04 +08:00
|
|
|
float getDistance() const;
|
|
|
|
void setDistance(float distance);
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
|
|
|
|
|
|
|
protected:
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsJointDistance() {}
|
|
|
|
virtual ~PhysicsJointDistance() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointSpring : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointSpring* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float stiffness, float damping);
|
|
|
|
Point getAnchr1() const;
|
|
|
|
void setAnchr1(const Point& anchr1);
|
|
|
|
Point getAnchr2() const;
|
|
|
|
void setAnchr2(const Point& anchr2);
|
|
|
|
float getRestLength() const;
|
|
|
|
void setRestLength(float restLength);
|
|
|
|
float getStiffness() const;
|
|
|
|
void setStiffness(float stiffness);
|
|
|
|
float getDamping() const;
|
|
|
|
void setDamping(float damping);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2, float stiffness, float damping);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointSpring() {}
|
|
|
|
virtual ~PhysicsJointSpring() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointGroove : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointGroove* construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr2);
|
|
|
|
|
|
|
|
Point getGrooveA() const;
|
|
|
|
void setGrooveA(const Point& grooveA);
|
|
|
|
Point getGrooveB() const;
|
|
|
|
void setGrooveB(const Point& grooveB);
|
|
|
|
Point getAnchr2() const;
|
|
|
|
void setAnchr2(const Point& anchr2);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointGroove() {}
|
|
|
|
virtual ~PhysicsJointGroove() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointRotarySpring : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointRotarySpring* construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping);
|
|
|
|
|
|
|
|
float getRestAngle() const;
|
|
|
|
void setRestAngle(float restAngle);
|
|
|
|
float getStiffness() const;
|
|
|
|
void setStiffness(float stiffness);
|
|
|
|
float getDamping() const;
|
|
|
|
void setDamping(float damping);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointRotarySpring() {}
|
|
|
|
virtual ~PhysicsJointRotarySpring() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointRotaryLimit : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointRotaryLimit* construct(PhysicsBody* a, PhysicsBody* b);
|
|
|
|
|
|
|
|
float getMin() const;
|
|
|
|
void setMin(float min);
|
|
|
|
float getMax() const;
|
|
|
|
void setMax(float max);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointRotaryLimit() {}
|
|
|
|
virtual ~PhysicsJointRotaryLimit() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointRatchet : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointRatchet* construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet);
|
|
|
|
|
|
|
|
float getAngle() const;
|
|
|
|
void setAngle(float angle);
|
|
|
|
float getPhase() const;
|
|
|
|
void setPhase(float phase);
|
|
|
|
float getRatchet() const;
|
|
|
|
void setRatchet(float ratchet);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointRatchet() {}
|
|
|
|
virtual ~PhysicsJointRatchet() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointGear : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointGear* construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio);
|
|
|
|
|
|
|
|
float getPhase() const;
|
|
|
|
void setPhase(float phase);
|
|
|
|
float getRatio() const;
|
|
|
|
void setRatio(float ratchet);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, float phase, float ratio);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointGear() {}
|
|
|
|
virtual ~PhysicsJointGear() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PhysicsJointMotor : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PhysicsJointMotor* construct(PhysicsBody* a, PhysicsBody* b, float rate);
|
|
|
|
|
|
|
|
float getRate() const;
|
|
|
|
void setRate(float rate);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, float rate);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointMotor() {}
|
|
|
|
virtual ~PhysicsJointMotor() {}
|
2013-10-29 17:31:35 +08:00
|
|
|
};
|
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
#endif // CC_USE_PHYSICS
|
2013-11-06 14:51:56 +08:00
|
|
|
#endif // __CCPHYSICS_JOINT_H__
|