2013-09-09 10:29:02 +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.
|
|
|
|
****************************************************************************/
|
2013-09-09 10:40:31 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
2013-09-10 17:36:49 +08:00
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
|
2013-09-17 14:31:43 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
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 "CCPhysicsShape.h"
|
|
|
|
#include "CCPhysicsJoint.h"
|
|
|
|
#include "CCPhysicsWorld.h"
|
|
|
|
|
|
|
|
#include "chipmunk/CCPhysicsBodyInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsBodyInfo.h"
|
|
|
|
#include "chipmunk/CCPhysicsJointInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsJointInfo.h"
|
|
|
|
#include "chipmunk/CCPhysicsWorldInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsWorldInfo.h"
|
2013-10-09 13:41:19 +08:00
|
|
|
#include "chipmunk/CCPhysicsShapeInfo.h"
|
|
|
|
#include "Box2D/CCPhysicsShapeInfo.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
#include "chipmunk/CCPhysicsHelper.h"
|
|
|
|
#include "Box2D/CCPhysicsHelper.h"
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static const float MASS_DEFAULT = 1.0;
|
2013-10-14 13:56:08 +08:00
|
|
|
static const float MOMENT_DEFAULT = 200;
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBody::PhysicsBody()
|
|
|
|
: _owner(nullptr)
|
|
|
|
, _world(nullptr)
|
|
|
|
, _info(nullptr)
|
2013-09-30 20:43:19 +08:00
|
|
|
, _dynamic(true)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _enable(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _massDefault(true)
|
2013-10-14 13:56:08 +08:00
|
|
|
, _momentDefault(true)
|
2013-09-16 21:22:22 +08:00
|
|
|
, _mass(MASS_DEFAULT)
|
|
|
|
, _area(0.0)
|
2013-10-14 13:56:08 +08:00
|
|
|
, _density(0)
|
|
|
|
, _moment(MOMENT_DEFAULT)
|
2013-09-29 09:39:20 +08:00
|
|
|
, _tag(0)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBody::~PhysicsBody()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_info);
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
removeAllShapes();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
for (auto it = _joints.begin(); it != _joints.end(); ++it)
|
|
|
|
{
|
|
|
|
PhysicsJoint* joint = *it;
|
|
|
|
PhysicsBody* other = joint->getBodyA() == this ? joint->getBodyA() : joint->getBodyB();
|
|
|
|
|
|
|
|
other->_joints.erase(std::find(other->_joints.begin(), other->_joints.end(), joint));
|
|
|
|
delete joint;
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-10-10 18:31:43 +08:00
|
|
|
PhysicsBody* PhysicsBody::create()
|
|
|
|
{
|
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body && body->init())
|
|
|
|
{
|
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createCircle(float radius, PhysicsMaterial material)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body && body->init())
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeCircle::create(radius, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createBox(Size size, PhysicsMaterial material)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body && body->init())
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeBox::create(size, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createPolygon(Point* points, int count, PhysicsMaterial material)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsBody* body = new PhysicsBody();
|
|
|
|
if (body && body->init())
|
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapePolygon::create(points, count, material));
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeSegment(Point a, Point b, PhysicsMaterial material, float border/* = 1*/)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-09-30 20:43:19 +08:00
|
|
|
if (body && body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeSegment::create(a, b, material, border));
|
2013-09-30 20:43:19 +08:00
|
|
|
body->_dynamic = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeBox(Size size, PhysicsMaterial material, float border/* = 1*/)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-09-30 20:43:19 +08:00
|
|
|
if (body && body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeBox::create(size, material, border));
|
2013-09-30 20:43:19 +08:00
|
|
|
body->_dynamic = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
|
|
|
|
return nullptr;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgePolygon(Point* points, int count, PhysicsMaterial material, float border/* = 1*/)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-09-30 20:43:19 +08:00
|
|
|
if (body && body->init())
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgePolygon::create(points, count, material, border));
|
2013-09-30 20:43:19 +08:00
|
|
|
body->_dynamic = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
2013-09-10 17:36:49 +08:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:53:12 +08:00
|
|
|
PhysicsBody* PhysicsBody::createEdgeChain(Point* points, int count, PhysicsMaterial material, float border/* = 1*/)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
|
|
|
PhysicsBody* body = new PhysicsBody();
|
2013-09-30 20:43:19 +08:00
|
|
|
if (body && body->init())
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-09 17:53:12 +08:00
|
|
|
body->addShape(PhysicsShapeEdgeChain::create(points, count, material, border));
|
2013-09-30 20:43:19 +08:00
|
|
|
body->_dynamic = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
body->autorelease();
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(body);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
bool PhysicsBody::init()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_info = new PhysicsBodyInfo();
|
|
|
|
CC_BREAK_IF(_info == nullptr);
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
_info->body = cpBodyNew(PhysicsHelper::float2cpfloat(_mass), PhysicsHelper::float2cpfloat(_moment));
|
2013-09-10 17:36:49 +08:00
|
|
|
CC_BREAK_IF(_info->body == nullptr);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
void PhysicsBody::setDynamic(bool dynamic)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-30 13:02:17 +08:00
|
|
|
if (dynamic != _dynamic)
|
2013-09-10 17:36:49 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
if (dynamic)
|
|
|
|
{
|
2013-09-30 13:02:17 +08:00
|
|
|
cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass));
|
2013-10-14 13:56:08 +08:00
|
|
|
cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_moment));
|
2013-09-16 21:22:22 +08:00
|
|
|
}else
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
cpBodySetMass(_info->body, PHYSICS_INFINITY);
|
|
|
|
cpBodySetMoment(_info->body, PHYSICS_INFINITY);
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
2013-09-30 13:02:17 +08:00
|
|
|
|
|
|
|
_dynamic = dynamic;
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setPosition(Point position)
|
|
|
|
{
|
|
|
|
cpBodySetPos(_info->body, PhysicsHelper::point2cpv(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setRotation(float rotation)
|
|
|
|
{
|
|
|
|
cpBodySetAngle(_info->body, PhysicsHelper::float2cpfloat(rotation));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsBody::getPosition() const
|
|
|
|
{
|
|
|
|
cpVect vec = cpBodyGetPos(_info->body);
|
|
|
|
return PhysicsHelper::cpv2point(vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
float PhysicsBody::getRotation() const
|
|
|
|
{
|
|
|
|
return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->body) / 3.14f * 180.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::addShape(PhysicsShape* shape)
|
|
|
|
{
|
|
|
|
if (shape == nullptr) return;
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
// add shape to body
|
2013-09-30 20:43:19 +08:00
|
|
|
if (std::find(_shapes.begin(), _shapes.end(), shape) == _shapes.end())
|
|
|
|
{
|
2013-10-09 13:41:19 +08:00
|
|
|
shape->setBody(this);
|
2013-09-30 20:43:19 +08:00
|
|
|
_shapes.push_back(shape);
|
|
|
|
|
2013-10-14 14:05:57 +08:00
|
|
|
// calculate the area, mass, and desity
|
|
|
|
// area must update before mass, because the density changes depend on it.
|
2013-09-30 20:43:19 +08:00
|
|
|
_area += shape->getArea();
|
2013-10-14 14:05:57 +08:00
|
|
|
addMass(shape->getMass());
|
2013-10-14 13:56:08 +08:00
|
|
|
addMoment(shape->getMoment());
|
2013-09-30 20:43:19 +08:00
|
|
|
|
|
|
|
if (_world != nullptr) _world->addShape(shape);
|
|
|
|
|
|
|
|
shape->retain();
|
|
|
|
}
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyForce(Point force)
|
|
|
|
{
|
|
|
|
applyForce(force, Point());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyForce(Point force, Point offset)
|
|
|
|
{
|
|
|
|
cpBodyApplyForce(_info->body, PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyImpulse(Point impulse)
|
|
|
|
{
|
|
|
|
applyImpulse(impulse, Point());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyImpulse(Point impulse, Point offset)
|
|
|
|
{
|
|
|
|
cpBodyApplyImpulse(_info->body, PhysicsHelper::point2cpv(impulse), PhysicsHelper::point2cpv(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::applyTorque(float torque)
|
|
|
|
{
|
|
|
|
cpBodySetTorque(_info->body, PhysicsHelper::float2cpfloat(torque));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setMass(float mass)
|
|
|
|
{
|
2013-10-14 13:56:08 +08:00
|
|
|
if (mass <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
_mass = mass;
|
|
|
|
_massDefault = false;
|
2013-09-10 17:36:49 +08:00
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
// update density
|
|
|
|
if (_mass == PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
_density = PHYSICS_INFINITY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_area > 0)
|
|
|
|
{
|
|
|
|
_density = _mass / _area;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
_density = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass));
|
2013-09-10 17:36:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
void PhysicsBody::addMass(float mass)
|
|
|
|
{
|
|
|
|
if (mass == PHYSICS_INFINITY)
|
|
|
|
{
|
2013-10-15 13:48:01 +08:00
|
|
|
_mass = PHYSICS_INFINITY;
|
2013-10-14 13:56:08 +08:00
|
|
|
_massDefault = false;
|
|
|
|
_density = PHYSICS_INFINITY;
|
|
|
|
}
|
|
|
|
else if (mass == -PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (_mass != PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
if (_massDefault)
|
|
|
|
{
|
|
|
|
_mass = 0;
|
|
|
|
_massDefault = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mass + mass > 0)
|
|
|
|
{
|
|
|
|
_mass += mass;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
_mass = MASS_DEFAULT;
|
|
|
|
_massDefault = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_area > 0)
|
|
|
|
{
|
|
|
|
_density = _mass / _area;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_density = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 13:48:01 +08:00
|
|
|
|
|
|
|
cpBodySetMass(_info->body, PhysicsHelper::float2cpfloat(_mass));
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::addMoment(float moment)
|
|
|
|
{
|
|
|
|
if (moment == PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
// if moment is INFINITY, the moment of the body will become INFINITY
|
|
|
|
_moment = PHYSICS_INFINITY;
|
|
|
|
_momentDefault = false;
|
|
|
|
}
|
|
|
|
else if (moment == -PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
// if moment is -INFINITY, it won't change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if moment of the body is INFINITY is has no effect
|
|
|
|
if (_moment != PHYSICS_INFINITY)
|
|
|
|
{
|
|
|
|
if (_momentDefault)
|
|
|
|
{
|
|
|
|
_moment = 0;
|
|
|
|
_momentDefault = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_moment + moment > 0)
|
|
|
|
{
|
|
|
|
_moment += moment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_moment = MOMENT_DEFAULT;
|
|
|
|
_momentDefault = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_moment));
|
2013-10-14 13:56:08 +08:00
|
|
|
}
|
|
|
|
|
2013-09-30 20:43:19 +08:00
|
|
|
void PhysicsBody::setVelocity(Point velocity)
|
|
|
|
{
|
|
|
|
cpBodySetVel(_info->body, PhysicsHelper::point2cpv(velocity));
|
|
|
|
}
|
|
|
|
|
|
|
|
Point PhysicsBody::getVelocity()
|
|
|
|
{
|
|
|
|
return PhysicsHelper::cpv2point(cpBodyGetVel(_info->body));
|
|
|
|
}
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
void PhysicsBody::setMoment(float moment)
|
2013-09-16 21:22:22 +08:00
|
|
|
{
|
2013-10-14 13:56:08 +08:00
|
|
|
_moment = moment;
|
|
|
|
_momentDefault = false;
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-10-15 13:48:01 +08:00
|
|
|
cpBodySetMoment(_info->body, PhysicsHelper::float2cpfloat(_moment));
|
2013-09-16 21:22:22 +08:00
|
|
|
}
|
|
|
|
|
2013-09-29 09:39:20 +08:00
|
|
|
PhysicsShape* PhysicsBody::getShapeByTag(int tag)
|
|
|
|
{
|
|
|
|
for (auto shape : _shapes)
|
|
|
|
{
|
|
|
|
if (shape->getTag() == tag)
|
|
|
|
{
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::removeShapeByTag(int tag)
|
|
|
|
{
|
|
|
|
for (auto shape : _shapes)
|
|
|
|
{
|
|
|
|
if (shape->getTag() == tag)
|
|
|
|
{
|
|
|
|
removeShape(shape);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::removeShape(PhysicsShape* shape)
|
|
|
|
{
|
|
|
|
auto it = std::find(_shapes.begin(), _shapes.end(), shape);
|
|
|
|
|
|
|
|
if (it != _shapes.end())
|
|
|
|
{
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeShape(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
_shapes.erase(it);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
|
|
|
|
2013-10-14 13:56:08 +08:00
|
|
|
// deduce the area, mass and moment
|
|
|
|
// area must update before mass, because the density changes depend on it.
|
|
|
|
_area -= shape->getArea();
|
|
|
|
addMass(-shape->getMass());
|
|
|
|
addMoment(-shape->getMoment());
|
2013-09-30 20:43:19 +08:00
|
|
|
|
|
|
|
shape->release();
|
2013-09-29 09:39:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::removeAllShapes()
|
|
|
|
{
|
|
|
|
for (auto shape : _shapes)
|
|
|
|
{
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
_world->removeShape(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
_shapes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsBody::setEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_enable != enable)
|
|
|
|
{
|
|
|
|
_enable = enable;
|
|
|
|
|
|
|
|
if (_world)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
_world->addBody(this);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
_world->removeBody(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 21:22:22 +08:00
|
|
|
//Clonable* PhysicsBody::clone() const
|
|
|
|
//{
|
|
|
|
// PhysicsBody* body = new PhysicsBody();
|
|
|
|
//
|
|
|
|
// body->autorelease();
|
|
|
|
//
|
|
|
|
// return body;
|
|
|
|
//}
|
|
|
|
|
2013-09-10 17:36:49 +08:00
|
|
|
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // CC_USE_PHYSICS
|