2013-09-09 10:29:02 +08:00
|
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
|
Copyright (c) 2013 Chukong Technologies Inc.
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
|
|
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-12-04 14:36:32 +08:00
|
|
|
|
#include "ccConfig.h"
|
2013-12-26 23:55:05 +08:00
|
|
|
|
#if CC_USE_PHYSICS
|
2013-11-06 14:51:56 +08:00
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
|
#include "CCRef.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
|
#include "CCGeometry.h"
|
2013-10-09 17:53:12 +08:00
|
|
|
|
#include "CCPhysicsShape.h"
|
2013-12-07 10:48:02 +08:00
|
|
|
|
#include "CCVector.h"
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2014-02-20 10:53:49 +08:00
|
|
|
|
|
|
|
|
|
class Node;
|
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-10 17:36:49 +08:00
|
|
|
|
class PhysicsBodyInfo;
|
2013-10-09 17:53:12 +08:00
|
|
|
|
|
2013-11-26 17:29:09 +08:00
|
|
|
|
typedef Point Vect;
|
|
|
|
|
|
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-11-12 15:28:07 +08:00
|
|
|
|
* if you create body with createXXX, it will automatically compute mass and moment with density your specified(which is PHYSICSBODY_MATERIAL_DEFAULT by default, and the density value is 0.1f), and it based on the formular: mass = density * area.
|
2013-11-27 17:33:33 +08:00
|
|
|
|
* if you create body with createEdgeXXX, the mass and moment will be PHYSICS_INFINITY by default. and it's a static body.
|
2013-11-12 15:28:07 +08:00
|
|
|
|
* you can change mass and moment with setMass() and setMoment(). and you can change the body to be dynamic or static by use function setDynamic().
|
2013-09-17 16:27:43 +08:00
|
|
|
|
*/
|
2014-02-20 10:53:49 +08:00
|
|
|
|
class PhysicsBody : public Ref
|
2013-09-09 10:29:02 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** create a body with defult mass and moment. */
|
2013-10-10 18:31:43 +08:00
|
|
|
|
static PhysicsBody* create();
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** create a body with mass and defult moment. */
|
2013-10-28 11:08:41 +08:00
|
|
|
|
static PhysicsBody* create(float mass);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** create a body with mass and moment. */
|
2013-10-28 11:08:41 +08:00
|
|
|
|
static PhysicsBody* create(float mass, float moment);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/*
|
|
|
|
|
* @brief add a shape to body
|
|
|
|
|
* @param shape the shape to be added
|
|
|
|
|
* @param addMassAndMoment if this is true, the shape's mass and moment will be added to body. the default is true
|
2013-09-17 16:27:43 +08:00
|
|
|
|
*/
|
2013-11-12 15:28:07 +08:00
|
|
|
|
virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
|
|
|
|
|
/*
|
|
|
|
|
* @brief remove a shape from body
|
|
|
|
|
* @param shape the shape to be removed
|
|
|
|
|
* @param reduceMassAndMoment if this is true, the body mass and moment will be reduced by shape. the default is true
|
2013-09-17 16:27:43 +08:00
|
|
|
|
*/
|
2013-11-12 15:28:07 +08:00
|
|
|
|
void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
|
|
|
|
|
/*
|
|
|
|
|
* @brief remove a shape from body
|
|
|
|
|
* @param tag the tag of the shape to be removed
|
|
|
|
|
* @param reduceMassAndMoment if this is true, the body mass and moment will be reduced by shape. the default is true
|
2013-09-17 16:27:43 +08:00
|
|
|
|
*/
|
2013-11-12 15:28:07 +08:00
|
|
|
|
void removeShape(int tag, bool reduceMassAndMoment = true);
|
|
|
|
|
/* remove all shapes */
|
|
|
|
|
void removeAllShapes(bool reduceMassAndMoment = true);
|
|
|
|
|
/* get the body shapes. */
|
2013-12-09 10:54:22 +08:00
|
|
|
|
inline const Vector<PhysicsShape*>& getShapes() const { return _shapes; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/* get the first shape of the body shapes. */
|
2013-12-07 10:48:02 +08:00
|
|
|
|
inline PhysicsShape* getFirstShape() const { return _shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/* get the shape of the body. */
|
|
|
|
|
PhysicsShape* getShape(int tag) const;
|
|
|
|
|
|
|
|
|
|
/** Applies a immediate force to body. */
|
|
|
|
|
virtual void applyForce(const Vect& force);
|
|
|
|
|
/** Applies a immediate force to body. */
|
|
|
|
|
virtual void applyForce(const Vect& force, const Point& offset);
|
|
|
|
|
/** reset all the force applied to body. */
|
2013-11-18 16:27:23 +08:00
|
|
|
|
virtual void resetForces();
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** Applies a continuous force to body. */
|
2013-11-05 20:02:58 +08:00
|
|
|
|
virtual void applyImpulse(const Vect& impulse);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** Applies a continuous force to body. */
|
2013-11-05 20:02:58 +08:00
|
|
|
|
virtual void applyImpulse(const Vect& impulse, const Point& offset);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** Applies a torque force to body. */
|
2013-09-09 10:29:02 +08:00
|
|
|
|
virtual void applyTorque(float torque);
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the velocity of a body */
|
2013-11-05 20:02:58 +08:00
|
|
|
|
virtual void setVelocity(const Vect& velocity);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the velocity of a body */
|
2013-09-30 20:43:19 +08:00
|
|
|
|
virtual Point getVelocity();
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the angular velocity of a body */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual void setAngularVelocity(float velocity);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the angular velocity of a body at a local point */
|
2013-11-05 20:02:58 +08:00
|
|
|
|
virtual Point getVelocityAtLocalPoint(const Point& point);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the angular velocity of a body at a world point */
|
2013-11-05 20:02:58 +08:00
|
|
|
|
virtual Point getVelocityAtWorldPoint(const Point& point);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the angular velocity of a body */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual float getAngularVelocity();
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the max of velocity */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual void setVelocityLimit(float limit);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the max of velocity */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual float getVelocityLimit();
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the max of angular velocity */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual void setAngularVelocityLimit(float limit);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the max of angular velocity */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
virtual float getAngularVelocityLimit();
|
2013-09-30 20:43:19 +08:00
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** remove the body from the world it added to */
|
2013-10-28 11:08:41 +08:00
|
|
|
|
void removeFromWorld();
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the world body added to. */
|
2013-09-10 17:36:49 +08:00
|
|
|
|
inline PhysicsWorld* getWorld() const { return _world; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/** 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-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* A mask that defines which categories this physics body belongs to.
|
|
|
|
|
* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
|
|
|
|
|
* The default value is 0xFFFFFFFF (all bits set).
|
|
|
|
|
*/
|
2013-10-29 17:31:35 +08:00
|
|
|
|
void setCategoryBitmask(int bitmask);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
|
|
|
|
|
* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
|
|
|
|
|
* The default value is 0x00000000 (all bits cleared).
|
|
|
|
|
*/
|
2013-10-29 17:31:35 +08:00
|
|
|
|
void setContactTestBitmask(int bitmask);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* A mask that defines which categories of physics bodies can collide with this physics body.
|
|
|
|
|
* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
|
|
|
|
|
* The default value is 0xFFFFFFFF (all bits set).
|
|
|
|
|
*/
|
2013-10-29 17:31:35 +08:00
|
|
|
|
void setCollisionBitmask(int bitmask);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the category bit mask */
|
2013-09-09 16:36:19 +08:00
|
|
|
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the contact test bit mask */
|
2013-09-09 16:36:19 +08:00
|
|
|
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the collision bit mask */
|
2013-09-09 16:36:19 +08:00
|
|
|
|
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* set the group of body
|
|
|
|
|
* Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)
|
|
|
|
|
* it have high priority than bit masks
|
|
|
|
|
*/
|
2013-10-29 17:31:35 +08:00
|
|
|
|
void setGroup(int group);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the group of body */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
inline int getGroup() const { return _group; }
|
2013-10-29 17:31:35 +08:00
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the body position. */
|
2013-09-16 21:22:22 +08:00
|
|
|
|
Point getPosition() const;
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the body rotation. */
|
2013-09-16 21:22:22 +08:00
|
|
|
|
float getRotation() const;
|
|
|
|
|
|
2014-02-28 15:31:25 +08:00
|
|
|
|
/** set body position offset, it's the position witch relative to node */
|
|
|
|
|
void setPositionOffset(const Point& position);
|
|
|
|
|
/** get body position offset. */
|
2014-03-03 14:35:44 +08:00
|
|
|
|
Point getPositionOffset() const;
|
2014-02-28 15:31:25 +08:00
|
|
|
|
/** set body rotation offset, it's the rotation witch relative to node */
|
|
|
|
|
void setRotationOffset(float rotation);
|
|
|
|
|
/** set the body rotation offset */
|
2014-03-03 14:35:44 +08:00
|
|
|
|
float getRotationOffset() const;
|
2014-02-28 15:31:25 +08:00
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
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-11-12 15:28:07 +08:00
|
|
|
|
/**
|
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-11-12 15:28:07 +08:00
|
|
|
|
/**
|
2013-09-17 16:27:43 +08:00
|
|
|
|
* @brief set the body mass.
|
2013-11-27 17:33:33 +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-11-12 15:28:07 +08:00
|
|
|
|
/** get the body mass. */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
inline float getMass() const { return _mass; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
2013-10-18 15:34:13 +08:00
|
|
|
|
* @brief add mass to body.
|
2013-11-27 17:33:33 +08:00
|
|
|
|
* 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.
|
2013-10-18 15:34:13 +08:00
|
|
|
|
* 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-11-12 15:28:07 +08:00
|
|
|
|
/**
|
2013-10-18 15:34:13 +08:00
|
|
|
|
* @brief set the body moment of inertia.
|
2013-11-27 17:33:33 +08:00
|
|
|
|
* @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.
|
2013-10-18 15:34:13 +08:00
|
|
|
|
*/
|
|
|
|
|
void setMoment(float moment);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the body moment of inertia. */
|
2014-03-13 20:56:51 +08:00
|
|
|
|
inline float getMoment() const { return _moment; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
2013-10-18 15:34:13 +08:00
|
|
|
|
* @brief add moment of inertia to body.
|
2013-11-27 17:33:33 +08:00
|
|
|
|
* 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.
|
2013-10-18 15:34:13 +08:00
|
|
|
|
* if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
|
|
|
|
|
* other wise, moment = moment + _moment;
|
|
|
|
|
*/
|
|
|
|
|
void addMoment(float moment);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get linear damping. */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
inline float getLinearDamping() const { return _linearDamping; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* set linear damping.
|
|
|
|
|
* it is used to simulate fluid or air friction forces on the body.
|
|
|
|
|
* the value is 0.0f to 1.0f.
|
|
|
|
|
*/
|
2014-02-25 15:27:25 +08:00
|
|
|
|
inline void setLinearDamping(float damping) { _linearDamping = damping; updateDamping(); }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get angular damping. */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
inline float getAngularDamping() const { return _angularDamping; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* set angular damping.
|
|
|
|
|
* it is used to simulate fluid or air friction forces on the body.
|
|
|
|
|
* the value is 0.0f to 1.0f.
|
|
|
|
|
*/
|
2014-02-25 15:27:25 +08:00
|
|
|
|
inline void setAngularDamping(float damping) { _angularDamping = damping; updateDamping(); }
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** whether the body is at rest */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
bool isResting() const;
|
2014-02-24 16:29:14 +08:00
|
|
|
|
/** set body to rest */
|
2014-02-26 19:56:52 +08:00
|
|
|
|
void setResting(bool rest) const;
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* whether the body is enabled
|
|
|
|
|
* if the body it isn't enabled, it will not has simulation by world
|
|
|
|
|
*/
|
2014-03-20 10:21:59 +08:00
|
|
|
|
inline bool isEnabled() const { return _enabled; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* set the enable value.
|
|
|
|
|
* if the body it isn't enabled, it will not has simulation by world
|
|
|
|
|
*/
|
2013-09-29 09:39:20 +08:00
|
|
|
|
void setEnable(bool enable);
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** whether the body can rotation */
|
2014-03-20 10:21:59 +08:00
|
|
|
|
inline bool isRotationEnabled() const { return _rotationEnabled; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the body is allow rotation or not */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
void setRotationEnable(bool enable);
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** whether this physics body is affected by the physics world’s gravitational force. */
|
2014-03-20 10:21:59 +08:00
|
|
|
|
inline bool isGravityEnabled() const { return _gravityEnabled; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the body is affected by the physics world's gravitational force or not. */
|
2013-10-18 15:34:13 +08:00
|
|
|
|
void setGravityEnable(bool enable);
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** get the body's tag */
|
2013-11-01 16:26:03 +08:00
|
|
|
|
inline int getTag() const { return _tag; }
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** set the body's tag */
|
2013-09-29 09:39:20 +08:00
|
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
|
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** convert the world point to local */
|
2013-10-29 17:31:35 +08:00
|
|
|
|
Point world2Local(const Point& point);
|
2013-11-12 15:28:07 +08:00
|
|
|
|
/** convert the local point to world */
|
2013-10-29 17:31:35 +08:00
|
|
|
|
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
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
|
void update(float delta);
|
2013-10-18 15:34:13 +08:00
|
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
|
void removeJoint(PhysicsJoint* joint);
|
2014-02-25 16:08:16 +08:00
|
|
|
|
inline void updateDamping() { _isDamping = _linearDamping != 0.0f || _angularDamping != 0.0f; }
|
2014-03-18 16:17:43 +08:00
|
|
|
|
void updateMass(float oldMass, float newMass);
|
2013-11-07 14:17:57 +08:00
|
|
|
|
|
2013-09-09 10:29:02 +08:00
|
|
|
|
protected:
|
|
|
|
|
PhysicsBody();
|
|
|
|
|
virtual ~PhysicsBody();
|
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
|
protected:
|
2014-01-17 13:35:58 +08:00
|
|
|
|
Node* _node;
|
|
|
|
|
std::vector<PhysicsJoint*> _joints;
|
|
|
|
|
Vector<PhysicsShape*> _shapes;
|
|
|
|
|
PhysicsWorld* _world;
|
|
|
|
|
PhysicsBodyInfo* _info;
|
|
|
|
|
bool _dynamic;
|
2014-03-20 10:21:59 +08:00
|
|
|
|
bool _enabled;
|
|
|
|
|
bool _rotationEnabled;
|
|
|
|
|
bool _gravityEnabled;
|
2014-01-17 13:35:58 +08:00
|
|
|
|
bool _massDefault;
|
|
|
|
|
bool _momentDefault;
|
|
|
|
|
float _mass;
|
|
|
|
|
float _area;
|
|
|
|
|
float _density;
|
|
|
|
|
float _moment;
|
2014-02-25 15:27:25 +08:00
|
|
|
|
bool _isDamping;
|
2014-01-17 13:35:58 +08:00
|
|
|
|
float _linearDamping;
|
|
|
|
|
float _angularDamping;
|
|
|
|
|
int _tag;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
2014-01-17 13:35:58 +08:00
|
|
|
|
int _categoryBitmask;
|
|
|
|
|
int _collisionBitmask;
|
|
|
|
|
int _contactTestBitmask;
|
|
|
|
|
int _group;
|
2013-09-09 10:29:02 +08:00
|
|
|
|
|
2014-02-27 14:36:09 +08:00
|
|
|
|
bool _positionResetTag; /// To avoid reset the body position when body invoke Node::setPosition().
|
|
|
|
|
bool _rotationResetTag; /// To avoid reset the body rotation when body invoke Node::setRotation().
|
2014-02-28 15:31:25 +08:00
|
|
|
|
Point _positionOffset;
|
|
|
|
|
float _rotationOffset;
|
2014-02-27 14:36:09 +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__
|