2013-09-09 10:29:02 +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-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-09 10:29:02 +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-09 10:29:02 +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-09 10:29:02 +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/CCPhysicsBody.h"
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_USE_PHYSICS
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <climits>
|
|
|
|
# include <algorithm>
|
|
|
|
# include <cmath>
|
2013-09-17 14:31:43 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "chipmunk/chipmunk_private.h"
|
2013-09-17 17:39:08 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "2d/CCScene.h"
|
|
|
|
# include "physics/CCPhysicsShape.h"
|
|
|
|
# include "physics/CCPhysicsJoint.h"
|
|
|
|
# include "physics/CCPhysicsWorld.h"
|
|
|
|
# include "physics/CCPhysicsHelper.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static void internalBodySetMass(cpBody* body, cpFloat mass)
|
2016-03-01 05:53:00 +08:00
|
|
|
{
|
|
|
|
cpBodyActivate(body);
|
2021-12-25 10:04:45 +08:00
|
|
|
body->m = mass;
|
|
|
|
body->m_inv = 1.0f / mass;
|
|
|
|
// cpAssertSaneBody(body);
|
2016-03-01 05:53:00 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static void internalBodyUpdateVelocity(cpBody* body, cpVect gravity, cpFloat damping, cpFloat dt)
|
2015-01-05 22:15:03 +08:00
|
|
|
{
|
|
|
|
cpBodyUpdateVelocity(body, cpvzero, damping, dt);
|
2016-03-01 05:53:00 +08:00
|
|
|
// Skip kinematic bodies.
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cpBodyGetType(body) == CP_BODY_TYPE_KINEMATIC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cpAssertSoft(body->m > 0.0f && body->i > 0.0f,
|
|
|
|
"Body's mass and moment must be positive to simulate. (Mass: %f Moment: f)", body->m, body->i);
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::PhysicsBody* physicsBody = static_cast<ax::PhysicsBody*>(body->userData);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (physicsBody->isGravityEnabled())
|
|
|
|
body->v =
|
|
|
|
cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt)),
|
|
|
|
physicsBody->getVelocityLimit());
|
2016-03-01 05:53:00 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
body->v = cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvmult(body->f, body->m_inv), dt)),
|
|
|
|
physicsBody->getVelocityLimit());
|
2016-03-01 05:53:00 +08:00
|
|
|
cpFloat w_limit = physicsBody->getAngularVelocityLimit();
|
2021-12-25 10:04:45 +08:00
|
|
|
body->w = cpfclamp(body->w * damping + body->t * body->i_inv * dt, -w_limit, w_limit);
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
// Reset forces.
|
|
|
|
body->f = cpvzero;
|
2021-12-25 10:04:45 +08:00
|
|
|
// to check body sanity
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetTorque(body, 0.0f);
|
2015-01-05 22:15:03 +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;
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
const std::string PhysicsBody::COMPONENT_NAME = "PhysicsBody";
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
namespace
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
static const float MASS_DEFAULT = 1.0;
|
|
|
|
static const float MOMENT_DEFAULT = 200;
|
|
|
|
} // namespace
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
PhysicsBody::PhysicsBody()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _world(nullptr)
|
|
|
|
, _cpBody(nullptr)
|
|
|
|
, _dynamic(true)
|
|
|
|
, _rotationEnabled(true)
|
|
|
|
, _gravityEnabled(true)
|
|
|
|
, _massDefault(true)
|
|
|
|
, _momentDefault(true)
|
|
|
|
, _mass(MASS_DEFAULT)
|
|
|
|
, _area(0.0f)
|
|
|
|
, _density(0.0f)
|
|
|
|
, _moment(MOMENT_DEFAULT)
|
|
|
|
, _velocityLimit(PHYSICS_INFINITY)
|
|
|
|
, _angularVelocityLimit(PHYSICS_INFINITY)
|
|
|
|
, _isDamping(false)
|
|
|
|
, _linearDamping(0.0f)
|
|
|
|
, _angularDamping(0.0f)
|
|
|
|
, _tag(0)
|
|
|
|
, _massSetByUser(false)
|
|
|
|
, _momentSetByUser(false)
|
|
|
|
, _rotationOffset(0)
|
|
|
|
, _recordedRotation(0.0f)
|
|
|
|
, _recordedAngle(0.0)
|
|
|
|
, _recordScaleX(1.f)
|
|
|
|
, _recordScaleY(1.f)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2015-11-27 21:33:20 +08:00
|
|
|
_name = COMPONENT_NAME;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBody::~PhysicsBody()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& joint : _joints)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-07 14:17:57 +08:00
|
|
|
PhysicsBody* other = joint->getBodyA() == this ? joint->getBodyB() : joint->getBodyA();
|
|
|
|
other->removeJoint(joint);
|
2013-09-16 21:22:22 +08:00
|
|
|
delete joint;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-01-06 10:29:07 +08:00
|
|
|
if (_cpBody)
|
|
|
|
{
|
|
|
|
cpBodyFree(_cpBody);
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-10-10 18:31:43 +08:00
|
|
|
PhysicsBody* PhysicsBody::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-10-10 18:31:43 +08:00
|
|
|
{
|
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-10-10 18:31:43 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
PhysicsBody* PhysicsBody::create(float mass)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-10-28 11:08:41 +08:00
|
|
|
if (body)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
body->_mass = mass;
|
2013-10-28 11:08:41 +08:00
|
|
|
body->_massDefault = false;
|
|
|
|
if (body->init())
|
|
|
|
{
|
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-10-28 11:08:41 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBody* PhysicsBody::create(float mass, float moment)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-10-28 11:08:41 +08:00
|
|
|
if (body)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
body->_mass = mass;
|
|
|
|
body->_massDefault = false;
|
|
|
|
body->_moment = moment;
|
2013-10-28 11:08:41 +08:00
|
|
|
body->_momentDefault = false;
|
|
|
|
if (body->init())
|
|
|
|
{
|
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-10-28 11:08:41 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
PhysicsBody* PhysicsBody::createCircle(float radius, const PhysicsMaterial& material, const Vec2& offset)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-05 15:54:33 +08:00
|
|
|
body->addShape(PhysicsShapeCircle::create(radius, material, offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
PhysicsBody* PhysicsBody::createBox(const Vec2& size, const PhysicsMaterial& material, const Vec2& offset)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-05 15:54:33 +08:00
|
|
|
body->addShape(PhysicsShapeBox::create(size, material, offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsBody* PhysicsBody::createPolygon(const Vec2* points,
|
|
|
|
int count,
|
|
|
|
const PhysicsMaterial& material,
|
|
|
|
const Vec2& offset)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-05 15:54:33 +08:00
|
|
|
body->addShape(PhysicsShapePolygon::create(points, count, material, offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeSegment(const Vec2& a,
|
|
|
|
const Vec2& b,
|
|
|
|
const PhysicsMaterial& material,
|
|
|
|
float border /* = 1*/)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeSegment::create(a, b, material, border));
|
2015-01-05 22:15:03 +08:00
|
|
|
body->setDynamic(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeBox(const Vec2& size,
|
|
|
|
const PhysicsMaterial& material,
|
|
|
|
float border /* = 1*/,
|
|
|
|
const Vec2& offset)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-11-05 15:54:33 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeBox::create(size, material, border, offset));
|
2015-01-05 22:15:03 +08:00
|
|
|
body->setDynamic(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2015-09-18 11:48:43 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgePolygon(const Vec2* points,
|
|
|
|
int count,
|
|
|
|
const PhysicsMaterial& material,
|
|
|
|
float border /* = 1*/)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgePolygon::create(points, count, material, border));
|
2015-01-05 22:15:03 +08:00
|
|
|
body->setDynamic(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
2013-09-10 17:36:49 +08:00
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeChain(const Vec2* points,
|
|
|
|
int count,
|
|
|
|
const PhysicsMaterial& material,
|
|
|
|
float border /* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeChain::create(points, count, material, border));
|
2015-01-05 22:15:03 +08:00
|
|
|
body->setDynamic(false);
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(body);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
bool PhysicsBody::init()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
_cpBody = cpBodyNew(_mass, _moment);
|
2016-03-01 05:53:00 +08:00
|
|
|
internalBodySetMass(_cpBody, _mass);
|
|
|
|
cpBodySetUserData(_cpBody, this);
|
|
|
|
cpBodySetVelocityUpdateFunc(_cpBody, internalBodyUpdateVelocity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(_cpBody == nullptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
void PhysicsBody::removeJoint(PhysicsJoint* joint)
|
|
|
|
{
|
|
|
|
auto it = std::find(_joints.begin(), _joints.end(), joint);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
if (it != _joints.end())
|
|
|
|
{
|
|
|
|
_joints.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
void PhysicsBody::setDynamic(bool dynamic)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-30 13:02:17 +08:00
|
|
|
if (dynamic != _dynamic)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-10-15 16:55:08 +08:00
|
|
|
_dynamic = dynamic;
|
2013-11-04 14:36:38 +08:00
|
|
|
if (dynamic)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetType(_cpBody, CP_BODY_TYPE_DYNAMIC);
|
|
|
|
internalBodySetMass(_cpBody, _mass);
|
|
|
|
cpBodySetMoment(_cpBody, _moment);
|
2013-11-19 18:30:12 +08:00
|
|
|
}
|
|
|
|
else
|
2013-11-04 14:36:38 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetType(_cpBody, CP_BODY_TYPE_KINEMATIC);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setRotationEnable(bool enable)
|
|
|
|
{
|
2014-03-20 10:21:59 +08:00
|
|
|
if (_rotationEnabled != enable)
|
2013-10-15 16:55:08 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpBodySetMoment(_cpBody, enable ? _moment : PHYSICS_INFINITY);
|
2014-03-20 10:21:59 +08:00
|
|
|
_rotationEnabled = enable;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setGravityEnable(bool enable)
|
|
|
|
{
|
2015-01-05 22:15:03 +08:00
|
|
|
_gravityEnabled = enable;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setRotation(float rotation)
|
|
|
|
{
|
2015-01-08 14:25:06 +08:00
|
|
|
_recordedRotation = rotation;
|
2021-12-25 10:04:45 +08:00
|
|
|
_recordedAngle = -(rotation + _rotationOffset) * (M_PI / 180.0);
|
2015-01-08 14:25:06 +08:00
|
|
|
cpBodySetAngle(_cpBody, _recordedAngle);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 17:30:21 +08:00
|
|
|
void PhysicsBody::setScale(float scaleX, float scaleY)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2014-06-27 17:30:21 +08:00
|
|
|
{
|
2015-09-08 09:54:01 +08:00
|
|
|
_area -= shape->getArea();
|
|
|
|
if (!_massSetByUser)
|
|
|
|
addMass(-shape->getMass());
|
|
|
|
if (!_momentSetByUser)
|
|
|
|
addMoment(-shape->getMoment());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-06-27 17:30:21 +08:00
|
|
|
shape->setScale(scaleX, scaleY);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-08 09:54:01 +08:00
|
|
|
_area += shape->getArea();
|
|
|
|
if (!_massSetByUser)
|
|
|
|
addMass(shape->getMass());
|
|
|
|
if (!_momentSetByUser)
|
|
|
|
addMoment(shape->getMoment());
|
2014-06-27 17:30:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsBody::setPosition(float positionX, float positionY)
|
|
|
|
{
|
|
|
|
cpVect tt;
|
|
|
|
|
|
|
|
tt.x = positionX + _positionOffset.x;
|
|
|
|
tt.y = positionY + _positionOffset.y;
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetPosition(_cpBody, tt);
|
2015-09-18 11:48:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 09:54:01 +08:00
|
|
|
Vec2 PhysicsBody::getPosition() const
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect tt = cpBodyGetPosition(_cpBody);
|
|
|
|
return Vec2(tt.x - _positionOffset.x, tt.y - _positionOffset.y);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsBody::setPositionOffset(const Vec2& position)
|
|
|
|
{
|
|
|
|
if (!_positionOffset.equals(position))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 pos = getPosition();
|
2015-09-18 11:48:43 +08:00
|
|
|
_positionOffset = position;
|
|
|
|
setPosition(pos.x, pos.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 14:25:06 +08:00
|
|
|
float PhysicsBody::getRotation()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_recordedAngle != cpBodyGetAngle(_cpBody))
|
|
|
|
{
|
|
|
|
_recordedAngle = cpBodyGetAngle(_cpBody);
|
|
|
|
_recordedRotation = -_recordedAngle * 180.0 / M_PI - _rotationOffset;
|
2015-01-08 14:25:06 +08:00
|
|
|
}
|
|
|
|
return _recordedRotation;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape, bool addMassAndMoment /* = true*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (shape == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
// add shape to body
|
2013-12-07 10:48:02 +08:00
|
|
|
if (_shapes.getIndex(shape) == -1)
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
shape->setBody(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-22 16:08:23 +08:00
|
|
|
// calculate the area, mass, and density
|
2013-10-14 14:05:57 +08:00
|
|
|
// area must update before mass, because the density changes depend on it.
|
2013-11-12 15:28:07 +08:00
|
|
|
if (addMassAndMoment)
|
|
|
|
{
|
|
|
|
_area += shape->getArea();
|
|
|
|
addMass(shape->getMass());
|
|
|
|
addMoment(shape->getMoment());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
if (_world && cpBodyGetSpace(_cpBody))
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
2013-10-15 16:55:08 +08:00
|
|
|
_world->addShape(shape);
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
_shapes.pushBack(shape);
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
return shape;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsBody::applyForce(const Vec2& force, const Vec2& offset)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2014-03-18 16:17:43 +08:00
|
|
|
if (_dynamic && _mass != PHYSICS_INFINITY)
|
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
cpBodyApplyForceAtLocalPoint(_cpBody, PhysicsHelper::vec22cpv(force), PhysicsHelper::vec22cpv(offset));
|
2014-03-18 16:17:43 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-18 16:27:23 +08:00
|
|
|
void PhysicsBody::resetForces()
|
2013-11-12 15:28:07 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
cpBodySetForce(_cpBody, PhysicsHelper::vec22cpv(Vec2(0, 0)));
|
2013-11-12 15:28:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsBody::applyImpulse(const Vec2& impulse, const Vec2& offset)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
cpBodyApplyImpulseAtLocalPoint(_cpBody, PhysicsHelper::vec22cpv(impulse), PhysicsHelper::vec22cpv(offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyTorque(float torque)
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpBodySetTorque(_cpBody, torque);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setMass(float mass)
|
|
|
|
{
|
2013-11-20 10:25:43 +08:00
|
|
|
if (mass <= 0)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass = mass;
|
|
|
|
_massDefault = false;
|
2015-09-08 09:54:01 +08:00
|
|
|
_massSetByUser = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
// update density
|
2013-11-27 17:33:33 +08:00
|
|
|
if (_mass == PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
_density = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_area > 0)
|
|
|
|
{
|
|
|
|
_density = _mass / _area;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
_density = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-20 10:25:43 +08:00
|
|
|
// the static body's mass and moment is always infinity
|
|
|
|
if (_dynamic)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
internalBodySetMass(_cpBody, _mass);
|
2013-11-20 10:25:43 +08:00
|
|
|
}
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
void PhysicsBody::addMass(float mass)
|
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
if (mass == PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_massDefault = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
_density = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
2013-11-27 17:33:33 +08:00
|
|
|
else if (mass == -PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-18 16:17:43 +08:00
|
|
|
else
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
if (_massDefault)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass = 0;
|
2013-10-14 13:56:08 +08:00
|
|
|
_massDefault = false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
if (_mass + mass > 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass += mass;
|
|
|
|
}
|
|
|
|
else
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass = MASS_DEFAULT;
|
2013-10-14 13:56:08 +08:00
|
|
|
_massDefault = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
if (_area > 0)
|
|
|
|
{
|
|
|
|
_density = _mass / _area;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_density = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-20 10:25:43 +08:00
|
|
|
// the static body's mass and moment is always infinity
|
|
|
|
if (_dynamic)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
internalBodySetMass(_cpBody, _mass);
|
2013-11-20 10:25:43 +08:00
|
|
|
}
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::addMoment(float moment)
|
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
if (moment == PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
// if moment is PHYSICS_INFINITY, the moment of the body will become PHYSICS_INFINITY
|
2021-12-25 10:04:45 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_momentDefault = false;
|
|
|
|
}
|
2013-11-27 17:33:33 +08:00
|
|
|
else if (moment == -PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
// if moment of the body is PHYSICS_INFINITY is has no effect
|
|
|
|
if (_moment != PHYSICS_INFINITY)
|
2013-10-14 13:56:08 +08:00
|
|
|
{
|
|
|
|
if (_momentDefault)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_moment = 0;
|
2013-10-14 13:56:08 +08:00
|
|
|
_momentDefault = false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
if (_moment + moment > 0)
|
|
|
|
{
|
|
|
|
_moment += moment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_moment = MOMENT_DEFAULT;
|
2013-10-14 13:56:08 +08:00
|
|
|
_momentDefault = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-20 10:25:43 +08:00
|
|
|
// the static body's mass and moment is always infinity
|
2014-03-20 10:21:59 +08:00
|
|
|
if (_rotationEnabled && _dynamic)
|
2013-10-15 16:55:08 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpBodySetMoment(_cpBody, _moment);
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void PhysicsBody::setVelocity(const Vec2& velocity)
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
2017-01-06 09:35:34 +08:00
|
|
|
if (cpBodyGetType(_cpBody) == CP_BODY_TYPE_STATIC)
|
2013-11-19 18:30:12 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("physics warning: you can't set velocity for a static body.");
|
2013-11-19 18:30:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-06 09:35:34 +08:00
|
|
|
|
2021-10-24 14:09:59 +08:00
|
|
|
cpBodySetVelocity(_cpBody, PhysicsHelper::vec22cpv(velocity));
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsBody::getVelocity()
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpBodyGetVelocity(_cpBody));
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsBody::getVelocityAtLocalPoint(const Vec2& point)
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpBodyGetVelocityAtLocalPoint(_cpBody, PhysicsHelper::vec22cpv(point)));
|
2013-11-05 15:54:33 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsBody::getVelocityAtWorldPoint(const Vec2& point)
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpBodyGetVelocityAtWorldPoint(_cpBody, PhysicsHelper::vec22cpv(point)));
|
2013-11-05 15:54:33 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 16:55:08 +08:00
|
|
|
void PhysicsBody::setAngularVelocity(float velocity)
|
|
|
|
{
|
2017-01-06 09:35:34 +08:00
|
|
|
if (cpBodyGetType(_cpBody) == CP_BODY_TYPE_STATIC)
|
2013-11-19 18:30:12 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("physics warning: you can't set angular velocity for a static body.");
|
2013-11-19 18:30:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-06 09:35:34 +08:00
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetAngularVelocity(_cpBody, velocity);
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsBody::getAngularVelocity()
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpBodyGetAngularVelocity(_cpBody));
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setVelocityLimit(float limit)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
_velocityLimit = limit;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsBody::getVelocityLimit()
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return _velocityLimit;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setAngularVelocityLimit(float limit)
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
_angularVelocityLimit = limit;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsBody::getAngularVelocityLimit()
|
|
|
|
{
|
2016-03-01 05:53:00 +08:00
|
|
|
return _angularVelocityLimit;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
void PhysicsBody::setMoment(float moment)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_moment = moment;
|
|
|
|
_momentDefault = false;
|
2015-09-08 09:54:01 +08:00
|
|
|
_momentSetByUser = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-20 10:25:43 +08:00
|
|
|
// the static body's mass and moment is always infinity
|
2014-03-20 10:21:59 +08:00
|
|
|
if (_rotationEnabled && _dynamic)
|
2013-10-15 16:55:08 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
cpBodySetMoment(_cpBody, _moment);
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShape* PhysicsBody::getShape(int tag) const
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
|
|
|
if (shape->getTag() == tag)
|
|
|
|
{
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PhysicsBody::removeShape(int tag, bool reduceMassAndMoment /* = true*/)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
|
|
|
if (shape->getTag() == tag)
|
|
|
|
{
|
2013-11-12 15:28:07 +08:00
|
|
|
removeShape(shape, reduceMassAndMoment);
|
2013-09-29 09:39:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PhysicsBody::removeShape(PhysicsShape* shape, bool reduceMassAndMoment /* = true*/)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-12-07 14:28:14 +08:00
|
|
|
if (_shapes.getIndex(shape) != -1)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-14 13:56:08 +08:00
|
|
|
// deduce the area, mass and moment
|
|
|
|
// area must update before mass, because the density changes depend on it.
|
2013-11-12 15:28:07 +08:00
|
|
|
if (reduceMassAndMoment)
|
|
|
|
{
|
|
|
|
_area -= shape->getArea();
|
|
|
|
addMass(-shape->getMass());
|
|
|
|
addMoment(-shape->getMoment());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// remove
|
2013-09-29 09:39:20 +08:00
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeShape(shape);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
// set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call.
|
|
|
|
shape->_body = nullptr;
|
2013-10-17 10:57:48 +08:00
|
|
|
shape->setBody(nullptr);
|
2013-12-12 14:45:30 +08:00
|
|
|
_shapes.eraseObject(shape);
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PhysicsBody::removeAllShapes(bool reduceMassAndMoment /* = true*/)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& child : _shapes)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-28 11:08:41 +08:00
|
|
|
PhysicsShape* shape = dynamic_cast<PhysicsShape*>(child);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-10-28 11:08:41 +08:00
|
|
|
// deduce the area, mass and moment
|
|
|
|
// area must update before mass, because the density changes depend on it.
|
2013-11-12 15:28:07 +08:00
|
|
|
if (reduceMassAndMoment)
|
|
|
|
{
|
|
|
|
_area -= shape->getArea();
|
|
|
|
addMass(-shape->getMass());
|
|
|
|
addMoment(-shape->getMoment());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeShape(shape);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-07 14:17:57 +08:00
|
|
|
// set shape->_body = nullptr make the shape->setBody will not trigger the _body->removeShape function call.
|
|
|
|
shape->_body = nullptr;
|
2013-10-28 11:08:41 +08:00
|
|
|
shape->setBody(nullptr);
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-07 10:48:02 +08:00
|
|
|
_shapes.clear();
|
2013-10-28 11:08:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::removeFromWorld()
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
removeFromPhysicsWorld();
|
2015-09-08 09:54:01 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
void PhysicsBody::setEnabled(bool enable)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2014-03-20 10:21:59 +08:00
|
|
|
if (_enabled != enable)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2014-03-20 10:21:59 +08:00
|
|
|
_enabled = enable;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
2013-11-07 15:12:13 +08:00
|
|
|
_world->addBodyOrDelay(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-11-07 15:12:13 +08:00
|
|
|
_world->removeBodyOrDelay(this);
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
bool PhysicsBody::isResting() const
|
2013-10-15 16:55:08 +08:00
|
|
|
{
|
2016-05-06 11:30:16 +08:00
|
|
|
return cpBodyIsSleeping(_cpBody) != cpFalse;
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
2014-02-26 19:56:52 +08:00
|
|
|
void PhysicsBody::setResting(bool rest) const
|
2014-02-24 16:29:14 +08:00
|
|
|
{
|
2014-02-26 19:56:52 +08:00
|
|
|
if (rest && !isResting())
|
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpBodySleep(_cpBody);
|
2015-09-18 11:48:43 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (!rest && isResting())
|
2014-02-26 19:56:52 +08:00
|
|
|
{
|
2015-01-06 10:29:07 +08:00
|
|
|
cpBodyActivate(_cpBody);
|
2014-02-26 19:56:52 +08:00
|
|
|
}
|
2014-02-24 16:29:14 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 16:55:08 +08:00
|
|
|
void PhysicsBody::update(float delta)
|
|
|
|
{
|
2015-09-08 09:54:01 +08:00
|
|
|
// damping compute
|
2016-05-24 21:53:23 +08:00
|
|
|
if (_isDamping && _dynamic && !isResting())
|
2014-02-24 16:17:42 +08:00
|
|
|
{
|
2015-09-08 09:54:01 +08:00
|
|
|
_cpBody->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
|
|
|
|
_cpBody->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
|
|
|
|
_cpBody->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f);
|
2016-05-24 21:53:23 +08:00
|
|
|
}
|
2013-10-15 16:55:08 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsBody::setCategoryBitmask(int bitmask)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2013-12-07 14:28:14 +08:00
|
|
|
shape->setCategoryBitmask(bitmask);
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:38:20 +08:00
|
|
|
int PhysicsBody::getCategoryBitmask() const
|
|
|
|
{
|
|
|
|
if (!_shapes.empty())
|
|
|
|
{
|
|
|
|
return _shapes.front()->getCategoryBitmask();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsBody::setContactTestBitmask(int bitmask)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2013-12-07 14:28:14 +08:00
|
|
|
shape->setContactTestBitmask(bitmask);
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:38:20 +08:00
|
|
|
int PhysicsBody::getContactTestBitmask() const
|
|
|
|
{
|
|
|
|
if (!_shapes.empty())
|
|
|
|
{
|
|
|
|
return _shapes.front()->getContactTestBitmask();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0x00000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsBody::setCollisionBitmask(int bitmask)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2013-12-07 14:28:14 +08:00
|
|
|
shape->setCollisionBitmask(bitmask);
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:38:20 +08:00
|
|
|
int PhysicsBody::getCollisionBitmask() const
|
|
|
|
{
|
|
|
|
if (!_shapes.empty())
|
|
|
|
{
|
|
|
|
return _shapes.front()->getCollisionBitmask();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsBody::setGroup(int group)
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& shape : _shapes)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2013-12-07 14:28:14 +08:00
|
|
|
shape->setGroup(group);
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:38:20 +08:00
|
|
|
int PhysicsBody::getGroup() const
|
|
|
|
{
|
|
|
|
if (!_shapes.empty())
|
|
|
|
{
|
|
|
|
return _shapes.front()->getGroup();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 15:31:25 +08:00
|
|
|
void PhysicsBody::setRotationOffset(float rotation)
|
|
|
|
{
|
|
|
|
if (std::abs(_rotationOffset - rotation) > 0.5f)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float rot = getRotation();
|
2014-02-28 15:31:25 +08:00
|
|
|
_rotationOffset = rotation;
|
2014-02-28 15:47:49 +08:00
|
|
|
setRotation(rot);
|
2014-02-28 15:31:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsBody::world2Local(const Vec2& point)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpBodyWorldToLocal(_cpBody, PhysicsHelper::vec22cpv(point)));
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 PhysicsBody::local2World(const Vec2& point)
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
2021-10-24 14:09:59 +08:00
|
|
|
return PhysicsHelper::cpv2vec2(cpBodyLocalToWorld(_cpBody, PhysicsHelper::vec22cpv(point)));
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PhysicsBody::beforeSimulation(const Mat4& parentToWorldTransform,
|
|
|
|
const Mat4& nodeToWorldTransform,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
float rotation)
|
2015-09-18 11:48:43 +08:00
|
|
|
{
|
|
|
|
if (_recordScaleX != scaleX || _recordScaleY != scaleY)
|
|
|
|
{
|
|
|
|
_recordScaleX = scaleX;
|
|
|
|
_recordScaleY = scaleY;
|
|
|
|
setScale(scaleX, scaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set rotation
|
|
|
|
if (_recordedRotation != rotation)
|
|
|
|
{
|
|
|
|
setRotation(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set position
|
|
|
|
auto worldPosition = _ownerCenterOffset;
|
|
|
|
nodeToWorldTransform.transformVector(worldPosition.x, worldPosition.y, worldPosition.z, 1.f, &worldPosition);
|
|
|
|
setPosition(worldPosition.x, worldPosition.y);
|
|
|
|
|
|
|
|
_recordPosX = worldPosition.x;
|
|
|
|
_recordPosY = worldPosition.y;
|
|
|
|
|
|
|
|
if (_owner->getAnchorPoint() != Vec2::ANCHOR_MIDDLE)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
parentToWorldTransform.getInversed().transformVector(worldPosition.x, worldPosition.y, worldPosition.z, 1.f,
|
|
|
|
&worldPosition);
|
2015-09-18 11:48:43 +08:00
|
|
|
_offset.x = worldPosition.x - _owner->getPositionX();
|
|
|
|
_offset.y = worldPosition.y - _owner->getPositionY();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::afterSimulation(const Mat4& parentToWorldTransform, float parentRotation)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// set Node position
|
2015-09-18 11:48:43 +08:00
|
|
|
auto tmp = getPosition();
|
|
|
|
Vec3 positionInParent(tmp.x, tmp.y, 0.f);
|
|
|
|
if (_recordPosX != positionInParent.x || _recordPosY != positionInParent.y)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
parentToWorldTransform.getInversed().transformVector(positionInParent.x, positionInParent.y, positionInParent.z,
|
|
|
|
1.f, &positionInParent);
|
2015-09-18 11:48:43 +08:00
|
|
|
_owner->setPosition(positionInParent.x - _offset.x, positionInParent.y - _offset.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set Node rotation
|
|
|
|
_owner->setRotation(getRotation() - parentRotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::onEnter()
|
|
|
|
{
|
|
|
|
addToPhysicsWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::onExit()
|
|
|
|
{
|
|
|
|
removeFromPhysicsWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::onAdd()
|
|
|
|
{
|
|
|
|
_owner->_physicsBody = this;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto contentSize = _owner->getContentSize();
|
2016-07-04 22:12:45 +08:00
|
|
|
_ownerCenterOffset.x = 0.5f * contentSize.width;
|
|
|
|
_ownerCenterOffset.y = 0.5f * contentSize.height;
|
2015-09-18 11:48:43 +08:00
|
|
|
|
|
|
|
setRotationOffset(_owner->getRotation());
|
|
|
|
|
|
|
|
// component may be added after onEnter() has been invoked, so we should add
|
|
|
|
// this line to make sure physics body is added to physics world
|
|
|
|
addToPhysicsWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::onRemove()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_owner != nullptr, "_owner can't be nullptr");
|
2016-06-23 05:12:27 +08:00
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
removeFromPhysicsWorld();
|
2016-06-23 05:12:27 +08:00
|
|
|
|
|
|
|
_owner->_physicsBody = nullptr;
|
2015-09-18 11:48:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::addToPhysicsWorld()
|
|
|
|
{
|
|
|
|
if (_owner)
|
|
|
|
{
|
|
|
|
auto scene = _owner->getScene();
|
|
|
|
if (scene)
|
|
|
|
scene->getPhysicsWorld()->addBody(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::removeFromPhysicsWorld()
|
|
|
|
{
|
2016-06-23 05:12:27 +08:00
|
|
|
if (_owner)
|
2015-09-18 11:48:43 +08:00
|
|
|
{
|
2016-06-23 05:12:27 +08:00
|
|
|
auto scene = _owner->getScene();
|
|
|
|
if (scene)
|
|
|
|
scene->getPhysicsWorld()->removeBody(this);
|
2015-09-18 11:48:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // AX_USE_PHYSICS
|