mirror of https://github.com/axmolengine/axmol.git
issue #2771: let PhysicsShape::create doesn't need to pass the PhysicsBody
This commit is contained in:
parent
35259588a3
commit
4747e37acf
|
@ -42,6 +42,8 @@
|
|||
#include "Box2D/CCPhysicsJointInfo.h"
|
||||
#include "chipmunk/CCPhysicsWorldInfo.h"
|
||||
#include "Box2D/CCPhysicsWorldInfo.h"
|
||||
#include "chipmunk/CCPhysicsShapeInfo.h"
|
||||
#include "Box2D/CCPhysicsShapeInfo.h"
|
||||
|
||||
#include "chipmunk/CCPhysicsHelper.h"
|
||||
#include "Box2D/CCPhysicsHelper.h"
|
||||
|
@ -95,7 +97,7 @@ PhysicsBody* PhysicsBody::createCircle(float radius, float density)
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapeCircle::create(body, radius, density));
|
||||
body->addShape(PhysicsShapeCircle::create(radius, density));
|
||||
body->autorelease();
|
||||
return body;
|
||||
}
|
||||
|
@ -109,7 +111,7 @@ PhysicsBody* PhysicsBody::createBox(Size size, float density)
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapeBox::create(body, size, density));
|
||||
body->addShape(PhysicsShapeBox::create(size, density));
|
||||
body->autorelease();
|
||||
return body;
|
||||
}
|
||||
|
@ -123,7 +125,7 @@ PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, float density)
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapePolygon::create(body, points, count, density));
|
||||
body->addShape(PhysicsShapePolygon::create(points, count, density));
|
||||
body->autorelease();
|
||||
return body;
|
||||
}
|
||||
|
@ -137,7 +139,7 @@ PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, float border/* = 1
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapeEdgeSegment::create(body, a, b, border));
|
||||
body->addShape(PhysicsShapeEdgeSegment::create(a, b, border));
|
||||
body->_dynamic = false;
|
||||
body->autorelease();
|
||||
return body;
|
||||
|
@ -152,7 +154,7 @@ PhysicsBody* PhysicsBody::createEdgeBox(Size size, float border/* = 1*/)
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapeEdgeBox::create(body, size, border));
|
||||
body->addShape(PhysicsShapeEdgeBox::create(size, border));
|
||||
body->_dynamic = false;
|
||||
body->autorelease();
|
||||
return body;
|
||||
|
@ -168,7 +170,7 @@ PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, float bord
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapePolygon::create(body, points, count, border));
|
||||
body->addShape(PhysicsShapePolygon::create(points, count, border));
|
||||
body->_dynamic = false;
|
||||
body->autorelease();
|
||||
return body;
|
||||
|
@ -184,7 +186,7 @@ PhysicsBody* PhysicsBody::createEdgeChain(Point* points, int count, float border
|
|||
PhysicsBody* body = new PhysicsBody();
|
||||
if (body && body->init())
|
||||
{
|
||||
body->addShape(PhysicsShapeEdgeChain::create(body, points, count, border));
|
||||
body->addShape(PhysicsShapeEdgeChain::create(points, count, border));
|
||||
body->_dynamic = false;
|
||||
body->autorelease();
|
||||
return body;
|
||||
|
@ -221,8 +223,8 @@ void PhysicsBody::setDynamic(bool dynamic)
|
|||
cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_angularDamping));
|
||||
}else
|
||||
{
|
||||
cpBodySetMass(_info->body, INFINITY);
|
||||
cpBodySetMoment(_info->body, INFINITY);
|
||||
cpBodySetMass(_info->body, PHYSICS_INFINITY);
|
||||
cpBodySetMoment(_info->body, PHYSICS_INFINITY);
|
||||
}
|
||||
|
||||
_dynamic = dynamic;
|
||||
|
@ -254,18 +256,37 @@ void PhysicsBody::addShape(PhysicsShape* shape)
|
|||
{
|
||||
if (shape == nullptr) return;
|
||||
|
||||
if (shape->getBody() != this) CCASSERT(false, "");
|
||||
// already added
|
||||
if (shape->getBody() == this)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (shape->getBody() != nullptr)
|
||||
{
|
||||
shape->getBody()->removeShape(shape);
|
||||
}
|
||||
|
||||
// reset the body
|
||||
if (shape->_info->body != _info->body)
|
||||
{
|
||||
for (cpShape* subShape : shape->_info->shapes)
|
||||
{
|
||||
cpShapeSetBody(subShape, _info->body);
|
||||
}
|
||||
shape->_info->body = _info->body;
|
||||
}
|
||||
|
||||
// add shape to body
|
||||
if (std::find(_shapes.begin(), _shapes.end(), shape) == _shapes.end())
|
||||
{
|
||||
shape->setBody(this);
|
||||
_shapes.push_back(shape);
|
||||
shape->_isAddToBody = true;
|
||||
|
||||
// calculate the mass, area and desity
|
||||
_area += shape->getArea();
|
||||
if (_mass == INFINITY || shape->getMass() == INFINITY)
|
||||
if (_mass == PHYSICS_INFINITY || shape->getMass() == PHYSICS_INFINITY)
|
||||
{
|
||||
_mass = INFINITY;
|
||||
_mass = PHYSICS_INFINITY;
|
||||
_massDefault = false;
|
||||
}else
|
||||
{
|
||||
|
@ -279,9 +300,9 @@ void PhysicsBody::addShape(PhysicsShape* shape)
|
|||
|
||||
if (!_massDefault)
|
||||
{
|
||||
if (_mass == INFINITY)
|
||||
if (_mass == PHYSICS_INFINITY)
|
||||
{
|
||||
_density = INFINITY;
|
||||
_density = PHYSICS_INFINITY;
|
||||
}else
|
||||
{
|
||||
_density = _mass / _area;
|
||||
|
@ -389,11 +410,10 @@ void PhysicsBody::removeShape(PhysicsShape* shape)
|
|||
}
|
||||
|
||||
_shapes.erase(it);
|
||||
shape->_isAddToBody = false;
|
||||
|
||||
|
||||
// deduce the mass, area and angularDamping
|
||||
if (_mass != INFINITY && shape->getMass() != INFINITY)
|
||||
if (_mass != PHYSICS_INFINITY && shape->getMass() != PHYSICS_INFINITY)
|
||||
{
|
||||
if (_mass - shape->getMass() <= 0)
|
||||
{
|
||||
|
@ -406,9 +426,9 @@ void PhysicsBody::removeShape(PhysicsShape* shape)
|
|||
|
||||
_area -= shape->getArea();
|
||||
|
||||
if (_mass == INFINITY)
|
||||
if (_mass == PHYSICS_INFINITY)
|
||||
{
|
||||
_density = INFINITY;
|
||||
_density = PHYSICS_INFINITY;
|
||||
}
|
||||
else if (_area > 0)
|
||||
{
|
||||
|
|
|
@ -43,4 +43,9 @@
|
|||
#define CC_USE_PHYSICS
|
||||
#endif
|
||||
|
||||
namespace cocos2d
|
||||
{
|
||||
extern const float PHYSICS_INFINITY;
|
||||
}
|
||||
|
||||
#endif // __CCPHYSICS_SETTING_H__
|
||||
|
|
|
@ -52,7 +52,6 @@ PhysicsShape::PhysicsShape()
|
|||
, _density(0)
|
||||
, _tag(0)
|
||||
, _enable(true)
|
||||
, _isAddToBody(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -62,12 +61,8 @@ PhysicsShape::~PhysicsShape()
|
|||
CC_SAFE_DELETE(_info);
|
||||
}
|
||||
|
||||
bool PhysicsShape::init(PhysicsBody* body, Type type)
|
||||
bool PhysicsShape::init(Type type)
|
||||
{
|
||||
if (body == nullptr) return false;
|
||||
|
||||
_body = body;
|
||||
|
||||
_info = new PhysicsShapeInfo(this);
|
||||
if (_info == nullptr) return false;
|
||||
|
||||
|
@ -107,7 +102,13 @@ void PhysicsShape::addToBody()
|
|||
|
||||
PhysicsBodyInfo* PhysicsShape::bodyInfo() const
|
||||
{
|
||||
return _body->_info;
|
||||
if (_body != nullptr)
|
||||
{
|
||||
return _body->_info;
|
||||
}else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
PhysicsShapeCircle::PhysicsShapeCircle()
|
||||
|
@ -182,11 +183,28 @@ PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment()
|
|||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
|
||||
|
||||
void PhysicsShape::setElasticity(float elasticity)
|
||||
{
|
||||
for (cpShape* shape : _info->shapes)
|
||||
{
|
||||
cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(elasticity));
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsShape::setFriction(float friction)
|
||||
{
|
||||
for (cpShape* shape : _info->shapes)
|
||||
{
|
||||
cpShapeSetFriction(shape, PhysicsHelper::float2cpfloat(friction));
|
||||
}
|
||||
}
|
||||
|
||||
// PhysicsShapeCircle
|
||||
PhysicsShapeCircle* PhysicsShapeCircle::create(PhysicsBody* body, float radius, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
{
|
||||
PhysicsShapeCircle* shape = new PhysicsShapeCircle();
|
||||
if (shape && shape->init(body, radius, density, offset))
|
||||
if (shape && shape->init(radius, density, offset))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -196,20 +214,20 @@ PhysicsShapeCircle* PhysicsShapeCircle::create(PhysicsBody* body, float radius,
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeCircle::init(PhysicsBody* body, float radius, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
bool PhysicsShapeCircle::init(float radius, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
{
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::CIRCLE));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE));
|
||||
|
||||
cpShape* shape = cpCircleShapeNew(bodyInfo()->body, radius, PhysicsHelper::point2cpv(offset));
|
||||
cpShape* shape = cpCircleShapeNew(_info->shareBody, radius, PhysicsHelper::point2cpv(offset));
|
||||
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
|
||||
_density = density;
|
||||
_area = PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
|
||||
_mass = _density == INFINITY ? INFINITY : _density * _area;
|
||||
_angularDamping = _mass == INFINITY ? INFINITY : cpMomentForCircle(_mass, 0, radius, PhysicsHelper::point2cpv(offset));
|
||||
_mass = _density == PHYSICS_INFINITY ? PHYSICS_INFINITY : _density * _area;
|
||||
_angularDamping = _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : cpMomentForCircle(_mass, 0, radius, PhysicsHelper::point2cpv(offset));
|
||||
|
||||
_info->add(shape);
|
||||
|
||||
|
@ -220,10 +238,10 @@ bool PhysicsShapeCircle::init(PhysicsBody* body, float radius, float density/* =
|
|||
}
|
||||
|
||||
// PhysicsShapeEdgeSegment
|
||||
PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(PhysicsBody* body, Point a, Point b, float border/* = 1*/)
|
||||
PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, float border/* = 1*/)
|
||||
{
|
||||
PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment();
|
||||
if (shape && shape->init(body, a, b, border))
|
||||
if (shape && shape->init(a, b, border))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -233,21 +251,21 @@ PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(PhysicsBody* body, Poin
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeEdgeSegment::init(PhysicsBody* body, Point a, Point b, float border/* = 1*/)
|
||||
bool PhysicsShapeEdgeSegment::init(Point a, Point b, float border/* = 1*/)
|
||||
{
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGESEGMENT));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT));
|
||||
|
||||
cpShape* shape = cpSegmentShapeNew(bodyInfo()->body,
|
||||
cpShape* shape = cpSegmentShapeNew(_info->shareBody,
|
||||
PhysicsHelper::point2cpv(a),
|
||||
PhysicsHelper::point2cpv(b),
|
||||
PhysicsHelper::float2cpfloat(border));
|
||||
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
|
||||
_density = INFINITY;
|
||||
_angularDamping = INFINITY;
|
||||
_density = PHYSICS_INFINITY;
|
||||
_angularDamping = PHYSICS_INFINITY;
|
||||
|
||||
_info->add(shape);
|
||||
|
||||
|
@ -258,10 +276,10 @@ bool PhysicsShapeEdgeSegment::init(PhysicsBody* body, Point a, Point b, float bo
|
|||
}
|
||||
|
||||
// PhysicsShapeBox
|
||||
PhysicsShapeBox* PhysicsShapeBox::create(PhysicsBody* body, Size size, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
PhysicsShapeBox* PhysicsShapeBox::create(Size size, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
{
|
||||
PhysicsShapeBox* shape = new PhysicsShapeBox();
|
||||
if (shape && shape->init(body, size, density, offset))
|
||||
if (shape && shape->init(size, density, offset))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -271,13 +289,13 @@ PhysicsShapeBox* PhysicsShapeBox::create(PhysicsBody* body, Size size, float den
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeBox::init(PhysicsBody* body, Size size, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
bool PhysicsShapeBox::init(Size size, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
{
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::BOX));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::BOX));
|
||||
|
||||
cpShape* shape = cpBoxShapeNew(bodyInfo()->body, PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height));
|
||||
cpShape* shape = cpBoxShapeNew(_info->shareBody, PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height));
|
||||
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
|
||||
|
@ -298,10 +316,10 @@ bool PhysicsShapeBox::init(PhysicsBody* body, Size size, float density/* = 0*/,
|
|||
}
|
||||
|
||||
// PhysicsShapeCircle
|
||||
PhysicsShapePolygon* PhysicsShapePolygon::create(PhysicsBody* body, Point* points, int count, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
PhysicsShapePolygon* PhysicsShapePolygon::create(Point* points, int count, float density/* = 0*/, Point offset/* = Point(0, 0)*/)
|
||||
{
|
||||
PhysicsShapePolygon* shape = new PhysicsShapePolygon();
|
||||
if (shape && shape->init(body, points, count, density, offset))
|
||||
if (shape && shape->init(points, count, density, offset))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -311,15 +329,15 @@ PhysicsShapePolygon* PhysicsShapePolygon::create(PhysicsBody* body, Point* point
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapePolygon::init(PhysicsBody* body, Point* points, int count, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
bool PhysicsShapePolygon::init(Point* points, int count, float density/* = 0*/, Point offset /*= Point(0, 0)*/)
|
||||
{
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::POLYGEN));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN));
|
||||
|
||||
cpVect* vecs = new cpVect[count];
|
||||
PhysicsHelper::points2cpvs(points, vecs, count);
|
||||
cpShape* shape = cpPolyShapeNew(bodyInfo()->body, count, vecs, PhysicsHelper::point2cpv(offset));
|
||||
cpShape* shape = cpPolyShapeNew(_info->shareBody, count, vecs, PhysicsHelper::point2cpv(offset));
|
||||
CC_SAFE_DELETE(vecs);
|
||||
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
|
@ -341,10 +359,10 @@ bool PhysicsShapePolygon::init(PhysicsBody* body, Point* points, int count, floa
|
|||
}
|
||||
|
||||
// PhysicsShapeEdgeBox
|
||||
PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(PhysicsBody* body, Size size, float border/* = 1*/, Point offset/* = Point(0, 0)*/)
|
||||
PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(Size size, float border/* = 1*/, Point offset/* = Point(0, 0)*/)
|
||||
{
|
||||
PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox();
|
||||
if (shape && shape->init(body, size, border, offset))
|
||||
if (shape && shape->init(size, border, offset))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -354,24 +372,22 @@ PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(PhysicsBody* body, Size size, f
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeEdgeBox::init(PhysicsBody* body, Size size, float border/* = 1*/, Point offset/*= Point(0, 0)*/)
|
||||
bool PhysicsShapeEdgeBox::init(Size size, float border/* = 1*/, Point offset/*= Point(0, 0)*/)
|
||||
{
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGEBOX));
|
||||
|
||||
Point bodyPos = body->getPosition();
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEBOX));
|
||||
|
||||
cpVect vec[4] = {};
|
||||
vec[0] = PhysicsHelper::point2cpv(Point(bodyPos.x-size.width/2+offset.x, bodyPos.y-size.height/2+offset.y));
|
||||
vec[1] = PhysicsHelper::point2cpv(Point(bodyPos.x+size.width/2+offset.x, bodyPos.y-size.height/2+offset.y));
|
||||
vec[2] = PhysicsHelper::point2cpv(Point(bodyPos.x+size.width/2+offset.x, bodyPos.y+size.height/2+offset.y));
|
||||
vec[3] = PhysicsHelper::point2cpv(Point(bodyPos.x-size.width/2+offset.x, bodyPos.y+size.height/2+offset.y));
|
||||
vec[0] = PhysicsHelper::point2cpv(Point(-size.width/2+offset.x, -size.height/2+offset.y));
|
||||
vec[1] = PhysicsHelper::point2cpv(Point(+size.width/2+offset.x, -size.height/2+offset.y));
|
||||
vec[2] = PhysicsHelper::point2cpv(Point(+size.width/2+offset.x, +size.height/2+offset.y));
|
||||
vec[3] = PhysicsHelper::point2cpv(Point(-size.width/2+offset.x, +size.height/2+offset.y));
|
||||
|
||||
int i = 0;
|
||||
for (; i < 4; ++i)
|
||||
{
|
||||
cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[(i+1)%4],
|
||||
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%4],
|
||||
PhysicsHelper::float2cpfloat(border));
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
cpShapeSetElasticity(shape, 1.0f);
|
||||
|
@ -380,8 +396,8 @@ bool PhysicsShapeEdgeBox::init(PhysicsBody* body, Size size, float border/* = 1*
|
|||
}
|
||||
CC_BREAK_IF(i < 4);
|
||||
|
||||
_mass = INFINITY;
|
||||
_angularDamping = INFINITY;
|
||||
_mass = PHYSICS_INFINITY;
|
||||
_angularDamping = PHYSICS_INFINITY;
|
||||
|
||||
return true;
|
||||
} while (false);
|
||||
|
@ -390,10 +406,10 @@ bool PhysicsShapeEdgeBox::init(PhysicsBody* body, Size size, float border/* = 1*
|
|||
}
|
||||
|
||||
// PhysicsShapeEdgeBox
|
||||
PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(PhysicsBody* body, Point* points, int count, float border/* = 1*/)
|
||||
PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(Point* points, int count, float border/* = 1*/)
|
||||
{
|
||||
PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon();
|
||||
if (shape && shape->init(body, points, count, border))
|
||||
if (shape && shape->init(points, count, border))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -403,12 +419,12 @@ PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(PhysicsBody* body, Poin
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeEdgePolygon::init(PhysicsBody* body, Point* points, int count, float border/* = 1*/)
|
||||
bool PhysicsShapeEdgePolygon::init(Point* points, int count, float border/* = 1*/)
|
||||
{
|
||||
cpVect* vec = nullptr;
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGEPOLYGEN));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEPOLYGEN));
|
||||
|
||||
vec = new cpVect[count];
|
||||
PhysicsHelper::points2cpvs(points, vec, count);
|
||||
|
@ -416,7 +432,7 @@ bool PhysicsShapeEdgePolygon::init(PhysicsBody* body, Point* points, int count,
|
|||
int i = 0;
|
||||
for (; i < count; ++i)
|
||||
{
|
||||
cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[(i+1)%count],
|
||||
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%count],
|
||||
PhysicsHelper::float2cpfloat(border));
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
cpShapeSetElasticity(shape, 1.0f);
|
||||
|
@ -427,8 +443,8 @@ bool PhysicsShapeEdgePolygon::init(PhysicsBody* body, Point* points, int count,
|
|||
|
||||
CC_BREAK_IF(i < count);
|
||||
|
||||
_mass = INFINITY;
|
||||
_angularDamping = INFINITY;
|
||||
_mass = PHYSICS_INFINITY;
|
||||
_angularDamping = PHYSICS_INFINITY;
|
||||
|
||||
return true;
|
||||
} while (false);
|
||||
|
@ -439,10 +455,10 @@ bool PhysicsShapeEdgePolygon::init(PhysicsBody* body, Point* points, int count,
|
|||
}
|
||||
|
||||
// PhysicsShapeEdgeChain
|
||||
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(PhysicsBody* body, Point* points, int count, float border/* = 1*/)
|
||||
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(Point* points, int count, float border/* = 1*/)
|
||||
{
|
||||
PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain();
|
||||
if (shape && shape->init(body, points, count, border))
|
||||
if (shape && shape->init(points, count, border))
|
||||
{
|
||||
shape->autorelease();
|
||||
return shape;
|
||||
|
@ -452,12 +468,12 @@ PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(PhysicsBody* body, Point* p
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsShapeEdgeChain::init(PhysicsBody* body, Point* points, int count, float border/* = 1*/)
|
||||
bool PhysicsShapeEdgeChain::init(Point* points, int count, float border/* = 1*/)
|
||||
{
|
||||
cpVect* vec = nullptr;
|
||||
do
|
||||
{
|
||||
CC_BREAK_IF(!PhysicsShape::init(body, Type::EDGECHAIN));
|
||||
CC_BREAK_IF(!PhysicsShape::init(Type::EDGECHAIN));
|
||||
|
||||
vec = new cpVect[count];
|
||||
PhysicsHelper::points2cpvs(points, vec, count);
|
||||
|
@ -465,7 +481,7 @@ bool PhysicsShapeEdgeChain::init(PhysicsBody* body, Point* points, int count, fl
|
|||
int i = 0;
|
||||
for (; i < count - 1; ++i)
|
||||
{
|
||||
cpShape* shape = cpSegmentShapeNew(bodyInfo()->body, vec[i], vec[i+1],
|
||||
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[i+1],
|
||||
PhysicsHelper::float2cpfloat(border));
|
||||
CC_BREAK_IF(shape == nullptr);
|
||||
cpShapeSetElasticity(shape, 1.0f);
|
||||
|
@ -475,8 +491,8 @@ bool PhysicsShapeEdgeChain::init(PhysicsBody* body, Point* points, int count, fl
|
|||
if (vec != nullptr) delete[] vec;
|
||||
CC_BREAK_IF(i < count);
|
||||
|
||||
_mass = INFINITY;
|
||||
_angularDamping = INFINITY;
|
||||
_mass = PHYSICS_INFINITY;
|
||||
_angularDamping = PHYSICS_INFINITY;
|
||||
|
||||
return true;
|
||||
} while (false);
|
||||
|
@ -486,7 +502,6 @@ bool PhysicsShapeEdgeChain::init(PhysicsBody* body, Point* points, int count, fl
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
||||
|
||||
#endif
|
||||
|
|
|
@ -71,14 +71,21 @@ public:
|
|||
inline bool isEnable() { return _enable; }
|
||||
void addToBody();
|
||||
|
||||
void setElasticity(float elasticity);
|
||||
void setFriction(float friction);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Type type);
|
||||
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;
|
||||
|
||||
inline void setBody(PhysicsBody* body) { _body = body; }
|
||||
|
||||
protected:
|
||||
PhysicsShape();
|
||||
virtual ~PhysicsShape() = 0;
|
||||
|
@ -93,7 +100,6 @@ protected:
|
|||
float _density;
|
||||
int _tag;
|
||||
bool _enable;
|
||||
bool _isAddToBody;
|
||||
|
||||
friend class PhysicsWorld;
|
||||
friend class PhysicsBody;
|
||||
|
@ -103,10 +109,10 @@ protected:
|
|||
class PhysicsShapeCircle : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeCircle* create(PhysicsBody* body, float radius, float density = 0, Point offset = Point(0, 0));
|
||||
static PhysicsShapeCircle* create(float radius, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, float radius, float density = 0, Point offset = Point(0, 0));
|
||||
bool init(float radius, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
PhysicsShapeCircle();
|
||||
|
@ -119,10 +125,10 @@ protected:
|
|||
class PhysicsShapeBox : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeBox* create(PhysicsBody* body, Size size, float density = 0, Point offset = Point(0, 0));
|
||||
static PhysicsShapeBox* create(Size size, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Size size, float density = 0, Point offset = Point(0, 0));
|
||||
bool init(Size size, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
PhysicsShapeBox();
|
||||
|
@ -135,10 +141,10 @@ protected:
|
|||
class PhysicsShapePolygon : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapePolygon* create(PhysicsBody* body, Point* points, int count, float density = 0, Point offset = Point(0, 0));
|
||||
static PhysicsShapePolygon* create(Point* points, int count, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Point* points, int count, float density = 0, Point offset = Point(0, 0));
|
||||
bool init(Point* points, int count, float density = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
PhysicsShapePolygon();
|
||||
|
@ -151,10 +157,10 @@ protected:
|
|||
class PhysicsShapeEdgeSegment : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeEdgeSegment* create(PhysicsBody* body, Point a, Point b, float border = 1);
|
||||
static PhysicsShapeEdgeSegment* create(Point a, Point b, float border = 1);
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Point a, Point b, float border = 1);
|
||||
bool init(Point a, Point b, float border = 1);
|
||||
|
||||
protected:
|
||||
PhysicsShapeEdgeSegment();
|
||||
|
@ -167,10 +173,10 @@ protected:
|
|||
class PhysicsShapeEdgeBox : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeEdgeBox* create(PhysicsBody* body, Size size, float border = 0, Point offset = Point(0, 0));
|
||||
static PhysicsShapeEdgeBox* create(Size size, float border = 0, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Size size, float border = 1, Point offset = Point(0, 0));
|
||||
bool init(Size size, float border = 1, Point offset = Point(0, 0));
|
||||
|
||||
protected:
|
||||
PhysicsShapeEdgeBox();
|
||||
|
@ -183,10 +189,10 @@ protected:
|
|||
class PhysicsShapeEdgePolygon : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeEdgePolygon* create(PhysicsBody* body, Point* points, int count, float border = 1);
|
||||
static PhysicsShapeEdgePolygon* create(Point* points, int count, float border = 1);
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Point* points, int count, float border = 1);
|
||||
bool init(Point* points, int count, float border = 1);
|
||||
|
||||
protected:
|
||||
PhysicsShapeEdgePolygon();
|
||||
|
@ -199,10 +205,10 @@ protected:
|
|||
class PhysicsShapeEdgeChain : public PhysicsShape
|
||||
{
|
||||
public:
|
||||
static PhysicsShapeEdgeChain* create(PhysicsBody* body, Point* points, int count, float border = 1);
|
||||
static PhysicsShapeEdgeChain* create(Point* points, int count, float border = 1);
|
||||
|
||||
protected:
|
||||
bool init(PhysicsBody* body, Point* points, int count, float border = 1);
|
||||
bool init(Point* points, int count, float border = 1);
|
||||
|
||||
protected:
|
||||
PhysicsShapeEdgeChain();
|
||||
|
|
|
@ -57,6 +57,8 @@ NS_CC_BEGIN
|
|||
|
||||
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
||||
|
||||
const float PHYSICS_INFINITY = INFINITY;
|
||||
|
||||
int PhysicsWorld::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data)
|
||||
{
|
||||
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
|
||||
|
|
|
@ -28,10 +28,17 @@
|
|||
NS_CC_BEGIN
|
||||
|
||||
std::map<cpShape*, PhysicsShapeInfo*> PhysicsShapeInfo::map;
|
||||
cpBody* PhysicsShapeInfo::shareBody = nullptr;
|
||||
|
||||
PhysicsShapeInfo::PhysicsShapeInfo(PhysicsShape* shape)
|
||||
: shape(shape)
|
||||
{
|
||||
if (shareBody == nullptr)
|
||||
{
|
||||
shareBody = cpBodyNewStatic();
|
||||
}
|
||||
|
||||
body = shareBody;
|
||||
}
|
||||
|
||||
PhysicsShapeInfo::~PhysicsShapeInfo()
|
||||
|
|
|
@ -46,8 +46,10 @@ public:
|
|||
|
||||
public:
|
||||
std::vector<cpShape*> shapes;
|
||||
static std::map<cpShape*, PhysicsShapeInfo*> map;
|
||||
PhysicsShape* shape;
|
||||
cpBody* body;
|
||||
static std::map<cpShape*, PhysicsShapeInfo*> map;
|
||||
static cpBody* shareBody;
|
||||
|
||||
private:
|
||||
PhysicsShapeInfo(PhysicsShape* shape);
|
||||
|
|
|
@ -51,11 +51,7 @@ namespace
|
|||
bool PhysicsTestScene::initTest()
|
||||
{
|
||||
#ifdef CC_USE_PHYSICS
|
||||
if (TestScene::initWithPhysics())
|
||||
{
|
||||
this->getPhysicsWorld()->setDebugDraw(true);
|
||||
return true;
|
||||
}
|
||||
return TestScene::initWithPhysics();
|
||||
#else
|
||||
return TestScene::init();
|
||||
#endif
|
||||
|
@ -297,6 +293,12 @@ Node* PhysicsDemoLogoSmash::makeBall(float x, float y)
|
|||
ball->setScale(0.1);
|
||||
|
||||
PhysicsBody* body = PhysicsBody::createCircle(0.95);
|
||||
body->setMass(1.0);
|
||||
body->setAngularDamping(PHYSICS_INFINITY);
|
||||
|
||||
body->getShape()->setElasticity(0);
|
||||
body->getShape()->setFriction(0);
|
||||
|
||||
//body->setDynamic(false);
|
||||
ball->setPhysicsBody(body);
|
||||
|
||||
|
@ -331,8 +333,10 @@ void PhysicsDemoLogoSmash::onEnter()
|
|||
Sprite* bullet = Sprite::create("Images/ball.png");
|
||||
bullet->setScale(0.5);
|
||||
|
||||
PhysicsBody* body = PhysicsBody::createCircle(3, INFINITY);
|
||||
PhysicsBody* body = PhysicsBody::createCircle(8, PHYSICS_INFINITY);
|
||||
body->setVelocity(Point(400, 0));
|
||||
body->getShape()->setElasticity(0);
|
||||
body->getShape()->setFriction(0);
|
||||
bullet->setPhysicsBody(body);
|
||||
|
||||
bullet->setPosition(Point(-1000, VisibleRect::getVisibleRect().size.height/2));
|
||||
|
|
Loading…
Reference in New Issue