2019-11-23 20:27:39 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-10-24 14:09:59 +08:00
|
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
|
https://axys1.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
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__
|
|
|
|
|
|
|
|
|
|
#include "base/ccConfig.h"
|
2022-07-16 10:43:05 +08:00
|
|
|
|
#if AX_USE_PHYSICS
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
# include "2d/CCComponent.h"
|
|
|
|
|
# include "math/CCMath.h"
|
|
|
|
|
# include "physics/CCPhysicsShape.h"
|
|
|
|
|
# include "base/CCVector.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
struct cpBody;
|
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
class Node;
|
|
|
|
|
class PhysicsWorld;
|
|
|
|
|
class PhysicsJoint;
|
|
|
|
|
|
|
|
|
|
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @addtogroup physics
|
|
|
|
|
* @{
|
|
|
|
|
* @addtogroup physics_2d
|
|
|
|
|
* @{
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A body affect by physics.
|
|
|
|
|
*
|
|
|
|
|
* It can attach one or more shapes.
|
2021-12-25 10:04:45 +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 formula: mass = density
|
|
|
|
|
* * area. If you create body with createEdgeXXX, the mass and moment will be PHYSICS_INFINITY by default. And it's a
|
|
|
|
|
* static body. 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().
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
|
class AX_DLL PhysicsBody : public Component
|
2019-11-23 20:27:39 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
const static std::string COMPONENT_NAME;
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Create a body with default mass and moment.
|
|
|
|
|
*
|
|
|
|
|
* This default mass value is 1.0.
|
|
|
|
|
* This default moment value is 200.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
|
|
|
|
static PhysicsBody* create();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Create a body with mass and default moment.
|
|
|
|
|
*
|
|
|
|
|
* @param mass This body's mass.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
|
|
|
|
static PhysicsBody* create(float mass);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Create a body with mass and moment.
|
|
|
|
|
*
|
|
|
|
|
* @param mass This body's mass.
|
|
|
|
|
* @param moment This body's moment.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
|
|
|
|
static PhysicsBody* create(float mass, float moment);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a circle.
|
|
|
|
|
*
|
|
|
|
|
* @param radius A float number, it is the circle's radius.
|
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createCircle(float radius,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
const Vec2& offset = Vec2::ZERO);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a box shape.
|
|
|
|
|
*
|
2021-10-24 14:09:59 +08:00
|
|
|
|
* @param size Size contains this box's width and height.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createBox(const Vec2& size,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
const Vec2& offset = Vec2::ZERO);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Create a body contains a polygon shape.
|
|
|
|
|
*
|
|
|
|
|
* @param points Points is an array of Vec2 structs defining a convex hull with a clockwise winding.
|
|
|
|
|
* @param count An integer number, contains the count of the points array.
|
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createPolygon(const Vec2* points,
|
|
|
|
|
int count,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
const Vec2& offset = Vec2::ZERO);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a EdgeSegment shape.
|
|
|
|
|
*
|
|
|
|
|
* @param a It's the edge's begin position.
|
|
|
|
|
* @param b It's the edge's end position.
|
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param border It's a edge's border width.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createEdgeSegment(const Vec2& a,
|
|
|
|
|
const Vec2& b,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
float border = 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a EdgeBox shape.
|
2021-10-24 22:06:13 +08:00
|
|
|
|
* @param size The size contains this box's width and height.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param border It's a edge's border width.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createEdgeBox(const Vec2& size,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
float border = 1,
|
|
|
|
|
const Vec2& offset = Vec2::ZERO);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a EdgePolygon shape.
|
|
|
|
|
*
|
|
|
|
|
* @param points Points is an array of Vec2 structs defining a convex hull with a clockwise winding.
|
|
|
|
|
* @param count An integer number, contains the count of the points array.
|
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param border It's a edge's border width.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createEdgePolygon(const Vec2* points,
|
|
|
|
|
int count,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
float border = 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a body contains a EdgeChain shape.
|
|
|
|
|
*
|
|
|
|
|
* @param points A Vec2 object pointer, it contains an array of points.
|
|
|
|
|
* @param count An integer number, contains the count of the points array.
|
|
|
|
|
* @param material A PhysicsMaterial object, the default value is PHYSICSSHAPE_MATERIAL_DEFAULT.
|
|
|
|
|
* @param border It's a edge's border width.
|
|
|
|
|
* @return An autoreleased PhysicsBody object pointer.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
static PhysicsBody* createEdgeChain(const Vec2* points,
|
|
|
|
|
int count,
|
|
|
|
|
const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT,
|
|
|
|
|
float border = 1);
|
2019-11-23 20:27:39 +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.
|
|
|
|
|
* @return This shape's pointer if added success or nullptr if failed.
|
|
|
|
|
*/
|
|
|
|
|
virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Remove a shape from body.
|
|
|
|
|
* @param shape Shape the shape to be removed.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* @param reduceMassAndMoment If this is true, the body mass and moment will be reduced by shape. The default is
|
|
|
|
|
* true.
|
2019-11-23 20:27:39 +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.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* @param reduceMassAndMoment If this is true, the body mass and moment will be reduced by shape. The default is
|
|
|
|
|
* true.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
void removeShape(int tag, bool reduceMassAndMoment = true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Remove all shapes.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* @param reduceMassAndMoment If this is true, the body mass and moment will be reduced by shape. The default is
|
|
|
|
|
* true.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
void removeAllShapes(bool reduceMassAndMoment = true);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the body shapes.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @return A Vector<PhysicsShape*> object contains PhysicsShape pointer.
|
|
|
|
|
*/
|
|
|
|
|
const Vector<PhysicsShape*>& getShapes() const { return _shapes; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the first shape of the body shapes.
|
|
|
|
|
*
|
|
|
|
|
* @return The first shape in this body.
|
|
|
|
|
*/
|
|
|
|
|
PhysicsShape* getFirstShape() const { return _shapes.size() >= 1 ? _shapes.at(0) : nullptr; }
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* get the shape of the body.
|
|
|
|
|
*
|
|
|
|
|
* @param tag An integer number that identifies a PhysicsShape object.
|
|
|
|
|
* @return A PhysicsShape object pointer or nullptr if no shapes were found.
|
|
|
|
|
*/
|
|
|
|
|
PhysicsShape* getShape(int tag) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Applies a continuous force to body.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param force The force is applies to this body.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in world coordinates.
|
|
|
|
|
*/
|
|
|
|
|
virtual void applyForce(const Vec2& force, const Vec2& offset = Vec2::ZERO);
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* reset all the force applied to body.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual void resetForces();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Applies a immediate force to body.
|
|
|
|
|
*
|
|
|
|
|
* @param impulse The impulse is applies to this body.
|
|
|
|
|
* @param offset A Vec2 object, it is the offset from the body's center of gravity in world coordinates.
|
|
|
|
|
*/
|
|
|
|
|
virtual void applyImpulse(const Vec2& impulse, const Vec2& offset = Vec2::ZERO);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Applies a torque force to body.
|
|
|
|
|
*
|
|
|
|
|
* @param torque The torque is applies to this body.
|
|
|
|
|
*/
|
|
|
|
|
virtual void applyTorque(float torque);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Set the velocity of a body.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param velocity The velocity is set to this body.
|
|
|
|
|
*/
|
|
|
|
|
virtual void setVelocity(const Vec2& velocity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get the velocity of a body. */
|
|
|
|
|
virtual Vec2 getVelocity();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Set the angular velocity of a body.
|
|
|
|
|
*
|
|
|
|
|
* @param velocity The angular velocity is set to this body.
|
|
|
|
|
*/
|
|
|
|
|
virtual void setAngularVelocity(float velocity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get the angular velocity of a body at a local point.*/
|
|
|
|
|
virtual Vec2 getVelocityAtLocalPoint(const Vec2& point);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the angular velocity of a body at a world point */
|
|
|
|
|
virtual Vec2 getVelocityAtWorldPoint(const Vec2& point);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the angular velocity of a body */
|
|
|
|
|
virtual float getAngularVelocity();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set the max of velocity */
|
|
|
|
|
virtual void setVelocityLimit(float limit);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the max of velocity */
|
|
|
|
|
virtual float getVelocityLimit();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set the max of angular velocity */
|
|
|
|
|
virtual void setAngularVelocityLimit(float limit);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the max of angular velocity */
|
|
|
|
|
virtual float getAngularVelocityLimit();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** remove the body from the world it added to */
|
|
|
|
|
void removeFromWorld();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the world body added to. */
|
|
|
|
|
PhysicsWorld* getWorld() const { return _world; }
|
|
|
|
|
|
|
|
|
|
/** get all joints the body have */
|
|
|
|
|
const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the node the body set to. */
|
|
|
|
|
Node* getNode() const { return _owner; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* A mask that defines which categories this physics body belongs to.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param bitmask An integer number, the default value is 0xFFFFFFFF (all bits set).
|
|
|
|
|
*/
|
|
|
|
|
void setCategoryBitmask(int bitmask);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* 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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param bitmask An integer number, the default value is 0x00000000 (all bits cleared).
|
|
|
|
|
*/
|
|
|
|
|
void setContactTestBitmask(int bitmask);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* A mask that defines which categories of physics bodies can collide with this physics body.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* 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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param bitmask An integer number, the default value is 0xFFFFFFFF (all bits set).
|
|
|
|
|
*/
|
|
|
|
|
void setCollisionBitmask(int bitmask);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Return bitmask of first shape.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @return If there is no shape in body, return default value.(0xFFFFFFFF)
|
|
|
|
|
*/
|
|
|
|
|
int getCategoryBitmask() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Return bitmask of first shape.
|
|
|
|
|
*
|
|
|
|
|
* @return If there is no shape in body, return default value.(0x00000000)
|
|
|
|
|
*/
|
|
|
|
|
int getContactTestBitmask() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Return bitmask of first shape.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @return If there is no shape in body, return default value.(0xFFFFFFFF)
|
|
|
|
|
*/
|
|
|
|
|
int getCollisionBitmask() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Set the group of body.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* 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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
void setGroup(int group);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Return group of first shape.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
|
|
|
|
* @return If there is no shape in body, return default value.(0)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
int getGroup() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get the body position. */
|
|
|
|
|
Vec2 getPosition() const;
|
|
|
|
|
|
|
|
|
|
/** get the body rotation. */
|
|
|
|
|
float getRotation();
|
|
|
|
|
|
|
|
|
|
/** set body position offset, it's the position witch relative to node */
|
|
|
|
|
void setPositionOffset(const Vec2& position);
|
|
|
|
|
|
|
|
|
|
/** get body position offset. */
|
|
|
|
|
const Vec2& getPositionOffset() const { return _positionOffset; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set body rotation offset, it's the rotation witch relative to node */
|
|
|
|
|
void setRotationOffset(float rotation);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set the body rotation offset */
|
|
|
|
|
float getRotationOffset() const { return _rotationOffset; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Test the body is dynamic or not.
|
|
|
|
|
*
|
|
|
|
|
* A dynamic body will effect with gravity.
|
|
|
|
|
*/
|
|
|
|
|
bool isDynamic() const { return _dynamic; }
|
|
|
|
|
/**
|
|
|
|
|
* @brief Set dynamic to body.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* A dynamic body will effect with gravity.
|
|
|
|
|
*/
|
|
|
|
|
void setDynamic(bool dynamic);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Set the body mass.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
|
* @attention 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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
void setMass(float mass);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get the body mass. */
|
|
|
|
|
float getMass() const { return _mass; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Add mass to body.
|
|
|
|
|
*
|
|
|
|
|
* @param mass 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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Set the body moment of inertia.
|
|
|
|
|
*
|
2021-12-25 10:04:45 +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.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
*/
|
|
|
|
|
void setMoment(float moment);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get the body moment of inertia. */
|
|
|
|
|
float getMoment() const { return _moment; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief Add moment of inertia to body.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* @param moment 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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** get linear damping. */
|
|
|
|
|
float getLinearDamping() const { return _linearDamping; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
|
* Set linear damping.
|
|
|
|
|
*
|
|
|
|
|
* it is used to simulate fluid or air friction forces on the body.
|
|
|
|
|
* @param damping The value is 0.0f to 1.0f.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void setLinearDamping(float damping)
|
|
|
|
|
{
|
|
|
|
|
_linearDamping = damping;
|
|
|
|
|
updateDamping();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get angular damping. */
|
|
|
|
|
float getAngularDamping() const { return _angularDamping; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set angular damping.
|
|
|
|
|
*
|
|
|
|
|
* It is used to simulate fluid or air friction forces on the body.
|
|
|
|
|
* @param damping The value is 0.0f to 1.0f.
|
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void setAngularDamping(float damping)
|
|
|
|
|
{
|
|
|
|
|
_angularDamping = damping;
|
|
|
|
|
updateDamping();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Whether the body is at rest. */
|
|
|
|
|
bool isResting() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set body to rest */
|
|
|
|
|
void setResting(bool rest) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/**
|
|
|
|
|
* Set the enable value.
|
|
|
|
|
*
|
|
|
|
|
* If the body it isn't enabled, it will not has simulation by world.
|
|
|
|
|
*/
|
|
|
|
|
virtual void setEnabled(bool enable) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Whether the body can rotation. */
|
|
|
|
|
bool isRotationEnabled() const { return _rotationEnabled; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Set the body is allow rotation or not */
|
|
|
|
|
void setRotationEnable(bool enable);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Whether this physics body is affected by the physics world's gravitational force. */
|
|
|
|
|
bool isGravityEnabled() const { return _gravityEnabled; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Set the body is affected by the physics world's gravitational force or not. */
|
|
|
|
|
void setGravityEnable(bool enable);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Get the body's tag. */
|
|
|
|
|
int getTag() const { return _tag; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** set the body's tag. */
|
|
|
|
|
void setTag(int tag) { _tag = tag; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Convert the world point to local. */
|
|
|
|
|
Vec2 world2Local(const Vec2& point);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
/** Convert the local point to world. */
|
|
|
|
|
Vec2 local2World(const Vec2& point);
|
|
|
|
|
|
|
|
|
|
/** Get the rigid body of chipmunk. */
|
|
|
|
|
cpBody* getCPBody() const { return _cpBody; }
|
|
|
|
|
|
|
|
|
|
virtual void onEnter() override;
|
|
|
|
|
virtual void onExit() override;
|
|
|
|
|
virtual void onAdd() override;
|
|
|
|
|
virtual void onRemove() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
protected:
|
|
|
|
|
PhysicsBody();
|
|
|
|
|
virtual ~PhysicsBody();
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
virtual bool init() override;
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
virtual void setPosition(float positionX, float positionY);
|
|
|
|
|
|
|
|
|
|
virtual void setRotation(float rotation);
|
|
|
|
|
|
|
|
|
|
virtual void setScale(float scaleX, float scaleY);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
void update(float delta) override;
|
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
void removeJoint(PhysicsJoint* joint);
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void updateDamping() { _isDamping = _linearDamping != 0.0f || _angularDamping != 0.0f; }
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
|
|
void addToPhysicsWorld();
|
|
|
|
|
void removeFromPhysicsWorld();
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void beforeSimulation(const Mat4& parentToWorldTransform,
|
|
|
|
|
const Mat4& nodeToWorldTransform,
|
|
|
|
|
float scaleX,
|
|
|
|
|
float scaleY,
|
|
|
|
|
float rotation);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
void afterSimulation(const Mat4& parentToWorldTransform, float parentRotation);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
protected:
|
|
|
|
|
std::vector<PhysicsJoint*> _joints;
|
|
|
|
|
Vector<PhysicsShape*> _shapes;
|
|
|
|
|
PhysicsWorld* _world;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
cpBody* _cpBody;
|
|
|
|
|
bool _dynamic;
|
|
|
|
|
bool _rotationEnabled;
|
|
|
|
|
bool _gravityEnabled;
|
|
|
|
|
bool _massDefault;
|
|
|
|
|
bool _momentDefault;
|
|
|
|
|
float _mass;
|
|
|
|
|
float _area;
|
|
|
|
|
float _density;
|
|
|
|
|
float _moment;
|
|
|
|
|
float _velocityLimit;
|
|
|
|
|
float _angularVelocityLimit;
|
|
|
|
|
bool _isDamping;
|
|
|
|
|
float _linearDamping;
|
|
|
|
|
float _angularDamping;
|
|
|
|
|
|
|
|
|
|
int _tag;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
// when setMass() is invoked, it means body's mass is not calculated by shapes
|
|
|
|
|
bool _massSetByUser;
|
|
|
|
|
// when setMoment() is invoked, it means body's moment is not calculated by shapes
|
|
|
|
|
bool _momentSetByUser;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
Vec2 _positionOffset;
|
|
|
|
|
float _rotationOffset;
|
|
|
|
|
float _recordedRotation;
|
|
|
|
|
double _recordedAngle;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
|
// offset between owner's center point and down left point
|
|
|
|
|
Vec3 _ownerCenterOffset;
|
|
|
|
|
// offset of owner's center point and anchor point in parent coordinate
|
|
|
|
|
Vec2 _offset;
|
|
|
|
|
float _recordScaleX;
|
|
|
|
|
float _recordScaleY;
|
|
|
|
|
|
|
|
|
|
float _recordPosX;
|
|
|
|
|
float _recordPosY;
|
|
|
|
|
|
|
|
|
|
friend class PhysicsWorld;
|
|
|
|
|
friend class PhysicsShape;
|
|
|
|
|
friend class PhysicsJoint;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
/** @} */
|
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
|
#endif // AX_USE_PHYSICS
|
2021-12-25 10:04:45 +08:00
|
|
|
|
#endif // __CCPHYSICS_BODY_H__
|