2013-09-16 21:22:22 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCPhysicsSetting.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
|
|
|
#ifndef __CCPHYSICS_SHAPE_H__
|
|
|
|
#define __CCPHYSICS_SHAPE_H__
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class PhysicsShapeInfo;
|
|
|
|
class PhysicsBody;
|
|
|
|
class PhysicsBodyInfo;
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
|
|
|
|
typedef struct PhysicsMaterial
|
|
|
|
{
|
|
|
|
float density;
|
|
|
|
float elasticity;
|
|
|
|
float friction;
|
|
|
|
|
|
|
|
PhysicsMaterial()
|
|
|
|
: density(0.0f)
|
|
|
|
, elasticity(0.0f)
|
|
|
|
, friction(0.0f){}
|
|
|
|
|
|
|
|
PhysicsMaterial(float density, float elasticity, float friction)
|
|
|
|
: density(density)
|
|
|
|
, elasticity(elasticity)
|
|
|
|
, friction(friction){}
|
|
|
|
}PhysicsMaterial;
|
|
|
|
|
2013-10-11 21:52:28 +08:00
|
|
|
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT(0.0f, 1.0f, 1.0f);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-17 16:27:43 +08:00
|
|
|
* @brief A shape for body. You do not create PhysicsWorld objects directly, instead, you can view PhysicsBody to see how to create it.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShape : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
UNKNOWN,
|
|
|
|
CIRCLE,
|
|
|
|
BOX,
|
|
|
|
POLYGEN,
|
|
|
|
EDGESEGMENT,
|
|
|
|
EDGEBOX,
|
|
|
|
EDGEPOLYGEN,
|
|
|
|
EDGECHAIN,
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline PhysicsBody* getBody(){ return _body; }
|
|
|
|
inline Type getType() { return _type; }
|
2013-09-30 20:43:19 +08:00
|
|
|
inline float getArea() { return _area; }
|
|
|
|
inline float getAngularDumping() { return _angularDamping; }
|
2013-09-30 21:48:25 +08:00
|
|
|
void setAngularDumping(float angularDumping);
|
2013-09-29 09:39:20 +08:00
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
|
|
|
inline int getTag() { return _tag; }
|
|
|
|
void setEnable(bool enable);
|
|
|
|
inline bool isEnable() { return _enable; }
|
2013-09-30 20:43:19 +08:00
|
|
|
void addToBody();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
inline float getMass() { return _mass; }
|
|
|
|
void setMass(float mass);
|
|
|
|
inline float getDensity() { return _material.density; }
|
|
|
|
void setDensity(float density);
|
2013-10-09 13:41:19 +08:00
|
|
|
void setElasticity(float elasticity);
|
|
|
|
void setFriction(float friction);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Type type, PhysicsMaterial material);
|
|
|
|
void initEnd();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-17 16:27:43 +08:00
|
|
|
* @brief 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-09-16 21:22:22 +08:00
|
|
|
*/
|
|
|
|
PhysicsBodyInfo* bodyInfo() const;
|
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
inline void setBody(PhysicsBody* body) { _body = body; }
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
|
|
|
PhysicsShape();
|
2013-09-17 16:27:43 +08:00
|
|
|
virtual ~PhysicsShape() = 0;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsBody* _body;
|
|
|
|
PhysicsShapeInfo* _info;
|
|
|
|
Type _type;
|
2013-09-30 20:43:19 +08:00
|
|
|
float _area;
|
|
|
|
float _mass;
|
|
|
|
float _angularDamping;
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsMaterial _material;
|
2013-09-29 09:39:20 +08:00
|
|
|
int _tag;
|
|
|
|
bool _enable;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
friend class PhysicsWorld;
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** A circle shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeCircle : public PhysicsShape
|
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeCircle* create(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeCircle();
|
|
|
|
virtual ~PhysicsShapeCircle();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** A box shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeBox : public PhysicsShape
|
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeBox();
|
|
|
|
virtual ~PhysicsShapeBox();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** A polygon shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapePolygon : public PhysicsShape
|
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapePolygon();
|
|
|
|
virtual ~PhysicsShapePolygon();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** A segment shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeEdgeSegment : public PhysicsShape
|
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeEdgeSegment* create(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeSegment();
|
|
|
|
virtual ~PhysicsShapeEdgeSegment();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** An edge box shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeEdgeBox : public PhysicsShape
|
|
|
|
{
|
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeEdgeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, Point offset = Point(0, 0));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, Point offset = Point(0, 0));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeBox();
|
|
|
|
virtual ~PhysicsShapeEdgeBox();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** An edge polygon shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeEdgePolygon : public PhysicsShape
|
|
|
|
{
|
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeEdgePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgePolygon();
|
|
|
|
virtual ~PhysicsShapeEdgePolygon();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/** a chain shape */
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsShapeEdgeChain : public PhysicsShape
|
|
|
|
{
|
|
|
|
public:
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsShapeEdgeChain* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-10-09 17:53:12 +08:00
|
|
|
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeChain();
|
|
|
|
virtual ~PhysicsShapeEdgeChain();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
#endif // __CCPHYSICS_FIXTURE_H__
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|