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_BODY_H__
|
|
|
|
#define __CCPHYSICS_BODY_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-10-28 11:08:41 +08:00
|
|
|
#include "CCArray.h"
|
2013-10-09 17:53:12 +08:00
|
|
|
|
|
|
|
#include "CCPhysicsShape.h"
|
|
|
|
|
2013-09-09 16:36:19 +08:00
|
|
|
#include <vector>
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2013-09-09 16:36:19 +08:00
|
|
|
class Sprite;
|
2013-09-09 10:29:02 +08:00
|
|
|
class PhysicsWorld;
|
2013-09-09 16:36:19 +08:00
|
|
|
class PhysicsJoint;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
class PhysicsBodyInfo;
|
2013-10-09 17:53:12 +08:00
|
|
|
|
|
|
|
|
2013-11-01 14:50:06 +08:00
|
|
|
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* A body affect by physics.
|
|
|
|
* it can attach one or more shapes.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
class PhysicsBody : public Object//, public Clonable
|
2013-09-09 10:29:02 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-10-10 18:31:43 +08:00
|
|
|
static PhysicsBody* create();
|
2013-10-28 11:08:41 +08:00
|
|
|
static PhysicsBody* create(float mass);
|
|
|
|
static PhysicsBody* create(float mass, float moment);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a circle shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a box shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a polygon shape.
|
|
|
|
* points is an array of Point structs defining a convex hull with a clockwise winding.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgeSegment shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgeBox shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgePolygon shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgeChain shape.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
virtual PhysicsShape* addShape(PhysicsShape* shape);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a immediate force to body.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual void applyForce(const Vect& force);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a immediate force to body.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual void applyForce(const Vect& force, const Point& offset);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a continuous force to body.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual void applyImpulse(const Vect& impulse);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a continuous force to body.
|
|
|
|
*/
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual void applyImpulse(const Vect& impulse, const Point& offset);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a torque force to body.
|
|
|
|
*/
|
2013-09-09 10:29:02 +08:00
|
|
|
virtual void applyTorque(float torque);
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual void setVelocity(const Vect& velocity);
|
2013-09-30 20:43:19 +08:00
|
|
|
virtual Point getVelocity();
|
2013-10-18 15:34:13 +08:00
|
|
|
virtual void setAngularVelocity(float velocity);
|
2013-11-05 20:02:58 +08:00
|
|
|
virtual Point getVelocityAtLocalPoint(const Point& point);
|
|
|
|
virtual Point getVelocityAtWorldPoint(const Point& point);
|
2013-10-18 15:34:13 +08:00
|
|
|
virtual float getAngularVelocity();
|
|
|
|
virtual void setVelocityLimit(float limit);
|
|
|
|
virtual float getVelocityLimit();
|
|
|
|
virtual void setAngularVelocityLimit(float limit);
|
|
|
|
virtual float getAngularVelocityLimit();
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the body shapes.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline Array* getShapes() const { return _shapes; }
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the first body shapes.
|
|
|
|
*/
|
2013-11-06 15:43:29 +08:00
|
|
|
inline PhysicsShape* getFirstShape() const { return _shapes->count() >= 1 ? dynamic_cast<PhysicsShape*>(_shapes->getObjectAtIndex(0)) : nullptr; }
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShape* getShape(int tag) const;
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief remove a shape from body
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
void removeShape(PhysicsShape* shape);
|
2013-11-05 20:02:58 +08:00
|
|
|
void removeShape(int tag);
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief remove all shapes
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
void removeAllShapes();
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
void removeFromWorld();
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the world body added to.
|
|
|
|
*/
|
2013-09-10 17:36:49 +08:00
|
|
|
inline PhysicsWorld* getWorld() const { return _world; }
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get all joints the body have
|
|
|
|
*/
|
2013-10-25 10:31:22 +08:00
|
|
|
inline const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
|
2013-09-09 16:36:19 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the sprite the body set to.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline Node* getNode() const { return _node; }
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void setCategoryBitmask(int bitmask);
|
|
|
|
void setContactTestBitmask(int bitmask);
|
|
|
|
void setCollisionBitmask(int bitmask);
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
|
|
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
|
|
|
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void setGroup(int group);
|
2013-11-01 16:26:03 +08:00
|
|
|
inline int getGroup() const { return _group; }
|
2013-10-29 17:31:35 +08:00
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the body position.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
Point getPosition() const;
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the body rotation.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
float getRotation() const;
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief test the body is dynamic or not.
|
|
|
|
* a dynamic body will effect with gravity.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline bool isDynamic() const { return _dynamic; }
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief set dynamic to body.
|
|
|
|
* a dynamic body will effect with gravity.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
void setDynamic(bool dynamic);
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief set the body mass.
|
2013-10-18 15:34:13 +08:00
|
|
|
* @note if you need add/subtract mass to body, don't use setMass(getMass() +/- mass), because the mass of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMass() instead.
|
2013-09-17 16:27:43 +08:00
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
void setMass(float mass);
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the body mass.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline float getMass() const { return _mass; }
|
2013-10-18 15:34:13 +08:00
|
|
|
/*
|
|
|
|
* @brief add mass to body.
|
|
|
|
* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
|
|
|
|
* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
|
|
|
|
* if mass == -PHYSICS_INFINITY, _mass will not change.
|
|
|
|
* if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
|
|
|
|
* other wise, mass = mass + _mass;
|
|
|
|
*/
|
|
|
|
void addMass(float mass);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
/*
|
|
|
|
* @brief set the body moment of inertia.
|
|
|
|
* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
|
|
|
|
*/
|
|
|
|
void setMoment(float moment);
|
|
|
|
/*
|
|
|
|
* @brief get the body moment of inertia.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline float getMoment(float moment) const { return _moment; }
|
2013-10-18 15:34:13 +08:00
|
|
|
/*
|
|
|
|
* @brief add moment of inertia to body.
|
|
|
|
* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
|
|
|
|
* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
|
|
|
|
* if moment == -PHYSICS_INFINITY, _moment will not change.
|
|
|
|
* if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
|
|
|
|
* other wise, moment = moment + _moment;
|
|
|
|
*/
|
|
|
|
void addMoment(float moment);
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief set angular damping.
|
|
|
|
*/
|
2013-10-18 15:34:13 +08:00
|
|
|
//void setAngularDamping(float angularDamping);
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get angular damping.
|
|
|
|
*/
|
2013-11-01 16:26:03 +08:00
|
|
|
inline float getLinearDamping() const { return _linearDamping; }
|
2013-10-18 15:34:13 +08:00
|
|
|
inline void setLinearDamping(float damping) { _linearDamping = damping; }
|
2013-11-01 16:26:03 +08:00
|
|
|
inline float getAngularDamping() const { return _angularDamping; }
|
2013-10-18 15:34:13 +08:00
|
|
|
inline void setAngularDamping(float damping) { _angularDamping = damping; }
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
//virtual Clonable* clone() const override;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
bool isResting() const;
|
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 isRotationEnabled() const { return _rotationEnable; }
|
2013-10-18 15:34:13 +08:00
|
|
|
void setRotationEnable(bool enable);
|
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
inline bool isGravityEnabled() const { return _gravityEnable; }
|
2013-10-18 15:34:13 +08:00
|
|
|
void setGravityEnable(bool enable);
|
|
|
|
|
|
|
|
|
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-10-29 17:31:35 +08:00
|
|
|
Point world2Local(const Point& point);
|
|
|
|
Point local2World(const Point& point);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
protected:
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
bool init();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
virtual void setPosition(Point position);
|
|
|
|
virtual void setRotation(float rotation);
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
virtual void update(float delta) override;
|
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
void removeJoint(PhysicsJoint* joint);
|
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
protected:
|
|
|
|
PhysicsBody();
|
|
|
|
virtual ~PhysicsBody();
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
protected:
|
2013-11-01 16:26:03 +08:00
|
|
|
Node* _node;
|
2013-09-16 21:22:22 +08:00
|
|
|
std::vector<PhysicsJoint*> _joints;
|
2013-10-28 11:08:41 +08:00
|
|
|
Array* _shapes;
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsWorld* _world;
|
|
|
|
PhysicsBodyInfo* _info;
|
|
|
|
bool _dynamic;
|
2013-09-29 09:39:20 +08:00
|
|
|
bool _enable;
|
2013-10-18 15:34:13 +08:00
|
|
|
bool _rotationEnable;
|
|
|
|
bool _gravityEnable;
|
2013-09-16 21:22:22 +08:00
|
|
|
bool _massDefault;
|
2013-10-18 15:34:13 +08:00
|
|
|
bool _momentDefault;
|
2013-09-16 21:22:22 +08:00
|
|
|
float _mass;
|
|
|
|
float _area;
|
|
|
|
float _density;
|
2013-10-18 15:34:13 +08:00
|
|
|
float _moment;
|
|
|
|
float _linearDamping;
|
2013-09-16 21:22:22 +08:00
|
|
|
float _angularDamping;
|
2013-09-29 09:39:20 +08:00
|
|
|
int _tag;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
int _categoryBitmask;
|
|
|
|
int _collisionBitmask;
|
|
|
|
int _contactTestBitmask;
|
|
|
|
int _group;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
friend class PhysicsWorld;
|
|
|
|
friend class PhysicsShape;
|
|
|
|
friend class PhysicsJoint;
|
2013-09-30 13:02:17 +08:00
|
|
|
friend class Node;
|
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_BODY_H__
|