2013-09-16 21:22:22 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +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-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +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
|
|
|
|
2013-09-16 21:22:22 +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
|
|
|
|
2013-09-16 21:22:22 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "physics/CCPhysicsShape.h"
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_USE_PHYSICS
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <climits>
|
|
|
|
# include <cmath>
|
|
|
|
# include <unordered_map>
|
2013-11-06 15:43:29 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "chipmunk/chipmunk.h"
|
|
|
|
# include "chipmunk/chipmunk_unsafe.h"
|
2013-09-17 17:39:08 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "physics/CCPhysicsBody.h"
|
|
|
|
# include "physics/CCPhysicsWorld.h"
|
|
|
|
# include "physics/CCPhysicsHelper.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-11-27 17:33:33 +08:00
|
|
|
extern const float PHYSICS_INFINITY;
|
2015-01-06 10:29:07 +08:00
|
|
|
static cpBody* s_sharedBody = nullptr;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
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)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
if (s_sharedBody == nullptr)
|
|
|
|
{
|
|
|
|
s_sharedBody = cpBodyNewStatic();
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShape::~PhysicsShape()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2015-01-06 10:29:07 +08:00
|
|
|
{
|
|
|
|
cpShapeFree(shape);
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
void PhysicsShape::setMass(float mass)
|
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
if (mass < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMass(-_mass);
|
|
|
|
_body->addMass(mass);
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
_mass = mass;
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
void PhysicsShape::setMoment(float moment)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
if (moment < 0)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
return;
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMoment(-_moment);
|
|
|
|
_body->addMoment(moment);
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
_moment = moment;
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
void PhysicsShape::setMaterial(const PhysicsMaterial& material)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
setDensity(material.density);
|
|
|
|
setRestitution(material.restitution);
|
|
|
|
setFriction(material.friction);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 17:30:21 +08:00
|
|
|
void PhysicsShape::setScale(float scaleX, float scaleY)
|
|
|
|
{
|
2016-07-12 03:13:56 +08:00
|
|
|
if (std::abs(_scaleX - scaleX) > FLT_EPSILON || std::abs(_scaleY - scaleY) > FLT_EPSILON)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
if (_type == Type::CIRCLE && scaleX != scaleY)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("PhysicsShapeCircle WARNING: CANNOT support setScale with different x and y");
|
2015-01-07 17:08:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_newScaleX = scaleX;
|
|
|
|
_newScaleY = scaleY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
updateScale();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-08 09:54:01 +08:00
|
|
|
// re-calculate area and mass
|
2021-12-25 10:04:45 +08:00
|
|
|
_area = calculateArea();
|
|
|
|
_mass = _material.density * _area;
|
2015-09-08 09:54:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShape::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
_scaleX = _newScaleX;
|
|
|
|
_scaleY = _newScaleY;
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
void PhysicsShape::addShape(cpShape* shape)
|
|
|
|
{
|
|
|
|
if (shape)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
|
|
|
cpShapeSetFilter(shape, cpShapeFilterNew(_group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
|
2022-08-08 13:18:33 +08:00
|
|
|
_cpShapes.emplace_back(shape);
|
2015-01-06 10:29:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeCircle::PhysicsShapeCircle() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeCircle::~PhysicsShapeCircle() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeBox::PhysicsShapeBox() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeBox::~PhysicsShapeBox() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapePolygon::PhysicsShapePolygon() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapePolygon::~PhysicsShapePolygon() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeBox::PhysicsShapeEdgeBox() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeBox::~PhysicsShapeEdgeBox() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeChain::PhysicsShapeEdgeChain() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeChain::~PhysicsShapeEdgeChain() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgePolygon::PhysicsShapeEdgePolygon() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgePolygon::~PhysicsShapeEdgePolygon() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeSegment::PhysicsShapeEdgeSegment() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment() {}
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
void PhysicsShape::setDensity(float density)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
if (density < 0)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
_material.density = density;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
if (_material.density == PHYSICS_INFINITY)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
setMass(PHYSICS_INFINITY);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (_area > 0)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
setMass(_material.density * _area);
|
2013-10-09 17:53:12 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-09 13:41:19 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
void PhysicsShape::setRestitution(float restitution)
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
_material.restitution = restitution;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
for (cpShape* shape : _cpShapes)
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpShapeSetElasticity(shape, restitution);
|
2013-10-09 13:41:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsShape::setFriction(float friction)
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
_material.friction = friction;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
for (cpShape* shape : _cpShapes)
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpShapeSetFriction(shape, friction);
|
2013-10-09 13:41:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 17:03:01 +08:00
|
|
|
void PhysicsShape::setSensor(bool sensor)
|
|
|
|
{
|
|
|
|
if (sensor != _sensor)
|
|
|
|
{
|
|
|
|
for (cpShape* shape : _cpShapes)
|
|
|
|
{
|
|
|
|
cpShapeSetSensor(shape, sensor);
|
|
|
|
}
|
|
|
|
_sensor = sensor;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 13:48:01 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsShape::recenterPoints(Vec2* points, int count, const Vec2& center)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect* cpvs = new cpVect[count];
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect centroid = cpCentroidForPoly(count, cpvs);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpvs[i] = cpvsub(cpvs[i], centroid);
|
|
|
|
}
|
2013-10-15 13:48:01 +08:00
|
|
|
PhysicsHelper::cpvs2points(cpvs, points, count);
|
|
|
|
delete[] cpvs;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
if (center != Vec2::ZERO)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
points[i] += center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 07:24:22 +08:00
|
|
|
Vec2 PhysicsShape::getPolygonCenter(const Vec2* points, int count)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect* cpvs = new cpVect[count];
|
2013-10-15 13:48:01 +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);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PhysicsShape::setBody(PhysicsBody* body)
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
|
|
|
// already added
|
2015-01-06 10:29:07 +08:00
|
|
|
if (body && _body == body)
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
if (_body)
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
|
|
|
_body->removeShape(this);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpShapeSetBody(shape, body == nullptr ? s_sharedBody : body->_cpBody);
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
2015-01-06 10:29:07 +08:00
|
|
|
_body = body;
|
2013-10-17 10:57:48 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeCircle
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeCircle* PhysicsShapeCircle::create(float radius,
|
|
|
|
const PhysicsMaterial& material /* = MaterialDefault*/,
|
|
|
|
const Vec2& offset /* = Vec2(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeCircle* shape = new PhysicsShapeCircle();
|
|
|
|
if (shape->init(radius, material, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
_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));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
addShape(shape);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_area = calculateArea();
|
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
float PhysicsShapeCircle::calculateArea(float radius)
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Vec2& offset)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return mass == PHYSICS_INFINITY
|
|
|
|
? PHYSICS_INFINITY
|
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForCircle(mass, 0, radius, PhysicsHelper::vec22cpv(offset)));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
float PhysicsShapeCircle::calculateArea()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_cpShapes.front())));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsShapeCircle::calculateDefaultMoment()
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
auto shape = _cpShapes.front();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-27 17:33:33 +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)));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsShapeCircle::getRadius() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_cpShapes.front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeCircle::getOffset()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpCircleShapeGetOffset(_cpShapes.front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShapeCircle::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpFloat factor = std::abs(_newScaleX / _scaleX);
|
2014-06-27 17:30:21 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
cpShape* shape = _cpShapes.front();
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect v = cpCircleShapeGetOffset(shape);
|
|
|
|
v = cpvmult(v, factor);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpCircleShapeSetOffset(shape, v);
|
2014-06-27 17:30:21 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
cpCircleShapeSetRadius(shape, cpCircleShapeGetRadius(shape) * factor);
|
2014-06-27 17:30:21 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsShape::updateScale();
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// 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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment();
|
|
|
|
if (shape->init(a, b, material, border))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
_type = Type::EDGESEGMENT;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto shape = cpSegmentShapeNew(s_sharedBody, PhysicsHelper::vec22cpv(a), PhysicsHelper::vec22cpv(b), border);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
addShape(shape);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-11-27 17:33:33 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeEdgeSegment::getPointA() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(_cpShapes.front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeEdgeSegment::getPointB() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpSegmentShapeGetB(_cpShapes.front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeEdgeSegment::getCenter()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
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;
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShapeEdgeSegment::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpFloat factorX = _newScaleX / _scaleX;
|
|
|
|
cpFloat factorY = _newScaleY / _scaleY;
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
cpShape* shape = _cpShapes.front();
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect a = cpSegmentShapeGetA(shape);
|
2015-01-07 17:08:04 +08:00
|
|
|
a.x *= factorX;
|
|
|
|
a.y *= factorY;
|
|
|
|
cpVect b = cpSegmentShapeGetB(shape);
|
|
|
|
b.x *= factorX;
|
|
|
|
b.y *= factorY;
|
|
|
|
cpSegmentShapeSetEndpoints(shape, a, b);
|
|
|
|
|
|
|
|
PhysicsShape::updateScale();
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// 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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeBox* shape = new PhysicsShapeBox();
|
|
|
|
if (shape->init(size, material, offset, radius))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
_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
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
auto shape = cpPolyShapeNew(s_sharedBody, 4, vec, transform, radius);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
addShape(shape);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_area = calculateArea();
|
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 PhysicsShapeBox::getSize() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +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)),
|
2014-03-13 20:56:51 +08:00
|
|
|
cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1))));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapePolygon* shape = new PhysicsShapePolygon();
|
|
|
|
if (shape->init(points, count, material, offset, radius))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2016-07-18 18:10:07 +08:00
|
|
|
_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);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
addShape(shape);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_area = calculateArea();
|
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float PhysicsShapePolygon::calculateArea(const Vec2* points, int count)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2013-10-15 13:48:01 +08:00
|
|
|
PhysicsHelper::points2cpvs(points, vecs, count);
|
2016-03-01 05:53:00 +08:00
|
|
|
float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, 0.0f));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
float PhysicsShapePolygon::calculateMoment(float mass, const Vec2* points, int count, const Vec2& offset, float radius)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2013-10-15 13:48:01 +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));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
return moment;
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
float PhysicsShapePolygon::calculateArea()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto shape = _cpShapes.front();
|
|
|
|
int count = cpPolyShapeGetCount(shape);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 05:53:00 +08:00
|
|
|
vecs[i] = cpPolyShapeGetVert(shape, i);
|
|
|
|
float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs, cpPolyShapeGetRadius(shape)));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2016-03-01 05:53:00 +08:00
|
|
|
return area;
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsShapePolygon::calculateDefaultMoment()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_mass == PHYSICS_INFINITY)
|
2016-03-01 05:53:00 +08:00
|
|
|
{
|
|
|
|
return PHYSICS_INFINITY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto shape = _cpShapes.front();
|
|
|
|
int count = cpPolyShapeGetCount(shape);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 05:53:00 +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)));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2016-03-01 05:53:00 +08:00
|
|
|
return moment;
|
|
|
|
}
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapePolygon::getPoint(int i) const
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpPolyShapeGetVert(_cpShapes.front(), i));
|
2013-11-05 15:54:33 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsShapePolygon::getPoints(Vec2* outPoints) const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto shape = _cpShapes.front();
|
|
|
|
int count = cpPolyShapeGetCount(shape);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 05:53:00 +08:00
|
|
|
vecs[i] = cpPolyShapeGetVert(shape, i);
|
|
|
|
PhysicsHelper::cpvs2points(vecs, outPoints, count);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapePolygon::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return cpPolyShapeGetCount(_cpShapes.front());
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapePolygon::getCenter()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto shape = _cpShapes.front();
|
|
|
|
int count = cpPolyShapeGetCount(shape);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect* vecs = new cpVect[count];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 05:53:00 +08:00
|
|
|
vecs[i] = cpPolyShapeGetVert(shape, i);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Vec2 center = PhysicsHelper::cpv2vec2(cpCentroidForPoly(count, vecs));
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vecs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
return center;
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShapePolygon::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpFloat factorX = _newScaleX / _scaleX;
|
|
|
|
cpFloat factorY = _newScaleY / _scaleY;
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto shape = _cpShapes.front();
|
|
|
|
int count = cpPolyShapeGetCount(shape);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect* vects = new cpVect[count];
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 05:53:00 +08:00
|
|
|
vects[i] = cpPolyShapeGetVert(shape, i);
|
2015-01-07 17:08:04 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
vects[i].x *= factorX;
|
|
|
|
vects[i].y *= factorY;
|
|
|
|
}
|
2014-06-27 17:30:21 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
// convert hole to clockwise
|
|
|
|
if (factorX * factorY < 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count / 2; ++i)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cpVect v = vects[i];
|
|
|
|
vects[i] = vects[count - i - 1];
|
2015-01-07 17:08:04 +08:00
|
|
|
vects[count - i - 1] = v;
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
cpPolyShapeSetVertsRaw(shape, count, vects);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vects);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsShape::updateScale();
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// 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)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox();
|
|
|
|
if (shape->init(size, material, border, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
_type = Type::EDGEBOX;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +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));
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
int i = 0;
|
|
|
|
for (; i < 4; ++i)
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % 4], border);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2015-01-06 10:29:07 +08:00
|
|
|
addShape(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(i < 4);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-11-27 17:33:33 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon();
|
|
|
|
if (shape->init(points, count, material, border))
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
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*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2016-07-18 18:10:07 +08:00
|
|
|
_type = Type::EDGEPOLYGON;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
vec = new cpVect[count];
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
int i = 0;
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % count], border);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2015-01-06 10:29:07 +08:00
|
|
|
cpShapeSetElasticity(shape, 1.0f);
|
|
|
|
cpShapeSetFriction(shape, 1.0f);
|
|
|
|
addShape(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vec);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(i < count);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-11-27 17:33:33 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vec);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeEdgePolygon::getCenter()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
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;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
points[i++] = cpSegmentShapeGetA(shape);
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-24 14:09:59 +08:00
|
|
|
Vec2 center = PhysicsHelper::cpv2vec2(cpCentroidForPoly(count, points));
|
2014-06-27 17:30:21 +08:00
|
|
|
delete[] points;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-27 17:30:21 +08:00
|
|
|
return center;
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void PhysicsShapeEdgePolygon::getPoints(ax::Vec2* outPoints) const
|
2014-01-02 11:45:11 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-01-02 11:45:11 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
outPoints[i++] = PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(shape));
|
2014-01-02 11:45:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapeEdgePolygon::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return static_cast<int>(_cpShapes.size());
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeEdgeChain
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(const Vec2* points,
|
|
|
|
int count,
|
|
|
|
const PhysicsMaterial& material /* = MaterialDefault*/,
|
|
|
|
float border /* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain();
|
|
|
|
if (shape->init(points, count, material, border))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShapeEdgePolygon::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpFloat factorX = _newScaleX / _scaleX;
|
|
|
|
cpFloat factorY = _newScaleY / _scaleY;
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
cpVect a = cpSegmentShapeGetA(shape);
|
|
|
|
a.x *= factorX;
|
|
|
|
a.y *= factorY;
|
|
|
|
cpVect b = cpSegmentShapeGetB(shape);
|
|
|
|
b.x *= factorX;
|
|
|
|
b.y *= factorY;
|
|
|
|
cpSegmentShapeSetEndpoints(shape, a, b);
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsShape::updateScale();
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool PhysicsShapeEdgeChain::init(const Vec2* points,
|
|
|
|
int count,
|
|
|
|
const PhysicsMaterial& material /* = MaterialDefault*/,
|
|
|
|
float border /* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
_type = Type::EDGECHAIN;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
vec = new cpVect[count];
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
int i = 0;
|
|
|
|
for (; i < count - 1; ++i)
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[i + 1], border);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(shape == nullptr);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetUserData(shape, this);
|
2015-01-06 10:29:07 +08:00
|
|
|
cpShapeSetElasticity(shape, 1.0f);
|
|
|
|
cpShapeSetFriction(shape, 1.0f);
|
|
|
|
addShape(shape);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vec);
|
|
|
|
AX_BREAK_IF(i < count - 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-11-27 17:33:33 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE_ARRAY(vec);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsShapeEdgeChain::getCenter()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
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;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
points[i++] = cpSegmentShapeGetA(shape);
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-01 05:53:00 +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));
|
2014-06-27 17:30:21 +08:00
|
|
|
delete[] points;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-27 17:30:21 +08:00
|
|
|
return center;
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsShapeEdgeChain::getPoints(Vec2* outPoints) const
|
2014-01-02 11:45:11 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-01-02 11:45:11 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
outPoints[i++] = PhysicsHelper::cpv2vec2(cpSegmentShapeGetA(shape));
|
2014-01-02 11:45:11 +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()));
|
2014-01-02 11:45:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapeEdgeChain::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
return static_cast<int>(_cpShapes.size() + 1);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
void PhysicsShapeEdgeChain::updateScale()
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpFloat factorX = _newScaleX / _scaleX;
|
|
|
|
cpFloat factorY = _newScaleY / _scaleY;
|
2015-01-07 17:08:04 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-01-07 17:08:04 +08:00
|
|
|
cpVect a = cpSegmentShapeGetA(shape);
|
|
|
|
a.x *= factorX;
|
|
|
|
a.y *= factorY;
|
|
|
|
cpVect b = cpSegmentShapeGetB(shape);
|
|
|
|
b.x *= factorX;
|
|
|
|
b.y *= factorY;
|
|
|
|
cpSegmentShapeSetEndpoints(shape, a, b);
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-07 17:08:04 +08:00
|
|
|
PhysicsShape::updateScale();
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsShape::setGroup(int group)
|
|
|
|
{
|
|
|
|
if (group < 0)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpShapeSetFilter(shape, cpShapeFilterNew(group, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-12 15:28:07 +08:00
|
|
|
_group = group;
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool PhysicsShape::containsPoint(const Vec2& point) const
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _cpShapes)
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
if (cpShapePointQuery(shape, PhysicsHelper::vec22cpv(point), nullptr) < 0)
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-05 15:54:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // AX_USE_PHYSICS
|