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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-10 14:15:19 +08:00
|
|
|
#include "CCPhysicsSetting.h"
|
2013-09-10 17:36:49 +08:00
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-09-10 14:15:19 +08:00
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
#ifndef __CCPHYSICS_BODY_H__
|
|
|
|
#define __CCPHYSICS_BODY_H__
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.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-10-18 17:47:29 +08:00
|
|
|
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT = {1.0f, 1.0f, 1.0f};
|
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-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a circle shape.
|
|
|
|
*/
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createCircle(float radius, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a box shape.
|
|
|
|
*/
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
|
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-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createPolygon(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
|
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-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createEdgeSegment(Point a, Point b, 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-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createEdgeBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgePolygon shape.
|
|
|
|
*/
|
2013-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createEdgePolygon(Point* points, int count, 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-10-09 17:53:12 +08:00
|
|
|
static PhysicsBody* createEdgeChain(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
virtual void 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-09-09 10:29:02 +08:00
|
|
|
virtual void applyForce(Point force);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a immediate force to body.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
virtual void applyForce(Point force, Point offset);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a continuous force to body.
|
|
|
|
*/
|
2013-09-09 10:29:02 +08:00
|
|
|
virtual void applyImpulse(Point impulse);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Applies a continuous force to body.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
virtual void applyImpulse(Point impulse, 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-09-30 20:43:19 +08:00
|
|
|
virtual void setVelocity(Point velocity);
|
|
|
|
virtual Point getVelocity();
|
2013-10-18 15:34:13 +08:00
|
|
|
virtual void setAngularVelocity(float velocity);
|
|
|
|
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-09-16 21:22:22 +08:00
|
|
|
inline std::vector<PhysicsShape*>& getShapes() { return _shapes; }
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the first body shapes.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
inline PhysicsShape* getShape() { return _shapes.size() >= 1 ? _shapes.front() : nullptr; }
|
2013-09-29 09:39:20 +08:00
|
|
|
PhysicsShape* getShapeByTag(int tag);
|
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-09-29 09:39:20 +08:00
|
|
|
void removeShapeByTag(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-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-09-09 16:36:19 +08:00
|
|
|
inline const std::vector<PhysicsJoint*>* getJoints() const { return &_joints; }
|
|
|
|
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get the sprite the body set to.
|
|
|
|
*/
|
2013-09-09 16:36:19 +08:00
|
|
|
inline Sprite* getOwner() const { return _owner; }
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
2013-10-18 15:34:13 +08:00
|
|
|
inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
2013-10-18 15:34:13 +08:00
|
|
|
inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
|
|
|
|
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-09-16 21:22:22 +08:00
|
|
|
inline bool isDynamic() { 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-09-16 21:22:22 +08:00
|
|
|
inline float getMass() { 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.
|
|
|
|
*/
|
|
|
|
inline float getMoment(float moment) { return _moment; }
|
|
|
|
/*
|
|
|
|
* @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-10-18 15:34:13 +08:00
|
|
|
inline float getLinearDamping() { return _linearDamping; }
|
|
|
|
inline void setLinearDamping(float damping) { _linearDamping = damping; }
|
2013-09-16 21:22:22 +08:00
|
|
|
inline float getAngularDamping() { 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-10-18 15:34:13 +08:00
|
|
|
bool isResting();
|
2013-09-29 09:39:20 +08:00
|
|
|
inline bool isEnable() { return _enable; }
|
|
|
|
void setEnable(bool enable);
|
|
|
|
|
2013-10-18 15:34:13 +08:00
|
|
|
inline bool isRotationEnable() { return _rotationEnable; }
|
|
|
|
void setRotationEnable(bool enable);
|
|
|
|
|
|
|
|
inline bool isGravityEnable() { return _gravityEnable; }
|
|
|
|
void setGravityEnable(bool enable);
|
|
|
|
|
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
inline int getTag() { return _tag; }
|
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
|
|
|
|
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-09-09 10:29:02 +08:00
|
|
|
protected:
|
|
|
|
PhysicsBody();
|
|
|
|
virtual ~PhysicsBody();
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
protected:
|
2013-09-09 16:36:19 +08:00
|
|
|
Sprite* _owner;
|
2013-09-16 21:22:22 +08:00
|
|
|
std::vector<PhysicsJoint*> _joints;
|
|
|
|
std::vector<PhysicsShape*> _shapes;
|
|
|
|
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
|
|
|
|
|
|
|
int _categoryBitmask;
|
|
|
|
int _contactTestBitmask;
|
|
|
|
int _collisionBitmask;
|
|
|
|
|
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-09 10:40:31 +08:00
|
|
|
#endif // __CCPHYSICS_BODY_H__
|
2013-09-10 14:15:19 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
#endif // CC_USE_PHYSICS
|