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"
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
#include <climits>
|
|
|
|
|
2013-09-17 17:39:08 +08:00
|
|
|
#include "chipmunk.h"
|
|
|
|
|
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
|
|
|
|
2013-10-31 18:18:02 +08:00
|
|
|
#include "chipmunk/CCPhysicsBodyInfo_chipmunk.h"
|
|
|
|
#include "chipmunk/CCPhysicsShapeInfo_chipmunk.h"
|
|
|
|
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2013-11-27 17:33:33 +08:00
|
|
|
extern const float PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
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)
|
2013-10-29 17:31:35 +08:00
|
|
|
, _categoryBitmask(UINT_MAX)
|
|
|
|
, _collisionBitmask(UINT_MAX)
|
2013-11-04 14:36:38 +08:00
|
|
|
, _contactTestBitmask(UINT_MAX)
|
2013-10-29 17:31:35 +08:00
|
|
|
, _group(0)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsShape::~PhysicsShape()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_info);
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
bool PhysicsShape::init(Type type)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
_info = new PhysicsShapeInfo(this);
|
|
|
|
if (_info == nullptr) return false;
|
|
|
|
|
|
|
|
_type = type;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
void PhysicsShape::setMass(float mass)
|
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
if (mass < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMass(-_mass);
|
|
|
|
_body->addMass(mass);
|
|
|
|
};
|
|
|
|
|
|
|
|
_mass = mass;
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
void PhysicsShape::setMoment(float moment)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
if (moment < 0)
|
2013-09-29 09:39:20 +08:00
|
|
|
{
|
2013-10-14 14:05:57 +08:00
|
|
|
return;
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
2013-10-14 14:05:57 +08:00
|
|
|
|
|
|
|
if (_body)
|
|
|
|
{
|
|
|
|
_body->addMoment(-_moment);
|
|
|
|
_body->addMoment(moment);
|
|
|
|
};
|
|
|
|
|
|
|
|
_moment = moment;
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
void PhysicsShape::setMaterial(const PhysicsMaterial& material)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
setDensity(material.density);
|
|
|
|
setRestitution(material.restitution);
|
|
|
|
setFriction(material.friction);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
void PhysicsShape::setDensity(float density)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
if (density < 0)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_material.density = density;
|
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
if (_material.density == PHYSICS_INFINITY)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
setMass(PHYSICS_INFINITY);
|
2013-10-15 13:48:01 +08:00
|
|
|
}else if (_area > 0)
|
2013-10-09 17:53:12 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
setMass(PhysicsHelper::float2cpfloat(_material.density * _area));
|
2013-10-09 17:53:12 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-09 13:41:19 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
void PhysicsShape::setRestitution(float restitution)
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
_material.restitution = restitution;
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
for (cpShape* shape : _info->getShapes())
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
cpShapeSetElasticity(shape, PhysicsHelper::float2cpfloat(restitution));
|
2013-10-09 13:41:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsShape::setFriction(float friction)
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
_material.friction = friction;
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
for (cpShape* shape : _info->getShapes())
|
2013-10-09 13:41:19 +08:00
|
|
|
{
|
|
|
|
cpShapeSetFriction(shape, PhysicsHelper::float2cpfloat(friction));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
Point* PhysicsShape::recenterPoints(Point* points, int count, const Point& center)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect* cpvs = new cpVect[count];
|
|
|
|
cpRecenterPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
|
|
|
|
PhysicsHelper::cpvs2points(cpvs, points, count);
|
|
|
|
delete[] cpvs;
|
|
|
|
|
|
|
|
if (center != Point::ZERO)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
points[i] += center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
Point PhysicsShape::getPolyonCenter(const Point* points, int count)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect* cpvs = new cpVect[count];
|
|
|
|
cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count));
|
|
|
|
delete[] cpvs;
|
|
|
|
|
|
|
|
return PhysicsHelper::cpv2point(center);
|
|
|
|
}
|
|
|
|
|
2013-10-17 10:57:48 +08:00
|
|
|
void PhysicsShape::setBody(PhysicsBody *body)
|
|
|
|
{
|
|
|
|
// already added
|
2013-11-07 14:17:57 +08:00
|
|
|
if (body != nullptr && _body == body)
|
2013-10-17 10:57:48 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_body != nullptr)
|
|
|
|
{
|
|
|
|
_body->removeShape(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body == nullptr)
|
|
|
|
{
|
|
|
|
_info->setBody(nullptr);
|
|
|
|
_body = nullptr;
|
|
|
|
}else
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
_info->setBody(body->_info->getBody());
|
2013-10-17 10:57:48 +08:00
|
|
|
_body = body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeCircle
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeCircle* PhysicsShapeCircle::create(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::CIRCLE));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpCircleShapeNew(_info->getSharedBody(), radius, PhysicsHelper::point2cpv(offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
|
|
|
_info->add(shape);
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
_area = calculateArea();
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
|
|
|
|
|
|
|
setMaterial(material);
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
float PhysicsShapeCircle::calculateArea(float radius)
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, radius));
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Point& offset)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-27 17:33:33 +08:00
|
|
|
return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(mass),
|
|
|
|
0,
|
|
|
|
PhysicsHelper::float2cpfloat(radius),
|
|
|
|
PhysicsHelper::point2cpv(offset)));
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
float PhysicsShapeCircle::calculateArea()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->getShapes().front())));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsShapeCircle::calculateDefaultMoment()
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-10-15 13:48:01 +08:00
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForCircle(PhysicsHelper::float2cpfloat(_mass),
|
|
|
|
0,
|
|
|
|
cpCircleShapeGetRadius(shape),
|
|
|
|
cpCircleShapeGetOffset(shape)));
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
float PhysicsShapeCircle::getRadius() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(_info->getShapes().front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsShapeCircle::getOffset()
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpCircleShapeGetOffset(_info->getShapes().front()));
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeEdgeSegment
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeEdgeSegment* PhysicsShapeEdgeSegment::create(const Point& a, const Point& b, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeEdgeSegment::init(const Point& a, const Point& b, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGESEGMENT));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(),
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsHelper::point2cpv(a),
|
|
|
|
PhysicsHelper::point2cpv(b),
|
|
|
|
PhysicsHelper::float2cpfloat(border));
|
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-10-15 13:48:01 +08:00
|
|
|
_center = a.getMidpoint(b);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Point PhysicsShapeEdgeSegment::getPointA() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->ta);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Point PhysicsShapeEdgeSegment::getPointB() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpv2point(((cpSegmentShape*)(_info->getShapes().front()))->tb);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsShapeEdgeSegment::getCenter()
|
|
|
|
{
|
|
|
|
return _center;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeBox
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeBox* PhysicsShapeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset /*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::BOX));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
cpVect 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}
|
|
|
|
};
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), 4, vec, PhysicsHelper::point2cpv(offset));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(shape == nullptr);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_info->add(shape);
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
_offset = offset;
|
2013-12-13 17:36:58 +08:00
|
|
|
_area = calculateArea();
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
|
|
|
|
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
float PhysicsShapeBox::calculateArea(const Size& size)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect 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}
|
|
|
|
};
|
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForPoly(4, vec));
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
float PhysicsShapeBox::calculateMoment(float mass, const Size& size, const Point& offset)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect 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}
|
|
|
|
};
|
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
return mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForPoly(PhysicsHelper::float2cpfloat(mass),
|
|
|
|
4,
|
|
|
|
vec,
|
|
|
|
PhysicsHelper::point2cpv(offset)));
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
float PhysicsShapeBox::calculateArea()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-10-15 13:48:01 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsShapeBox::calculateDefaultMoment()
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-11-27 17:33:33 +08:00
|
|
|
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
|
|
|
|
}
|
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
void PhysicsShapeBox::getPoints(Point* points) const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-11-06 15:43:29 +08:00
|
|
|
PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, points, ((cpPolyShape*)shape)->numVerts);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-11-01 16:26:03 +08:00
|
|
|
Size PhysicsShapeBox::getSize() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-10-15 13:48:01 +08:00
|
|
|
return PhysicsHelper::cpv2size(cpv(cpvdist(cpPolyShapeGetVert(shape, 0), cpPolyShapeGetVert(shape, 1)),
|
|
|
|
cpvdist(cpPolyShapeGetVert(shape, 1), cpPolyShapeGetVert(shape, 2))));
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapePolygon
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapePolygon* PhysicsShapePolygon::create(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
cpVect* vecs = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vecs, count);
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), count, vecs, PhysicsHelper::point2cpv(offset));
|
2013-11-12 08:07:46 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vecs);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(shape == nullptr);
|
|
|
|
|
|
|
|
_info->add(shape);
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
_area = calculateArea();
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
|
2013-10-15 13:48:01 +08:00
|
|
|
_moment = calculateDefaultMoment();
|
|
|
|
_center = PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
|
|
|
|
|
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
float PhysicsShapePolygon::calculateArea(const Point* points, int count)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect* vecs = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vecs, count);
|
|
|
|
float area = PhysicsHelper::cpfloat2float(cpAreaForPoly(count, vecs));
|
2013-11-12 08:07:46 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vecs);
|
2013-10-15 13:48:01 +08:00
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
float PhysicsShapePolygon::calculateMoment(float mass, const Point* points, int count, const Point& offset)
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
|
|
|
cpVect* vecs = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vecs, count);
|
2013-11-27 17:33:33 +08:00
|
|
|
float moment = mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForPoly(mass, count, vecs, PhysicsHelper::point2cpv(offset)));
|
2013-11-12 08:07:46 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vecs);
|
2013-10-15 13:48:01 +08:00
|
|
|
|
|
|
|
return moment;
|
|
|
|
}
|
|
|
|
|
2013-12-13 17:36:58 +08:00
|
|
|
float PhysicsShapePolygon::calculateArea()
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-10-15 13:48:01 +08:00
|
|
|
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsShapePolygon::calculateDefaultMoment()
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-11-27 17:33:33 +08:00
|
|
|
return _mass == PHYSICS_INFINITY ? PHYSICS_INFINITY
|
2013-10-15 13:48:01 +08:00
|
|
|
: PhysicsHelper::cpfloat2float(cpMomentForPoly(_mass, ((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts, cpvzero));
|
|
|
|
}
|
|
|
|
|
2013-11-05 15:54:33 +08:00
|
|
|
Point PhysicsShapePolygon::getPoint(int i) const
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return PhysicsHelper::cpv2point(cpPolyShapeGetVert(_info->getShapes().front(), i));
|
2013-11-05 15:54:33 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 15:43:29 +08:00
|
|
|
void PhysicsShapePolygon::getPoints(Point* outPoints) const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = _info->getShapes().front();
|
2013-11-06 15:43:29 +08:00
|
|
|
PhysicsHelper::cpvs2points(((cpPolyShape*)shape)->verts, outPoints, ((cpPolyShape*)shape)->numVerts);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapePolygon::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
return ((cpPolyShape*)_info->getShapes().front())->numVerts;
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsShapePolygon::getCenter()
|
|
|
|
{
|
|
|
|
return _center;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeEdgeBox
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeEdgeBox* PhysicsShapeEdgeBox::create(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeEdgeBox::init(const Size& size, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/, const Point& offset/*= Point(0, 0)*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEBOX));
|
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-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), 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-15 13:48:01 +08:00
|
|
|
_offset = offset;
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhysicsShapeEdgeBox
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeEdgePolygon* PhysicsShapeEdgePolygon::create(const Point* points, int count, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeEdgePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGEPOLYGEN));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
vec = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
2013-10-15 13:48:01 +08:00
|
|
|
_center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, vec));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < count; ++i)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), 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-10-15 13:48:01 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vec);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_BREAK_IF(i < count);
|
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vec);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
Point PhysicsShapeEdgePolygon::getCenter()
|
|
|
|
{
|
|
|
|
return _center;
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapeEdgePolygon::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return static_cast<int>(_info->getShapes().size() + 1);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
// PhysicsShapeEdgeChain
|
2013-11-05 20:02:58 +08:00
|
|
|
PhysicsShapeEdgeChain* PhysicsShapeEdgeChain::create(const Point* points, int count, const 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-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShapeEdgeChain::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
cpVect* vec = nullptr;
|
|
|
|
do
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_BREAK_IF(!PhysicsShape::init(Type::EDGECHAIN));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
vec = new cpVect[count];
|
|
|
|
PhysicsHelper::points2cpvs(points, vec, count);
|
2013-10-15 13:48:01 +08:00
|
|
|
_center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, vec));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (; i < count - 1; ++i)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), 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-10-15 13:48:01 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vec);
|
2013-09-16 21:22:22 +08:00
|
|
|
CC_BREAK_IF(i < count);
|
|
|
|
|
2013-11-27 17:33:33 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
|
|
|
_moment = PHYSICS_INFINITY;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
setMaterial(material);
|
2013-10-09 17:53:12 +08:00
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(vec);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
Point PhysicsShapeEdgeChain::getCenter()
|
|
|
|
{
|
|
|
|
return _center;
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int PhysicsShapeEdgeChain::getPointsCount() const
|
2013-10-15 13:48:01 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return static_cast<int>(_info->getShapes().size() + 1);
|
2013-10-15 13:48:01 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 17:31:35 +08:00
|
|
|
void PhysicsShape::setGroup(int group)
|
|
|
|
{
|
|
|
|
if (group < 0)
|
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
for (auto shape : _info->getShapes())
|
2013-10-29 17:31:35 +08:00
|
|
|
{
|
|
|
|
cpShapeSetGroup(shape, (cpGroup)group);
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 15:28:07 +08:00
|
|
|
|
|
|
|
_group = group;
|
2013-10-29 17:31:35 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 20:02:58 +08:00
|
|
|
bool PhysicsShape::containsPoint(const Point& point) const
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
2013-11-05 20:02:58 +08:00
|
|
|
for (auto shape : _info->getShapes())
|
2013-11-05 15:54:33 +08:00
|
|
|
{
|
|
|
|
if (cpShapePointQuery(shape, PhysicsHelper::point2cpv(point)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2013-10-22 18:01:56 +08:00
|
|
|
#endif // CC_USE_PHYSICS
|