Remove XXXXInfo_chipmunk class in 'chipmunk' folder to simplify implementation of physics

This commit is contained in:
WenhaiLin 2015-01-06 10:29:07 +08:00
parent 019cda3110
commit fdaa0bbfc6
21 changed files with 348 additions and 1095 deletions

View File

@ -31,16 +31,10 @@
#include "chipmunk.h"
#include "2d/CCScene.h"
#include "physics/CCPhysicsShape.h"
#include "physics/CCPhysicsJoint.h"
#include "physics/CCPhysicsWorld.h"
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
#include "chipmunk/CCPhysicsJointInfo_chipmunk.h"
#include "chipmunk/CCPhysicsWorldInfo_chipmunk.h"
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsShape.h"
#include "CCPhysicsJoint.h"
#include "CCPhysicsWorld.h"
#include "CCPhysicsHelper.h"
static inline void cpBodyUpdateVelocityWithoutGravity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
@ -59,7 +53,7 @@ namespace
PhysicsBody::PhysicsBody()
: _node(nullptr)
, _world(nullptr)
, _info(nullptr)
, _cpBody(nullptr)
, _dynamic(true)
, _enabled(true)
, _rotationEnabled(true)
@ -91,7 +85,10 @@ PhysicsBody::~PhysicsBody()
delete joint;
}
CC_SAFE_DELETE(_info);
if (_cpBody)
{
cpBodyFree(_cpBody);
}
}
PhysicsBody* PhysicsBody::create()
@ -255,12 +252,9 @@ bool PhysicsBody::init()
{
do
{
_info = new (std::nothrow) PhysicsBodyInfo();
CC_BREAK_IF(_info == nullptr);
_cpBody = cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_moment));
_info->setBody(cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_moment)));
CC_BREAK_IF(_info->getBody() == nullptr);
CC_BREAK_IF(_cpBody == nullptr);
return true;
} while (false);
@ -283,33 +277,32 @@ void PhysicsBody::setDynamic(bool dynamic)
if (dynamic != _dynamic)
{
_dynamic = dynamic;
auto body = _info->getBody();
if (dynamic)
{
if (_world && body->CP_PRIVATE(space))
if (_world && _cpBody->CP_PRIVATE(space))
{
cpSpaceConvertBodyToDynamic(_world->_info->getSpace(), body, _mass, _moment);
cpSpaceAddBody(_world->_info->getSpace(), body);
cpSpaceConvertBodyToDynamic(_world->_cpSpace, _cpBody, _mass, _moment);
cpSpaceAddBody(_world->_cpSpace, _cpBody);
}
else
{
cpBodySetMass(body, _mass);
cpBodySetMoment(body, _moment);
body->CP_PRIVATE(node).idleTime = 0.0f;
cpBodySetMass(_cpBody, _mass);
cpBodySetMoment(_cpBody, _moment);
_cpBody->CP_PRIVATE(node).idleTime = 0.0f;
}
}
else
{
if (_world && body->CP_PRIVATE(space))
if (_world && _cpBody->CP_PRIVATE(space))
{
cpSpaceRemoveBody(_world->_info->getSpace(), body);
cpSpaceConvertBodyToStatic(_world->_info->getSpace(), body);
cpSpaceRemoveBody(_world->_cpSpace, _cpBody);
cpSpaceConvertBodyToStatic(_world->_cpSpace, _cpBody);
}
else
{
cpBodySetMass(body, PHYSICS_INFINITY);
cpBodySetMoment(body, PHYSICS_INFINITY);
body->CP_PRIVATE(node).idleTime = (cpFloat)INFINITY;
cpBodySetMass(_cpBody, PHYSICS_INFINITY);
cpBodySetMoment(_cpBody, PHYSICS_INFINITY);
_cpBody->CP_PRIVATE(node).idleTime = (cpFloat)INFINITY;
}
}
}
@ -319,7 +312,7 @@ void PhysicsBody::setRotationEnable(bool enable)
{
if (_rotationEnabled != enable)
{
cpBodySetMoment(_info->getBody(), enable ? _moment : PHYSICS_INFINITY);
cpBodySetMoment(_cpBody, enable ? _moment : PHYSICS_INFINITY);
_rotationEnabled = enable;
}
}
@ -330,22 +323,22 @@ void PhysicsBody::setGravityEnable(bool enable)
if (enable)
{
_info->getBody()->velocity_func = cpBodyUpdateVelocity;
_cpBody->velocity_func = cpBodyUpdateVelocity;
}
else
{
_info->getBody()->velocity_func = cpBodyUpdateVelocityWithoutGravity;
_cpBody->velocity_func = cpBodyUpdateVelocityWithoutGravity;
}
}
void PhysicsBody::setPosition(const Vec2& position)
{
cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset));
cpBodySetPos(_cpBody, PhysicsHelper::point2cpv(position + _positionOffset));
}
void PhysicsBody::setRotation(float rotation)
{
cpBodySetAngle(_info->getBody(), -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
cpBodySetAngle(_cpBody, -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
}
void PhysicsBody::setScale(float scale)
@ -382,13 +375,13 @@ void PhysicsBody::setScaleY(float scaleY)
Vec2 PhysicsBody::getPosition() const
{
cpVect vec = cpBodyGetPos(_info->getBody());
cpVect vec = cpBodyGetPos(_cpBody);
return PhysicsHelper::cpv2point(vec) - _positionOffset;
}
float PhysicsBody::getRotation() const
{
return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->getBody()) * (180.0f / M_PI)) - _rotationOffset;
return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_cpBody) * (180.0f / M_PI)) - _rotationOffset;
}
PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape, bool addMassAndMoment/* = true*/)
@ -429,13 +422,13 @@ void PhysicsBody::applyForce(const Vect& force, const Vec2& offset)
{
if (_dynamic && _mass != PHYSICS_INFINITY)
{
cpBodyApplyForce(_info->getBody(), PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset));
cpBodyApplyForce(_cpBody, PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset));
}
}
void PhysicsBody::resetForces()
{
cpBodyResetForces(_info->getBody());
cpBodyResetForces(_cpBody);
}
void PhysicsBody::applyImpulse(const Vect& impulse)
@ -445,12 +438,12 @@ void PhysicsBody::applyImpulse(const Vect& impulse)
void PhysicsBody::applyImpulse(const Vect& impulse, const Vec2& offset)
{
cpBodyApplyImpulse(_info->getBody(), PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset));
cpBodyApplyImpulse(_cpBody, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset));
}
void PhysicsBody::applyTorque(float torque)
{
cpBodySetTorque(_info->getBody(), PhysicsHelper::float2cpfloat(torque));
cpBodySetTorque(_cpBody, PhysicsHelper::float2cpfloat(torque));
}
void PhysicsBody::setMass(float mass)
@ -481,7 +474,7 @@ void PhysicsBody::setMass(float mass)
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), _mass);
cpBodySetMass(_cpBody, _mass);
}
}
@ -527,7 +520,7 @@ void PhysicsBody::addMass(float mass)
// the static body's mass and moment is always infinity
if (_dynamic)
{
cpBodySetMass(_info->getBody(), _mass);
cpBodySetMass(_cpBody, _mass);
}
}
@ -569,7 +562,7 @@ void PhysicsBody::addMoment(float moment)
// the static body's mass and moment is always infinity
if (_rotationEnabled && _dynamic)
{
cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment));
cpBodySetMoment(_cpBody, PhysicsHelper::float2cpfloat(_moment));
}
}
@ -581,22 +574,22 @@ void PhysicsBody::setVelocity(const Vec2& velocity)
return;
}
cpBodySetVel(_info->getBody(), PhysicsHelper::point2cpv(velocity));
cpBodySetVel(_cpBody, PhysicsHelper::point2cpv(velocity));
}
Vec2 PhysicsBody::getVelocity()
{
return PhysicsHelper::cpv2point(cpBodyGetVel(_info->getBody()));
return PhysicsHelper::cpv2point(cpBodyGetVel(_cpBody));
}
Vec2 PhysicsBody::getVelocityAtLocalPoint(const Vec2& point)
{
return PhysicsHelper::cpv2point(cpBodyGetVelAtLocalPoint(_info->getBody(), PhysicsHelper::point2cpv(point)));
return PhysicsHelper::cpv2point(cpBodyGetVelAtLocalPoint(_cpBody, PhysicsHelper::point2cpv(point)));
}
Vec2 PhysicsBody::getVelocityAtWorldPoint(const Vec2& point)
{
return PhysicsHelper::cpv2point(cpBodyGetVelAtWorldPoint(_info->getBody(), PhysicsHelper::point2cpv(point)));
return PhysicsHelper::cpv2point(cpBodyGetVelAtWorldPoint(_cpBody, PhysicsHelper::point2cpv(point)));
}
void PhysicsBody::setAngularVelocity(float velocity)
@ -607,32 +600,32 @@ void PhysicsBody::setAngularVelocity(float velocity)
return;
}
cpBodySetAngVel(_info->getBody(), PhysicsHelper::float2cpfloat(velocity));
cpBodySetAngVel(_cpBody, PhysicsHelper::float2cpfloat(velocity));
}
float PhysicsBody::getAngularVelocity()
{
return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_info->getBody()));
return PhysicsHelper::cpfloat2float(cpBodyGetAngVel(_cpBody));
}
void PhysicsBody::setVelocityLimit(float limit)
{
cpBodySetVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit));
cpBodySetVelLimit(_cpBody, PhysicsHelper::float2cpfloat(limit));
}
float PhysicsBody::getVelocityLimit()
{
return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_info->getBody()));
return PhysicsHelper::cpfloat2float(cpBodyGetVelLimit(_cpBody));
}
void PhysicsBody::setAngularVelocityLimit(float limit)
{
cpBodySetAngVelLimit(_info->getBody(), PhysicsHelper::float2cpfloat(limit));
cpBodySetAngVelLimit(_cpBody, PhysicsHelper::float2cpfloat(limit));
}
float PhysicsBody::getAngularVelocityLimit()
{
return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_info->getBody()));
return PhysicsHelper::cpfloat2float(cpBodyGetAngVelLimit(_cpBody));
}
void PhysicsBody::setMoment(float moment)
@ -643,7 +636,7 @@ void PhysicsBody::setMoment(float moment)
// the static body's mass and moment is always infinity
if (_rotationEnabled && _dynamic)
{
cpBodySetMoment(_info->getBody(), PhysicsHelper::float2cpfloat(_moment));
cpBodySetMoment(_cpBody, PhysicsHelper::float2cpfloat(_moment));
}
}
@ -755,17 +748,17 @@ void PhysicsBody::setEnable(bool enable)
bool PhysicsBody::isResting() const
{
return CP_PRIVATE(_info->getBody()->node).root != ((cpBody*)0);
return CP_PRIVATE(_cpBody->node).root != ((cpBody*)0);
}
void PhysicsBody::setResting(bool rest) const
{
if (rest && !isResting())
{
cpBodySleep(_info->getBody());
cpBodySleep(_cpBody);
}else if(!rest && isResting())
{
cpBodyActivate(_info->getBody());
cpBodyActivate(_cpBody);
}
}
@ -798,9 +791,9 @@ void PhysicsBody::update(float delta)
// damping compute
if (_isDamping && _dynamic && !isResting())
{
_info->getBody()->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
_info->getBody()->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
_info->getBody()->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f);
_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);
}
}
}
@ -917,12 +910,12 @@ float PhysicsBody::getRotationOffset() const
Vec2 PhysicsBody::world2Local(const Vec2& point)
{
return PhysicsHelper::cpv2point(cpBodyWorld2Local(_info->getBody(), PhysicsHelper::point2cpv(point)));
return PhysicsHelper::cpv2point(cpBodyWorld2Local(_cpBody, PhysicsHelper::point2cpv(point)));
}
Vec2 PhysicsBody::local2World(const Vec2& point)
{
return PhysicsHelper::cpv2point(cpBodyLocal2World(_info->getBody(), PhysicsHelper::point2cpv(point)));
return PhysicsHelper::cpv2point(cpBodyLocal2World(_cpBody, PhysicsHelper::point2cpv(point)));
}
NS_CC_END

