2013-09-16 21:22:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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 "CCPhysicsShape.h"
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
2013-09-17 17:39:08 +08:00
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
|
|
|
#include "chipmunk.h"
|
|
|
|
#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D)
|
|
|
|
#include "Box2D.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
2013-09-29 09:39:20 +08:00
|
|
|
#include "CCPhysicsWorld.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
#include "chipmunk/CCPhysicsBodyInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsBodyInfo.h"
|
|
|
|
#include "chipmunk/CCPhysicsShapeInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsShapeInfo.h"
|
|
|
|
#include "chipmunk/CCPhysicsHelper.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
PhysicsShape::PhysicsShape()
|
|
|
|
: _body(nullptr)
|
|
|
|
, _info(nullptr)
|
|
|
|
, _type(Type::UNKNOWN)
|
2013-09-30 20:43:19 +08:00
|
|
|
, _area(0)
|
|
|
|
, _mass(0)
|
2013-10-14 13:56:08 +08:00
|
|
|
, _moment(0)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _tag(0)
|
|
|
|
, _enable(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShape::~PhysicsShape()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_info);
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShape::init(Type type, PhysicsMaterial material/* = MaterialDefault*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
_info = new PhysicsShapeInfo(this);
|
|
|
|
if (_info == nullptr) return false;
|
|
|
|
|
|
|
|
_type = type;
|
2013-10-09 17:53:12 +08:00
|
|
|
_material = material;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
void PhysicsShape::setEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_enable != enable)
|
|
|
|
{
|
|
|
|
_enable = enable;
|
|
|
|
|
|
|
|
if (_body->getWorld() && _body->isEnable())
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
_body->getWorld()->addShape(this);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
_body->getWorld()->removeShape(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
void PhysicsShape::setMass(float mass)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
if (mass < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMass(-_mass);
|
|
|
|
_body->addMass(mass);
|
|
|
|
};
|
|
|
|
|
|
|
|
_mass = mass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsShape::setMoment(float moment)
|
|
|
|
{
|
|
|
|
if (moment < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMoment(-_moment);
|
|
|
|
_body->addMoment(moment);
|
|
|
|
};
|
|
|
|
|
|
|
|
_moment = moment;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBodyInfo* PhysicsShape::bodyInfo() const
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
if (_body != nullptr)
|
|
|
|
{
|
|
|
|
return _body->_info;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeCircle::PhysicsShapeCircle()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeCircle::~PhysicsShapeCircle()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeBox::PhysicsShapeBox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeBox::~PhysicsShapeBox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapePolygon::PhysicsShapePolygon()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapePolygon::~PhysicsShapePolygon()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeBox::PhysicsShapeEdgeBox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeBox::~PhysicsShapeEdgeBox()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeChain::PhysicsShapeEdgeChain()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeChain::~PhysicsShapeEdgeChain()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgePolygon::PhysicsShapeEdgePolygon()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgePolygon::~PhysicsShapeEdgePolygon()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeSegment::PhysicsShapeEdgeSegment()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShapeEdgeSegment::~PhysicsShapeEdgeSegment()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
void PhysicsShape::initEnd()
|
|
|
|
{
|
|
|
|
for (auto shape : _info->shapes)
|
|
|
|
{
|
|
|
|
cpShapeSetElasticity(shape, _material.elasticity);
|
|
|
|
cpShapeSetFriction(shape, _material.friction);
|
|
|
|
}
|
|
|
|
}
|
2013-10-09 13:41:19 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeCircle
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeCircle* shape = new PhysicsShapeCircle();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(radius, material, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeCircle::init(float radius, PhysicsMaterial material/* = MaterialDefault*/, Point offset /*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpCircleShapeNew(_info->shareBody, radius, PhysicsHelper::point2cpv(offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
_area = PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
|
2013-10-09 17:53:12 +08:00
|
|
|
_mass = _material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : _material.density * _area;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : cpMomentForCircle(_mass, 0, radius, PhysicsHelper::point2cpv(offset));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeEdgeSegment
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(Point a, Point b, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeEdgeSegment* shape = new PhysicsShapeEdgeSegment();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(a, b, material, border))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeEdgeSegment::init(Point a, Point b, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->shareBody,
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(a),
|
|
|
|
PhysicsHelper::point2cpv(b),
|
|
|
|
PhysicsHelper::float2cpfloat(border));
|
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeBox
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeBox* PhysicsShapeBox::create(Size size, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeBox* shape = new PhysicsShapeBox();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(size, material, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeBox::init(Size size, PhysicsMaterial material/* = MaterialDefault*/, Point offset /*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::BOX, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpBoxShapeNew(_info->shareBody, PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
_area = PhysicsHelper::cpfloat2float(cpAreaForPoly(4, ((cpPolyShape*)shape)->verts));
|
2013-10-09 17:53:12 +08:00
|
|
|
_mass = _material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : _material.density * _area;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : cpMomentForBox(_mass, PhysicsHelper::float2cpfloat(size.width), PhysicsHelper::float2cpfloat(size.height));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeCircle
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapePolygon* PhysicsShapePolygon::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, Point offset/* = Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapePolygon* shape = new PhysicsShapePolygon();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(points, count, material, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapePolygon::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, Point offset /*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
cpVect* vecs = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vecs, count);
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpPolyShapeNew(_info->shareBody, count, vecs, PhysicsHelper::point2cpv(offset));
|
2013-09-30 20:43:19 +08:00
|
|
|
CC_SAFE_DELETE(vecs);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
_area = PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
|
2013-10-09 17:53:12 +08:00
|
|
|
_mass = _material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : _material.density * _area;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY : cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, PhysicsHelper::point2cpv(offset));
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeEdgeBox
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(Size size, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/, Point offset/* = Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeEdgeBox* shape = new PhysicsShapeEdgeBox();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(size, material, border, offset))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeEdgeBox::init(Size size, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/, Point offset/*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEBOX, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
cpVect vec[4] = {};
|
2013-10-09 13:41:19 +08:00
|
|
|
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));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < 4; ++i)
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%4],
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::float2cpfloat(border));
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
_info->add(shape);
|
|
|
|
}
|
|
|
|
CC_BREAK_IF(i < 4);
|
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeEdgeBox
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeEdgePolygon* shape = new PhysicsShapeEdgePolygon();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(points, count, material, border))
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeEdgePolygon::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEPOLYGEN, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
vec = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[(i+1)%count],
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::float2cpfloat(border));
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
cpShapeSetElasticity(shape, 1.0f);
|
|
|
|
cpShapeSetFriction(shape, 1.0f);
|
|
|
|
_info->add(shape);
|
|
|
|
}
|
2013-09-30 20:43:19 +08:00
|
|
|
CC_SAFE_DELETE(vec);
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_BREAK_IF(i < count);
|
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (vec != nullptr) delete[] vec;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeEdgeChain
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsShapeEdgeChain* shape = new PhysicsShapeEdgeChain();
|
2013-10-09 17:53:12 +08:00
|
|
|
if (shape && shape->init(points, count, material, border))
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-09-30 20:43:19 +08:00
|
|
|
shape->autorelease();
|
2013-09-16 21:22:22 +08:00
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(shape);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
bool PhysicsShapeEdgeChain::init(Point* points, int count, PhysicsMaterial material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGECHAIN, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
vec = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < count - 1; ++i)
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->shareBody, vec[i], vec[i+1],
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::float2cpfloat(border));
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
cpShapeSetElasticity(shape, 1.0f);
|
|
|
|
cpShapeSetFriction(shape, 1.0f);
|
|
|
|
_info->add(shape);
|
|
|
|
}
|
2013-09-30 20:43:19 +08:00
|
|
|
if (vec != nullptr) delete[] vec;
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_BREAK_IF(i < count);
|
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
initEnd();
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
if (vec != nullptr) delete[] vec;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|