axmol/cocos/physics/CCPhysicsJoint.h

319 lines
9.7 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2013 Chukong Technologies Inc.
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__
#include "2d/ccConfig.h"
#if CC_USE_PHYSICS
#include "base/CCRef.h"
#include "base/CCGeometry.h"
NS_CC_BEGIN
class Node;
class PhysicsBody;
class PhysicsWorld;
class PhysicsJointInfo;
class PhysicsBodyInfo;
2013-09-17 16:27:43 +08:00
/*
* @brief An PhysicsJoint object connects two physics bodies together.
*/
class PhysicsJoint
{
protected:
PhysicsJoint();
virtual ~PhysicsJoint() = 0;
public:
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; }
inline void setTag(int tag) { _tag = tag; }
2013-11-06 15:43:29 +08:00
inline bool isEnabled() const { return _enable; }
/** Enable/Disable the joint */
void setEnable(bool enable);
2013-11-06 15:43:29 +08:00
inline bool isCollisionEnabled() const { return _collisionEnable; }
/** Enable/disable the collision between two bodies */
void setCollisionEnable(bool enable);
/** Remove the joint from the world */
void removeFormWorld();
/** Distory the joint*/
static void destroy(PhysicsJoint* joint);
/** Set the max force between two bodies */
2013-12-02 18:29:04 +08:00
void setMaxForce(float force);
/** Get the max force setting */
2013-12-02 18:29:04 +08:00
float getMaxForce() const;
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;
protected:
PhysicsBody* _bodyA;
PhysicsBody* _bodyB;
PhysicsWorld* _world;
PhysicsJointInfo* _info;
bool _enable;
bool _collisionEnable;
bool _destoryMark;
int _tag;
friend class PhysicsBody;
friend class PhysicsWorld;
friend class PhysicsDebugDraw;
};
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.
*/
class PhysicsJointFixed : public PhysicsJoint
{
public:
2014-04-15 18:08:29 +08:00
static PhysicsJointFixed* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr);
protected:
2014-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr);
protected:
2013-12-02 18:29:04 +08:00
PhysicsJointFixed() {}
virtual ~PhysicsJointFixed() {}
};
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.
*/
class PhysicsJointLimit : public PhysicsJoint
{
public:
2014-04-15 18:08:29 +08:00
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2);
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2, float min, float max);
2014-04-15 18:08:29 +08:00
Vector2 getAnchr1() const;
void setAnchr1(const Vector2& anchr1);
Vector2 getAnchr2() const;
void setAnchr2(const Vector2& anchr2);
2013-11-01 16:26:03 +08:00
float getMin() const;
void setMin(float min);
2013-11-01 16:26:03 +08:00
float getMax() const;
void setMax(float max);
protected:
2014-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2, float min, float max);
protected:
2013-12-02 18:29:04 +08:00
PhysicsJointLimit() {}
virtual ~PhysicsJointLimit() {}
};
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.
*/
class PhysicsJointPin : public PhysicsJoint
{
public:
2014-04-15 18:08:29 +08:00
static PhysicsJointPin* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr);
protected:
2014-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr);
protected:
2013-12-02 18:29:04 +08:00
PhysicsJointPin() {}
virtual ~PhysicsJointPin() {}
};
/** Set the fixed distance with two bodies */
class PhysicsJointDistance : public PhysicsJoint
{
public:
2014-04-15 18:08:29 +08:00
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2);
2013-12-02 18:29:04 +08:00
float getDistance() const;
void setDistance(float distance);
protected:
2014-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2);
protected:
2013-12-02 18:29:04 +08:00
PhysicsJointDistance() {}
virtual ~PhysicsJointDistance() {}
};
/** Connecting two physics bodies together with a spring. */
2013-12-02 18:29:04 +08:00
class PhysicsJointSpring : public PhysicsJoint
{
public:
2014-04-15 18:08:29 +08:00
static PhysicsJointSpring* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2, float stiffness, float damping);
Vector2 getAnchr1() const;
void setAnchr1(const Vector2& anchr1);
Vector2 getAnchr2() const;
void setAnchr2(const Vector2& 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-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& anchr1, const Vector2& anchr2, float stiffness, float damping);
2013-12-02 18:29:04 +08:00
protected:
PhysicsJointSpring() {}
virtual ~PhysicsJointSpring() {}
};
/** 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-04-15 18:08:29 +08:00
static PhysicsJointGroove* construct(PhysicsBody* a, PhysicsBody* b, const Vector2& grooveA, const Vector2& grooveB, const Vector2& anchr2);
2013-12-02 18:29:04 +08:00
2014-04-15 18:08:29 +08:00
Vector2 getGrooveA() const;
void setGrooveA(const Vector2& grooveA);
Vector2 getGrooveB() const;
void setGrooveB(const Vector2& grooveB);
Vector2 getAnchr2() const;
void setAnchr2(const Vector2& anchr2);
2013-12-02 18:29:04 +08:00
protected:
2014-04-15 18:08:29 +08:00
bool init(PhysicsBody* a, PhysicsBody* b, const Vector2& grooveA, const Vector2& grooveB, const Vector2& anchr);
2013-12-02 18:29:04 +08:00
protected:
PhysicsJointGroove() {}
virtual ~PhysicsJointGroove() {}
};
/** 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() {}
};
/** Likes a limit joint, but works with rotary */
2013-12-02 18:29:04 +08:00
class PhysicsJointRotaryLimit : public PhysicsJoint
{
public:
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:
bool init(PhysicsBody* a, PhysicsBody* b, float min, float max);
2013-12-02 18:29:04 +08:00
protected:
PhysicsJointRotaryLimit() {}
virtual ~PhysicsJointRotaryLimit() {}
};
/** 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() {}
};
/** 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() {}
};
/** 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() {}
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_JOINT_H__