View File

@ -33,13 +33,14 @@
#include "physics/CCPhysicsShape.h"
#include "base/CCVector.h"
struct cpBody;
NS_CC_BEGIN
class Node;
class Sprite;
class PhysicsWorld;
class PhysicsJoint;
class PhysicsBodyInfo;
typedef Vec2 Vect;
@ -296,6 +297,8 @@ public:
Vec2 world2Local(const Vec2& point);
/** convert the local point to world */
Vec2 local2World(const Vec2& point);
cpBody* getCPBody() { return _cpBody; }
protected:
@ -322,7 +325,7 @@ protected:
std::vector<PhysicsJoint*> _joints;
Vector<PhysicsShape*> _shapes;
PhysicsWorld* _world;
PhysicsBodyInfo* _info;
cpBody* _cpBody;
bool _dynamic;
bool _enabled;
bool _rotationEnabled;

View File

@ -25,11 +25,8 @@
#if CC_USE_PHYSICS
#include "chipmunk.h"
#include "physics/CCPhysicsBody.h"
#include "chipmunk/CCPhysicsContactInfo_chipmunk.h"
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsBody.h"
#include "CCPhysicsHelper.h"
#include "base/CCEventCustom.h"
NS_CC_BEGIN
@ -42,7 +39,6 @@ PhysicsContact::PhysicsContact()
, _shapeA(nullptr)
, _shapeB(nullptr)
, _eventCode(EventCode::NONE)
, _info(nullptr)
, _notificationEnable(true)
, _result(true)
, _data(nullptr)
@ -55,7 +51,6 @@ PhysicsContact::PhysicsContact()
PhysicsContact::~PhysicsContact()
{
CC_SAFE_DELETE(_info);
CC_SAFE_DELETE(_contactData);
CC_SAFE_DELETE(_preContactData);
}
@ -78,8 +73,6 @@ bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b)
{
CC_BREAK_IF(a == nullptr || b == nullptr);
CC_BREAK_IF(!(_info = new (std::nothrow) PhysicsContactInfo(this)));
_shapeA = a;
_shapeB = b;

View File

@ -40,8 +40,6 @@ class PhysicsShape;
class PhysicsBody;
class PhysicsWorld;
class PhysicsContactInfo;
typedef Vec2 Vect;
typedef struct CC_DLL PhysicsContactData
@ -112,7 +110,6 @@ private:
PhysicsShape* _shapeA;
PhysicsShape* _shapeB;
EventCode _eventCode;
PhysicsContactInfo* _info;
bool _notificationEnable;
bool _result;

View File

@ -26,13 +26,9 @@
#if CC_USE_PHYSICS
#include "chipmunk.h"
#include "physics/CCPhysicsBody.h"
#include "physics/CCPhysicsWorld.h"
#include "chipmunk/CCPhysicsJointInfo_chipmunk.h"
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsBody.h"
#include "CCPhysicsWorld.h"
#include "CCPhysicsHelper.h"
#include "2d/CCNode.h"
NS_CC_BEGIN
@ -41,7 +37,6 @@ PhysicsJoint::PhysicsJoint()
: _bodyA(nullptr)
, _bodyB(nullptr)
, _world(nullptr)
, _info(nullptr)
, _enable(false)
, _collisionEnable(true)
, _destoryMark(false)
@ -55,7 +50,10 @@ PhysicsJoint::~PhysicsJoint()
// reset the shapes collision group
setCollisionEnable(true);
CC_SAFE_DELETE(_info);
for (auto constraint : _cpConstraints)
{
cpConstraintFree(constraint);
}
}
bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
@ -65,8 +63,6 @@ bool PhysicsJoint::init(cocos2d::PhysicsBody *a, cocos2d::PhysicsBody *b)
CCASSERT(a != nullptr && b != nullptr, "the body passed in is nil");
CCASSERT(a != b, "the two bodies are equal");
CC_BREAK_IF(!(_info = new (std::nothrow) PhysicsJointInfo(this)));
_bodyA = a;
_bodyA->_joints.push_back(this);
_bodyB = b;
@ -97,11 +93,6 @@ void PhysicsJoint::setEnable(bool enable)
}
}
PhysicsBodyInfo* PhysicsJoint::getBodyInfo(PhysicsBody* body) const
{
return body->_info;
}
Node* PhysicsJoint::getBodyNode(PhysicsBody* body) const
{
return body->_node;
@ -151,7 +142,7 @@ void PhysicsJoint::destroy(PhysicsJoint* joint)
void PhysicsJoint::setMaxForce(float force)
{
for (cpConstraint* joint : _info->getJoints())
for (auto joint : _cpConstraints)
{
joint->maxForce = PhysicsHelper::float2cpfloat(force);
}
@ -159,7 +150,7 @@ void PhysicsJoint::setMaxForce(float force)
float PhysicsJoint::getMaxForce() const
{
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
return PhysicsHelper::cpfloat2float(_cpConstraints.front()->maxForce);
}
PhysicsJointFixed* PhysicsJointFixed::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
@ -185,15 +176,15 @@ bool PhysicsJointFixed::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr)
getBodyNode(b)->setPosition(anchr);
// add a pivot joint to fixed two body together
cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
auto constraint = cpPivotJointNew(a->getCPBody(), b->getCPBody(),
PhysicsHelper::point2cpv(anchr));
CC_BREAK_IF(joint == nullptr);
_info->add(joint);
CC_BREAK_IF(constraint == nullptr);
_cpConstraints.push_back(constraint);
// add a gear joint to make two body have the same rotation.
joint = cpGearJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(), 0, 1);
CC_BREAK_IF(joint == nullptr);
_info->add(joint);
constraint = cpGearJointNew(a->getCPBody(), b->getCPBody(), 0, 1);
CC_BREAK_IF(constraint == nullptr);
_cpConstraints.push_back(constraint);
setCollisionEnable(false);
@ -221,12 +212,12 @@ bool PhysicsJointPin::init(PhysicsBody *a, PhysicsBody *b, const Vec2& anchr)
do
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpPivotJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
auto constraint = cpPivotJointNew(a->getCPBody(), b->getCPBody(),
PhysicsHelper::point2cpv(anchr));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -258,15 +249,15 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1,
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpSlideJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
auto constraint = cpSlideJointNew(a->getCPBody(), b->getCPBody(),
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2),
PhysicsHelper::float2cpfloat(min),
PhysicsHelper::float2cpfloat(max));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -276,42 +267,42 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1,
float PhysicsJointLimit::getMin() const
{
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpSlideJointGetMin(_cpConstraints.front()));
}
void PhysicsJointLimit::setMin(float min)
{
cpSlideJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min));
cpSlideJointSetMin(_cpConstraints.front(), PhysicsHelper::float2cpfloat(min));
}
float PhysicsJointLimit::getMax() const
{
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpSlideJointGetMax(_cpConstraints.front()));
}
void PhysicsJointLimit::setMax(float max)
{
cpSlideJointSetMax(_info->getJoints().front(), PhysicsHelper::float2cpfloat(max));
cpSlideJointSetMax(_cpConstraints.front(), PhysicsHelper::float2cpfloat(max));
}
Vec2 PhysicsJointLimit::getAnchr1() const
{
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr1(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr1(_cpConstraints.front()));
}
void PhysicsJointLimit::setAnchr1(const Vec2& anchr)
{
cpSlideJointSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
cpSlideJointSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
}
Vec2 PhysicsJointLimit::getAnchr2() const
{
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr2(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpSlideJointGetAnchr2(_cpConstraints.front()));
}
void PhysicsJointLimit::setAnchr2(const Vec2& anchr)
{
cpSlideJointSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
cpSlideJointSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
}
PhysicsJointDistance* PhysicsJointDistance::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2)
@ -333,14 +324,14 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anch
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpPinJointNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -350,12 +341,12 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anch
float PhysicsJointDistance::getDistance() const
{
return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpPinJointGetDist(_cpConstraints.front()));
}
void PhysicsJointDistance::setDistance(float distance)
{
cpPinJointSetDist(_info->getJoints().front(), PhysicsHelper::float2cpfloat(distance));
cpPinJointSetDist(_cpConstraints.front(), PhysicsHelper::float2cpfloat(distance));
}
PhysicsJointSpring* PhysicsJointSpring::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping)
@ -376,17 +367,17 @@ bool PhysicsJointSpring::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1
do {
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpDampedSpringNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpDampedSpringNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2),
PhysicsHelper::float2cpfloat(_bodyB->local2World(anchr1).getDistance(_bodyA->local2World(anchr2))),
PhysicsHelper::float2cpfloat(stiffness),
PhysicsHelper::float2cpfloat(damping));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -396,52 +387,52 @@ bool PhysicsJointSpring::init(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1
Vec2 PhysicsJointSpring::getAnchr1() const
{
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr1(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr1(_cpConstraints.front()));
}
void PhysicsJointSpring::setAnchr1(const Vec2& anchr)
{
cpDampedSpringSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
cpDampedSpringSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
}
Vec2 PhysicsJointSpring::getAnchr2() const
{
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr2(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpDampedSpringGetAnchr2(_cpConstraints.front()));
}
void PhysicsJointSpring::setAnchr2(const Vec2& anchr)
{
cpDampedSpringSetAnchr1(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr));
cpDampedSpringSetAnchr1(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr));
}
float PhysicsJointSpring::getRestLength() const
{
return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedSpringGetRestLength(_cpConstraints.front()));
}
void PhysicsJointSpring::setRestLength(float restLength)
{
cpDampedSpringSetRestLength(_info->getJoints().front(), PhysicsHelper::float2cpfloat(restLength));
cpDampedSpringSetRestLength(_cpConstraints.front(), PhysicsHelper::float2cpfloat(restLength));
}
float PhysicsJointSpring::getStiffness() const
{
return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedSpringGetStiffness(_cpConstraints.front()));
}
void PhysicsJointSpring::setStiffness(float stiffness)
{
cpDampedSpringSetStiffness(_info->getJoints().front(), PhysicsHelper::float2cpfloat(stiffness));
cpDampedSpringSetStiffness(_cpConstraints.front(), PhysicsHelper::float2cpfloat(stiffness));
}
float PhysicsJointSpring::getDamping() const
{
return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedSpringGetDamping(_cpConstraints.front()));
}
void PhysicsJointSpring::setDamping(float damping)
{
cpDampedSpringSetDamping(_info->getJoints().front(), PhysicsHelper::float2cpfloat(damping));
cpDampedSpringSetDamping(_cpConstraints.front(), PhysicsHelper::float2cpfloat(damping));
}
PhysicsJointGroove* PhysicsJointGroove::construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2)
@ -462,15 +453,15 @@ bool PhysicsJointGroove::init(PhysicsBody* a, PhysicsBody* b, const Vec2& groove
do {
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpGrooveJointNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::point2cpv(grooveA),
PhysicsHelper::point2cpv(grooveB),
PhysicsHelper::point2cpv(anchr2));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -480,32 +471,32 @@ bool PhysicsJointGroove::init(PhysicsBody* a, PhysicsBody* b, const Vec2& groove
Vec2 PhysicsJointGroove::getGrooveA() const
{
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveA(_cpConstraints.front()));
}
void PhysicsJointGroove::setGrooveA(const Vec2& grooveA)
{
cpGrooveJointSetGrooveA(_info->getJoints().front(), PhysicsHelper::point2cpv(grooveA));
cpGrooveJointSetGrooveA(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveA));
}
Vec2 PhysicsJointGroove::getGrooveB() const
{
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpGrooveJointGetGrooveB(_cpConstraints.front()));
}
void PhysicsJointGroove::setGrooveB(const Vec2& grooveB)
{
cpGrooveJointSetGrooveB(_info->getJoints().front(), PhysicsHelper::point2cpv(grooveB));
cpGrooveJointSetGrooveB(_cpConstraints.front(), PhysicsHelper::point2cpv(grooveB));
}
Vec2 PhysicsJointGroove::getAnchr2() const
{
return PhysicsHelper::cpv2point(cpGrooveJointGetAnchr2(_info->getJoints().front()));
return PhysicsHelper::cpv2point(cpGrooveJointGetAnchr2(_cpConstraints.front()));
}
void PhysicsJointGroove::setAnchr2(const Vec2& anchr2)
{
cpGrooveJointSetAnchr2(_info->getJoints().front(), PhysicsHelper::point2cpv(anchr2));
cpGrooveJointSetAnchr2(_cpConstraints.front(), PhysicsHelper::point2cpv(anchr2));
}
PhysicsJointRotarySpring* PhysicsJointRotarySpring::construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping)
@ -526,15 +517,15 @@ bool PhysicsJointRotarySpring::init(PhysicsBody* a, PhysicsBody* b, float stiffn
do {
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpDampedRotarySpringNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpDampedRotarySpringNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::float2cpfloat(_bodyB->getRotation() - _bodyA->getRotation()),
PhysicsHelper::float2cpfloat(stiffness),
PhysicsHelper::float2cpfloat(damping));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -544,32 +535,32 @@ bool PhysicsJointRotarySpring::init(PhysicsBody* a, PhysicsBody* b, float stiffn
float PhysicsJointRotarySpring::getRestAngle() const
{
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetRestAngle(_cpConstraints.front()));
}
void PhysicsJointRotarySpring::setRestAngle(float restAngle)
{
cpDampedRotarySpringSetRestAngle(_info->getJoints().front(), PhysicsHelper::float2cpfloat(restAngle));
cpDampedRotarySpringSetRestAngle(_cpConstraints.front(), PhysicsHelper::float2cpfloat(restAngle));
}
float PhysicsJointRotarySpring::getStiffness() const
{
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetStiffness(_cpConstraints.front()));
}
void PhysicsJointRotarySpring::setStiffness(float stiffness)
{
cpDampedRotarySpringSetStiffness(_info->getJoints().front(), PhysicsHelper::float2cpfloat(stiffness));
cpDampedRotarySpringSetStiffness(_cpConstraints.front(), PhysicsHelper::float2cpfloat(stiffness));
}
float PhysicsJointRotarySpring::getDamping() const
{
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpDampedRotarySpringGetDamping(_cpConstraints.front()));
}
void PhysicsJointRotarySpring::setDamping(float damping)
{
cpDampedRotarySpringSetDamping(_info->getJoints().front(), PhysicsHelper::float2cpfloat(damping));
cpDampedRotarySpringSetDamping(_cpConstraints.front(), PhysicsHelper::float2cpfloat(damping));
}
PhysicsJointRotaryLimit* PhysicsJointRotaryLimit::construct(PhysicsBody* a, PhysicsBody* b, float min, float max)
@ -596,14 +587,14 @@ bool PhysicsJointRotaryLimit::init(PhysicsBody* a, PhysicsBody* b, float min, fl
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpRotaryLimitJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpRotaryLimitJointNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::float2cpfloat(min),
PhysicsHelper::float2cpfloat(max));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -613,22 +604,22 @@ bool PhysicsJointRotaryLimit::init(PhysicsBody* a, PhysicsBody* b, float min, fl
float PhysicsJointRotaryLimit::getMin() const
{
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMin(_cpConstraints.front()));
}
void PhysicsJointRotaryLimit::setMin(float min)
{
cpRotaryLimitJointSetMin(_info->getJoints().front(), PhysicsHelper::float2cpfloat(min));
cpRotaryLimitJointSetMin(_cpConstraints.front(), PhysicsHelper::float2cpfloat(min));
}
float PhysicsJointRotaryLimit::getMax() const
{
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpRotaryLimitJointGetMax(_cpConstraints.front()));
}
void PhysicsJointRotaryLimit::setMax(float max)
{
cpRotaryLimitJointSetMax(_info->getJoints().front(), PhysicsHelper::float2cpfloat(max));
cpRotaryLimitJointSetMax(_cpConstraints.front(), PhysicsHelper::float2cpfloat(max));
}
PhysicsJointRatchet* PhysicsJointRatchet::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
@ -650,14 +641,14 @@ bool PhysicsJointRatchet::init(PhysicsBody* a, PhysicsBody* b, float phase, floa
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpRatchetJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpRatchetJointNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::float2cpfloat(phase),
PhysicsHelper::cpfloat2float(ratchet));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -667,32 +658,32 @@ bool PhysicsJointRatchet::init(PhysicsBody* a, PhysicsBody* b, float phase, floa
float PhysicsJointRatchet::getAngle() const
{
return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpRatchetJointGetAngle(_cpConstraints.front()));
}
void PhysicsJointRatchet::setAngle(float angle)
{
cpRatchetJointSetAngle(_info->getJoints().front(), PhysicsHelper::float2cpfloat(angle));
cpRatchetJointSetAngle(_cpConstraints.front(), PhysicsHelper::float2cpfloat(angle));
}
float PhysicsJointRatchet::getPhase() const
{
return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpRatchetJointGetPhase(_cpConstraints.front()));
}
void PhysicsJointRatchet::setPhase(float phase)
{
cpRatchetJointSetPhase(_info->getJoints().front(), PhysicsHelper::float2cpfloat(phase));
cpRatchetJointSetPhase(_cpConstraints.front(), PhysicsHelper::float2cpfloat(phase));
}
float PhysicsJointRatchet::getRatchet() const
{
return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpRatchetJointGetRatchet(_cpConstraints.front()));
}
void PhysicsJointRatchet::setRatchet(float ratchet)
{
cpRatchetJointSetRatchet(_info->getJoints().front(), PhysicsHelper::float2cpfloat(ratchet));
cpRatchetJointSetRatchet(_cpConstraints.front(), PhysicsHelper::float2cpfloat(ratchet));
}
PhysicsJointGear* PhysicsJointGear::construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet)
@ -714,14 +705,14 @@ bool PhysicsJointGear::init(PhysicsBody* a, PhysicsBody* b, float phase, float r
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpGearJointNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpGearJointNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::float2cpfloat(phase),
PhysicsHelper::float2cpfloat(ratio));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -731,22 +722,22 @@ bool PhysicsJointGear::init(PhysicsBody* a, PhysicsBody* b, float phase, float r
float PhysicsJointGear::getPhase() const
{
return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpGearJointGetPhase(_cpConstraints.front()));
}
void PhysicsJointGear::setPhase(float phase)
{
cpGearJointSetPhase(_info->getJoints().front(), PhysicsHelper::float2cpfloat(phase));
cpGearJointSetPhase(_cpConstraints.front(), PhysicsHelper::float2cpfloat(phase));
}
float PhysicsJointGear::getRatio() const
{
return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpGearJointGetRatio(_cpConstraints.front()));
}
void PhysicsJointGear::setRatio(float ratio)
{
cpGearJointSetRatio(_info->getJoints().front(), PhysicsHelper::float2cpfloat(ratio));
cpGearJointSetRatio(_cpConstraints.front(), PhysicsHelper::float2cpfloat(ratio));
}
PhysicsJointMotor* PhysicsJointMotor::construct(PhysicsBody* a, PhysicsBody* b, float rate)
@ -768,13 +759,13 @@ bool PhysicsJointMotor::init(PhysicsBody* a, PhysicsBody* b, float rate)
{
CC_BREAK_IF(!PhysicsJoint::init(a, b));
cpConstraint* joint = cpSimpleMotorNew(getBodyInfo(a)->getBody(),
getBodyInfo(b)->getBody(),
auto constraint = cpSimpleMotorNew(a->getCPBody(),
b->getCPBody(),
PhysicsHelper::float2cpfloat(rate));
CC_BREAK_IF(joint == nullptr);
CC_BREAK_IF(constraint == nullptr);
_info->add(joint);
_cpConstraints.push_back(constraint);
return true;
} while (false);
@ -784,12 +775,12 @@ bool PhysicsJointMotor::init(PhysicsBody* a, PhysicsBody* b, float rate)
float PhysicsJointMotor::getRate() const
{
return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_info->getJoints().front()));
return PhysicsHelper::cpfloat2float(cpSimpleMotorGetRate(_cpConstraints.front()));
}
void PhysicsJointMotor::setRate(float rate)
{
cpSimpleMotorSetRate(_info->getJoints().front(), PhysicsHelper::float2cpfloat(rate));
cpSimpleMotorSetRate(_cpConstraints.front(), PhysicsHelper::float2cpfloat(rate));
}
NS_CC_END

