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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCPHYSICS_SHAPE_H__
|
|
|
|
#define __CCPHYSICS_SHAPE_H__
|
|
|
|
|
2013-11-06 14:51:56 +08:00
|
|
|
#include "CCPhysicsSetting.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
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;
|
2013-10-18 15:34:13 +08:00
|
|
|
float restitution;
|
2013-10-09 17:53:12 +08:00
|
|
|
float friction;
|
|
|
|
|
2013-10-23 18:52:09 +08:00
|
|
|
PhysicsMaterial()
|
|
|
|
: density(0.0f)
|
|
|
|
, restitution(0.0f)
|
|
|
|
, friction(0.0f)
|
|
|
|
{}
|
|
|
|
|
2013-11-05 08:31:36 +08:00
|
|
|
PhysicsMaterial(float aDensity, float aRestitution, float aFriction)
|
|
|
|
: density(aDensity)
|
|
|
|
, restitution(aRestitution)
|
|
|
|
, friction(aFriction)
|
2013-10-23 18:52:09 +08:00
|
|
|
{}
|
2013-10-09 17:53:12 +08:00
|
|
|
}PhysicsMaterial;
|
|
|
|
|
2013-10-28 16:17:19 +08:00
|
|
|
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT(0.0f, 0.5f, 0.5f);
|
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:
|
2013-10-18 15:34:13 +08:00
|
|
|
inline PhysicsBody* getBody() const { return _body; }
|
|
|
|
inline Type getType() const { return _type; }
|
|
|
|
inline float getArea() const { return _area; }
|
|
|
|
inline float getMoment() const { return _moment; }
|
|
|
|
void setMoment(float moment);
|
2013-09-29 09:39:20 +08:00
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
2013-10-18 15:34:13 +08:00
|
|
|
inline int getTag() const { return _tag; }
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
inline float getMass() const { return _mass; }
|
2013-10-09 17:53:12 +08:00
|
|
|
void setMass(float mass);
|
2013-10-18 15:34:13 +08:00
|
|
|
inline float getDensity() const { return _material.density; }
|
2013-10-09 17:53:12 +08:00
|
|
|
void setDensity(float density);
|
2013-10-18 15:34:13 +08:00
|
|
|
void setRestitution(float restitution);
|
2013-10-09 13:41:19 +08:00
|
|
|
void setFriction(float friction);
|
2013-11-05 20:02:58 +08:00
|
|
|
void setMaterial(const PhysicsMaterial& material);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
|
|
|
virtual float calculateDefaultMoment() { return 0; }
|
|
|
|
virtual float calculateDefaultArea() { return 0; }
|
|
|
|
virtual Point getOffset() { return Point::ZERO; }
|
|
|
|
virtual Point getCenter() { return getOffset(); }
|
2013-11-05 20:02:58 +08:00
|
|
|
bool containsPoint(const Point& point) const;
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
static Point* recenterPoints(Point* points, int count, const Point& center = Point::ZERO);
|
|
|
|
static Point getPolyonCenter(const Point* points, int count);
|
2013-10-09 13:41:19 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
|
|
|
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
|
|
|
inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
|
|
|
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
|
|
|
inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
|
|
|
|
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
|
|
|
|
|
|
|
void setGroup(int group);
|
|
|
|
inline int getGroup() { return _group; }
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-10-18 15:34:13 +08:00
|
|
|
bool init(Type type);
|
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-18 15:34:13 +08:00
|
|
|
void setBody(PhysicsBody* body);
|
2013-10-09 13:41:19 +08:00
|
|
|
|
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;
|
2013-10-18 15:34:13 +08:00
|
|
|
float _moment;
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsMaterial _material;
|
2013-09-29 09:39:20 +08:00
|
|
|
int _tag;
|
2013-10-29 17:31:35 +08:00
|
|
|
int _categoryBitmask;
|
|
|
|
int _collisionBitmask;
|
|
|
|
int _contactTestBitmask;
|
|
|
|
int _group;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
friend class PhysicsWorld;
|
|
|
|
friend class PhysicsBody;
|
2013-10-18 15:34:13 +08:00
|
|
|
friend class PhysicsJoint;
|
2013-09-16 21:22:22 +08:00
|
|
|
};
|
|
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeCircle* create(float radius, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point(0, 0));
|
2013-10-18 15:34:13 +08:00
|
|
|
static float calculateArea(float radius);
|
2013-11-05 20:02:58 +08:00
|
|
|
static float calculateMoment(float mass, float radius, const Point& offset = Point::ZERO);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual float calculateDefaultArea() override;
|
|
|
|
virtual float calculateDefaultMoment() override;
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float getRadius() const;
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getOffset() override;
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(float radius, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeCircle();
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual ~PhysicsShapeCircle();
|
2013-09-16 21:22:22 +08:00
|
|
|
};
|
|
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeBox* create(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
|
|
|
static float calculateArea(const Size& size);
|
|
|
|
static float calculateMoment(float mass, const Size& size, const Point& offset = Point::ZERO);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual float calculateDefaultArea() override;
|
|
|
|
virtual float calculateDefaultMoment() override;
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
void getPoints(Point* outPoints) const;
|
2013-11-01 16:26:03 +08:00
|
|
|
Size getSize() const;
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getOffset() override { return _offset; }
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeBox();
|
|
|
|
virtual ~PhysicsShapeBox();
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
protected:
|
|
|
|
Point _offset;
|
2013-09-16 21:22:22 +08:00
|
|
|
};
|
|
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapePolygon* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
|
|
|
static float calculateArea(const Point* points, int count);
|
|
|
|
static float calculateMoment(float mass, const Point* points, int count, const Point& offset = Point::ZERO);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
float calculateDefaultArea() override;
|
|
|
|
float calculateDefaultMoment() override;
|
|
|
|
|
2013-11-05 15:54:33 +08:00
|
|
|
Point getPoint(int i) const;
|
2013-11-06 15:43:29 +08:00
|
|
|
void getPoints(Point* outPoints) const;
|
2013-11-05 08:31:36 +08:00
|
|
|
long getPointsCount() const;
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getCenter() override;
|
2013-09-16 21:22:22 +08:00
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapePolygon();
|
|
|
|
virtual ~PhysicsShapePolygon();
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
protected:
|
|
|
|
Point _center;
|
2013-09-16 21:22:22 +08:00
|
|
|
};
|
|
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeEdgeSegment* create(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Point getPointA() const;
|
|
|
|
Point getPointB() const;
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getCenter() override;
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeSegment();
|
|
|
|
virtual ~PhysicsShapeEdgeSegment();
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
protected:
|
|
|
|
Point _center;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeEdgeBox* create(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, const Point& offset = Point::ZERO);
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getOffset() override { return _offset; }
|
|
|
|
void getPoints(const Point* outPoints) const;
|
2013-11-05 08:31:36 +08:00
|
|
|
long getPointsCount() const;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeBox();
|
|
|
|
virtual ~PhysicsShapeEdgeBox();
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
protected:
|
|
|
|
Point _offset;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeEdgePolygon* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getCenter() override;
|
|
|
|
void getPoints(Point* outPoints) const;
|
2013-11-05 08:31:36 +08:00
|
|
|
long getPointsCount() const;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgePolygon();
|
|
|
|
virtual ~PhysicsShapeEdgePolygon();
|
|
|
|
|
|
|
|
friend class PhysicsBody;
|
2013-10-18 15:34:13 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Point _center;
|
2013-09-16 21:22:22 +08:00
|
|
|
};
|
|
|
|
|
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-11-05 20:02:58 +08:00
|
|
|
static PhysicsShapeEdgeChain* create(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-11-06 15:43:29 +08:00
|
|
|
virtual Point getCenter() override;
|
|
|
|
void getPoints(Point* outPoints) const;
|
2013-11-05 08:31:36 +08:00
|
|
|
long getPointsCount() const;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
2013-11-05 20:02:58 +08:00
|
|
|
bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
PhysicsShapeEdgeChain();
|
|
|
|
virtual ~PhysicsShapeEdgeChain();
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
protected:
|
|
|
|
Point _center;
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
friend class PhysicsBody;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|
2013-11-06 14:51:56 +08:00
|
|
|
#endif // __CCPHYSICS_FIXTURE_H__
|