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-09-09 16:58:55 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "cocoa/CCGeometry.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
|
|
|
class PhysicsShape;
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
class PhysicsBodyInfo;
|
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-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a circle shape.
|
|
|
|
*/
|
2013-09-30 20:43:19 +08:00
|
|
|
static PhysicsBody* createCircle(float radius, float density = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a box shape.
|
|
|
|
*/
|
2013-09-30 20:43:19 +08:00
|
|
|
static PhysicsBody* createBox(Size size, float density = 1);
|
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-09-30 20:43:19 +08:00
|
|
|
static PhysicsBody* createPolygon(Point* points, int count, float density = 1);
|
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-09-16 21:22:22 +08:00
|
|
|
static PhysicsBody* createEdgeSegment(Point a, Point b, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgeBox shape.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
static PhysicsBody* createEdgeBox(Size size, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgePolygon shape.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
static PhysicsBody* createEdgePolygon(Point* points, int count, float border = 1);
|
2013-09-17 16:27:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Create a body contains a EdgeChain shape.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
static PhysicsBody* createEdgeChain(Point* points, int count, float border = 1);
|
|
|
|
|
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-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
|
|
|
|
|
|
|
void setCategoryBitmask(int bitmask);
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
2013-09-09 10:29:02 +08:00
|
|
|
void setContactTestBitmask(int bitmask);
|
2013-09-09 16:36:19 +08:00
|
|
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
2013-09-09 10:29:02 +08:00
|
|
|
void setCollisionBitmask(int 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-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-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief set angular damping.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
void setAngularDamping(float angularDamping);
|
2013-09-17 16:27:43 +08:00
|
|
|
/*
|
|
|
|
* @brief get angular damping.
|
|
|
|
*/
|
2013-09-16 21:22:22 +08:00
|
|
|
inline float getAngularDamping() { return _angularDamping; }
|
|
|
|
|
|
|
|
//virtual Clonable* clone() const override;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
inline bool isEnable() { return _enable; }
|
|
|
|
void setEnable(bool enable);
|
|
|
|
|
|
|
|
inline int getTag() { return _tag; }
|
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
|
|
|
|
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
|
|
|
|
|
|
|
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-09-16 21:22:22 +08:00
|
|
|
bool _massDefault;
|
|
|
|
bool _angularDampingDefault;
|
|
|
|
float _mass;
|
|
|
|
float _area;
|
|
|
|
float _density;
|
|
|
|
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
|