mirror of https://github.com/axmolengine/axmol.git
Merge branch 'develop' of https://github.com/cocos2d/cocos2d-x into developCCS
This commit is contained in:
commit
e6ed691e7c
|
@ -102,7 +102,7 @@ bool Scene::initWithPhysics()
|
||||||
Director * pDirector;
|
Director * pDirector;
|
||||||
CC_BREAK_IF( ! (pDirector = Director::getInstance()) );
|
CC_BREAK_IF( ! (pDirector = Director::getInstance()) );
|
||||||
this->setContentSize(pDirector->getWinSize());
|
this->setContentSize(pDirector->getWinSize());
|
||||||
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::create(*this)));
|
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
||||||
|
|
||||||
this->scheduleUpdate();
|
this->scheduleUpdate();
|
||||||
// success
|
// success
|
||||||
|
|
|
@ -1793,10 +1793,6 @@ bool Image::saveToFile(const char *pszFilePath, bool bIsToRGB)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
||||||
assert(false);
|
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
bool bRet = false;
|
bool bRet = false;
|
||||||
|
|
||||||
do
|
do
|
||||||
|
@ -1829,7 +1825,6 @@ bool Image::saveToFile(const char *pszFilePath, bool bIsToRGB)
|
||||||
} while (0);
|
} while (0);
|
||||||
|
|
||||||
return bRet;
|
return bRet;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1126,6 +1126,8 @@ void WidgetPropertiesReader0300::setPropsForWidgetFromJsonDictionary(UIWidget*wi
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (parameter)
|
||||||
|
{
|
||||||
float mgl = dicHelper->getFloatValue_json(layoutParameterDic, "marginLeft");
|
float mgl = dicHelper->getFloatValue_json(layoutParameterDic, "marginLeft");
|
||||||
float mgt = dicHelper->getFloatValue_json(layoutParameterDic, "marginTop");
|
float mgt = dicHelper->getFloatValue_json(layoutParameterDic, "marginTop");
|
||||||
float mgr = dicHelper->getFloatValue_json(layoutParameterDic, "marginRight");
|
float mgr = dicHelper->getFloatValue_json(layoutParameterDic, "marginRight");
|
||||||
|
@ -1133,6 +1135,7 @@ void WidgetPropertiesReader0300::setPropsForWidgetFromJsonDictionary(UIWidget*wi
|
||||||
parameter->setMargin(UIMargin(mgl, mgt, mgr, mgb));
|
parameter->setMargin(UIMargin(mgl, mgt, mgr, mgb));
|
||||||
widget->setLayoutParameter(parameter);
|
widget->setLayoutParameter(parameter);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
CC_SAFE_DELETE(layoutParameterDic);
|
CC_SAFE_DELETE(layoutParameterDic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -143,7 +143,7 @@ void UILayout::onSizeChanged()
|
||||||
if (_backGroundImage)
|
if (_backGroundImage)
|
||||||
{
|
{
|
||||||
_backGroundImage->setPosition(cocos2d::Point(_size.width/2.0f, _size.height/2.0f));
|
_backGroundImage->setPosition(cocos2d::Point(_size.width/2.0f, _size.height/2.0f));
|
||||||
if (_backGroundScale9Enabled)
|
if (_backGroundScale9Enabled && _backGroundImage)
|
||||||
{
|
{
|
||||||
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_backGroundImage)->setPreferredSize(_size);
|
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_backGroundImage)->setPreferredSize(_size);
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,7 @@ void UILayout::setBackGroundImage(const char* fileName,TextureResType texType)
|
||||||
void UILayout::setBackGroundImageCapInsets(const cocos2d::Rect &capInsets)
|
void UILayout::setBackGroundImageCapInsets(const cocos2d::Rect &capInsets)
|
||||||
{
|
{
|
||||||
_backGroundImageCapInsets = capInsets;
|
_backGroundImageCapInsets = capInsets;
|
||||||
if (_backGroundScale9Enabled)
|
if (_backGroundScale9Enabled && _backGroundImage)
|
||||||
{
|
{
|
||||||
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_backGroundImage)->setCapInsets(capInsets);
|
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_backGroundImage)->setCapInsets(capInsets);
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,20 +360,23 @@ float PhysicsBody::getRotation() const
|
||||||
return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->getBody()) / 3.14f * 180.0f);
|
return -PhysicsHelper::cpfloat2float(cpBodyGetAngle(_info->getBody()) / 3.14f * 180.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape)
|
PhysicsShape* PhysicsBody::addShape(PhysicsShape* shape, bool addMassAndMoment/* = true*/)
|
||||||
{
|
{
|
||||||
if (shape == nullptr) return nullptr;
|
if (shape == nullptr) return nullptr;
|
||||||
|
|
||||||
// add shape to body
|
// add shape to body
|
||||||
if (_shapes->getIndexOfObject(shape) == UINT_MAX)
|
if (_shapes->getIndexOfObject(shape) == CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
shape->setBody(this);
|
shape->setBody(this);
|
||||||
|
|
||||||
// calculate the area, mass, and desity
|
// calculate the area, mass, and desity
|
||||||
// area must update before mass, because the density changes depend on it.
|
// area must update before mass, because the density changes depend on it.
|
||||||
|
if (addMassAndMoment)
|
||||||
|
{
|
||||||
_area += shape->getArea();
|
_area += shape->getArea();
|
||||||
addMass(shape->getMass());
|
addMass(shape->getMass());
|
||||||
addMoment(shape->getMoment());
|
addMoment(shape->getMoment());
|
||||||
|
}
|
||||||
|
|
||||||
if (_world != nullptr)
|
if (_world != nullptr)
|
||||||
{
|
{
|
||||||
|
@ -401,6 +404,17 @@ void PhysicsBody::applyForce(const Vect& force, const Point& offset)
|
||||||
cpBodyApplyForce(_info->getBody(), PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset));
|
cpBodyApplyForce(_info->getBody(), PhysicsHelper::point2cpv(force), PhysicsHelper::point2cpv(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhysicsBody::resetForce()
|
||||||
|
{
|
||||||
|
cpBodyResetForces(_info->getBody());
|
||||||
|
|
||||||
|
// if _gravityEnable is false, add a reverse of gravity force to body
|
||||||
|
if (_world != nullptr && !_gravityEnable)
|
||||||
|
{
|
||||||
|
applyForce(-_world->getGravity() * _mass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PhysicsBody::applyImpulse(const Vect& impulse)
|
void PhysicsBody::applyImpulse(const Vect& impulse)
|
||||||
{
|
{
|
||||||
applyImpulse(impulse, Point());
|
applyImpulse(impulse, Point());
|
||||||
|
@ -604,28 +618,31 @@ PhysicsShape* PhysicsBody::getShape(int tag) const
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsBody::removeShape(int tag)
|
void PhysicsBody::removeShape(int tag, bool reduceMassAndMoment/* = true*/)
|
||||||
{
|
{
|
||||||
for (auto child : *_shapes)
|
for (auto child : *_shapes)
|
||||||
{
|
{
|
||||||
PhysicsShape* shape = dynamic_cast<PhysicsShape*>(child);
|
PhysicsShape* shape = dynamic_cast<PhysicsShape*>(child);
|
||||||
if (shape->getTag() == tag)
|
if (shape->getTag() == tag)
|
||||||
{
|
{
|
||||||
removeShape(shape);
|
removeShape(shape, reduceMassAndMoment);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsBody::removeShape(PhysicsShape* shape)
|
void PhysicsBody::removeShape(PhysicsShape* shape, bool reduceMassAndMoment/* = true*/)
|
||||||
{
|
{
|
||||||
if (_shapes->getIndexOfObject(shape) != UINT_MAX)
|
if (_shapes->getIndexOfObject(shape) != CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
// deduce the area, mass and moment
|
// deduce the area, mass and moment
|
||||||
// area must update before mass, because the density changes depend on it.
|
// area must update before mass, because the density changes depend on it.
|
||||||
|
if (reduceMassAndMoment)
|
||||||
|
{
|
||||||
_area -= shape->getArea();
|
_area -= shape->getArea();
|
||||||
addMass(-shape->getMass());
|
addMass(-shape->getMass());
|
||||||
addMoment(-shape->getMoment());
|
addMoment(-shape->getMoment());
|
||||||
|
}
|
||||||
|
|
||||||
//remove
|
//remove
|
||||||
if (_world)
|
if (_world)
|
||||||
|
@ -640,7 +657,7 @@ void PhysicsBody::removeShape(PhysicsShape* shape)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsBody::removeAllShapes()
|
void PhysicsBody::removeAllShapes(bool reduceMassAndMoment/* = true*/)
|
||||||
{
|
{
|
||||||
for (auto child : *_shapes)
|
for (auto child : *_shapes)
|
||||||
{
|
{
|
||||||
|
@ -648,9 +665,12 @@ void PhysicsBody::removeAllShapes()
|
||||||
|
|
||||||
// deduce the area, mass and moment
|
// deduce the area, mass and moment
|
||||||
// area must update before mass, because the density changes depend on it.
|
// area must update before mass, because the density changes depend on it.
|
||||||
|
if (reduceMassAndMoment)
|
||||||
|
{
|
||||||
_area -= shape->getArea();
|
_area -= shape->getArea();
|
||||||
addMass(-shape->getMass());
|
addMass(-shape->getMass());
|
||||||
addMoment(-shape->getMoment());
|
addMoment(-shape->getMoment());
|
||||||
|
}
|
||||||
|
|
||||||
if (_world)
|
if (_world)
|
||||||
{
|
{
|
||||||
|
|
|
@ -49,20 +49,22 @@ const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
|
||||||
/**
|
/**
|
||||||
* A body affect by physics.
|
* A body affect by physics.
|
||||||
* it can attach one or more shapes.
|
* it can attach one or more shapes.
|
||||||
|
* if you create body with createXXX, it will automatically compute mass and moment with density your specified(which is PHYSICSBODY_MATERIAL_DEFAULT by default, and the density value is 0.1f), and it based on the formular: mass = density * area.
|
||||||
|
* if you create body with createEdgeXXX, the mass and moment will be INFINITY by default. and it's a static body.
|
||||||
|
* you can change mass and moment with setMass() and setMoment(). and you can change the body to be dynamic or static by use function setDynamic().
|
||||||
*/
|
*/
|
||||||
class PhysicsBody : public Object//, public Clonable
|
class PhysicsBody : public Object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/** create a body with defult mass and moment. */
|
||||||
static PhysicsBody* create();
|
static PhysicsBody* create();
|
||||||
|
/** create a body with mass and defult moment. */
|
||||||
static PhysicsBody* create(float mass);
|
static PhysicsBody* create(float mass);
|
||||||
|
/** create a body with mass and moment. */
|
||||||
static PhysicsBody* create(float mass, float moment);
|
static PhysicsBody* create(float mass, float moment);
|
||||||
/**
|
/** Create a body contains a circle shape. */
|
||||||
* @brief Create a body contains a circle shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
||||||
/**
|
/** Create a body contains a box shape. */
|
||||||
* @brief Create a body contains a box shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
static PhysicsBody* createBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
||||||
/**
|
/**
|
||||||
* @brief Create a body contains a polygon shape.
|
* @brief Create a body contains a polygon shape.
|
||||||
|
@ -70,132 +72,145 @@ public:
|
||||||
*/
|
*/
|
||||||
static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
|
||||||
|
|
||||||
/**
|
/** Create a body contains a EdgeSegment shape. */
|
||||||
* @brief Create a body contains a EdgeSegment shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
static PhysicsBody* createEdgeSegment(const Point& a, const Point& b, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
||||||
/**
|
/** Create a body contains a EdgeBox shape. */
|
||||||
* @brief Create a body contains a EdgeBox shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO);
|
static PhysicsBody* createEdgeBox(const Size& size, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1, const Point& offset = Point::ZERO);
|
||||||
/**
|
/** Create a body contains a EdgePolygon shape. */
|
||||||
* @brief Create a body contains a EdgePolygon shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
||||||
/**
|
/** Create a body contains a EdgeChain shape. */
|
||||||
* @brief Create a body contains a EdgeChain shape.
|
|
||||||
*/
|
|
||||||
static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
static PhysicsBody* createEdgeChain(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
|
||||||
|
|
||||||
virtual PhysicsShape* addShape(PhysicsShape* shape);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Applies a immediate force to body.
|
|
||||||
*/
|
|
||||||
virtual void applyForce(const Vect& force);
|
|
||||||
/**
|
|
||||||
* @brief Applies a immediate force to body.
|
|
||||||
*/
|
|
||||||
virtual void applyForce(const Vect& force, const Point& offset);
|
|
||||||
/**
|
|
||||||
* @brief Applies a continuous force to body.
|
|
||||||
*/
|
|
||||||
virtual void applyImpulse(const Vect& impulse);
|
|
||||||
/**
|
|
||||||
* @brief Applies a continuous force to body.
|
|
||||||
*/
|
|
||||||
virtual void applyImpulse(const Vect& impulse, const Point& offset);
|
|
||||||
/**
|
|
||||||
* @brief Applies a torque force to body.
|
|
||||||
*/
|
|
||||||
virtual void applyTorque(float torque);
|
|
||||||
|
|
||||||
virtual void setVelocity(const Vect& velocity);
|
|
||||||
virtual Point getVelocity();
|
|
||||||
virtual void setAngularVelocity(float velocity);
|
|
||||||
virtual Point getVelocityAtLocalPoint(const Point& point);
|
|
||||||
virtual Point getVelocityAtWorldPoint(const Point& point);
|
|
||||||
virtual float getAngularVelocity();
|
|
||||||
virtual void setVelocityLimit(float limit);
|
|
||||||
virtual float getVelocityLimit();
|
|
||||||
virtual void setAngularVelocityLimit(float limit);
|
|
||||||
virtual float getAngularVelocityLimit();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief get the body shapes.
|
* @brief add a shape to body
|
||||||
|
* @param shape the shape to be added
|
||||||
|
* @param addMassAndMoment if this is true, the shape's mass and moment will be added to body. the default is true
|
||||||
*/
|
*/
|
||||||
inline Array* getShapes() const { return _shapes; }
|
virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
|
||||||
/*
|
|
||||||
* @brief get the first body shapes.
|
|
||||||
*/
|
|
||||||
inline PhysicsShape* getFirstShape() const { return _shapes->count() >= 1 ? dynamic_cast<PhysicsShape*>(_shapes->getObjectAtIndex(0)) : nullptr; }
|
|
||||||
PhysicsShape* getShape(int tag) const;
|
|
||||||
/*
|
/*
|
||||||
* @brief remove a shape from body
|
* @brief remove a shape from body
|
||||||
|
* @param shape the shape to be removed
|
||||||
|
* @param reduceMassAndMoment if this is true, the body mass and moment will be reduced by shape. the default is true
|
||||||
*/
|
*/
|
||||||
void removeShape(PhysicsShape* shape);
|
void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
|
||||||
void removeShape(int tag);
|
|
||||||
/*
|
/*
|
||||||
* @brief remove all shapes
|
* @brief remove a shape from body
|
||||||
|
* @param tag the tag of the shape to be removed
|
||||||
|
* @param reduceMassAndMoment if this is true, the body mass and moment will be reduced by shape. the default is true
|
||||||
*/
|
*/
|
||||||
void removeAllShapes();
|
void removeShape(int tag, bool reduceMassAndMoment = true);
|
||||||
|
/* remove all shapes */
|
||||||
|
void removeAllShapes(bool reduceMassAndMoment = true);
|
||||||
|
/* get the body shapes. */
|
||||||
|
inline Array* getShapes() const { return _shapes; }
|
||||||
|
/* get the first shape of the body shapes. */
|
||||||
|
inline PhysicsShape* getFirstShape() const { return _shapes->count() >= 1 ? dynamic_cast<PhysicsShape*>(_shapes->getObjectAtIndex(0)) : nullptr; }
|
||||||
|
/* get the shape of the body. */
|
||||||
|
PhysicsShape* getShape(int tag) const;
|
||||||
|
|
||||||
|
/** Applies a immediate force to body. */
|
||||||
|
virtual void applyForce(const Vect& force);
|
||||||
|
/** Applies a immediate force to body. */
|
||||||
|
virtual void applyForce(const Vect& force, const Point& offset);
|
||||||
|
/** reset all the force applied to body. */
|
||||||
|
virtual void resetForce();
|
||||||
|
/** Applies a continuous force to body. */
|
||||||
|
virtual void applyImpulse(const Vect& impulse);
|
||||||
|
/** Applies a continuous force to body. */
|
||||||
|
virtual void applyImpulse(const Vect& impulse, const Point& offset);
|
||||||
|
/** Applies a torque force to body. */
|
||||||
|
virtual void applyTorque(float torque);
|
||||||
|
|
||||||
|
/** set the velocity of a body */
|
||||||
|
virtual void setVelocity(const Vect& velocity);
|
||||||
|
/** get the velocity of a body */
|
||||||
|
virtual Point getVelocity();
|
||||||
|
/** set the angular velocity of a body */
|
||||||
|
virtual void setAngularVelocity(float velocity);
|
||||||
|
/** get the angular velocity of a body at a local point */
|
||||||
|
virtual Point getVelocityAtLocalPoint(const Point& point);
|
||||||
|
/** get the angular velocity of a body at a world point */
|
||||||
|
virtual Point getVelocityAtWorldPoint(const Point& point);
|
||||||
|
/** get the angular velocity of a body */
|
||||||
|
virtual float getAngularVelocity();
|
||||||
|
/** set the max of velocity */
|
||||||
|
virtual void setVelocityLimit(float limit);
|
||||||
|
/** get the max of velocity */
|
||||||
|
virtual float getVelocityLimit();
|
||||||
|
/** set the max of angular velocity */
|
||||||
|
virtual void setAngularVelocityLimit(float limit);
|
||||||
|
/** get the max of angular velocity */
|
||||||
|
virtual float getAngularVelocityLimit();
|
||||||
|
|
||||||
|
/** remove the body from the world it added to */
|
||||||
void removeFromWorld();
|
void removeFromWorld();
|
||||||
|
|
||||||
/*
|
/** get the world body added to. */
|
||||||
* @brief get the world body added to.
|
|
||||||
*/
|
|
||||||
inline PhysicsWorld* getWorld() const { return _world; }
|
inline PhysicsWorld* getWorld() const { return _world; }
|
||||||
/*
|
/** get all joints the body have */
|
||||||
* @brief get all joints the body have
|
|
||||||
*/
|
|
||||||
inline const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
|
inline const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
|
||||||
|
|
||||||
/*
|
/** get the sprite the body set to. */
|
||||||
* @brief get the sprite the body set to.
|
|
||||||
*/
|
|
||||||
inline Node* getNode() const { return _node; }
|
inline Node* getNode() const { return _node; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A mask that defines which categories this physics body belongs to.
|
||||||
|
* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
|
||||||
|
* The default value is 0xFFFFFFFF (all bits set).
|
||||||
|
*/
|
||||||
void setCategoryBitmask(int bitmask);
|
void setCategoryBitmask(int bitmask);
|
||||||
|
/**
|
||||||
|
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
|
||||||
|
* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
|
||||||
|
* The default value is 0x00000000 (all bits cleared).
|
||||||
|
*/
|
||||||
void setContactTestBitmask(int bitmask);
|
void setContactTestBitmask(int bitmask);
|
||||||
|
/**
|
||||||
|
* A mask that defines which categories of physics bodies can collide with this physics body.
|
||||||
|
* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
|
||||||
|
* The default value is 0xFFFFFFFF (all bits set).
|
||||||
|
*/
|
||||||
void setCollisionBitmask(int bitmask);
|
void setCollisionBitmask(int bitmask);
|
||||||
|
/** get the category bit mask */
|
||||||
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
inline int getCategoryBitmask() const { return _categoryBitmask; }
|
||||||
|
/** get the contact test bit mask */
|
||||||
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
inline int getContactTestBitmask() const { return _contactTestBitmask; }
|
||||||
|
/** get the collision bit mask */
|
||||||
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
inline int getCollisionBitmask() const { return _collisionBitmask; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the group of body
|
||||||
|
* Collision groups let you specify an integral group index. You can have all fixtures with the same group index always collide (positive index) or never collide (negative index)
|
||||||
|
* it have high priority than bit masks
|
||||||
|
*/
|
||||||
void setGroup(int group);
|
void setGroup(int group);
|
||||||
|
/** get the group of body */
|
||||||
inline int getGroup() const { return _group; }
|
inline int getGroup() const { return _group; }
|
||||||
|
|
||||||
/*
|
/** get the body position. */
|
||||||
* @brief get the body position.
|
|
||||||
*/
|
|
||||||
Point getPosition() const;
|
Point getPosition() const;
|
||||||
/*
|
/** get the body rotation. */
|
||||||
* @brief get the body rotation.
|
|
||||||
*/
|
|
||||||
float getRotation() const;
|
float getRotation() const;
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @brief test the body is dynamic or not.
|
* @brief test the body is dynamic or not.
|
||||||
* a dynamic body will effect with gravity.
|
* a dynamic body will effect with gravity.
|
||||||
*/
|
*/
|
||||||
inline bool isDynamic() const { return _dynamic; }
|
inline bool isDynamic() const { return _dynamic; }
|
||||||
/*
|
/**
|
||||||
* @brief set dynamic to body.
|
* @brief set dynamic to body.
|
||||||
* a dynamic body will effect with gravity.
|
* a dynamic body will effect with gravity.
|
||||||
*/
|
*/
|
||||||
void setDynamic(bool dynamic);
|
void setDynamic(bool dynamic);
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @brief set the body mass.
|
* @brief set the body mass.
|
||||||
* @note if you need add/subtract mass to body, don't use setMass(getMass() +/- mass), because the mass of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMass() instead.
|
* @note if you need add/subtract mass to body, don't use setMass(getMass() +/- mass), because the mass of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMass() instead.
|
||||||
*/
|
*/
|
||||||
void setMass(float mass);
|
void setMass(float mass);
|
||||||
/*
|
/** get the body mass. */
|
||||||
* @brief get the body mass.
|
|
||||||
*/
|
|
||||||
inline float getMass() const { return _mass; }
|
inline float getMass() const { return _mass; }
|
||||||
/*
|
/**
|
||||||
* @brief add mass to body.
|
* @brief add mass to body.
|
||||||
* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
|
* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
|
||||||
* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
|
* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
|
||||||
|
@ -205,16 +220,14 @@ public:
|
||||||
*/
|
*/
|
||||||
void addMass(float mass);
|
void addMass(float mass);
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* @brief set the body moment of inertia.
|
* @brief set the body moment of inertia.
|
||||||
* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
|
* @note if you need add/subtract moment to body, don't use setMoment(getMoment() +/- moment), because the moment of body may be equal to PHYSICS_INFINITY, it will cause some unexpected result, please use addMoment() instead.
|
||||||
*/
|
*/
|
||||||
void setMoment(float moment);
|
void setMoment(float moment);
|
||||||
/*
|
/** get the body moment of inertia. */
|
||||||
* @brief get the body moment of inertia.
|
|
||||||
*/
|
|
||||||
inline float getMoment(float moment) const { return _moment; }
|
inline float getMoment(float moment) const { return _moment; }
|
||||||
/*
|
/**
|
||||||
* @brief add moment of inertia to body.
|
* @brief add moment of inertia to body.
|
||||||
* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
|
* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
|
||||||
* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
|
* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
|
||||||
|
@ -223,35 +236,54 @@ public:
|
||||||
* other wise, moment = moment + _moment;
|
* other wise, moment = moment + _moment;
|
||||||
*/
|
*/
|
||||||
void addMoment(float moment);
|
void addMoment(float moment);
|
||||||
/*
|
/** get linear damping. */
|
||||||
* @brief set angular damping.
|
|
||||||
*/
|
|
||||||
//void setAngularDamping(float angularDamping);
|
|
||||||
/*
|
|
||||||
* @brief get angular damping.
|
|
||||||
*/
|
|
||||||
inline float getLinearDamping() const { return _linearDamping; }
|
inline float getLinearDamping() const { return _linearDamping; }
|
||||||
|
/**
|
||||||
|
* set linear damping.
|
||||||
|
* it is used to simulate fluid or air friction forces on the body.
|
||||||
|
* the value is 0.0f to 1.0f.
|
||||||
|
*/
|
||||||
inline void setLinearDamping(float damping) { _linearDamping = damping; }
|
inline void setLinearDamping(float damping) { _linearDamping = damping; }
|
||||||
|
/** get angular damping. */
|
||||||
inline float getAngularDamping() const { return _angularDamping; }
|
inline float getAngularDamping() const { return _angularDamping; }
|
||||||
|
/**
|
||||||
|
* set angular damping.
|
||||||
|
* it is used to simulate fluid or air friction forces on the body.
|
||||||
|
* the value is 0.0f to 1.0f.
|
||||||
|
*/
|
||||||
inline void setAngularDamping(float damping) { _angularDamping = damping; }
|
inline void setAngularDamping(float damping) { _angularDamping = damping; }
|
||||||
|
|
||||||
//virtual Clonable* clone() const override;
|
/** whether the body is at rest */
|
||||||
|
|
||||||
bool isResting() const;
|
bool isResting() const;
|
||||||
|
/**
|
||||||
|
* whether the body is enabled
|
||||||
|
* if the body it isn't enabled, it will not has simulation by world
|
||||||
|
*/
|
||||||
inline bool isEnabled() const { return _enable; }
|
inline bool isEnabled() const { return _enable; }
|
||||||
|
/**
|
||||||
|
* set the enable value.
|
||||||
|
* if the body it isn't enabled, it will not has simulation by world
|
||||||
|
*/
|
||||||
void setEnable(bool enable);
|
void setEnable(bool enable);
|
||||||
|
|
||||||
|
/** whether the body can rotation */
|
||||||
inline bool isRotationEnabled() const { return _rotationEnable; }
|
inline bool isRotationEnabled() const { return _rotationEnable; }
|
||||||
|
/** set the body is allow rotation or not */
|
||||||
void setRotationEnable(bool enable);
|
void setRotationEnable(bool enable);
|
||||||
|
|
||||||
|
/** whether this physics body is affected by the physics world’s gravitational force. */
|
||||||
inline bool isGravityEnabled() const { return _gravityEnable; }
|
inline bool isGravityEnabled() const { return _gravityEnable; }
|
||||||
|
/** set the body is affected by the physics world's gravitational force or not. */
|
||||||
void setGravityEnable(bool enable);
|
void setGravityEnable(bool enable);
|
||||||
|
|
||||||
|
/** get the body's tag */
|
||||||
inline int getTag() const { return _tag; }
|
inline int getTag() const { return _tag; }
|
||||||
|
/** set the body's tag */
|
||||||
inline void setTag(int tag) { _tag = tag; }
|
inline void setTag(int tag) { _tag = tag; }
|
||||||
|
|
||||||
|
/** convert the world point to local */
|
||||||
Point world2Local(const Point& point);
|
Point world2Local(const Point& point);
|
||||||
|
/** convert the local point to world */
|
||||||
Point local2World(const Point& point);
|
Point local2World(const Point& point);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -130,7 +130,7 @@ float PhysicsContactPreSolve::getElasticity() const
|
||||||
return static_cast<cpArbiter*>(_contactInfo)->e;
|
return static_cast<cpArbiter*>(_contactInfo)->e;
|
||||||
}
|
}
|
||||||
|
|
||||||
float PhysicsContactPreSolve::getFriciton() const
|
float PhysicsContactPreSolve::getFriction() const
|
||||||
{
|
{
|
||||||
return static_cast<cpArbiter*>(_contactInfo)->u;
|
return static_cast<cpArbiter*>(_contactInfo)->u;
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ float PhysicsContactPostSolve::getElasticity() const
|
||||||
return static_cast<cpArbiter*>(_contactInfo)->e;
|
return static_cast<cpArbiter*>(_contactInfo)->e;
|
||||||
}
|
}
|
||||||
|
|
||||||
float PhysicsContactPostSolve::getFriciton() const
|
float PhysicsContactPostSolve::getFriction() const
|
||||||
{
|
{
|
||||||
return static_cast<cpArbiter*>(_contactInfo)->u;
|
return static_cast<cpArbiter*>(_contactInfo)->u;
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
if (onContactBegin != nullptr
|
if (onContactBegin != nullptr
|
||||||
&& test(contact.getShapeA(), contact.getShapeB()))
|
&& hitTest(contact.getShapeA(), contact.getShapeB()))
|
||||||
{
|
{
|
||||||
contact._begin = true;
|
contact._begin = true;
|
||||||
contact.generateContactData();
|
contact.generateContactData();
|
||||||
|
@ -240,7 +240,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
if (onContactPreSolve != nullptr
|
if (onContactPreSolve != nullptr
|
||||||
&& test(contact.getShapeA(), contact.getShapeB()))
|
&& hitTest(contact.getShapeA(), contact.getShapeB()))
|
||||||
{
|
{
|
||||||
PhysicsContactPreSolve solve(contact._begin ? nullptr : contact._contactData, contact._contactInfo);
|
PhysicsContactPreSolve solve(contact._begin ? nullptr : contact._contactData, contact._contactInfo);
|
||||||
contact._begin = false;
|
contact._begin = false;
|
||||||
|
@ -255,7 +255,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
|
||||||
case PhysicsContact::EventCode::POSTSOLVE:
|
case PhysicsContact::EventCode::POSTSOLVE:
|
||||||
{
|
{
|
||||||
if (onContactPostSolve != nullptr
|
if (onContactPostSolve != nullptr
|
||||||
&& test(contact.getShapeA(), contact.getShapeB()))
|
&& hitTest(contact.getShapeA(), contact.getShapeB()))
|
||||||
{
|
{
|
||||||
PhysicsContactPostSolve solve(contact._contactInfo);
|
PhysicsContactPostSolve solve(contact._contactInfo);
|
||||||
onContactPostSolve(event, contact, solve);
|
onContactPostSolve(event, contact, solve);
|
||||||
|
@ -265,7 +265,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
|
||||||
case PhysicsContact::EventCode::SEPERATE:
|
case PhysicsContact::EventCode::SEPERATE:
|
||||||
{
|
{
|
||||||
if (onContactSeperate != nullptr
|
if (onContactSeperate != nullptr
|
||||||
&& test(contact.getShapeA(), contact.getShapeB()))
|
&& hitTest(contact.getShapeA(), contact.getShapeB()))
|
||||||
{
|
{
|
||||||
onContactSeperate(event, contact);
|
onContactSeperate(event, contact);
|
||||||
}
|
}
|
||||||
|
@ -295,7 +295,7 @@ EventListenerPhysicsContact* EventListenerPhysicsContact::create()
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EventListenerPhysicsContact::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
bool EventListenerPhysicsContact::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
||||||
{
|
{
|
||||||
CC_UNUSED_PARAM(shapeA);
|
CC_UNUSED_PARAM(shapeA);
|
||||||
CC_UNUSED_PARAM(shapeB);
|
CC_UNUSED_PARAM(shapeB);
|
||||||
|
@ -361,7 +361,7 @@ EventListenerPhysicsContactWithBodies::~EventListenerPhysicsContactWithBodies()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool EventListenerPhysicsContactWithBodies::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
bool EventListenerPhysicsContactWithBodies::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
||||||
{
|
{
|
||||||
if ((shapeA->getBody() == _a && shapeB->getBody() == _b)
|
if ((shapeA->getBody() == _a && shapeB->getBody() == _b)
|
||||||
|| (shapeA->getBody() == _b && shapeB->getBody() == _a))
|
|| (shapeA->getBody() == _b && shapeB->getBody() == _a))
|
||||||
|
@ -416,7 +416,7 @@ EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::cr
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EventListenerPhysicsContactWithShapes::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
||||||
{
|
{
|
||||||
if ((shapeA == _a && shapeB == _b)
|
if ((shapeA == _a && shapeB == _b)
|
||||||
|| (shapeA == _b && shapeB == _a))
|
|| (shapeA == _b && shapeB == _a))
|
||||||
|
@ -469,7 +469,7 @@ EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::crea
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EventListenerPhysicsContactWithGroup::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
|
||||||
{
|
{
|
||||||
if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
|
if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
|
||||||
{
|
{
|
||||||
|
|
|
@ -69,24 +69,19 @@ public:
|
||||||
SEPERATE
|
SEPERATE
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/** get contact shape A. */
|
||||||
* @brief get contact shape A.
|
|
||||||
*/
|
|
||||||
inline PhysicsShape* getShapeA() const { return _shapeA; }
|
inline PhysicsShape* getShapeA() const { return _shapeA; }
|
||||||
/*
|
/** get contact shape B. */
|
||||||
* @brief get contact shape B.
|
|
||||||
*/
|
|
||||||
inline PhysicsShape* getShapeB() const { return _shapeB; }
|
inline PhysicsShape* getShapeB() const { return _shapeB; }
|
||||||
|
/** get contact data */
|
||||||
inline const PhysicsContactData* getContactData() const { return _contactData; }
|
inline const PhysicsContactData* getContactData() const { return _contactData; }
|
||||||
/*
|
/** get data. */
|
||||||
* @brief get data.
|
|
||||||
*/
|
|
||||||
inline void* getData() const { return _data; }
|
inline void* getData() const { return _data; }
|
||||||
/*
|
/**
|
||||||
* @brief set data to contact. you must manage the memory yourself, Generally you can set data at contact begin, and distory it at contact end.
|
* @brief set data to contact. you must manage the memory yourself, Generally you can set data at contact begin, and distory it at contact seperate.
|
||||||
*/
|
*/
|
||||||
inline void setData(void* data) { _data = data; }
|
inline void setData(void* data) { _data = data; }
|
||||||
|
/** get the event code */
|
||||||
EventCode getEventCode() const { return _eventCode; };
|
EventCode getEventCode() const { return _eventCode; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -132,13 +127,19 @@ private:
|
||||||
class PhysicsContactPreSolve
|
class PhysicsContactPreSolve
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// getter/setter
|
/** get elasticity between two bodies*/
|
||||||
float getElasticity() const;
|
float getElasticity() const;
|
||||||
float getFriciton() const;
|
/** get friction between two bodies*/
|
||||||
|
float getFriction() const;
|
||||||
|
/** get surface velocity between two bodies*/
|
||||||
Point getSurfaceVelocity() const;
|
Point getSurfaceVelocity() const;
|
||||||
|
/** set the elasticity*/
|
||||||
void setElasticity(float elasticity);
|
void setElasticity(float elasticity);
|
||||||
|
/** set the friction*/
|
||||||
void setFriction(float friction);
|
void setFriction(float friction);
|
||||||
|
/** set the surface velocity*/
|
||||||
void setSurfaceVelocity(const Vect& velocity);
|
void setSurfaceVelocity(const Vect& velocity);
|
||||||
|
/** ignore the rest of the contact presolve and postsolve callbacks */
|
||||||
void ignore();
|
void ignore();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -161,9 +162,11 @@ private:
|
||||||
class PhysicsContactPostSolve
|
class PhysicsContactPostSolve
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// getter
|
/** get elasticity between two bodies*/
|
||||||
float getElasticity() const;
|
float getElasticity() const;
|
||||||
float getFriciton() const;
|
/** get friction between two bodies*/
|
||||||
|
float getFriction() const;
|
||||||
|
/** get surface velocity between two bodies*/
|
||||||
Point getSurfaceVelocity() const;
|
Point getSurfaceVelocity() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -176,18 +179,22 @@ private:
|
||||||
friend class EventListenerPhysicsContact;
|
friend class EventListenerPhysicsContact;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/* contact listener. it will recive all the contact callbacks. */
|
||||||
* @brief contact listener.
|
|
||||||
*/
|
|
||||||
class EventListenerPhysicsContact : public EventListenerCustom
|
class EventListenerPhysicsContact : public EventListenerCustom
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/** create the listener */
|
||||||
static EventListenerPhysicsContact* create();
|
static EventListenerPhysicsContact* create();
|
||||||
|
|
||||||
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
|
||||||
virtual bool checkAvailable() override;
|
virtual bool checkAvailable() override;
|
||||||
virtual EventListenerPhysicsContact* clone() override;
|
virtual EventListenerPhysicsContact* clone() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
* it will be call when two body have contact.
|
||||||
|
* if return false, it will not invoke callbacks
|
||||||
|
*/
|
||||||
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/*
|
/*
|
||||||
* @brief it will called at two shapes start to contact, and only call it once.
|
* @brief it will called at two shapes start to contact, and only call it once.
|
||||||
|
@ -214,14 +221,17 @@ protected:
|
||||||
protected:
|
protected:
|
||||||
EventListenerPhysicsContact();
|
EventListenerPhysicsContact();
|
||||||
virtual ~EventListenerPhysicsContact();
|
virtual ~EventListenerPhysicsContact();
|
||||||
|
|
||||||
|
friend class PhysicsWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** this event listener only be called when bodyA and bodyB have contacts */
|
||||||
class EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
|
class EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB);
|
static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB);
|
||||||
|
|
||||||
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
||||||
virtual EventListenerPhysicsContactWithBodies* clone() override;
|
virtual EventListenerPhysicsContactWithBodies* clone() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -233,12 +243,13 @@ protected:
|
||||||
virtual ~EventListenerPhysicsContactWithBodies();
|
virtual ~EventListenerPhysicsContactWithBodies();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** this event listener only be called when shapeA and shapeB have contacts */
|
||||||
class EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
|
class EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
|
||||||
|
|
||||||
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
||||||
virtual EventListenerPhysicsContactWithShapes* clone() override;
|
virtual EventListenerPhysicsContactWithShapes* clone() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -250,12 +261,13 @@ protected:
|
||||||
virtual ~EventListenerPhysicsContactWithShapes();
|
virtual ~EventListenerPhysicsContactWithShapes();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** this event listener only be called when shapeA or shapeB is in the group your specified */
|
||||||
class EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
|
class EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static EventListenerPhysicsContactWithGroup* create(int group);
|
static EventListenerPhysicsContactWithGroup* create(int group);
|
||||||
|
|
||||||
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
|
||||||
virtual EventListenerPhysicsContactWithGroup* clone() override;
|
virtual EventListenerPhysicsContactWithGroup* clone() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -130,16 +130,6 @@ PhysicsJointFixed::~PhysicsJointFixed()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsJointSliding::PhysicsJointSliding()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
PhysicsJointSliding::~PhysicsJointSliding()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
PhysicsJointLimit::PhysicsJointLimit()
|
PhysicsJointLimit::PhysicsJointLimit()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -296,41 +286,6 @@ float PhysicsJointPin::getMaxForce() const
|
||||||
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
|
return PhysicsHelper::cpfloat2float(_info->getJoints().front()->maxForce);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsJointSliding* PhysicsJointSliding::construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr)
|
|
||||||
{
|
|
||||||
PhysicsJointSliding* joint = new PhysicsJointSliding();
|
|
||||||
|
|
||||||
if (joint && joint->init(a, b, grooveA, grooveB, anchr))
|
|
||||||
{
|
|
||||||
return joint;
|
|
||||||
}
|
|
||||||
|
|
||||||
CC_SAFE_DELETE(joint);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PhysicsJointSliding::init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr)
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
CC_BREAK_IF(!PhysicsJoint::init(a, b));
|
|
||||||
|
|
||||||
cpConstraint* joint = cpGrooveJointNew(getBodyInfo(a)->getBody(), getBodyInfo(b)->getBody(),
|
|
||||||
PhysicsHelper::point2cpv(grooveA),
|
|
||||||
PhysicsHelper::point2cpv(grooveB),
|
|
||||||
PhysicsHelper::point2cpv(anchr));
|
|
||||||
|
|
||||||
CC_BREAK_IF(joint == nullptr);
|
|
||||||
|
|
||||||
_info->add(joint);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
} while (false);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
PhysicsJointLimit* PhysicsJointLimit::construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2)
|
||||||
{
|
{
|
||||||
PhysicsJointLimit* joint = new PhysicsJointLimit();
|
PhysicsJointLimit* joint = new PhysicsJointLimit();
|
||||||
|
@ -354,7 +309,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1
|
||||||
PhysicsHelper::point2cpv(anchr1),
|
PhysicsHelper::point2cpv(anchr1),
|
||||||
PhysicsHelper::point2cpv(anchr2),
|
PhysicsHelper::point2cpv(anchr2),
|
||||||
0,
|
0,
|
||||||
PhysicsHelper::float2cpfloat(anchr1.getDistance(anchr2)));
|
PhysicsHelper::float2cpfloat(_bodyA->local2World(anchr1).getDistance(_bodyB->local2World(anchr2))));
|
||||||
|
|
||||||
CC_BREAK_IF(joint == nullptr);
|
CC_BREAK_IF(joint == nullptr);
|
||||||
|
|
||||||
|
@ -407,7 +362,8 @@ bool PhysicsJointDistance::init(PhysicsBody* a, PhysicsBody* b, const Point& anc
|
||||||
|
|
||||||
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
|
cpConstraint* joint = cpPinJointNew(getBodyInfo(a)->getBody(),
|
||||||
getBodyInfo(b)->getBody(),
|
getBodyInfo(b)->getBody(),
|
||||||
PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2));
|
PhysicsHelper::point2cpv(anchr1),
|
||||||
|
PhysicsHelper::point2cpv(anchr2));
|
||||||
|
|
||||||
CC_BREAK_IF(joint == nullptr);
|
CC_BREAK_IF(joint == nullptr);
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,7 @@ protected:
|
||||||
|
|
||||||
friend class PhysicsBody;
|
friend class PhysicsBody;
|
||||||
friend class PhysicsWorld;
|
friend class PhysicsWorld;
|
||||||
|
friend class PhysicsDebugDraw;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -99,45 +100,13 @@ protected:
|
||||||
virtual ~PhysicsJointFixed();
|
virtual ~PhysicsJointFixed();
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* @brief A sliding joint allows the two bodies to slide along a chosen axis.
|
|
||||||
*/
|
|
||||||
class PhysicsJointSliding : public PhysicsJoint
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static PhysicsJointSliding* construct(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
bool init(PhysicsBody* a, PhysicsBody* b, const Point& grooveA, const Point& grooveB, const Point& anchr);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
PhysicsJointSliding();
|
|
||||||
virtual ~PhysicsJointSliding();
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* @brief A spring joint connects the two bodies with a spring whose length is the initial distance between the two bodies.
|
|
||||||
*/
|
|
||||||
class PhysicsJointSpring : public PhysicsJoint
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PhysicsJointSpring* construct();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
bool init();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
PhysicsJointSpring();
|
|
||||||
virtual ~PhysicsJointSpring();
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope.
|
* @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope.
|
||||||
*/
|
*/
|
||||||
class PhysicsJointLimit : public PhysicsJoint
|
class PhysicsJointLimit : public PhysicsJoint
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
||||||
|
|
||||||
float getMin() const;
|
float getMin() const;
|
||||||
void setMin(float min);
|
void setMin(float min);
|
||||||
|
@ -173,7 +142,6 @@ protected:
|
||||||
|
|
||||||
class PhysicsJointDistance : public PhysicsJoint
|
class PhysicsJointDistance : public PhysicsJoint
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
|
||||||
|
|
||||||
|
|
|
@ -793,6 +793,8 @@ void PhysicsShape::setGroup(int group)
|
||||||
cpShapeSetGroup(shape, (cpGroup)group);
|
cpShapeSetGroup(shape, (cpGroup)group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_group = group;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicsShape::containsPoint(const Point& point) const
|
bool PhysicsShape::containsPoint(const Point& point) const
|
||||||
|
|
|
@ -144,6 +144,7 @@ protected:
|
||||||
friend class PhysicsWorld;
|
friend class PhysicsWorld;
|
||||||
friend class PhysicsBody;
|
friend class PhysicsBody;
|
||||||
friend class PhysicsJoint;
|
friend class PhysicsJoint;
|
||||||
|
friend class PhysicsDebugDraw;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A circle shape */
|
/** A circle shape */
|
||||||
|
|
|
@ -85,6 +85,13 @@ namespace
|
||||||
PhysicsRectQueryCallbackFunc func;
|
PhysicsRectQueryCallbackFunc func;
|
||||||
void* data;
|
void* data;
|
||||||
}RectQueryCallbackInfo;
|
}RectQueryCallbackInfo;
|
||||||
|
|
||||||
|
typedef struct PointQueryCallbackInfo
|
||||||
|
{
|
||||||
|
PhysicsWorld* world;
|
||||||
|
PhysicsPointQueryCallbackFunc func;
|
||||||
|
void* data;
|
||||||
|
}PointQueryCallbackInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
class PhysicsWorldCallback
|
class PhysicsWorldCallback
|
||||||
|
@ -95,8 +102,9 @@ public:
|
||||||
static void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, PhysicsWorld *world);
|
static void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, PhysicsWorld *world);
|
||||||
static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, PhysicsWorld *world);
|
static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, PhysicsWorld *world);
|
||||||
static void rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect n, RayCastCallbackInfo *info);
|
static void rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect n, RayCastCallbackInfo *info);
|
||||||
static void rectQueryCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info);
|
static void queryRectCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info);
|
||||||
static void nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr);
|
static void queryPointFunc(cpShape *shape, cpFloat distance, cpVect point, PointQueryCallbackInfo *info);
|
||||||
|
static void getShapesAtPointFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static bool continues;
|
static bool continues;
|
||||||
|
@ -161,7 +169,7 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect
|
||||||
PhysicsWorldCallback::continues = info->func(*info->world, callbackInfo, info->data);
|
PhysicsWorldCallback::continues = info->func(*info->world, callbackInfo, info->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info)
|
void PhysicsWorldCallback::queryRectCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info)
|
||||||
{
|
{
|
||||||
auto it = PhysicsShapeInfo::getMap().find(shape);
|
auto it = PhysicsShapeInfo::getMap().find(shape);
|
||||||
|
|
||||||
|
@ -175,7 +183,7 @@ void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallba
|
||||||
PhysicsWorldCallback::continues = info->func(*info->world, *it->second->getShape(), info->data);
|
PhysicsWorldCallback::continues = info->func(*info->world, *it->second->getShape(), info->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr)
|
void PhysicsWorldCallback::getShapesAtPointFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr)
|
||||||
{
|
{
|
||||||
auto it = PhysicsShapeInfo::getMap().find(shape);
|
auto it = PhysicsShapeInfo::getMap().find(shape);
|
||||||
|
|
||||||
|
@ -184,6 +192,15 @@ void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distanc
|
||||||
arr->addObject(it->second->getShape());
|
arr->addObject(it->second->getShape());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhysicsWorldCallback::queryPointFunc(cpShape *shape, cpFloat distance, cpVect point, PointQueryCallbackInfo *info)
|
||||||
|
{
|
||||||
|
auto it = PhysicsShapeInfo::getMap().find(shape);
|
||||||
|
|
||||||
|
CC_ASSERT(it != PhysicsShapeInfo::getMap().end());
|
||||||
|
|
||||||
|
PhysicsWorldCallback::continues = info->func(*info->world, *it->second->getShape(), info->data);
|
||||||
|
}
|
||||||
|
|
||||||
bool PhysicsWorld::init(Scene& scene)
|
bool PhysicsWorld::init(Scene& scene)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
|
@ -219,7 +236,7 @@ bool PhysicsWorld::init(Scene& scene)
|
||||||
|
|
||||||
void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
|
void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
|
||||||
{
|
{
|
||||||
if (_delayRemoveBodies->getIndexOfObject(body) != UINT_MAX)
|
if (_delayRemoveBodies->getIndexOfObject(body) != CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
_delayRemoveBodies->removeObject(body);
|
_delayRemoveBodies->removeObject(body);
|
||||||
return;
|
return;
|
||||||
|
@ -227,7 +244,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
|
||||||
|
|
||||||
if (_info->getSpace()->locked_private)
|
if (_info->getSpace()->locked_private)
|
||||||
{
|
{
|
||||||
if (_delayAddBodies->getIndexOfObject(body) == UINT_MAX)
|
if (_delayAddBodies->getIndexOfObject(body) == CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
_delayAddBodies->addObject(body);
|
_delayAddBodies->addObject(body);
|
||||||
_delayDirty = true;
|
_delayDirty = true;
|
||||||
|
@ -240,7 +257,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
|
||||||
|
|
||||||
void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
|
void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
|
||||||
{
|
{
|
||||||
if (_delayAddBodies->getIndexOfObject(body) != UINT_MAX)
|
if (_delayAddBodies->getIndexOfObject(body) != CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
_delayAddBodies->removeObject(body);
|
_delayAddBodies->removeObject(body);
|
||||||
return;
|
return;
|
||||||
|
@ -248,7 +265,7 @@ void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
|
||||||
|
|
||||||
if (_info->getSpace()->locked_private)
|
if (_info->getSpace()->locked_private)
|
||||||
{
|
{
|
||||||
if (_delayRemoveBodies->getIndexOfObject(body) == UINT_MAX)
|
if (_delayRemoveBodies->getIndexOfObject(body) == CC_INVALID_INDEX)
|
||||||
{
|
{
|
||||||
_delayRemoveBodies->addObject(body);
|
_delayRemoveBodies->addObject(body);
|
||||||
_delayDirty = true;
|
_delayDirty = true;
|
||||||
|
@ -601,13 +618,7 @@ void PhysicsWorld::update(float delta)
|
||||||
|
|
||||||
cpSpaceStep(_info->getSpace(), delta);
|
cpSpaceStep(_info->getSpace(), delta);
|
||||||
|
|
||||||
if (_drawNode)
|
if (_debugDrawMask != DEBUGDRAW_NONE)
|
||||||
{
|
|
||||||
_drawNode->removeFromParent();
|
|
||||||
_drawNode = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_debugDraw)
|
|
||||||
{
|
{
|
||||||
debugDraw();
|
debugDraw();
|
||||||
}
|
}
|
||||||
|
@ -615,144 +626,49 @@ void PhysicsWorld::update(float delta)
|
||||||
|
|
||||||
void PhysicsWorld::debugDraw()
|
void PhysicsWorld::debugDraw()
|
||||||
{
|
{
|
||||||
|
if (_debugDraw == nullptr)
|
||||||
|
{
|
||||||
|
_debugDraw = new PhysicsDebugDraw(*this);
|
||||||
|
}
|
||||||
|
|
||||||
if (_debugDraw && _bodies != nullptr)
|
if (_debugDraw && _bodies != nullptr)
|
||||||
{
|
{
|
||||||
_drawNode= DrawNode::create();
|
if (_debugDraw->begin())
|
||||||
|
{
|
||||||
|
if (_debugDrawMask & DEBUGDRAW_SHAPE)
|
||||||
|
{
|
||||||
for (Object* obj : *_bodies)
|
for (Object* obj : *_bodies)
|
||||||
{
|
{
|
||||||
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
|
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
|
||||||
|
|
||||||
for (auto shape : *body->getShapes())
|
for (auto shape : *body->getShapes())
|
||||||
{
|
{
|
||||||
drawWithShape(_drawNode, dynamic_cast<PhysicsShape*>(shape));
|
_debugDraw->drawShape(*dynamic_cast<PhysicsShape*>(shape));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_debugDrawMask & DEBUGDRAW_JOINT)
|
||||||
|
{
|
||||||
for (auto joint : _joints)
|
for (auto joint : _joints)
|
||||||
{
|
{
|
||||||
drawWithJoint(_drawNode, joint);
|
_debugDraw->drawJoint(*joint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_scene)
|
_debugDraw->end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsWorld::setDebugDrawMask(int mask)
|
||||||
{
|
{
|
||||||
_scene->addChild(_drawNode);
|
if (mask == DEBUGDRAW_NONE)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhysicsWorld::drawWithJoint(DrawNode* node, PhysicsJoint* joint)
|
|
||||||
{
|
{
|
||||||
for (auto it = joint->_info->getJoints().begin(); it != joint->_info->getJoints().end(); ++it)
|
CC_SAFE_DELETE(_debugDraw);
|
||||||
{
|
|
||||||
cpConstraint *constraint = *it;
|
|
||||||
|
|
||||||
|
|
||||||
cpBody *body_a = constraint->a;
|
|
||||||
cpBody *body_b = constraint->b;
|
|
||||||
|
|
||||||
const cpConstraintClass *klass = constraint->klass_private;
|
|
||||||
if(klass == cpPinJointGetClass())
|
|
||||||
{
|
|
||||||
cpPinJoint *subJoint = (cpPinJoint *)constraint;
|
|
||||||
|
|
||||||
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
|
||||||
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
|
||||||
|
|
||||||
node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
}
|
|
||||||
else if(klass == cpSlideJointGetClass())
|
|
||||||
{
|
|
||||||
cpSlideJoint *subJoint = (cpSlideJoint *)constraint;
|
|
||||||
|
|
||||||
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
|
||||||
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
|
||||||
|
|
||||||
node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
}
|
|
||||||
else if(klass == cpPivotJointGetClass())
|
|
||||||
{
|
|
||||||
cpPivotJoint *subJoint = (cpPivotJoint *)constraint;
|
|
||||||
|
|
||||||
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
|
||||||
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
|
||||||
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
}
|
|
||||||
else if(klass == cpGrooveJointGetClass())
|
|
||||||
{
|
|
||||||
cpGrooveJoint *subJoint = (cpGrooveJoint *)constraint;
|
|
||||||
|
|
||||||
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->grv_a, body_a->rot));
|
|
||||||
cpVect b = cpvadd(body_a->p, cpvrotate(subJoint->grv_b, body_a->rot));
|
|
||||||
cpVect c = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
|
||||||
|
|
||||||
node->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
|
||||||
node->drawDot(PhysicsHelper::cpv2point(c), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
|
||||||
}
|
|
||||||
else if(klass == cpDampedSpringGetClass())
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape)
|
_debugDrawMask = mask;
|
||||||
{
|
|
||||||
for (auto it = shape->_info->getShapes().begin(); it != shape->_info->getShapes().end(); ++it)
|
|
||||||
{
|
|
||||||
cpShape *subShape = *it;
|
|
||||||
|
|
||||||
switch ((*it)->klass_private->type)
|
|
||||||
{
|
|
||||||
case CP_CIRCLE_SHAPE:
|
|
||||||
{
|
|
||||||
float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(subShape));
|
|
||||||
Point centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(subShape)))
|
|
||||||
+ PhysicsHelper::cpv2point(cpCircleShapeGetOffset(subShape));
|
|
||||||
|
|
||||||
static const int CIRCLE_SEG_NUM = 12;
|
|
||||||
Point seg[CIRCLE_SEG_NUM] = {};
|
|
||||||
|
|
||||||
for (int i = 0; i < CIRCLE_SEG_NUM; ++i)
|
|
||||||
{
|
|
||||||
float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f;
|
|
||||||
Point d(radius * cosf(angle), radius * sinf(angle));
|
|
||||||
seg[i] = centre + d;
|
|
||||||
}
|
|
||||||
node->drawPolygon(seg, CIRCLE_SEG_NUM, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1, Color4F(1, 0, 0, 1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CP_SEGMENT_SHAPE:
|
|
||||||
{
|
|
||||||
cpSegmentShape *seg = (cpSegmentShape *)subShape;
|
|
||||||
node->drawSegment(PhysicsHelper::cpv2point(seg->ta),
|
|
||||||
PhysicsHelper::cpv2point(seg->tb),
|
|
||||||
PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), Color4F(1, 0, 0, 1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CP_POLY_SHAPE:
|
|
||||||
{
|
|
||||||
cpPolyShape* poly = (cpPolyShape*)subShape;
|
|
||||||
int num = poly->numVerts;
|
|
||||||
Point* seg = new Point[num];
|
|
||||||
|
|
||||||
PhysicsHelper::cpvs2points(poly->tVerts, seg, num);
|
|
||||||
|
|
||||||
node->drawPolygon(seg, num, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1.0f, Color4F(1.0f, 0.0f, 0.0f, 1.0f));
|
|
||||||
|
|
||||||
delete[] seg;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
|
int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
|
||||||
|
@ -883,7 +799,7 @@ void PhysicsWorld::setGravity(const Vect& gravity)
|
||||||
|
|
||||||
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data)
|
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data)
|
||||||
{
|
{
|
||||||
CCASSERT(func != nullptr, "callback.report shouldn't be nullptr");
|
CCASSERT(func != nullptr, "func shouldn't be nullptr");
|
||||||
|
|
||||||
if (func != nullptr)
|
if (func != nullptr)
|
||||||
{
|
{
|
||||||
|
@ -901,9 +817,9 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PhysicsWorld::rectQuery(PhysicsRectQueryCallbackFunc func, const Rect& rect, void* data)
|
void PhysicsWorld::queryRect(PhysicsRectQueryCallbackFunc func, const Rect& rect, void* data)
|
||||||
{
|
{
|
||||||
CCASSERT(func != nullptr, "callback.report shouldn't be nullptr");
|
CCASSERT(func != nullptr, "func shouldn't be nullptr");
|
||||||
|
|
||||||
if (func != nullptr)
|
if (func != nullptr)
|
||||||
{
|
{
|
||||||
|
@ -914,7 +830,26 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallbackFunc func, const Rect& rect
|
||||||
PhysicsHelper::rect2cpbb(rect),
|
PhysicsHelper::rect2cpbb(rect),
|
||||||
CP_ALL_LAYERS,
|
CP_ALL_LAYERS,
|
||||||
CP_NO_GROUP,
|
CP_NO_GROUP,
|
||||||
(cpSpaceBBQueryFunc)PhysicsWorldCallback::rectQueryCallbackFunc,
|
(cpSpaceBBQueryFunc)PhysicsWorldCallback::queryRectCallbackFunc,
|
||||||
|
&info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsWorld::queryPoint(PhysicsPointQueryCallbackFunc func, const Point& point, void* data)
|
||||||
|
{
|
||||||
|
CCASSERT(func != nullptr, "func shouldn't be nullptr");
|
||||||
|
|
||||||
|
if (func != nullptr)
|
||||||
|
{
|
||||||
|
PointQueryCallbackInfo info = {this, func, data};
|
||||||
|
|
||||||
|
PhysicsWorldCallback::continues = true;
|
||||||
|
cpSpaceNearestPointQuery(this->_info->getSpace(),
|
||||||
|
PhysicsHelper::point2cpv(point),
|
||||||
|
0,
|
||||||
|
CP_ALL_LAYERS,
|
||||||
|
CP_NO_GROUP,
|
||||||
|
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::queryPointFunc,
|
||||||
&info);
|
&info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -927,7 +862,7 @@ Array* PhysicsWorld::getShapes(const Point& point) const
|
||||||
0,
|
0,
|
||||||
CP_ALL_LAYERS,
|
CP_ALL_LAYERS,
|
||||||
CP_NO_GROUP,
|
CP_NO_GROUP,
|
||||||
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::nearestPointQueryFunc,
|
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::getShapesAtPointFunc,
|
||||||
arr);
|
arr);
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
|
@ -963,11 +898,128 @@ PhysicsBody* PhysicsWorld::getBody(int tag) const
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void PhysicsDebugDraw::drawShape(PhysicsShape& shape)
|
||||||
|
{
|
||||||
|
for (auto it = shape._info->getShapes().begin(); it != shape._info->getShapes().end(); ++it)
|
||||||
|
{
|
||||||
|
cpShape *subShape = *it;
|
||||||
|
|
||||||
|
switch ((*it)->klass_private->type)
|
||||||
|
{
|
||||||
|
case CP_CIRCLE_SHAPE:
|
||||||
|
{
|
||||||
|
float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(subShape));
|
||||||
|
Point centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(subShape)))
|
||||||
|
+ PhysicsHelper::cpv2point(cpCircleShapeGetOffset(subShape));
|
||||||
|
|
||||||
|
static const int CIRCLE_SEG_NUM = 12;
|
||||||
|
Point seg[CIRCLE_SEG_NUM] = {};
|
||||||
|
|
||||||
|
for (int i = 0; i < CIRCLE_SEG_NUM; ++i)
|
||||||
|
{
|
||||||
|
float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f;
|
||||||
|
Point d(radius * cosf(angle), radius * sinf(angle));
|
||||||
|
seg[i] = centre + d;
|
||||||
|
}
|
||||||
|
_drawNode->drawPolygon(seg, CIRCLE_SEG_NUM, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1, Color4F(1, 0, 0, 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CP_SEGMENT_SHAPE:
|
||||||
|
{
|
||||||
|
cpSegmentShape *seg = (cpSegmentShape *)subShape;
|
||||||
|
_drawNode->drawSegment(PhysicsHelper::cpv2point(seg->ta),
|
||||||
|
PhysicsHelper::cpv2point(seg->tb),
|
||||||
|
PhysicsHelper::cpfloat2float(seg->r==0 ? 1 : seg->r), Color4F(1, 0, 0, 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CP_POLY_SHAPE:
|
||||||
|
{
|
||||||
|
cpPolyShape* poly = (cpPolyShape*)subShape;
|
||||||
|
int num = poly->numVerts;
|
||||||
|
Point* seg = new Point[num];
|
||||||
|
|
||||||
|
PhysicsHelper::cpvs2points(poly->tVerts, seg, num);
|
||||||
|
|
||||||
|
_drawNode->drawPolygon(seg, num, Color4F(1.0f, 0.0f, 0.0f, 0.3f), 1.0f, Color4F(1.0f, 0.0f, 0.0f, 1.0f));
|
||||||
|
|
||||||
|
delete[] seg;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsDebugDraw::drawJoint(PhysicsJoint& joint)
|
||||||
|
{
|
||||||
|
for (auto it = joint._info->getJoints().begin(); it != joint._info->getJoints().end(); ++it)
|
||||||
|
{
|
||||||
|
cpConstraint *constraint = *it;
|
||||||
|
|
||||||
|
|
||||||
|
cpBody *body_a = constraint->a;
|
||||||
|
cpBody *body_b = constraint->b;
|
||||||
|
|
||||||
|
const cpConstraintClass *klass = constraint->klass_private;
|
||||||
|
if(klass == cpPinJointGetClass())
|
||||||
|
{
|
||||||
|
cpPinJoint *subJoint = (cpPinJoint *)constraint;
|
||||||
|
|
||||||
|
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
||||||
|
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
||||||
|
|
||||||
|
_drawNode->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
}
|
||||||
|
else if(klass == cpSlideJointGetClass())
|
||||||
|
{
|
||||||
|
cpSlideJoint *subJoint = (cpSlideJoint *)constraint;
|
||||||
|
|
||||||
|
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
||||||
|
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
||||||
|
|
||||||
|
_drawNode->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
}
|
||||||
|
else if(klass == cpPivotJointGetClass())
|
||||||
|
{
|
||||||
|
cpPivotJoint *subJoint = (cpPivotJoint *)constraint;
|
||||||
|
|
||||||
|
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->anchr1, body_a->rot));
|
||||||
|
cpVect b = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
||||||
|
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(a), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(b), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
}
|
||||||
|
else if(klass == cpGrooveJointGetClass())
|
||||||
|
{
|
||||||
|
cpGrooveJoint *subJoint = (cpGrooveJoint *)constraint;
|
||||||
|
|
||||||
|
cpVect a = cpvadd(body_a->p, cpvrotate(subJoint->grv_a, body_a->rot));
|
||||||
|
cpVect b = cpvadd(body_a->p, cpvrotate(subJoint->grv_b, body_a->rot));
|
||||||
|
cpVect c = cpvadd(body_b->p, cpvrotate(subJoint->anchr2, body_b->rot));
|
||||||
|
|
||||||
|
_drawNode->drawSegment(PhysicsHelper::cpv2point(a), PhysicsHelper::cpv2point(b), 1, Color4F(0.0f, 0.0f, 1.0f, 1.0f));
|
||||||
|
_drawNode->drawDot(PhysicsHelper::cpv2point(c), 2, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsDebugDraw::drawContact()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PhysicsWorld* PhysicsWorld::create(Scene& scene)
|
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
|
||||||
{
|
{
|
||||||
PhysicsWorld * world = new PhysicsWorld();
|
PhysicsWorld * world = new PhysicsWorld();
|
||||||
if(world && world->init(scene))
|
if(world && world->init(scene))
|
||||||
|
@ -986,8 +1038,8 @@ PhysicsWorld::PhysicsWorld()
|
||||||
, _bodies(nullptr)
|
, _bodies(nullptr)
|
||||||
, _scene(nullptr)
|
, _scene(nullptr)
|
||||||
, _delayDirty(false)
|
, _delayDirty(false)
|
||||||
, _debugDraw(false)
|
, _debugDraw(nullptr)
|
||||||
, _drawNode(nullptr)
|
, _debugDrawMask(DEBUGDRAW_NONE)
|
||||||
, _delayAddBodies(nullptr)
|
, _delayAddBodies(nullptr)
|
||||||
, _delayRemoveBodies(nullptr)
|
, _delayRemoveBodies(nullptr)
|
||||||
{
|
{
|
||||||
|
@ -1001,6 +1053,48 @@ PhysicsWorld::~PhysicsWorld()
|
||||||
CC_SAFE_RELEASE(_delayRemoveBodies);
|
CC_SAFE_RELEASE(_delayRemoveBodies);
|
||||||
CC_SAFE_RELEASE(_delayAddBodies);
|
CC_SAFE_RELEASE(_delayAddBodies);
|
||||||
CC_SAFE_DELETE(_info);
|
CC_SAFE_DELETE(_info);
|
||||||
|
CC_SAFE_DELETE(_debugDraw);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world)
|
||||||
|
: _drawNode(nullptr)
|
||||||
|
, _world(world)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysicsDebugDraw::~PhysicsDebugDraw()
|
||||||
|
{
|
||||||
|
if (_drawNode != nullptr)
|
||||||
|
{
|
||||||
|
_drawNode->removeFromParent();
|
||||||
|
_drawNode = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PhysicsDebugDraw::begin()
|
||||||
|
{
|
||||||
|
if (_drawNode != nullptr)
|
||||||
|
{
|
||||||
|
_drawNode->removeFromParent();
|
||||||
|
_drawNode = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
_drawNode = DrawNode::create();
|
||||||
|
|
||||||
|
if (_drawNode == nullptr)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_world.getScene().addChild(_drawNode);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsDebugDraw::end()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -46,10 +46,10 @@ class Array;
|
||||||
class Sprite;
|
class Sprite;
|
||||||
class Scene;
|
class Scene;
|
||||||
class DrawNode;
|
class DrawNode;
|
||||||
|
class PhysicsDebugDraw;
|
||||||
|
|
||||||
class PhysicsWorld;
|
class PhysicsWorld;
|
||||||
|
|
||||||
|
|
||||||
typedef struct PhysicsRayCastInfo
|
typedef struct PhysicsRayCastInfo
|
||||||
{
|
{
|
||||||
PhysicsShape* shape;
|
PhysicsShape* shape;
|
||||||
|
@ -73,12 +73,20 @@ typedef struct PhysicsRayCastInfo
|
||||||
*/
|
*/
|
||||||
typedef std::function<bool(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data)> PhysicsRayCastCallbackFunc;
|
typedef std::function<bool(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data)> PhysicsRayCastCallbackFunc;
|
||||||
typedef std::function<bool(PhysicsWorld&, PhysicsShape&, void*)> PhysicsRectQueryCallbackFunc;
|
typedef std::function<bool(PhysicsWorld&, PhysicsShape&, void*)> PhysicsRectQueryCallbackFunc;
|
||||||
|
typedef PhysicsRectQueryCallbackFunc PhysicsPointQueryCallbackFunc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief An PhysicsWorld object simulates collisions and other physical properties. You do not create PhysicsWorld objects directly; instead, you can get it from an Scene object.
|
* @brief An PhysicsWorld object simulates collisions and other physical properties. You do not create PhysicsWorld objects directly; instead, you can get it from an Scene object.
|
||||||
*/
|
*/
|
||||||
class PhysicsWorld
|
class PhysicsWorld
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
static const long DEBUGDRAW_NONE = 0x00;
|
||||||
|
static const long DEBUGDRAW_SHAPE = 0x01;
|
||||||
|
static const long DEBUGDRAW_JOINT = 0x02;
|
||||||
|
static const long DEBUGDRAW_CONTACT = 0x04;
|
||||||
|
static const long DEBUGDRAW_ALL = DEBUGDRAW_SHAPE | DEBUGDRAW_JOINT | DEBUGDRAW_CONTACT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** Adds a joint to the physics world.*/
|
/** Adds a joint to the physics world.*/
|
||||||
virtual void addJoint(PhysicsJoint* joint);
|
virtual void addJoint(PhysicsJoint* joint);
|
||||||
|
@ -92,7 +100,8 @@ public:
|
||||||
virtual void removeAllBodies();
|
virtual void removeAllBodies();
|
||||||
|
|
||||||
void rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data);
|
void rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data);
|
||||||
void rectQuery(PhysicsRectQueryCallbackFunc func, const Rect& rect, void* data);
|
void queryRect(PhysicsRectQueryCallbackFunc func, const Rect& rect, void* data);
|
||||||
|
void queryPoint(PhysicsPointQueryCallbackFunc func, const Point& point, void* data);
|
||||||
Array* getShapes(const Point& point) const;
|
Array* getShapes(const Point& point) const;
|
||||||
PhysicsShape* getShape(const Point& point) const;
|
PhysicsShape* getShape(const Point& point) const;
|
||||||
Array* getAllBodies() const;
|
Array* getAllBodies() const;
|
||||||
|
@ -109,13 +118,12 @@ public:
|
||||||
/** set the gravity value */
|
/** set the gravity value */
|
||||||
void setGravity(const Vect& gravity);
|
void setGravity(const Vect& gravity);
|
||||||
|
|
||||||
/** test the debug draw is enabled */
|
|
||||||
inline bool isDebugDraw() const { return _debugDraw; }
|
|
||||||
/** set the debug draw */
|
/** set the debug draw */
|
||||||
inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; }
|
void setDebugDrawMask(int mask);
|
||||||
|
inline int getDebugDrawMask() { return _debugDrawMask; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static PhysicsWorld* create(Scene& scene);
|
static PhysicsWorld* construct(Scene& scene);
|
||||||
bool init(Scene& scene);
|
bool init(Scene& scene);
|
||||||
|
|
||||||
virtual void addBody(PhysicsBody* body);
|
virtual void addBody(PhysicsBody* body);
|
||||||
|
@ -124,8 +132,6 @@ protected:
|
||||||
virtual void update(float delta);
|
virtual void update(float delta);
|
||||||
|
|
||||||
virtual void debugDraw();
|
virtual void debugDraw();
|
||||||
virtual void drawWithShape(DrawNode* node, PhysicsShape* shape);
|
|
||||||
virtual void drawWithJoint(DrawNode* node, PhysicsJoint* joint);
|
|
||||||
|
|
||||||
virtual int collisionBeginCallback(PhysicsContact& contact);
|
virtual int collisionBeginCallback(PhysicsContact& contact);
|
||||||
virtual int collisionPreSolveCallback(PhysicsContact& contact);
|
virtual int collisionPreSolveCallback(PhysicsContact& contact);
|
||||||
|
@ -153,8 +159,9 @@ protected:
|
||||||
Scene* _scene;
|
Scene* _scene;
|
||||||
|
|
||||||
bool _delayDirty;
|
bool _delayDirty;
|
||||||
bool _debugDraw;
|
PhysicsDebugDraw* _debugDraw;
|
||||||
DrawNode* _drawNode;
|
int _debugDrawMask;
|
||||||
|
|
||||||
|
|
||||||
Array* _delayAddBodies;
|
Array* _delayAddBodies;
|
||||||
Array* _delayRemoveBodies;
|
Array* _delayRemoveBodies;
|
||||||
|
@ -171,6 +178,28 @@ protected:
|
||||||
friend class PhysicsShape;
|
friend class PhysicsShape;
|
||||||
friend class PhysicsJoint;
|
friend class PhysicsJoint;
|
||||||
friend class PhysicsWorldCallback;
|
friend class PhysicsWorldCallback;
|
||||||
|
friend class PhysicsDebugDraw;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class PhysicsDebugDraw
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
virtual bool begin();
|
||||||
|
virtual void end();
|
||||||
|
virtual void drawShape(PhysicsShape& shape);
|
||||||
|
virtual void drawJoint(PhysicsJoint& joint);
|
||||||
|
virtual void drawContact();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
PhysicsDebugDraw(PhysicsWorld& world);
|
||||||
|
virtual ~PhysicsDebugDraw();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
DrawNode* _drawNode;
|
||||||
|
PhysicsWorld& _world;
|
||||||
|
|
||||||
|
friend class PhysicsWorld;
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -44,7 +44,7 @@ public:
|
||||||
static float cpfloat2float(cpFloat f) { return f; }
|
static float cpfloat2float(cpFloat f) { return f; }
|
||||||
static cpFloat float2cpfloat(float f) { return f; }
|
static cpFloat float2cpfloat(float f) { return f; }
|
||||||
static cpBB rect2cpbb(const Rect& rect) { return cpBBNew(rect.origin.x, rect.origin.y, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); }
|
static cpBB rect2cpbb(const Rect& rect) { return cpBBNew(rect.origin.x, rect.origin.y, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); }
|
||||||
static Rect cpbb2rect(const cpBB& bb) { return Rect(bb.l, bb.b, bb.r, bb.t); }
|
static Rect cpbb2rect(const cpBB& bb) { return Rect(bb.l, bb.b, bb.r - bb.l, bb.t - bb.b); }
|
||||||
|
|
||||||
static Point* cpvs2points(const cpVect* cpvs, Point* out, int count)
|
static Point* cpvs2points(const cpVect* cpvs, Point* out, int count)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace
|
||||||
{
|
{
|
||||||
static std::function<Layer*()> createFunctions[] = {
|
static std::function<Layer*()> createFunctions[] = {
|
||||||
CL(PhysicsDemoLogoSmash),
|
CL(PhysicsDemoLogoSmash),
|
||||||
CL(PhysicsDemoPlink),
|
CL(PhysicsDemoPyramidStack),
|
||||||
CL(PhysicsDemoClickAdd),
|
CL(PhysicsDemoClickAdd),
|
||||||
CL(PhysicsDemoRayCast),
|
CL(PhysicsDemoRayCast),
|
||||||
CL(PhysicsDemoJoints),
|
CL(PhysicsDemoJoints),
|
||||||
|
@ -62,21 +62,6 @@ namespace
|
||||||
|
|
||||||
bool PhysicsTestScene::_debugDraw = false;
|
bool PhysicsTestScene::_debugDraw = false;
|
||||||
|
|
||||||
bool PhysicsTestScene::initTest()
|
|
||||||
{
|
|
||||||
#ifdef CC_USE_PHYSICS
|
|
||||||
if(TestScene::initWithPhysics())
|
|
||||||
{
|
|
||||||
this->getPhysicsWorld()->setDebugDraw(_debugDraw);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
return TestScene::init();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhysicsTestScene::runThisTest()
|
void PhysicsTestScene::runThisTest()
|
||||||
{
|
{
|
||||||
#ifdef CC_USE_PHYSICS
|
#ifdef CC_USE_PHYSICS
|
||||||
|
@ -91,7 +76,7 @@ void PhysicsTestScene::runThisTest()
|
||||||
void PhysicsTestScene::toggleDebug()
|
void PhysicsTestScene::toggleDebug()
|
||||||
{
|
{
|
||||||
_debugDraw = !_debugDraw;
|
_debugDraw = !_debugDraw;
|
||||||
getPhysicsWorld()->setDebugDraw(_debugDraw);
|
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsDemo::PhysicsDemo()
|
PhysicsDemo::PhysicsDemo()
|
||||||
|
@ -99,7 +84,6 @@ PhysicsDemo::PhysicsDemo()
|
||||||
, _spriteTexture(nullptr)
|
, _spriteTexture(nullptr)
|
||||||
, _ball(nullptr)
|
, _ball(nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsDemo::~PhysicsDemo()
|
PhysicsDemo::~PhysicsDemo()
|
||||||
|
@ -120,7 +104,6 @@ std::string PhysicsDemo::subtitle()
|
||||||
void PhysicsDemo::restartCallback(Object* sender)
|
void PhysicsDemo::restartCallback(Object* sender)
|
||||||
{
|
{
|
||||||
auto s = new PhysicsTestScene();
|
auto s = new PhysicsTestScene();
|
||||||
s->initTest();
|
|
||||||
s->addChild( restart() );
|
s->addChild( restart() );
|
||||||
Director::getInstance()->replaceScene(s);
|
Director::getInstance()->replaceScene(s);
|
||||||
s->release();
|
s->release();
|
||||||
|
@ -129,7 +112,6 @@ void PhysicsDemo::restartCallback(Object* sender)
|
||||||
void PhysicsDemo::nextCallback(Object* sender)
|
void PhysicsDemo::nextCallback(Object* sender)
|
||||||
{
|
{
|
||||||
auto s = new PhysicsTestScene();
|
auto s = new PhysicsTestScene();
|
||||||
s->initTest();
|
|
||||||
s->addChild( next() );
|
s->addChild( next() );
|
||||||
Director::getInstance()->replaceScene(s);
|
Director::getInstance()->replaceScene(s);
|
||||||
s->release();
|
s->release();
|
||||||
|
@ -138,7 +120,6 @@ void PhysicsDemo::nextCallback(Object* sender)
|
||||||
void PhysicsDemo::backCallback(Object* sender)
|
void PhysicsDemo::backCallback(Object* sender)
|
||||||
{
|
{
|
||||||
auto s = new PhysicsTestScene();
|
auto s = new PhysicsTestScene();
|
||||||
s->initTest();
|
|
||||||
s->addChild( back() );
|
s->addChild( back() );
|
||||||
Director::getInstance()->replaceScene(s);
|
Director::getInstance()->replaceScene(s);
|
||||||
s->release();
|
s->release();
|
||||||
|
@ -403,7 +384,6 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
||||||
|
|
||||||
if (shape != nullptr)
|
if (shape != nullptr)
|
||||||
{
|
{
|
||||||
|
|
||||||
Node* mouse = Node::create();
|
Node* mouse = Node::create();
|
||||||
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
|
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
|
||||||
mouse->getPhysicsBody()->setDynamic(false);
|
mouse->getPhysicsBody()->setDynamic(false);
|
||||||
|
@ -485,39 +465,38 @@ void PhysicsDemoLogoSmash::onEnter()
|
||||||
std::string PhysicsDemoLogoSmash::title()
|
std::string PhysicsDemoLogoSmash::title()
|
||||||
{
|
{
|
||||||
return "Logo Smash";
|
return "Logo Smash";
|
||||||
}
|
}void PhysicsDemoPyramidStack::onEnter()
|
||||||
|
|
||||||
void PhysicsDemoPlink::onEnter()
|
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
|
|
||||||
auto node = DrawNode::create();
|
auto touchListener = EventListenerTouchOneByOne::create();
|
||||||
auto body = PhysicsBody::create();
|
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchBegan, this);
|
||||||
body->setDynamic(false);
|
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchMoved, this);
|
||||||
node->setPhysicsBody(body);
|
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchEnded, this);
|
||||||
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
||||||
|
|
||||||
Point tris[] = { Point(-15, -15), Point(0, 10), Point(15, -15) };
|
auto node = Node::create();
|
||||||
|
node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Point(0, 50), VisibleRect::rightBottom() + Point(0, 50)));
|
||||||
|
this->addChild(node);
|
||||||
|
|
||||||
auto rect = VisibleRect::getVisibleRect();
|
auto ball = Sprite::create("Images/ball.png");
|
||||||
for (int i = 0; i < 9; ++i)
|
ball->setScale(1);
|
||||||
|
ball->setPhysicsBody(PhysicsBody::createCircle(10));
|
||||||
|
ball->setPosition(VisibleRect::bottom() + Point(0, 60));
|
||||||
|
this->addChild(ball);
|
||||||
|
|
||||||
|
for(int i=0; i<14; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < 4; ++j)
|
for(int j=0; j<=i; j++)
|
||||||
{
|
{
|
||||||
Point offset(rect.origin.x + rect.size.width/9*i + (j%2)*40 - 20, rect.origin.y + j*70);
|
auto sp = addGrossiniAtPosition(VisibleRect::bottom() + Point((i/2 - j) * 11, (14 - i) * 23 + 100), 0.2f);
|
||||||
body->addShape(PhysicsShapePolygon::create(tris, 3, PHYSICSSHAPE_MATERIAL_DEFAULT, offset));
|
sp->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
Point drawVec[] = {tris[0] + offset, tris[1] + offset, tris[2] + offset};
|
|
||||||
node->drawPolygon(drawVec, 3, STATIC_COLOR, 1, STATIC_COLOR);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addChild(node);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
std::string PhysicsDemoPyramidStack::title()
|
||||||
std::string PhysicsDemoPlink::title()
|
|
||||||
{
|
{
|
||||||
return "Plink";
|
return "Pyramid Stack";
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsDemoRayCast::PhysicsDemoRayCast()
|
PhysicsDemoRayCast::PhysicsDemoRayCast()
|
||||||
|
@ -757,6 +736,37 @@ void PhysicsDemoJoints::onEnter()
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
|
||||||
|
auto sp1 = makeBall(offset - Point(30, 0), 10);
|
||||||
|
sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
|
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
|
||||||
|
_scene->getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
|
this->addChild(sp1);
|
||||||
|
this->addChild(sp2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
{
|
||||||
|
auto sp1 = makeBall(offset - Point(30, 0), 10);
|
||||||
|
sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
|
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
|
||||||
|
joint->setMin(30.0f);
|
||||||
|
joint->setMax(60.0f);
|
||||||
|
_scene->getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
|
this->addChild(sp1);
|
||||||
|
this->addChild(sp2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,9 @@
|
||||||
class PhysicsTestScene : public TestScene
|
class PhysicsTestScene : public TestScene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual bool initTest() override;
|
PhysicsTestScene():TestScene(false, true){}
|
||||||
|
|
||||||
|
public:
|
||||||
virtual void runThisTest();
|
virtual void runThisTest();
|
||||||
|
|
||||||
void toggleDebug();
|
void toggleDebug();
|
||||||
|
@ -69,7 +71,7 @@ public:
|
||||||
std::string title() override;
|
std::string title() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PhysicsDemoPlink : public PhysicsDemo
|
class PhysicsDemoPyramidStack : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
|
|
|
@ -158,7 +158,7 @@ void TestController::menuCallback(Object * sender)
|
||||||
// create the test scene and run it
|
// create the test scene and run it
|
||||||
auto scene = g_aTestNames[idx].callback();
|
auto scene = g_aTestNames[idx].callback();
|
||||||
|
|
||||||
if (scene && scene->initTest())
|
if (scene)
|
||||||
{
|
{
|
||||||
scene->runThisTest();
|
scene->runThisTest();
|
||||||
scene->release();
|
scene->release();
|
||||||
|
|
|
@ -3,13 +3,20 @@
|
||||||
#include "extensions/cocos-ext.h"
|
#include "extensions/cocos-ext.h"
|
||||||
#include "cocostudio/CocoStudio.h"
|
#include "cocostudio/CocoStudio.h"
|
||||||
|
|
||||||
TestScene::TestScene(bool bPortrait)
|
TestScene::TestScene(bool bPortrait, bool physics/* = false*/)
|
||||||
{
|
{
|
||||||
|
if (physics)
|
||||||
|
{
|
||||||
|
#ifdef CC_USE_PHYSICS
|
||||||
|
TestScene::initWithPhysics();
|
||||||
|
#else
|
||||||
|
Scene::init();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
else
|
||||||
bool TestScene::initTest()
|
|
||||||
{
|
{
|
||||||
return Scene::init();
|
Scene::init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestScene::onEnter()
|
void TestScene::onEnter()
|
||||||
|
|
|
@ -11,8 +11,7 @@ using namespace std;
|
||||||
class TestScene : public Scene
|
class TestScene : public Scene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TestScene(bool bPortrait = false);
|
TestScene(bool bPortrait = false, bool physics = false);
|
||||||
virtual bool initTest();
|
|
||||||
virtual void onEnter();
|
virtual void onEnter();
|
||||||
|
|
||||||
virtual void runThisTest() = 0;
|
virtual void runThisTest() = 0;
|
||||||
|
|
Loading…
Reference in New Issue