axmol/core/physics/CCPhysicsShape.h

791 lines
24 KiB
C
Raw Normal View History

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-10-01 16:24:52 +08:00
https://axmolengine.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_SHAPE_H__
#define __CCPHYSICS_SHAPE_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 "base/CCRef.h"
# include "math/CCMath.h"
2019-11-23 20:27:39 +08:00
struct cpShape;
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
class PhysicsBody;
2022-07-16 10:43:05 +08:00
typedef struct AX_DLL PhysicsMaterial
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float density; ///< The density of the object.
float restitution; ///< The bounciness of the physics body.
float friction; ///< The roughness of the surface of a shape.
PhysicsMaterial() : density(0.0f), restitution(0.0f), friction(0.0f) {}
2019-11-23 20:27:39 +08:00
PhysicsMaterial(float aDensity, float aRestitution, float aFriction)
2021-12-25 10:04:45 +08:00
: density(aDensity), restitution(aRestitution), friction(aFriction)
2019-11-23 20:27:39 +08:00
{}
2021-12-25 10:04:45 +08:00
} PhysicsMaterial;
2019-11-23 20:27:39 +08:00
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT;
/**
* @addtogroup physics
* @{
* @addtogroup physics_2d
* @{
*/
/**
2021-12-25 10:04:45 +08:00
* @brief A shape for body. You do not create PhysicsWorld objects directly, instead, you can view PhysicsBody to see
* how to create it.
2019-11-23 20:27:39 +08:00
*/
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShape : public Ref
2019-11-23 20:27:39 +08:00
{
public:
enum class Type
{
UNKNOWN,
CIRCLE,
BOX,
POLYGON,
EDGESEGMENT,
EDGEBOX,
EDGEPOLYGON,
EDGECHAIN,
/** @deprecated Use Type::POLYGON instead. */
POLYGEN = POLYGON,
/** @deprecated Use Type::EDGEPOLYGON instead. */
EDGEPOLYGEN = EDGEPOLYGON,
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Get the body that this shape attaches.
*
* @return A PhysicsBody object pointer.
*/
PhysicsBody* getBody() const { return _body; }
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Return this shape's type.
*
* @return A Type object.
*/
Type getType() const { return _type; }
/**
* Return this shape's area.
*
* @return A float number.
*/
float getArea() const { return _area; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's moment.
*
* @return A float number.
*/
float getMoment() const { return _moment; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's moment.
*
* It will change the body's moment this shape attaches.
*
* @param moment A float number.
*/
void setMoment(float moment);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's tag.
*
* @param tag An integer number that identifies a shape object.
*/
void setTag(int tag) { _tag = tag; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's tag.
*
* @return An integer number.
*/
int getTag() const { return _tag; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the mass of this shape.
*
* @return A float number.
*/
float getMass() const { return _mass; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's mass.
*
* It will change the body's mass this shape attaches.
*
* @param mass A float number.
*/
void setMass(float mass);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's density.
*
* @return A float number.
*/
float getDensity() const { return _material.density; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's density.
*
* It will change the body's mass this shape attaches.
*
* @param density A float number.
*/
void setDensity(float density);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's restitution.
*
* @return A float number.
*/
float getRestitution() const { return _material.restitution; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's restitution.
*
* It will change the shape's elasticity.
*
* @param restitution A float number.
*/
void setRestitution(float restitution);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's friction.
*
* @return A float number.
*/
float getFriction() const { return _material.friction; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's friction.
*
* It will change the shape's friction.
*
* @param friction A float number.
*/
void setFriction(float friction);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's PhysicsMaterial object.
*
* @return A PhysicsMaterial object reference.
*/
const PhysicsMaterial& getMaterial() const { return _material; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set this shape's material.
*
* It will change the shape's mass, elasticity and friction.
*
* @param material A PhysicsMaterial object.
*/
void setMaterial(const PhysicsMaterial& material);
bool isSensor() const { return _sensor; }
void setSensor(bool sensor);
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Calculate the default moment value.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* This function should be overridden in inherit classes.
* @return A float number, equals 0.0.
*/
virtual float calculateDefaultMoment() { return 0.0f; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's position offset.
*
* This function should be overridden in inherit classes.
* @return A Vec2 object.
*/
virtual Vec2 getOffset() { return Vec2::ZERO; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this shape's center position.
*
* This function should be overridden in inherit classes.
* @return A Vec2 object.
*/
virtual Vec2 getCenter() { return getOffset(); }
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Test point is inside this shape or not.
*
* @param point A Vec2 object.
* @return A bool object.
*/
bool containsPoint(const Vec2& point) const;
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* Move the points to the center.
2021-12-25 10:04:45 +08:00
*
2019-11-23 20:27:39 +08:00
* @param points A Vec2 object pointer.
* @param count An integer number.
* @param center A Vec2 object, default value is Vec2(0,0).
*/
static void recenterPoints(Vec2* points, int count, const Vec2& center = Vec2::ZERO);
/**
* Get center of the polygon points.
*
* @param points A Vec2 object pointer.
* @param count An integer number.
* @return A Vec2 object.
*/
static Vec2 getPolygonCenter(const Vec2* points, int count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set 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) { _categoryBitmask = bitmask; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a mask that defines which categories this physics body belongs to.
*
* @return An integer number.
*/
int getCategoryBitmask() const { return _categoryBitmask; }
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 worlds 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) { _contactTestBitmask = bitmask; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a mask that defines which categories of bodies cause intersection notifications with this physics body.
*
* @return An integer number.
*/
int getContactTestBitmask() const { return _contactTestBitmask; }
/**
* 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) { _collisionBitmask = bitmask; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a mask that defines which categories of physics bodies can collide with this physics body.
*
* @return An integer number.
*/
int getCollisionBitmask() const { return _collisionBitmask; }
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).
2019-11-23 20:27:39 +08:00
* @param group An integer number, it have high priority than bit masks.
*/
void setGroup(int group);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the group of body.
*
* @return An integer number.
*/
int getGroup() { return _group; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
void setBody(PhysicsBody* body);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** calculate the area of this shape */
virtual float calculateArea() { return 0.0f; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void setScale(float scaleX, float scaleY);
virtual void updateScale();
void addShape(cpShape* shape);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShape();
virtual ~PhysicsShape() = 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsBody* _body;
std::vector<cpShape*> _cpShapes;
Type _type;
float _area;
float _mass;
float _moment;
bool _sensor;
float _scaleX;
float _scaleY;
float _newScaleX;
float _newScaleY;
PhysicsMaterial _material;
int _tag;
2021-12-25 10:04:45 +08:00
int _categoryBitmask;
int _collisionBitmask;
int _contactTestBitmask;
int _group;
2019-11-23 20:27:39 +08:00
friend class PhysicsWorld;
friend class PhysicsBody;
friend class PhysicsJoint;
friend class PhysicsDebugDraw;
};
/** A circle shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeCircle : public PhysicsShape
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeCircle with specified value.
*
* @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 PhysicsShapeCircle object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeCircle* create(float radius,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2(0.0f, 0.0f));
2019-11-23 20:27:39 +08:00
/**
* Calculate the area of a circle with specified radius.
*
* @param radius A float number
* @return A float number
*/
static float calculateArea(float radius);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Calculate the moment of a circle with specified value.
*
* @param mass A float number
* @param radius A float number
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
* @return A float number
*/
static float calculateMoment(float mass, float radius, const Vec2& offset = Vec2::ZERO);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Calculate the moment for a circle.
*
* @return A float number.
*/
virtual float calculateDefaultMoment() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the circle's radius.
*
* @return A float number.
*/
float getRadius() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this circle's position offset.
*
* @return A Vec2 object.
*/
virtual Vec2 getOffset() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(float radius,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2::ZERO);
2019-11-23 20:27:39 +08:00
virtual float calculateArea() override;
virtual void updateScale() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeCircle();
virtual ~PhysicsShapeCircle();
};
/** A polygon shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapePolygon : public PhysicsShape
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapePolygon with specified value.
*
* @param points A Vec2 object pointer, it is an array of Vec2.
* @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 PhysicsShapePolygon object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapePolygon* create(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2::ZERO,
float radius = 0.0f);
2019-11-23 20:27:39 +08:00
/**
* Calculate the area of a polygon with specified value.
*
* @param points A Vec2 object pointer, it is an array of Vec2.
* @param count An integer number, contains the count of the points array.
* @return A float number.
*/
static float calculateArea(const Vec2* points, int count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Calculate the moment of a polygon with specified value.
*
* @param mass A float number
* @param points A Vec2 object pointer, it is an array of Vec2.
* @param count An integer number, contains the count of the points array.
* @param offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
* @return A float number
*/
2021-12-25 10:04:45 +08:00
static float calculateMoment(float mass,
const Vec2* points,
int count,
const Vec2& offset = Vec2::ZERO,
float radius = 0.0f);
2019-11-23 20:27:39 +08:00
/**
* Calculate the moment for a polygon.
*
* @return A float number.
*/
float calculateDefaultMoment() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get a point of this polygon's points array.
*
* @param i A index of this polygon's points array.
* @return A point value.
*/
Vec2 getPoint(int i) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's points array.
*
* @param outPoints A Vec2 array pointer.
*/
void getPoints(Vec2* outPoints) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's points array count.
*
* @return An integer number.
*/
int getPointsCount() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's center position.
*
* @return A Vec2 object.
*/
virtual Vec2 getCenter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2::ZERO,
float radius = 0.0f);
2019-11-23 20:27:39 +08:00
float calculateArea() override;
virtual void updateScale() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapePolygon();
virtual ~PhysicsShapePolygon();
};
/** A box shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeBox : public PhysicsShapePolygon
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeBox with specified value.
*
2021-10-24 14:09:59 +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 offset A Vec2 object, it is the offset from the body's center of gravity in body local coordinates.
* @return An autoreleased PhysicsShapeBox object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeBox* create(const Vec2& size,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2::ZERO,
float radius = 0.0f);
2019-11-23 20:27:39 +08:00
/**
* Get this box's width and height.
*
2021-10-23 23:27:14 +08:00
* @return An Vec2 object.
2019-11-23 20:27:39 +08:00
*/
2021-10-23 23:27:14 +08:00
Vec2 getSize() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this box's position offset.
*
* @return A Vec2 object.
*/
virtual Vec2 getOffset() override { return getCenter(); }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2& size,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
const Vec2& offset = Vec2::ZERO,
float radius = 0.0f);
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeBox();
virtual ~PhysicsShapeBox();
};
/** A segment shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeEdgeSegment : public PhysicsShape
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeEdgeSegment with specified value.
*
* @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 PhysicsShapeEdgeSegment object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeEdgeSegment* create(const Vec2& a,
const Vec2& b,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
/**
* Get this edge's begin position.
*
* @return A Vec2 object.
*/
Vec2 getPointA() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this edge's end position.
*
* @return A Vec2 object.
*/
Vec2 getPointB() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this edge's center position.
*
* @return A Vec2 object.
*/
virtual Vec2 getCenter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2& a,
const Vec2& b,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
virtual void updateScale() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeEdgeSegment();
virtual ~PhysicsShapeEdgeSegment();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
friend class PhysicsBody;
};
/** An edge polygon shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeEdgePolygon : public PhysicsShape
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeEdgePolygon with specified value.
*
* @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 PhysicsShapeEdgePolygon object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeEdgePolygon* create(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's center position.
*
* @return A Vec2 object.
*/
virtual Vec2 getCenter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's points array.
*
* @param outPoints A Vec2 array pointer.
*/
void getPoints(Vec2* outPoints) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this polygon's points array count.
*
* @return An integer number.
*/
int getPointsCount() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
virtual void updateScale() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeEdgePolygon();
virtual ~PhysicsShapeEdgePolygon();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
friend class PhysicsBody;
};
/** An edge box shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeEdgeBox : public PhysicsShapeEdgePolygon
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeEdgeBox with specified value.
*
2021-10-24 14:09:59 +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 PhysicsShapeEdgeBox object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeEdgeBox* create(const Vec2& size,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 0,
const Vec2& offset = Vec2::ZERO);
2019-11-23 20:27:39 +08:00
/**
* Get this box's position offset.
*
* @return A Vec2 object.
*/
virtual Vec2 getOffset() override { return getCenter(); }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2& size,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1,
const Vec2& offset = Vec2::ZERO);
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeEdgeBox();
virtual ~PhysicsShapeEdgeBox();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
friend class PhysicsBody;
};
/** A chain shape. */
2022-07-16 10:43:05 +08:00
class AX_DLL PhysicsShapeEdgeChain : public PhysicsShape
2019-11-23 20:27:39 +08:00
{
public:
/**
* Creates a PhysicsShapeEdgeChain with specified value.
*
* @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 PhysicsShapeEdgeChain object pointer.
*/
2021-12-25 10:04:45 +08:00
static PhysicsShapeEdgeChain* create(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
/**
* Get this chain's center position.
*
* @return A Vec2 object.
*/
virtual Vec2 getCenter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this chain's points array.
*
* @param outPoints A Vec2 array pointer.
*/
void getPoints(Vec2* outPoints) const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get this chain's points array count.
*
* @return An integer number.
*/
int getPointsCount() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool init(const Vec2* points,
int count,
const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT,
float border = 1);
2019-11-23 20:27:39 +08:00
virtual void updateScale() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
PhysicsShapeEdgeChain();
virtual ~PhysicsShapeEdgeChain();
friend class PhysicsBody;
};
/** @} */
/** @} */
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_FIXTURE_H__