View File

@ -31,13 +31,13 @@
#include "base/CCRef.h"
#include "math/CCGeometry.h"
struct cpConstraint;
NS_CC_BEGIN
class Node;
class PhysicsBody;
class PhysicsWorld;
class PhysicsJointInfo;
class PhysicsBodyInfo;
/*
* @brief An PhysicsJoint object connects two physics bodies together.
@ -72,18 +72,13 @@ public:
protected:
bool init(PhysicsBody* a, PhysicsBody* b);
/**
* PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody.
*/
PhysicsBodyInfo* getBodyInfo(PhysicsBody* body) const;
Node* getBodyNode(PhysicsBody* body) const;
protected:
PhysicsBody* _bodyA;
PhysicsBody* _bodyB;
PhysicsWorld* _world;
PhysicsJointInfo* _info;
std::vector<cpConstraint*> _cpConstraints;
bool _enable;
bool _collisionEnable;
bool _destoryMark;

View File

@ -26,23 +26,22 @@
#if CC_USE_PHYSICS
#include <climits>
#include <unordered_map>
#include "chipmunk.h"
#include "chipmunk_unsafe.h"
#include "physics/CCPhysicsBody.h"
#include "physics/CCPhysicsWorld.h"
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsBody.h"
#include "CCPhysicsWorld.h"
#include "CCPhysicsHelper.h"
NS_CC_BEGIN
extern const float PHYSICS_INFINITY;
std::unordered_map<cpShape*, PhysicsShape*> s_physicsShapeMap;
static cpBody* s_sharedBody = nullptr;
PhysicsShape::PhysicsShape()
: _body(nullptr)
, _info(nullptr)
, _type(Type::UNKNOWN)
, _area(0.0f)
, _mass(0.0f)
@ -58,22 +57,20 @@ PhysicsShape::PhysicsShape()
, _contactTestBitmask(0)
, _group(0)
{
if (s_sharedBody == nullptr)
{
s_sharedBody = cpBodyNewStatic();
}
}
PhysicsShape::~PhysicsShape()
{
CC_SAFE_DELETE(_info);
}
for (auto shape : _cpShapes)
{
s_physicsShapeMap.erase(shape);
bool PhysicsShape::init(Type type)
{
_info = new (std::nothrow) PhysicsShapeInfo(this);
if (_info == nullptr) return false;
_type = type;
return true;
cpShapeFree(shape);
}
}
void PhysicsShape::setMass(float mass)
@ -115,17 +112,6 @@ void PhysicsShape::setMaterial(const PhysicsMaterial& material)
setFriction(material.friction);
}
PhysicsBodyInfo* PhysicsShape::bodyInfo() const
{
if (_body != nullptr)
{
return _body->_info;
}else
{
return nullptr;
}
}
void PhysicsShape::setScale(float scale)
{
setScaleX(scale);
@ -172,6 +158,16 @@ void PhysicsShape::update(float delta)
}
}
void PhysicsShape::addShape(cpShape* shape)
{
if (shape)
{
cpShapeSetGroup(shape, _group);
_cpShapes.push_back(shape);
s_physicsShapeMap.insert(std::pair<cpShape*, PhysicsShape*>(shape, this));
}
}
PhysicsShapeCircle::PhysicsShapeCircle()
{
@ -264,7 +260,7 @@ void PhysicsShape::setRestitution(float restitution)
{
_material.restitution = restitution;
for (cpShape* shape : _info->getShapes())
for (cpShape* shape : _cpShapes)
{
cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution));
}
@ -274,7 +270,7 @@ void PhysicsShape::setFriction(float friction)
{
_material.friction = friction;
for (cpShape* shape : _info->getShapes())
for (cpShape* shape : _cpShapes)
{
cpShapeSetFriction(shape, PhysicsHelper::float2cpfloat(friction));
}
@ -309,25 +305,21 @@ Vec2 PhysicsShape::getPolyonCenter(const Vec2* points, int count)
void PhysicsShape::setBody(PhysicsBody *body)
{
// already added
if (body != nullptr && _body == body)
if (body && _body == body)
{
return;
}
if (_body != nullptr)
if (_body)
{
_body->removeShape(this);
}
if (body == nullptr)
for (auto shape : _cpShapes)
{
_info->setBody(nullptr);
_body = nullptr;
}else
{
_info->setBody(body->_info->getBody());
_body = body;
cpShapeSetBody(shape, body == nullptr ? s_sharedBody : body->_cpBody);
}
_body = body;
}
// PhysicsShapeCircle
@ -348,13 +340,13 @@ bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* =
{
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE));
_type = Type::CIRCLE;
cpShape* shape = cpCircleShapeNew(_info->getSharedBody(), radius, PhysicsHelper::point2cpv(offset));
auto shape = cpCircleShapeNew(s_sharedBody, radius, PhysicsHelper::point2cpv(offset));
CC_BREAK_IF(shape == nullptr);
_info->add(shape);
addShape(shape);
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
@ -383,12 +375,12 @@ float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Vec2&
float PhysicsShapeCircle::calculateArea()
{
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->getShapes().front())));
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_cpShapes.front())));
}
float PhysicsShapeCircle::calculateDefaultMoment()
{
cpShape* shape = _info->getShapes().front();
auto shape = _cpShapes.front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass),
@ -399,12 +391,12 @@ float PhysicsShapeCircle::calculateDefaultMoment()
float PhysicsShapeCircle::getRadius() const
{
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->getShapes().front()));
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_cpShapes.front()));
}
Vec2 PhysicsShapeCircle::getOffset()
{
return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->getShapes().front()));
return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_cpShapes.front()));
}
void PhysicsShapeCircle::setScale(float scale)
@ -450,13 +442,12 @@ void PhysicsShapeCircle::setScaleY(float scale)
void PhysicsShapeCircle::update(float delta)
{
if (_dirty)
{
cpFloat factor = std::abs(PhysicsHelper::float2cpfloat( _newScaleX / _scaleX ));
cpShape* shape = _info->getShapes().front();
cpVect v = cpCircleShapeGetOffset(shape);
auto shape = _cpShapes.front();
auto v = cpCircleShapeGetOffset(shape);
v = cpvmult(v, PhysicsHelper::float2cpfloat(factor));
((cpCircleShape*)shape)->c = v;
@ -485,21 +476,20 @@ bool PhysicsShapeEdgeSegment::init(const Vec2& a, const Vec2& b, const PhysicsMa
{
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT));
_type = Type::EDGESEGMENT;
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(),
auto shape = cpSegmentShapeNew(s_sharedBody,
PhysicsHelper::point2cpv(a),
PhysicsHelper::point2cpv(b),
PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr);
_info->add(shape);
addShape(shape);
_mass = PHYSICS_INFINITY;
_moment = PHYSICS_INFINITY;
setMaterial(material);
return true;
@ -510,18 +500,18 @@ bool PhysicsShapeEdgeSegment::init(const Vec2& a, const Vec2& b, const PhysicsMa
Vec2 PhysicsShapeEdgeSegment::getPointA() const
{
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->ta);
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_cpShapes.front()))->ta);
}
Vec2 PhysicsShapeEdgeSegment::getPointB() const
{
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->tb);
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_cpShapes.front()))->tb);
}
Vec2 PhysicsShapeEdgeSegment::getCenter()
{
Vec2 a = PhysicsHelper::cpv2point(cpSegmentShapeGetA(_info->getShapes().front()));
Vec2 b = PhysicsHelper::cpv2point(cpSegmentShapeGetB(_info->getShapes().front()));
auto a = PhysicsHelper::cpv2point(cpSegmentShapeGetA(_cpShapes.front()));
auto b = PhysicsHelper::cpv2point(cpSegmentShapeGetB(_cpShapes.front()));
return ( a + b ) / 2;
}
@ -529,14 +519,14 @@ void PhysicsShapeEdgeSegment::update(float delta)
{
if (_dirty)
{
cpFloat factorX = PhysicsHelper::float2cpfloat(_newScaleX / _scaleX);
cpFloat factorY = PhysicsHelper::float2cpfloat(_newScaleY / _scaleY);
auto factorX = PhysicsHelper::float2cpfloat(_newScaleX / _scaleX);
auto factorY = PhysicsHelper::float2cpfloat(_newScaleY / _scaleY);
cpShape* shape = _info->getShapes().front();
cpVect a = cpSegmentShapeGetA(shape);
auto shape = _cpShapes.front();
auto a = cpSegmentShapeGetA(shape);
a.x *= factorX;
a.y *= factorY;
cpVect b = cpSegmentShapeGetB(shape);
auto b = cpSegmentShapeGetB(shape);
b.x *= factorX;
b.y *= factorY;
cpSegmentShapeSetEndpoints(shape, a, b);
@ -563,19 +553,19 @@ bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* =
{
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::BOX));
_type = Type::BOX;
cpVect wh = PhysicsHelper::size2cpv(size);
auto wh = PhysicsHelper::size2cpv(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}
};
cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), 4, vec, PhysicsHelper::point2cpv(offset));
auto shape = cpPolyShapeNew(s_sharedBody, 4, vec, PhysicsHelper::point2cpv(offset));
CC_BREAK_IF(shape == nullptr);
_info->add(shape);
addShape(shape);
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
@ -591,7 +581,7 @@ bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* =
Size PhysicsShapeBox::getSize() const
{
cpShape* shape = _info->getShapes().front();
cpShape* shape = _cpShapes.front();
return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2)),
cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1))));
}
@ -614,16 +604,16 @@ bool PhysicsShapePolygon::init(const Vec2* points, int count, const PhysicsMater
{
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN));
_type = Type::POLYGEN;
cpVect* vecs = new cpVect[count];
auto vecs = new cpVect[count];
PhysicsHelper::points2cpvs(points, vecs, count);
cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), count, vecs, PhysicsHelper::point2cpv(offset));
auto shape = cpPolyShapeNew(s_sharedBody, count, vecs, PhysicsHelper::point2cpv(offset));
CC_SAFE_DELETE_ARRAY(vecs);
CC_BREAK_IF(shape == nullptr);
_info->add(shape);
addShape(shape);
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
@ -660,36 +650,36 @@ float PhysicsShapePolygon::calculateMoment(float mass, const Vec2* points, int c
float PhysicsShapePolygon::calculateArea()
{
cpShape* shape = _info->getShapes().front();
auto shape = _cpShapes.front();
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
}
float PhysicsShapePolygon::calculateDefaultMoment()
{
cpShape* shape = _info->getShapes().front();
auto shape = _cpShapes.front();
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
}
Vec2 PhysicsShapePolygon::getPoint(int i) const
{
return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_info->getShapes().front(), i));
return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_cpShapes.front(), i));
}
void PhysicsShapePolygon::getPoints(Vec2* outPoints) const
{
cpShape* shape = _info->getShapes().front();
auto shape = _cpShapes.front();
PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, outPoints, ((cpPolyShape*)shape)->numVerts);
}
int PhysicsShapePolygon::getPointsCount() const
{
return ((cpPolyShape*)_info->getShapes().front())->numVerts;
return ((cpPolyShape*)_cpShapes.front())->numVerts;
}
Vec2 PhysicsShapePolygon::getCenter()
{
return PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)_info->getShapes().front())->numVerts, ((cpPolyShape*)_info->getShapes().front())->verts));
return PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)_cpShapes.front())->numVerts, ((cpPolyShape*)_cpShapes.front())->verts));
}
void PhysicsShapePolygon::update(float delta)
@ -699,7 +689,7 @@ void PhysicsShapePolygon::update(float delta)
cpFloat factorX = PhysicsHelper::float2cpfloat( _newScaleX / _scaleX );
cpFloat factorY = PhysicsHelper::float2cpfloat( _newScaleY / _scaleY );
cpShape* shape = _info->getShapes().front();
auto shape = _cpShapes.front();
int count = cpPolyShapeGetNumVerts(shape);
cpVect* vects = ((cpPolyShape*)shape)->verts;
cpSplittingPlane* planes = ((cpPolyShape*)shape)->planes;
@ -751,7 +741,7 @@ bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material
{
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEBOX));
_type = Type::EDGEBOX;
cpVect vec[4] = {};
vec[0] = PhysicsHelper::point2cpv(Vec2(-size.width/2+offset.x, -size.height/2+offset.y));
@ -762,10 +752,10 @@ bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material
int i = 0;
for (; i < 4; ++i)
{
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[(i+1)%4],
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % 4],
PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr);
_info->add(shape);
addShape(shape);
}
CC_BREAK_IF(i < 4);
@ -799,7 +789,7 @@ bool PhysicsShapeEdgePolygon::init(const Vec2* points, int count, const PhysicsM
cpVect* vec = nullptr;
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEPOLYGEN));
_type = Type::EDGEPOLYGEN;
vec = new cpVect[count];
PhysicsHelper::points2cpvs(points, vec, count);
@ -807,12 +797,12 @@ bool PhysicsShapeEdgePolygon::init(const Vec2* points, int count, const PhysicsM
int i = 0;
for (; i < count; ++i)
{
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[(i+1)%count],
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[(i + 1) % count],
PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
_info->add(shape);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
addShape(shape);
}
CC_SAFE_DELETE_ARRAY(vec);
@ -833,10 +823,10 @@ bool PhysicsShapeEdgePolygon::init(const Vec2* points, int count, const PhysicsM
Vec2 PhysicsShapeEdgePolygon::getCenter()
{
int count = (int)_info->getShapes().size();
int count = (int)_cpShapes.size();
cpVect* points = new cpVect[count];
int i = 0;
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
points[i++] = ((cpSegmentShape*)shape)->a;
}
@ -850,7 +840,7 @@ Vec2 PhysicsShapeEdgePolygon::getCenter()
void PhysicsShapeEdgePolygon::getPoints(cocos2d::Vec2 *outPoints) const
{
int i = 0;
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
outPoints[i++] = PhysicsHelper::cpv2point(((cpSegmentShape*)shape)->a);
}
@ -858,7 +848,7 @@ void PhysicsShapeEdgePolygon::getPoints(cocos2d::Vec2 *outPoints) const
int PhysicsShapeEdgePolygon::getPointsCount() const
{
return static_cast<int>(_info->getShapes().size());
return static_cast<int>(_cpShapes.size());
}
// PhysicsShapeEdgeChain
@ -882,7 +872,7 @@ void PhysicsShapeEdgePolygon::update(float delta)
cpFloat factorX = PhysicsHelper::float2cpfloat(_newScaleX / _scaleX);
cpFloat factorY = PhysicsHelper::float2cpfloat(_newScaleY / _scaleY);
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
cpVect a = cpSegmentShapeGetA(shape);
a.x *= factorX;
@ -902,7 +892,7 @@ bool PhysicsShapeEdgeChain::init(const Vec2* points, int count, const PhysicsMat
cpVect* vec = nullptr;
do
{
CC_BREAK_IF(!PhysicsShape::init(Type::EDGECHAIN));
_type = Type::EDGECHAIN;
vec = new cpVect[count];
PhysicsHelper::points2cpvs(points, vec, count);
@ -910,12 +900,12 @@ bool PhysicsShapeEdgeChain::init(const Vec2* points, int count, const PhysicsMat
int i = 0;
for (; i < count - 1; ++i)
{
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[i+1],
auto shape = cpSegmentShapeNew(s_sharedBody, vec[i], vec[i + 1],
PhysicsHelper::float2cpfloat(border));
CC_BREAK_IF(shape == nullptr);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
_info->add(shape);
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
addShape(shape);
}
CC_SAFE_DELETE_ARRAY(vec);
CC_BREAK_IF(i < count - 1);
@ -935,15 +925,15 @@ bool PhysicsShapeEdgeChain::init(const Vec2* points, int count, const PhysicsMat
Vec2 PhysicsShapeEdgeChain::getCenter()
{
int count = (int)_info->getShapes().size() + 1;
int count = (int)_cpShapes.size() + 1;
cpVect* points = new cpVect[count];
int i = 0;
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
points[i++] = ((cpSegmentShape*)shape)->a;
}
points[i++] = ((cpSegmentShape*)_info->getShapes().back())->b;
points[i++] = ((cpSegmentShape*)_cpShapes.back())->b;
Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, points));
delete[] points;
@ -954,17 +944,17 @@ Vec2 PhysicsShapeEdgeChain::getCenter()
void PhysicsShapeEdgeChain::getPoints(Vec2* outPoints) const
{
int i = 0;
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
outPoints[i++] = PhysicsHelper::cpv2point(((cpSegmentShape*)shape)->a);
}
outPoints[i++] = PhysicsHelper::cpv2point(((cpSegmentShape*)_info->getShapes().back())->b);
outPoints[i++] = PhysicsHelper::cpv2point(((cpSegmentShape*)_cpShapes.back())->b);
}
int PhysicsShapeEdgeChain::getPointsCount() const
{
return static_cast<int>(_info->getShapes().size() + 1);
return static_cast<int>(_cpShapes.size() + 1);
}
void PhysicsShapeEdgeChain::update(float delta)
@ -974,7 +964,7 @@ void PhysicsShapeEdgeChain::update(float delta)
cpFloat factorX = PhysicsHelper::float2cpfloat(_newScaleX / _scaleX);
cpFloat factorY = PhysicsHelper::float2cpfloat(_newScaleY / _scaleY);
for(auto shape : _info->getShapes())
for(auto shape : _cpShapes)
{
cpVect a = cpSegmentShapeGetA(shape);
a.x *= factorX;
@ -993,7 +983,7 @@ void PhysicsShape::setGroup(int group)
{
if (group < 0)
{
for (auto shape : _info->getShapes())
for (auto shape : _cpShapes)
{
cpShapeSetGroup(shape, (cpGroup)group);
}
@ -1004,7 +994,7 @@ void PhysicsShape::setGroup(int group)
bool PhysicsShape::containsPoint(const Vec2& point) const
{
for (auto shape : _info->getShapes())
for (auto shape : _cpShapes)
{
if (cpShapePointQuery(shape, PhysicsHelper::point2cpv(point)))
{

View File

@ -31,12 +31,11 @@
#include "base/CCRef.h"
#include "math/CCGeometry.h"
struct cpShape;
NS_CC_BEGIN
class PhysicsShapeInfo;
class PhysicsBody;
class PhysicsBodyInfo;
typedef struct CC_DLL PhysicsMaterial
{
@ -149,13 +148,6 @@ public:
inline int getGroup() { return _group; }
protected:
bool init(Type type);
/**
* @brief PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody.
*/
PhysicsBodyInfo* bodyInfo() const;
void setBody(PhysicsBody* body);
/** calculate the area of this shape */
@ -166,6 +158,7 @@ protected:
virtual void setScaleX(float scaleX);
virtual void setScaleY(float scaleY);
virtual void update(float delta);
void addShape(cpShape* shape);
protected:
PhysicsShape();
@ -173,7 +166,8 @@ protected:
protected:
PhysicsBody* _body;
PhysicsShapeInfo* _info;
std::vector<cpShape*> _cpShapes;
Type _type;
float _area;
float _mass;

View File

@ -24,23 +24,16 @@
#include "physics/CCPhysicsWorld.h"
#if CC_USE_PHYSICS
#include <algorithm>
#include <climits>
#include "chipmunk.h"
#include "physics/CCPhysicsBody.h"
#include "physics/CCPhysicsShape.h"
#include "CCPhysicsBody.h"
#include "CCPhysicsShape.h"
#include "CCPhysicsContact.h"
#include "physics/CCPhysicsJoint.h"
#include "CCPhysicsJoint.h"
#include "CCPhysicsContact.h"
#include "chipmunk/CCPhysicsWorldInfo_chipmunk.h"
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
#include "chipmunk/CCPhysicsContactInfo_chipmunk.h"
#include "chipmunk/CCPhysicsJointInfo_chipmunk.h"
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsHelper.h"
#include "2d/CCDrawNode.h"
#include "2d/CCScene.h"
@ -48,11 +41,10 @@
#include "base/CCEventDispatcher.h"
#include "base/CCEventCustom.h"
#include <algorithm>
NS_CC_BEGIN
const float PHYSICS_INFINITY = INFINITY;
extern const char* PHYSICSCONTACT_EVENT_NAME;
extern std::unordered_map<cpShape*, PhysicsShape*> s_physicsShapeMap;
const int PhysicsWorld::DEBUGDRAW_NONE = 0x00;
const int PhysicsWorld::DEBUGDRAW_SHAPE = 0x01;
@ -108,11 +100,11 @@ int PhysicsWorldCallback::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSp
{
CP_ARBITER_GET_SHAPES(arb, a, b);
auto ita = PhysicsShapeInfo::getMap().find(a);
auto itb = PhysicsShapeInfo::getMap().find(b);
CC_ASSERT(ita != PhysicsShapeInfo::getMap().end() && itb != PhysicsShapeInfo::getMap().end());
auto ita = s_physicsShapeMap.find(a);
auto itb = s_physicsShapeMap.find(b);
CC_ASSERT(ita != s_physicsShapeMap.end() && itb != s_physicsShapeMap.end());
PhysicsContact* contact = PhysicsContact::construct(ita->second->getShape(), itb->second->getShape());
auto contact = PhysicsContact::construct(ita->second, itb->second);
arb->data = contact;
contact->_contactInfo = arb;
@ -145,12 +137,12 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect
return;
}
auto it = PhysicsShapeInfo::getMap().find(shape);
CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
auto it = s_physicsShapeMap.find(shape);
CC_ASSERT(it != s_physicsShapeMap.end());
PhysicsRayCastInfo callbackInfo =
{
it->second->getShape(),
it->second,
info->p1,
info->p2,
Vec2(info->p1.x+(info->p2.x-info->p1.x)*t, info->p1.y+(info->p2.y-info->p1.y)*t),
@ -163,34 +155,34 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect
void PhysicsWorldCallback::queryRectCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info)
{
auto it = PhysicsShapeInfo::getMap().find(shape);
auto it = s_physicsShapeMap.find(shape);
CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
CC_ASSERT(it != s_physicsShapeMap.end());
if (!PhysicsWorldCallback::continues)
{
return;
}
PhysicsWorldCallback::continues = info->func(*info->world, *it->second->getShape(), info->data);
PhysicsWorldCallback::continues = info->func(*info->world, *it->second, info->data);
}
void PhysicsWorldCallback::getShapesAtPointFunc(cpShape *shape, cpFloat distance, cpVect point, Vector<PhysicsShape*>* arr)
{
auto it = PhysicsShapeInfo::getMap().find(shape);
auto it = s_physicsShapeMap.find(shape);
CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
CC_ASSERT(it != s_physicsShapeMap.end());
arr->pushBack(it->second->getShape());
arr->pushBack(it->second);
}
void PhysicsWorldCallback::queryPointFunc(cpShape *shape, cpFloat distance, cpVect point, PointQueryCallbackInfo *info)
{
auto it = PhysicsShapeInfo::getMap().find(shape);
auto it = s_physicsShapeMap.find(shape);
CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
CC_ASSERT(it != s_physicsShapeMap.end());
PhysicsWorldCallback::continues = info->func(*info->world, *it->second->getShape(), info->data);
PhysicsWorldCallback::continues = info->func(*info->world, *it->second, info->data);
}
void PhysicsWorld::debugDraw()
@ -343,7 +335,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vec2& point1,
RayCastCallbackInfo info = { this, func, point1, point2, data };
PhysicsWorldCallback::continues = true;
cpSpaceSegmentQuery(this->_info->getSpace(),
cpSpaceSegmentQuery(_cpSpace,
PhysicsHelper::point2cpv(point1),
PhysicsHelper::point2cpv(point2),
CP_ALL_LAYERS,
@ -362,7 +354,7 @@ void PhysicsWorld::queryRect(PhysicsQueryRectCallbackFunc func, const Rect& rect
RectQueryCallbackInfo info = {this, func, data};
PhysicsWorldCallback::continues = true;
cpSpaceBBQuery(this->_info->getSpace(),
cpSpaceBBQuery(_cpSpace,
PhysicsHelper::rect2cpbb(rect),
CP_ALL_LAYERS,
CP_NO_GROUP,
@ -380,7 +372,7 @@ void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Vec2& po
PointQueryCallbackInfo info = {this, func, data};
PhysicsWorldCallback::continues = true;
cpSpaceNearestPointQuery(this->_info->getSpace(),
cpSpaceNearestPointQuery(_cpSpace,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
@ -393,7 +385,7 @@ void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Vec2& po
Vector<PhysicsShape*> PhysicsWorld::getShapes(const Vec2& point) const
{
Vector<PhysicsShape*> arr;
cpSpaceNearestPointQuery(this->_info->getSpace(),
cpSpaceNearestPointQuery(_cpSpace,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
@ -406,14 +398,14 @@ Vector<PhysicsShape*> PhysicsWorld::getShapes(const Vec2& point) const
PhysicsShape* PhysicsWorld::getShape(const Vec2& point) const
{
cpShape* shape = cpSpaceNearestPointQueryNearest(this->_info->getSpace(),
cpShape* shape = cpSpaceNearestPointQueryNearest(_cpSpace,
PhysicsHelper::point2cpv(point),
0,
CP_ALL_LAYERS,
CP_NO_GROUP,
nullptr);
return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape();
return shape == nullptr ? nullptr : s_physicsShapeMap.find(shape)->second;
}
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
@ -432,14 +424,14 @@ bool PhysicsWorld::init(Scene& scene)
{
do
{
_info = new (std::nothrow) PhysicsWorldInfo();
CC_BREAK_IF(_info == nullptr);
_cpSpace = cpSpaceNew();
CC_BREAK_IF(_cpSpace == nullptr);
_scene = &scene;
_info->setGravity(_gravity);
cpSpaceSetGravity(_cpSpace, PhysicsHelper::point2cpv(_gravity));
cpSpaceSetDefaultCollisionHandler(_info->getSpace(),
cpSpaceSetDefaultCollisionHandler(_cpSpace,
(cpCollisionBeginFunc)PhysicsWorldCallback::collisionBeginCallbackFunc,
(cpCollisionPreSolveFunc)PhysicsWorldCallback::collisionPreSolveCallbackFunc,
(cpCollisionPostSolveFunc)PhysicsWorldCallback::collisionPostSolveCallbackFunc,
@ -476,9 +468,9 @@ void PhysicsWorld::doAddBody(PhysicsBody* body)
if (body->isEnabled())
{
// add body to space
if (body->isDynamic())
if (body->isDynamic() && !cpSpaceContainsBody(_cpSpace, body->_cpBody))
{
_info->addBody(*body->_info);
cpSpaceAddBody(_cpSpace, body->_cpBody);
}
// add shapes to space
@ -499,7 +491,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
return;
}
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
if (_delayAddBodies.find(body) == _delayAddBodies.end())
{
@ -515,7 +507,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
void PhysicsWorld::updateBodies()
{
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
return;
}
@ -593,7 +585,7 @@ void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
return;
}
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
if (_delayRemoveBodies.getIndex(body) == CC_INVALID_INDEX)
{
@ -608,12 +600,13 @@ void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
void PhysicsWorld::doAddJoint(PhysicsJoint *joint)
{
if (joint == nullptr || joint->_info == nullptr)
if (joint)
{
return;
for (auto constraint : joint->_cpConstraints)
{
cpSpaceAddConstraint(_cpSpace, constraint);
}
}
_info->addJoint(*joint->_info);
}
void PhysicsWorld::removeJoint(PhysicsJoint* joint, bool destroy)
@ -659,7 +652,7 @@ void PhysicsWorld::removeJoint(PhysicsJoint* joint, bool destroy)
void PhysicsWorld::updateJoints()
{
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
return;
}
@ -686,9 +679,15 @@ void PhysicsWorld::updateJoints()
void PhysicsWorld::removeShape(PhysicsShape* shape)
{
if (shape != nullptr)
if (shape)
{
_info->removeShape(*shape->_info);
for (auto cps : shape->_cpShapes)
{
if (cpSpaceContainsShape(_cpSpace, cps))
{
cpSpaceRemoveShape(_cpSpace, cps);
}
}
}
}
@ -701,7 +700,7 @@ void PhysicsWorld::addJointOrDelay(PhysicsJoint* joint)
return;
}
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
if (std::find(_delayAddJoints.begin(), _delayAddJoints.end(), joint) == _delayAddJoints.end())
{
@ -723,7 +722,7 @@ void PhysicsWorld::removeJointOrDelay(PhysicsJoint* joint)
return;
}
if (_info->isLocked())
if (cpSpaceIsLocked(_cpSpace))
{
if (std::find(_delayRemoveJoints.rbegin(), _delayRemoveJoints.rend(), joint) == _delayRemoveJoints.rend())
{
@ -783,11 +782,14 @@ void PhysicsWorld::removeAllJoints(bool destroy)
_joints.clear();
}
void PhysicsWorld::addShape(PhysicsShape* shape)
void PhysicsWorld::addShape(PhysicsShape* physicsShape)
{
if (shape != nullptr)
if (physicsShape)
{
_info->addShape(*shape->_info);
for (auto shape : physicsShape->_cpShapes)
{
cpSpaceAddShape(_cpSpace, shape);
}
}
}
@ -802,12 +804,18 @@ void PhysicsWorld::doRemoveBody(PhysicsBody* body)
}
// remove body
_info->removeBody(*body->_info);
if (cpSpaceContainsBody(_cpSpace, body->_cpBody))
{
cpSpaceRemoveBody(_cpSpace, body->_cpBody);
}
}
void PhysicsWorld::doRemoveJoint(PhysicsJoint* joint)
{
_info->removeJoint(*joint->_info);
for (auto constraint : joint->_cpConstraints)
{
cpSpaceRemoveConstraint(_cpSpace, constraint);
}
}
void PhysicsWorld::removeAllBodies()
@ -852,7 +860,7 @@ PhysicsBody* PhysicsWorld::getBody(int tag) const
void PhysicsWorld::setGravity(const Vect& gravity)
{
_gravity = gravity;
_info->setGravity(gravity);
cpSpaceSetGravity(_cpSpace, PhysicsHelper::point2cpv(gravity));
}
void PhysicsWorld::setSubsteps(int steps)
@ -891,7 +899,7 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
if (userCall)
{
_info->step(delta);
cpSpaceStep(_cpSpace, delta);
for (auto& body : _bodies)
{
body->update(delta);
@ -905,7 +913,7 @@ void PhysicsWorld::update(float delta, bool userCall/* = false*/)
const float dt = _updateTime * _speed / _substeps;
for (int i = 0; i < _substeps; ++i)
{
_info->step(dt);
cpSpaceStep(_cpSpace, dt);
for (auto& body : _bodies)
{
body->update(dt);
@ -929,7 +937,7 @@ PhysicsWorld::PhysicsWorld()
, _updateRateCount(0)
, _updateTime(0.0f)
, _substeps(1)
, _info(nullptr)
, _cpSpace(nullptr)
, _scene(nullptr)
, _delayDirty(false)
, _autoStep(true)
@ -943,7 +951,10 @@ PhysicsWorld::~PhysicsWorld()
{
removeAllJoints(true);
removeAllBodies();
CC_SAFE_DELETE(_info);
if (_cpSpace)
{
cpSpaceFree(_cpSpace);
}
CC_SAFE_DELETE(_debugDraw);
}
@ -976,7 +987,7 @@ void PhysicsDebugDraw::drawShape(PhysicsShape& shape)
const Color4F fillColor(1.0f, 0.0f, 0.0f, 0.3f);
const Color4F outlineColor(1.0f, 0.0f, 0.0f, 1.0f);
for (auto it = shape._info->getShapes().begin(); it != shape._info->getShapes().end(); ++it)
for (auto it = shape._cpShapes.begin(); it != shape._cpShapes.end(); ++it)
{
cpShape *subShape = *it;
@ -1032,7 +1043,7 @@ void PhysicsDebugDraw::drawJoint(PhysicsJoint& joint)
const Color4F lineColor(0.0f, 0.0f, 1.0f, 1.0f);
const Color4F jointPointColor(0.0f, 1.0f, 0.0f, 1.0f);
for (auto it = joint._info->getJoints().begin(); it != joint._info->getJoints().end(); ++it)
for (auto it = joint._cpConstraints.begin(); it != joint._cpConstraints.end(); ++it)
{
cpConstraint *constraint = *it;

View File

@ -34,11 +34,12 @@
#include "physics/CCPhysicsBody.h"
#include <list>
struct cpSpace;
NS_CC_BEGIN
class PhysicsBody;
class PhysicsJoint;
class PhysicsWorldInfo;
class PhysicsShape;
class PhysicsContact;
@ -203,7 +204,7 @@ protected:
int _updateRateCount;
float _updateTime;
int _substeps;
PhysicsWorldInfo* _info;
cpSpace* _cpSpace;
Vector<PhysicsBody*> _bodies;
std::list<PhysicsJoint*> _joints;

View File

@ -1,41 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "CCPhysicsBodyInfo_chipmunk.h"
#if CC_USE_PHYSICS
#include "chipmunk.h"
NS_CC_BEGIN
PhysicsBodyInfo::PhysicsBodyInfo()
: _body(nullptr)
{
}
PhysicsBodyInfo::~PhysicsBodyInfo()
{
if (_body) cpBodyFree(_body);
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,57 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCPHYSICS_BODY_INFO_CHIPMUNK_H__
#define __CCPHYSICS_BODY_INFO_CHIPMUNK_H__
#include "base/ccConfig.h"
#if CC_USE_PHYSICS
#include "platform/CCPlatformMacros.h"
#include "base/CCRef.h"
struct cpBody;
NS_CC_BEGIN
class PhysicsBodyInfo
{
public:
inline cpBody* getBody() const { return _body; }
inline void setBody(cpBody* body) { _body = body; }
private:
PhysicsBodyInfo();
~PhysicsBodyInfo();
private:
cpBody* _body;
friend class PhysicsBody;
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_BODY_INFO_CHIPMUNK_H__

View File

@ -1,40 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "CCPhysicsContactInfo_chipmunk.h"
#if CC_USE_PHYSICS
#include "chipmunk.h"
NS_CC_BEGIN
PhysicsContactInfo::PhysicsContactInfo(PhysicsContact* contact)
: _contact(contact)
{
}
PhysicsContactInfo::~PhysicsContactInfo()
{
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,53 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCPHYSICS_CONTACT_INFO_CHIPMUNK_H__
#define __CCPHYSICS_CONTACT_INFO_CHIPMUNK_H__
#include "base/ccConfig.h"
#if CC_USE_PHYSICS
#include "platform/CCPlatformMacros.h"
NS_CC_BEGIN
class PhysicsContact;
class PhysicsContactInfo
{
public:
inline PhysicsContact* getContact() const { return _contact; }
private:
PhysicsContactInfo(PhysicsContact* contact);
~PhysicsContactInfo();
private:
PhysicsContact* _contact;
friend class PhysicsContact;
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_CONTACT_INFO_CHIPMUNK_H__

View File

@ -1,85 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "CCPhysicsJointInfo_chipmunk.h"
#if CC_USE_PHYSICS
#include <algorithm>
#include <unordered_map>
#include "chipmunk.h"
NS_CC_BEGIN
std::unordered_map<cpConstraint*, PhysicsJointInfo*> PhysicsJointInfo::_map;
PhysicsJointInfo::PhysicsJointInfo(PhysicsJoint* joint)
: _joint(joint)
{
}
PhysicsJointInfo::~PhysicsJointInfo()
{
for (cpConstraint* joint : _joints)
{
cpConstraintFree(joint);
}
}
void PhysicsJointInfo::add(cpConstraint* joint)
{
if (joint == nullptr) return;
_joints.push_back(joint);
_map.insert(std::pair<cpConstraint*, PhysicsJointInfo*>(joint, this));
}
void PhysicsJointInfo::remove(cpConstraint* joint)
{
if (joint == nullptr) return;
auto it = std::find(_joints.begin(), _joints.end(), joint);
if (it != _joints.end())
{
_joints.erase(it);
auto mit = _map.find(joint);
if (mit != _map.end()) _map.erase(mit);
cpConstraintFree(joint);
}
}
void PhysicsJointInfo::removeAll()
{
for (cpConstraint* joint : _joints)
{
auto mit = _map.find(joint);
if (mit != _map.end()) _map.erase(mit);
cpConstraintFree(joint);
}
_joints.clear();
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,65 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCPHYSICS_JOINT_INFO_CHIPMUNK_H__
#define __CCPHYSICS_JOINT_INFO_CHIPMUNK_H__
#include "base/ccConfig.h"
#if CC_USE_PHYSICS
#include "platform/CCPlatformMacros.h"
#include <vector>
#include <unordered_map>
struct cpConstraint;
NS_CC_BEGIN
class PhysicsJoint;
class PhysicsJointInfo
{
public:
void add(cpConstraint* shape);
void remove(cpConstraint* shape);
void removeAll();
PhysicsJoint* getJoint() const { return _joint; }
std::vector<cpConstraint*>& getJoints() { return _joints; }
static std::unordered_map<cpConstraint*, PhysicsJointInfo*>& getMap() { return _map; }
protected:
PhysicsJointInfo(PhysicsJoint* joint);
~PhysicsJointInfo();
std::vector<cpConstraint*> _joints;
PhysicsJoint* _joint;
static std::unordered_map<cpConstraint*, PhysicsJointInfo*> _map;
friend class PhysicsJoint;
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_JOINT_INFO_CHIPMUNK_H__

View File

@ -1,118 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "CCPhysicsShapeInfo_chipmunk.h"
#if CC_USE_PHYSICS
#include <algorithm>
#include <unordered_map>
NS_CC_BEGIN
std::unordered_map<cpShape*, PhysicsShapeInfo*> PhysicsShapeInfo::_map;
cpBody* PhysicsShapeInfo::_sharedBody = nullptr;
PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape)
: _shape(shape)
, _group(CP_NO_GROUP)
{
if (_sharedBody == nullptr)
{
_sharedBody = cpBodyNewStatic();
}
_body = _sharedBody;
}
PhysicsShapeInfo::~PhysicsShapeInfo()
{
for (auto shape : _shapes)
{
auto it = _map.find(shape);
if (it != _map.end()) _map.erase(shape);
cpShapeFree(shape);
}
}
void PhysicsShapeInfo::setGroup(cpGroup group)
{
this->_group = group;
for (cpShape* shape : _shapes)
{
cpShapeSetGroup(shape, group);
}
}
void PhysicsShapeInfo::setBody(cpBody* body)
{
if (this->_body != body)
{
this->_body = body;
for (cpShape* shape : _shapes)
{
cpShapeSetBody(shape, body == nullptr ? _sharedBody : body);
}
}
}
void PhysicsShapeInfo::add(cpShape* shape)
{
if (shape == nullptr) return;
cpShapeSetGroup(shape, _group);
_shapes.push_back(shape);
_map.insert(std::pair<cpShape*, PhysicsShapeInfo*>(shape, this));
}
void PhysicsShapeInfo::remove(cpShape* shape)
{
if (shape == nullptr) return;
auto it = std::find(_shapes.begin(), _shapes.end(), shape);
if (it != _shapes.end())
{
_shapes.erase(it);
auto mit = _map.find(shape);
if (mit != _map.end()) _map.erase(mit);
cpShapeFree(shape);
}
}
void PhysicsShapeInfo::removeAll()
{
for (cpShape* shape : _shapes)
{
auto mit = _map.find(shape);
if (mit != _map.end()) _map.erase(mit);
cpShapeFree(shape);
}
_shapes.clear();
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,73 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCPHYSICS_SHAPE_INFO_CHIPMUNK_H__
#define __CCPHYSICS_SHAPE_INFO_CHIPMUNK_H__
#include "base/ccConfig.h"
#if CC_USE_PHYSICS
#include <vector>
#include <unordered_map>
#include "platform/CCPlatformMacros.h"
#include "chipmunk.h"
NS_CC_BEGIN
class PhysicsShape;
class PhysicsShapeInfo
{
public:
void add(cpShape* shape);
void remove(cpShape* shape);
void removeAll();
void setGroup(cpGroup group);
void setBody(cpBody* body);
PhysicsShape* getShape() const { return _shape; }
std::vector<cpShape*>& getShapes() { return _shapes; }
cpBody* getBody() const { return _body; }
cpGroup getGourp() const { return _group; }
static std::unordered_map<cpShape*, PhysicsShapeInfo*>& getMap() { return _map; }
static cpBody* getSharedBody() { return _sharedBody; }
protected:
PhysicsShapeInfo(PhysicsShape* shape);
~PhysicsShapeInfo();
std::vector<cpShape*> _shapes;
PhysicsShape* _shape;
cpBody* _body;
cpGroup _group;
static std::unordered_map<cpShape*, PhysicsShapeInfo*> _map;
static cpBody* _sharedBody;
friend class PhysicsShape;
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_SHAPE_INFO_CHIPMUNK_H__

View File

@ -1,112 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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 "CCPhysicsWorldInfo_chipmunk.h"
#if CC_USE_PHYSICS
#include "CCPhysicsHelper_chipmunk.h"
#include "CCPhysicsBodyInfo_chipmunk.h"
#include "CCPhysicsShapeInfo_chipmunk.h"
#include "CCPhysicsJointInfo_chipmunk.h"
#include "chipmunk.h"
NS_CC_BEGIN
PhysicsWorldInfo::PhysicsWorldInfo()
{
_space = cpSpaceNew();
}
PhysicsWorldInfo::~PhysicsWorldInfo()
{
cpSpaceFree(_space);
}
void PhysicsWorldInfo::setGravity(const Vect& gravity)
{
cpSpaceSetGravity(_space, PhysicsHelper::point2cpv(gravity));
}
void PhysicsWorldInfo::addBody(PhysicsBodyInfo& body)
{
if (!cpSpaceContainsBody(_space, body.getBody()))
{
cpSpaceAddBody(_space, body.getBody());
}
}
void PhysicsWorldInfo::removeBody(PhysicsBodyInfo& body)
{
if (cpSpaceContainsBody(_space, body.getBody()))
{
cpSpaceRemoveBody(_space, body.getBody());
}
}
void PhysicsWorldInfo::addShape(PhysicsShapeInfo& shape)
{
for (auto cps : shape.getShapes())
{
cpSpaceAddShape(_space, cps);
}
}
void PhysicsWorldInfo::removeShape(PhysicsShapeInfo& shape)
{
for (auto cps : shape.getShapes())
{
if (cpSpaceContainsShape(_space, cps))
{
cpSpaceRemoveShape(_space, cps);
}
}
}
void PhysicsWorldInfo::addJoint(PhysicsJointInfo& joint)
{
for (auto subjoint : joint.getJoints())
{
cpSpaceAddConstraint(_space, subjoint);
}
}
void PhysicsWorldInfo::removeJoint(PhysicsJointInfo& joint)
{
for (auto subjoint : joint.getJoints())
{
cpSpaceRemoveConstraint(_space, subjoint);
}
}
bool PhysicsWorldInfo::isLocked()
{
return 0 == _space->locked_private ? false : true;
}
void PhysicsWorldInfo::step(float delta)
{
cpSpaceStep(_space, delta);
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,71 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCPHYSICS_WORLD_INFO_CHIPMUNK_H__
#define __CCPHYSICS_WORLD_INFO_CHIPMUNK_H__
#include "base/ccConfig.h"
#if CC_USE_PHYSICS
#include <vector>
#include "platform/CCPlatformMacros.h"
#include "math/CCGeometry.h"
struct cpSpace;
NS_CC_BEGIN
typedef Vec2 Vect;
class PhysicsBodyInfo;
class PhysicsJointInfo;
class PhysicsShapeInfo;
class PhysicsWorldInfo
{
public:
cpSpace* getSpace() const { return _space; }
void addShape(PhysicsShapeInfo& shape);
void removeShape(PhysicsShapeInfo& shape);
void addBody(PhysicsBodyInfo& body);
void removeBody(PhysicsBodyInfo& body);
void addJoint(PhysicsJointInfo& joint);
void removeJoint(PhysicsJointInfo& joint);
void setGravity(const Vect& gravity);
bool isLocked();
void step(float delta);
private:
PhysicsWorldInfo();
~PhysicsWorldInfo();
private:
cpSpace* _space;
friend class PhysicsWorld;
};
NS_CC_END
#endif // CC_USE_PHYSICS
#endif // __CCPHYSICS_WORLD_INFO_CHIPMUNK_H__