axmol/core/physics/CCPhysicsShape.cpp

979 lines
26 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-12-25 10:04:45 +08:00
2022-01-04 12:36:20 +08:00
https://adxeproject.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.
****************************************************************************/
#include "physics/CCPhysicsShape.h"
#if CC_USE_PHYSICS
2021-12-25 10:04:45 +08:00
# include <climits>
# include <cmath>
# include <unordered_map>
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
# include "chipmunk/chipmunk.h"
# include "chipmunk/chipmunk_unsafe.h"
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
# include "physics/CCPhysicsBody.h"
# include "physics/CCPhysicsWorld.h"
# include "physics/CCPhysicsHelper.h"
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
extern const float PHYSICS_INFINITY;
static cpBody* s_sharedBody = nullptr;
PhysicsShape::PhysicsShape()
2021-12-25 10:04:45 +08:00
: _body(nullptr)
, _type(Type::UNKNOWN)
, _area(0.0f)
, _mass(0.0f)
, _moment(0.0f)
, _sensor(false)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _newScaleX(1.0f)
, _newScaleY(1.0f)
, _tag(0)
, _categoryBitmask(UINT_MAX)
, _collisionBitmask(UINT_MAX)
, _contactTestBitmask(0)
, _group(0)
2019-11-23 20:27:39 +08:00
{
if (s_sharedBody == nullptr)
{
s_sharedBody = cpBodyNewStatic();
}
}
PhysicsShape::~PhysicsShape()
{
for (auto shape : _cpShapes)
{
cpShapeFree(shape);
}
}
void PhysicsShape::setMass(float mass)
{
if (mass < 0)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_body)
{
_body->addMass(-_mass);
_body->addMass(mass);
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_mass = mass;
}
void PhysicsShape::setMoment(float moment)
{
if (moment < 0)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_body)
{
_body->addMoment(-_moment);
_body->addMoment(moment);
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_moment = moment;
}
void PhysicsShape::setMaterial(const PhysicsMaterial& material)
{
setDensity(material.density);
setRestitution(material.restitution);
setFriction(material.friction);
}
void PhysicsShape::setScale(float scaleX, float scaleY)
{
if (std::abs(_scaleX - scaleX) > FLT_EPSILON || std::abs(_scaleY - scaleY) > FLT_EPSILON)
{
if (_type == Type::CIRCLE && scaleX != scaleY)
{
CCLOG("PhysicsShapeCircle WARNING: CANNOT support setScale with different x and y");
return;
}
_newScaleX = scaleX;
_newScaleY = scaleY;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
updateScale();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// re-calculate area and mass
2021-12-25 10:04:45 +08:00
_area = calculateArea();
_mass = _material.density * _area;
2019-11-23 20:27:39 +08:00
_moment = calculateDefaultMoment();
}
}
void PhysicsShape::updateScale()
{
_scaleX = _newScaleX;
_scaleY = _newScaleY;
}
void PhysicsShape::addShape(cpShape* shape)
{
if (shape)
{
cpShapeSetUserData(shape, this);
cpShapeSetFilter(shape, cpShapeFilterNew(_group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
_cpShapes.push_back(shape);
}
}
2021-12-25 10:04:45 +08:00
PhysicsShapeCircle::PhysicsShapeCircle() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeCircle::~PhysicsShapeCircle() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeBox::PhysicsShapeBox() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeBox::~PhysicsShapeBox() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapePolygon::PhysicsShapePolygon() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapePolygon::~PhysicsShapePolygon() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeBox::PhysicsShapeEdgeBox() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeBox::~PhysicsShapeEdgeBox() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeChain::PhysicsShapeEdgeChain() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeChain::~PhysicsShapeEdgeChain() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgePolygon::PhysicsShapeEdgePolygon() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgePolygon::~PhysicsShapeEdgePolygon() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeSegment::PhysicsShapeEdgeSegment() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment() {}
2019-11-23 20:27:39 +08:00
void PhysicsShape::setDensity(float density)
{
if (density < 0)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_material.density = density;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_material.density == PHYSICS_INFINITY)
{
setMass(PHYSICS_INFINITY);
2021-12-25 10:04:45 +08:00
}
else if (_area > 0)
2019-11-23 20:27:39 +08:00
{
setMass(_material.density * _area);
}
}
void PhysicsShape::setRestitution(float restitution)
{
_material.restitution = restitution;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (cpShape* shape : _cpShapes)
{
cpShapeSetElasticity(shape, restitution);
}
}
void PhysicsShape::setFriction(float friction)
{
_material.friction = friction;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (cpShape* shape : _cpShapes)
{
cpShapeSetFriction(shape, friction);
}
}
void PhysicsShape::setSensor(bool sensor)
{
if (sensor != _sensor)
{
for (cpShape* shape : _cpShapes)
{
cpShapeSetSensor(shape, sensor);
}
_sensor = sensor;
}
}
void PhysicsShape::recenterPoints(Vec2* points, int count, const Vec2& center)
{
2021-12-25 10:04:45 +08:00
cpVect* cpvs = new cpVect[count];
2019-11-23 20:27:39 +08:00
cpVect centroid = cpCentroidForPoly(count, cpvs);
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; i++)
{
2019-11-23 20:27:39 +08:00
cpvs[i] = cpvsub(cpvs[i], centroid);
}
PhysicsHelper::cpvs2points(cpvs, points, count);
delete[] cpvs;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (center != Vec2::ZERO)
{
for (int i = 0; i < count; ++i)
{
points[i] += center;
}
}
}
Vec2 PhysicsShape::getPolygonCenter(const Vec2* points, int count)
{
2021-12-25 10:04:45 +08:00
cpVect* cpvs = new cpVect[count];
2019-11-23 20:27:39 +08:00
cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
delete[] cpvs;
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(center);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void PhysicsShape::setBody(PhysicsBody* body)
2019-11-23 20:27:39 +08:00
{
// already added
if (body && _body == body)
{
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_body)
{
_body->removeShape(this);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (auto shape : _cpShapes)
{
cpShapeSetBody(shape, body == nullptr ? s_sharedBody : body->_cpBody);
}
_body = body;
}
// PhysicsShapeCircle
2021-12-25 10:04:45 +08:00
PhysicsShapeCircle* PhysicsShapeCircle::create(float radius,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /* = Vec2(0, 0)*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeCircle* shape = new PhysicsShapeCircle();
if (shape->init(radius, material, offset))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeCircle::init(float radius,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /*= Vec2(0, 0)*/)
2019-11-23 20:27:39 +08:00
{
do
{
_type = Type::CIRCLE;
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
auto shape = cpCircleShapeNew(s_sharedBody, radius, PhysicsHelper::vec22cpv(offset));
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addShape(shape);
2021-12-25 10:04:45 +08:00
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
2019-11-23 20:27:39 +08:00
_moment = calculateDefaultMoment();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
float PhysicsShapeCircle::calculateArea(float radius)
{
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
}
float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Vec2& offset)
{
2021-12-25 10:04:45 +08:00
return mass == PHYSICS_INFINITY
? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(mass, 0, radius, PhysicsHelper::vec22cpv(offset)));
2019-11-23 20:27:39 +08:00
}
float PhysicsShapeCircle::calculateArea()
{
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_cpShapes.front())));
}
float PhysicsShapeCircle::calculateDefaultMoment()
{
auto shape = _cpShapes.front();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
2021-12-25 10:04:45 +08:00
: PhysicsHelper::cpfloat2float(cpMomentForCircle(
_mass, 0, cpCircleShapeGetRadius(shape), cpCircleShapeGetOffset(shape)));
2019-11-23 20:27:39 +08:00
}
float PhysicsShapeCircle::getRadius() const
{
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_cpShapes.front()));
}
Vec2 PhysicsShapeCircle::getOffset()
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpCircleShapeGetOffset(_cpShapes.front()));
2019-11-23 20:27:39 +08:00
}
void PhysicsShapeCircle::updateScale()
{
cpFloat factor = std::abs(_newScaleX / _scaleX);
cpShape* shape = _cpShapes.front();
2021-12-25 10:04:45 +08:00
cpVect v = cpCircleShapeGetOffset(shape);
v = cpvmult(v, factor);
2019-11-23 20:27:39 +08:00
cpCircleShapeSetOffset(shape, v);
cpCircleShapeSetRadius(shape, cpCircleShapeGetRadius(shape) * factor);
PhysicsShape::updateScale();
}
// PhysicsShapeEdgeSegment
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(const Vec2& a,
const Vec2& b,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment();
if (shape->init(a, b, material, border))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeEdgeSegment::init(const Vec2& a,
const Vec2& b,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
do
{
_type = Type::EDGESEGMENT;
2021-12-25 10:04:45 +08:00
auto shape = cpSegmentShapeNew(s_sharedBody, PhysicsHelper::vec22cpv(a), PhysicsHelper::vec22cpv(b), border);
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addShape(shape);
2021-12-25 10:04:45 +08:00
_mass = PHYSICS_INFINITY;
2019-11-23 20:27:39 +08:00
_moment = PHYSICS_INFINITY;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
Vec2 PhysicsShapeEdgeSegment::getPointA() const
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(_cpShapes.front()));
2019-11-23 20:27:39 +08:00
}
Vec2 PhysicsShapeEdgeSegment::getPointB() const
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpSegmentShapeGetB(_cpShapes.front()));
2019-11-23 20:27:39 +08:00
}
Vec2 PhysicsShapeEdgeSegment::getCenter()
{
2021-10-24 14:09:59 +08:00
auto a = PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(_cpShapes.front()));
auto b = PhysicsHelper::cpv2vec2(cpSegmentShapeGetB(_cpShapes.front()));
2021-12-25 10:04:45 +08:00
return (a + b) / 2;
2019-11-23 20:27:39 +08:00
}
void PhysicsShapeEdgeSegment::updateScale()
{
cpFloat factorX = _newScaleX / _scaleX;
cpFloat factorY = _newScaleY / _scaleY;
cpShape* shape = _cpShapes.front();
2021-12-25 10:04:45 +08:00
cpVect a = cpSegmentShapeGetA(shape);
2019-11-23 20:27:39 +08:00
a.x *= factorX;
a.y *= factorY;
cpVect b = cpSegmentShapeGetB(shape);
b.x *= factorX;
b.y *= factorY;
cpSegmentShapeSetEndpoints(shape, a, b);
PhysicsShape::updateScale();
}
// PhysicsShapeBox
2021-12-25 10:04:45 +08:00
PhysicsShapeBox* PhysicsShapeBox::create(const Vec2& size,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /* = Vec2(0, 0)*/,
float radius /* = 0.0f*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeBox* shape = new PhysicsShapeBox();
if (shape->init(size, material, offset, radius))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeBox::init(const Vec2& size,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /*= Vec2(0, 0)*/,
float radius /* = 0.0f*/)
2019-11-23 20:27:39 +08:00
{
do
{
_type = Type::BOX;
2021-12-25 10:04:45 +08:00
auto wh = PhysicsHelper::vec22cpv(size);
cpVect vec[4] = {{-wh.x / 2.0f, -wh.y / 2.0f},
{-wh.x / 2.0f, wh.y / 2.0f},
{wh.x / 2.0f, wh.y / 2.0f},
{wh.x / 2.0f, -wh.y / 2.0f}};
2021-10-24 14:09:59 +08:00
cpTransform transform = cpTransformTranslate(PhysicsHelper::vec22cpv(offset));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto shape = cpPolyShapeNew(s_sharedBody, 4, vec, transform, radius);
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addShape(shape);
2021-12-25 10:04:45 +08:00
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
2019-11-23 20:27:39 +08:00
_moment = calculateDefaultMoment();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
2021-10-23 23:27:14 +08:00
Vec2 PhysicsShapeBox::getSize() const
2019-11-23 20:27:39 +08:00
{
cpShape* shape = _cpShapes.front();
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpv(cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2)),
2019-11-23 20:27:39 +08:00
cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1))));
}
// PhysicsShapePolygon
2021-12-25 10:04:45 +08:00
PhysicsShapePolygon* PhysicsShapePolygon::create(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /* = Vec2(0, 0)*/,
float radius /* = 0.0f*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapePolygon* shape = new PhysicsShapePolygon();
if (shape->init(points, count, material, offset, radius))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapePolygon::init(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
const Vec2& offset /* = Vec2(0, 0)*/,
float radius /* = 0.0f*/)
2019-11-23 20:27:39 +08:00
{
do
{
_type = Type::POLYGON;
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
auto vecs = new cpVect[count];
2021-12-25 10:04:45 +08:00
PhysicsHelper::points2cpvs(points, vecs, count); // count = cpConvexHull((int)count, vecs, nullptr, nullptr,
// 0);
2021-10-24 14:09:59 +08:00
cpTransform transform = cpTransformTranslate(PhysicsHelper::vec22cpv(offset));
2021-12-25 10:04:45 +08:00
auto shape = cpPolyShapeNew(s_sharedBody, count, vecs, transform, radius);
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vecs);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
addShape(shape);
2021-12-25 10:04:45 +08:00
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
2019-11-23 20:27:39 +08:00
_moment = calculateDefaultMoment();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
float PhysicsShapePolygon::calculateArea(const Vec2* points, int count)
{
2021-12-08 00:11:53 +08:00
cpVect* vecs = new cpVect[count];
2019-11-23 20:27:39 +08:00
PhysicsHelper::points2cpvs(points, vecs, count);
float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, 0.0f));
CC_SAFE_DELETE_ARRAY(vecs);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return area;
}
float PhysicsShapePolygon::calculateMoment(float mass, const Vec2* points, int count, const Vec2& offset, float radius)
{
2021-12-08 00:11:53 +08:00
cpVect* vecs = new cpVect[count];
2019-11-23 20:27:39 +08:00
PhysicsHelper::points2cpvs(points, vecs, count);
2021-12-25 10:04:45 +08:00
float moment =
mass == PHYSICS_INFINITY
? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(mass, count, vecs, PhysicsHelper::vec22cpv(offset), radius));
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vecs);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return moment;
}
float PhysicsShapePolygon::calculateArea()
{
2021-12-25 10:04:45 +08:00
auto shape = _cpShapes.front();
int count = cpPolyShapeGetCount(shape);
2019-11-23 20:27:39 +08:00
cpVect* vecs = new cpVect[count];
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; ++i)
2019-11-23 20:27:39 +08:00
vecs[i] = cpPolyShapeGetVert(shape, i);
float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, cpPolyShapeGetRadius(shape)));
CC_SAFE_DELETE_ARRAY(vecs);
return area;
}
float PhysicsShapePolygon::calculateDefaultMoment()
{
2021-12-25 10:04:45 +08:00
if (_mass == PHYSICS_INFINITY)
2019-11-23 20:27:39 +08:00
{
return PHYSICS_INFINITY;
}
else
{
2021-12-25 10:04:45 +08:00
auto shape = _cpShapes.front();
int count = cpPolyShapeGetCount(shape);
2019-11-23 20:27:39 +08:00
cpVect* vecs = new cpVect[count];
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; ++i)
2019-11-23 20:27:39 +08:00
vecs[i] = cpPolyShapeGetVert(shape, i);
2021-12-25 10:04:45 +08:00
float moment =
PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, count, vecs, cpvzero, cpPolyShapeGetRadius(shape)));
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vecs);
return moment;
}
}
Vec2 PhysicsShapePolygon::getPoint(int i) const
{
2021-10-24 14:09:59 +08:00
return PhysicsHelper::cpv2vec2(cpPolyShapeGetVert(_cpShapes.front(), i));
2019-11-23 20:27:39 +08:00
}
void PhysicsShapePolygon::getPoints(Vec2* outPoints) const
{
2021-12-25 10:04:45 +08:00
auto shape = _cpShapes.front();
int count = cpPolyShapeGetCount(shape);
2019-11-23 20:27:39 +08:00
cpVect* vecs = new cpVect[count];
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; ++i)
2019-11-23 20:27:39 +08:00
vecs[i] = cpPolyShapeGetVert(shape, i);
PhysicsHelper::cpvs2points(vecs, outPoints, count);
CC_SAFE_DELETE_ARRAY(vecs);
}
int PhysicsShapePolygon::getPointsCount() const
{
return cpPolyShapeGetCount(_cpShapes.front());
}
Vec2 PhysicsShapePolygon::getCenter()
{
2021-12-25 10:04:45 +08:00
auto shape = _cpShapes.front();
int count = cpPolyShapeGetCount(shape);
2019-11-23 20:27:39 +08:00
cpVect* vecs = new cpVect[count];
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; ++i)
2019-11-23 20:27:39 +08:00
vecs[i] = cpPolyShapeGetVert(shape, i);
2021-12-25 10:04:45 +08:00
Vec2 center = PhysicsHelper::cpv2vec2(cpCentroidForPoly(count, vecs));
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vecs);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return center;
}
void PhysicsShapePolygon::updateScale()
{
cpFloat factorX = _newScaleX / _scaleX;
cpFloat factorY = _newScaleY / _scaleY;
2021-12-25 10:04:45 +08:00
auto shape = _cpShapes.front();
int count = cpPolyShapeGetCount(shape);
2019-11-23 20:27:39 +08:00
cpVect* vects = new cpVect[count];
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; ++i)
2019-11-23 20:27:39 +08:00
vects[i] = cpPolyShapeGetVert(shape, i);
for (int i = 0; i < count; ++i)
{
vects[i].x *= factorX;
vects[i].y *= factorY;
}
// convert hole to clockwise
if (factorX * factorY < 0)
{
for (int i = 0; i < count / 2; ++i)
{
2021-12-25 10:04:45 +08:00
cpVect v = vects[i];
vects[i] = vects[count - i - 1];
2019-11-23 20:27:39 +08:00
vects[count - i - 1] = v;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
cpPolyShapeSetVertsRaw(shape, count, vects);
CC_SAFE_DELETE_ARRAY(vects);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
PhysicsShape::updateScale();
}
// PhysicsShapeEdgeBox
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(const Vec2& size,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/,
const Vec2& offset /* = Vec2(0, 0)*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox();
if (shape->init(size, material, border, offset))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeEdgeBox::init(const Vec2& size,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/,
const Vec2& offset /*= Vec2(0, 0)*/)
2019-11-23 20:27:39 +08:00
{
do
{
_type = Type::EDGEBOX;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
cpVect vec[4] = {};
2021-12-25 10:04:45 +08:00
vec[0] = PhysicsHelper::vec22cpv(Vec2(-size.width / 2 + offset.x, -size.height / 2 + offset.y));
vec[1] = PhysicsHelper::vec22cpv(Vec2(+size.width / 2 + offset.x, -size.height / 2 + offset.y));
vec[2] = PhysicsHelper::vec22cpv(Vec2(+size.width / 2 + offset.x, +size.height / 2 + offset.y));
vec[3] = PhysicsHelper::vec22cpv(Vec2(-size.width / 2 + offset.x, +size.height / 2 + offset.y));
2019-11-23 20:27:39 +08:00
int i = 0;
for (; i < 4; ++i)
{
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % 4], border);
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
addShape(shape);
}
CC_BREAK_IF(i < 4);
2021-12-25 10:04:45 +08:00
_mass = PHYSICS_INFINITY;
2019-11-23 20:27:39 +08:00
_moment = PHYSICS_INFINITY;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
// PhysicsShapeEdgeBox
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon();
if (shape->init(points, count, material, border))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeEdgePolygon::init(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
cpVect* vec = nullptr;
do
{
_type = Type::EDGEPOLYGON;
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
vec = new cpVect[count];
2019-11-23 20:27:39 +08:00
PhysicsHelper::points2cpvs(points, vec, count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int i = 0;
for (; i < count; ++i)
{
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % count], border);
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
addShape(shape);
}
CC_SAFE_DELETE_ARRAY(vec);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_BREAK_IF(i < count);
2021-12-25 10:04:45 +08:00
_mass = PHYSICS_INFINITY;
2019-11-23 20:27:39 +08:00
_moment = PHYSICS_INFINITY;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vec);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
Vec2 PhysicsShapeEdgePolygon::getCenter()
{
2021-12-25 10:04:45 +08:00
int count = (int)_cpShapes.size();
2021-12-08 00:11:53 +08:00
cpVect* points = new cpVect[count];
2021-12-25 10:04:45 +08:00
int i = 0;
for (auto shape : _cpShapes)
2019-11-23 20:27:39 +08:00
{
points[i++] = cpSegmentShapeGetA(shape);
}
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
Vec2 center = PhysicsHelper::cpv2vec2(cpCentroidForPoly(count, points));
2019-11-23 20:27:39 +08:00
delete[] points;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return center;
}
2021-12-25 10:04:45 +08:00
void PhysicsShapeEdgePolygon::getPoints(cocos2d::Vec2* outPoints) const
2019-11-23 20:27:39 +08:00
{
int i = 0;
2021-12-25 10:04:45 +08:00
for (auto shape : _cpShapes)
2019-11-23 20:27:39 +08:00
{
2021-10-24 14:09:59 +08:00
outPoints[i++] = PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(shape));
2019-11-23 20:27:39 +08:00
}
}
int PhysicsShapeEdgePolygon::getPointsCount() const
{
return static_cast<int>(_cpShapes.size());
}
// PhysicsShapeEdgeChain
2021-12-25 10:04:45 +08:00
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain();
if (shape->init(points, count, material, border))
2019-11-23 20:27:39 +08:00
{
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE(shape);
return nullptr;
}
void PhysicsShapeEdgePolygon::updateScale()
{
cpFloat factorX = _newScaleX / _scaleX;
cpFloat factorY = _newScaleY / _scaleY;
for (auto shape : _cpShapes)
{
cpVect a = cpSegmentShapeGetA(shape);
a.x *= factorX;
a.y *= factorY;
cpVect b = cpSegmentShapeGetB(shape);
b.x *= factorX;
b.y *= factorY;
cpSegmentShapeSetEndpoints(shape, a, b);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
PhysicsShape::updateScale();
}
2021-12-25 10:04:45 +08:00
bool PhysicsShapeEdgeChain::init(const Vec2* points,
int count,
const PhysicsMaterial& material /* = MaterialDefault*/,
float border /* = 1*/)
2019-11-23 20:27:39 +08:00
{
cpVect* vec = nullptr;
do
{
_type = Type::EDGECHAIN;
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
vec = new cpVect[count];
2019-11-23 20:27:39 +08:00
PhysicsHelper::points2cpvs(points, vec, count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int i = 0;
for (; i < count - 1; ++i)
{
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[i + 1], border);
CC_BREAK_IF(shape == nullptr);
cpShapeSetUserData(shape, this);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
addShape(shape);
}
CC_SAFE_DELETE_ARRAY(vec);
CC_BREAK_IF(i < count - 1);
2021-12-25 10:04:45 +08:00
_mass = PHYSICS_INFINITY;
2019-11-23 20:27:39 +08:00
_moment = PHYSICS_INFINITY;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setMaterial(material);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
} while (false);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_SAFE_DELETE_ARRAY(vec);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
Vec2 PhysicsShapeEdgeChain::getCenter()
{
2021-12-25 10:04:45 +08:00
int count = (int)_cpShapes.size() + 1;
2021-12-08 00:11:53 +08:00
cpVect* points = new cpVect[count];
2021-12-25 10:04:45 +08:00
int i = 0;
for (auto shape : _cpShapes)
2019-11-23 20:27:39 +08:00
{
points[i++] = cpSegmentShapeGetA(shape);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
points[i++] = cpSegmentShapeGetB(_cpShapes.back());
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
Vec2 center = PhysicsHelper::cpv2vec2(cpCentroidForPoly(count, points));
2019-11-23 20:27:39 +08:00
delete[] points;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return center;
}
void PhysicsShapeEdgeChain::getPoints(Vec2* outPoints) const
{
int i = 0;
2021-12-25 10:04:45 +08:00
for (auto shape : _cpShapes)
2019-11-23 20:27:39 +08:00
{
2021-10-24 14:09:59 +08:00
outPoints[i++] = PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(shape));
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2021-10-24 14:09:59 +08:00
outPoints[i++] = PhysicsHelper::cpv2vec2(cpSegmentShapeGetB(_cpShapes.back()));
2019-11-23 20:27:39 +08:00
}
int PhysicsShapeEdgeChain::getPointsCount() const
{
return static_cast<int>(_cpShapes.size() + 1);
}
void PhysicsShapeEdgeChain::updateScale()
{
cpFloat factorX = _newScaleX / _scaleX;
cpFloat factorY = _newScaleY / _scaleY;
for (auto shape : _cpShapes)
{
cpVect a = cpSegmentShapeGetA(shape);
a.x *= factorX;
a.y *= factorY;
cpVect b = cpSegmentShapeGetB(shape);
b.x *= factorX;
b.y *= factorY;
cpSegmentShapeSetEndpoints(shape, a, b);
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
PhysicsShape::updateScale();
}
void PhysicsShape::setGroup(int group)
{
if (group < 0)
{
for (auto shape : _cpShapes)
{
cpShapeSetFilter(shape, cpShapeFilterNew(group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_group = group;
}
bool PhysicsShape::containsPoint(const Vec2& point) const
{
for (auto shape : _cpShapes)
{
2021-10-24 14:09:59 +08:00
if (cpShapePointQuery(shape, PhysicsHelper::vec22cpv(point), nullptr) < 0)
2019-11-23 20:27:39 +08:00
{
return true;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
NS_CC_END
2021-12-25 10:04:45 +08:00
#endif // CC_USE_PHYSICS