2013-09-09 10:29:02 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013 Chukong Technologies Inc.
|
2013-09-09 10:29:02 +08: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_JOINT_H__
|
|
|
|
#define __CCPHYSICS_JOINT_H__
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccConfig.h"
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-11-06 14:51:56 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "math/CCGeometry.h"
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class Node;
|
2013-09-09 10:29:02 +08:00
|
|
|
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-12-13 17:36:58 +08:00
|
|
|
/** Enable/Disable the joint */
|
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-12-13 17:36:58 +08:00
|
|
|
/** Enable/disable the collision between two bodies */
|
2013-10-18 15:34:13 +08:00
|
|
|
void setCollisionEnable(bool enable);
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Remove the joint from the world */
|
2013-11-07 14:17:57 +08:00
|
|
|
void removeFormWorld();
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Distory the joint*/
|
2013-11-07 14:17:57 +08:00
|
|
|
static void destroy(PhysicsJoint* joint);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Set the max force between two bodies */
|
2013-12-02 18:29:04 +08:00
|
|
|
void setMaxForce(float force);
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Get the max force setting */
|
2013-12-02 18:29:04 +08:00
|
|
|
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:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointFixed* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& 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:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2);
|
|
|
|
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 getAnchr1() const;
|
|
|
|
void setAnchr1(const Vec2& anchr1);
|
|
|
|
Vec2 getAnchr2() const;
|
|
|
|
void setAnchr2(const Vec2& 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:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max);
|
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:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointPin* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
protected:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& 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-12-13 17:36:58 +08:00
|
|
|
/** Set the fixed distance with two bodies */
|
2013-10-29 17:31:35 +08:00
|
|
|
class PhysicsJointDistance : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& 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:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2);
|
2013-10-29 17:31:35 +08:00
|
|
|
|
|
|
|
protected:
|
2013-12-02 18:29:04 +08:00
|
|
|
PhysicsJointDistance() {}
|
|
|
|
virtual ~PhysicsJointDistance() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Connecting two physics bodies together with a spring. */
|
2013-12-02 18:29:04 +08:00
|
|
|
class PhysicsJointSpring : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointSpring* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping);
|
|
|
|
Vec2 getAnchr1() const;
|
|
|
|
void setAnchr1(const Vec2& anchr1);
|
|
|
|
Vec2 getAnchr2() const;
|
|
|
|
void setAnchr2(const Vec2& anchr2);
|
2013-12-02 18:29:04 +08:00
|
|
|
float getRestLength() const;
|
|
|
|
void setRestLength(float restLength);
|
|
|
|
float getStiffness() const;
|
|
|
|
void setStiffness(float stiffness);
|
|
|
|
float getDamping() const;
|
|
|
|
void setDamping(float damping);
|
|
|
|
|
|
|
|
protected:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointSpring() {}
|
|
|
|
virtual ~PhysicsJointSpring() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Attach body a to a line, and attach body b to a dot */
|
2013-12-02 18:29:04 +08:00
|
|
|
class PhysicsJointGroove : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PhysicsJointGroove* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 getGrooveA() const;
|
|
|
|
void setGrooveA(const Vec2& grooveA);
|
|
|
|
Vec2 getGrooveB() const;
|
|
|
|
void setGrooveB(const Vec2& grooveB);
|
|
|
|
Vec2 getAnchr2() const;
|
|
|
|
void setAnchr2(const Vec2& anchr2);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
protected:
|
2014-05-15 01:07:09 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointGroove() {}
|
|
|
|
virtual ~PhysicsJointGroove() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Likes a spring joint, but works with rotary */
|
2013-12-02 18:29:04 +08:00
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Likes a limit joint, but works with rotary */
|
2013-12-02 18:29:04 +08:00
|
|
|
class PhysicsJointRotaryLimit : public PhysicsJoint
|
|
|
|
{
|
|
|
|
public:
|
2013-12-03 12:47:03 +08:00
|
|
|
static PhysicsJointRotaryLimit* construct(PhysicsBody* a, PhysicsBody* b, float min, float max);
|
2013-12-02 18:29:04 +08:00
|
|
|
static PhysicsJointRotaryLimit* construct(PhysicsBody* a, PhysicsBody* b);
|
|
|
|
|
|
|
|
float getMin() const;
|
|
|
|
void setMin(float min);
|
|
|
|
float getMax() const;
|
|
|
|
void setMax(float max);
|
|
|
|
|
|
|
|
protected:
|
2013-12-03 12:47:03 +08:00
|
|
|
bool init(PhysicsBody* a, PhysicsBody* b, float min, float max);
|
2013-12-02 18:29:04 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsJointRotaryLimit() {}
|
|
|
|
virtual ~PhysicsJointRotaryLimit() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Works like a socket wrench. */
|
2013-12-02 18:29:04 +08:00
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Keeps the angular velocity ratio of a pair of bodies constant. */
|
2013-12-02 18:29:04 +08:00
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
/** Keeps the relative angular velocity of a pair of bodies constant */
|
2013-12-02 18:29:04 +08:00
|
|
|
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__
|