Merge branch 'develop' of https://github.com/cocos2d/cocos2d-x into developCCS

This commit is contained in:
samuele3 2013-11-18 14:13:05 +08:00
commit e6ed691e7c
20 changed files with 594 additions and 464 deletions

View File

@ -102,7 +102,7 @@ bool Scene::initWithPhysics()
Director * pDirector;
CC_BREAK_IF( ! (pDirector = Director::getInstance()) );
this->setContentSize(pDirector->getWinSize());
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::create(*this)));
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
this->scheduleUpdate();
// success

View File

@ -1793,10 +1793,6 @@ bool Image::saveToFile(const char *pszFilePath, bool bIsToRGB)
return false;
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
assert(false);
return false;
#else
bool bRet = false;
do
@ -1829,7 +1825,6 @@ bool Image::saveToFile(const char *pszFilePath, bool bIsToRGB)
} while (0);
return bRet;
#endif
}
#endif

View File

@ -1126,12 +1126,15 @@ void WidgetPropertiesReader0300::setPropsForWidgetFromJsonDictionary(UIWidget*wi
default:
break;
}
float mgl = dicHelper->getFloatValue_json(layoutParameterDic, "marginLeft");
float mgt = dicHelper->getFloatValue_json(layoutParameterDic, "marginTop");
float mgr = dicHelper->getFloatValue_json(layoutParameterDic, "marginRight");
float mgb = dicHelper->getFloatValue_json(layoutParameterDic, "marginDown");
parameter->setMargin(UIMargin(mgl, mgt, mgr, mgb));
widget->setLayoutParameter(parameter);
if (parameter)
{
float mgl = dicHelper->getFloatValue_json(layoutParameterDic, "marginLeft");
float mgt = dicHelper->getFloatValue_json(layoutParameterDic, "marginTop");
float mgr = dicHelper->getFloatValue_json(layoutParameterDic, "marginRight");
float mgb = dicHelper->getFloatValue_json(layoutParameterDic, "marginDown");
parameter->setMargin(UIMargin(mgl, mgt, mgr, mgb));
widget->setLayoutParameter(parameter);
}
}
CC_SAFE_DELETE(layoutParameterDic);
}

View File

@ -143,7 +143,7 @@ void UILayout::onSizeChanged()
if (_backGroundImage)
{
_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);
}
@ -240,7 +240,7 @@ void UILayout::setBackGroundImage(const char* fileName,TextureResType texType)
void UILayout::setBackGroundImageCapInsets(const cocos2d::Rect &capInsets)
{
_backGroundImageCapInsets = capInsets;
if (_backGroundScale9Enabled)
if (_backGroundScale9Enabled && _backGroundImage)
{
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_backGroundImage)->setCapInsets(capInsets);
}

View File

@ -360,20 +360,23 @@ float PhysicsBody::getRotation() const
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;
// add shape to body
if (_shapes->getIndexOfObject(shape) == UINT_MAX)
if (_shapes->getIndexOfObject(shape) == CC_INVALID_INDEX)
{
shape->setBody(this);
// calculate the area, mass, and desity
// area must update before mass, because the density changes depend on it.
_area += shape->getArea();
addMass(shape->getMass());
addMoment(shape->getMoment());
if (addMassAndMoment)
{
_area += shape->getArea();
addMass(shape->getMass());
addMoment(shape->getMoment());
}
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));
}
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)
{
applyImpulse(impulse, Point());
@ -604,28 +618,31 @@ PhysicsShape* PhysicsBody::getShape(int tag) const
return nullptr;
}
void PhysicsBody::removeShape(int tag)
void PhysicsBody::removeShape(int tag, bool reduceMassAndMoment/* = true*/)
{
for (auto child : *_shapes)
{
PhysicsShape* shape = dynamic_cast<PhysicsShape*>(child);
if (shape->getTag() == tag)
{
removeShape(shape);
removeShape(shape, reduceMassAndMoment);
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
// area must update before mass, because the density changes depend on it.
_area -= shape->getArea();
addMass(-shape->getMass());
addMoment(-shape->getMoment());
if (reduceMassAndMoment)
{
_area -= shape->getArea();
addMass(-shape->getMass());
addMoment(-shape->getMoment());
}
//remove
if (_world)
@ -640,7 +657,7 @@ void PhysicsBody::removeShape(PhysicsShape* shape)
}
}
void PhysicsBody::removeAllShapes()
void PhysicsBody::removeAllShapes(bool reduceMassAndMoment/* = true*/)
{
for (auto child : *_shapes)
{
@ -648,9 +665,12 @@ void PhysicsBody::removeAllShapes()
// 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());
if (reduceMassAndMoment)
{
_area -= shape->getArea();
addMass(-shape->getMass());
addMoment(-shape->getMoment());
}
if (_world)
{

View File

@ -49,20 +49,22 @@ const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT(0.1f, 0.5f, 0.5f);
/**
* A body affect by physics.
* 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:
/** create a body with defult mass and moment. */
static PhysicsBody* create();
/** create a body with mass and defult moment. */
static PhysicsBody* create(float mass);
/** create a body with mass and moment. */
static PhysicsBody* create(float mass, float moment);
/**
* @brief Create a body contains a circle shape.
*/
/** Create a body contains a circle shape. */
static PhysicsBody* createCircle(float radius, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
/**
* @brief Create a body contains a box shape.
*/
/** Create a body contains a box shape. */
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.
@ -70,132 +72,145 @@ public:
*/
static PhysicsBody* createPolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
/**
* @brief Create a body contains a EdgeSegment shape.
*/
/** 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);
/**
* @brief Create a body contains a EdgeBox shape.
*/
/** 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);
/**
* @brief Create a body contains a EdgePolygon shape.
*/
/** Create a body contains a EdgePolygon shape. */
static PhysicsBody* createEdgePolygon(const Point* points, int count, const PhysicsMaterial& material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
/**
* @brief Create a body contains a EdgeChain shape.
*/
/** Create a body contains a EdgeChain shape. */
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; }
/*
* @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;
virtual PhysicsShape* addShape(PhysicsShape* shape, bool addMassAndMoment = true);
/*
* @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(int tag);
void removeShape(PhysicsShape* shape, bool reduceMassAndMoment = true);
/*
* @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();
/*
* @brief get the world body added to.
*/
/** get the world body added to. */
inline PhysicsWorld* getWorld() const { return _world; }
/*
* @brief get all joints the body have
*/
/** get all joints the body have */
inline const std::vector<PhysicsJoint*>& getJoints() const { return _joints; }
/*
* @brief get the sprite the body set to.
*/
/** get the sprite the body set to. */
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);
/**
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
* When two bodies share the same space, each bodys category mask is tested against the other bodys 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 worlds 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);
/**
* 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 bodys collision mask is compared to the other bodys 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 bodys velocity.
* The default value is 0xFFFFFFFF (all bits set).
*/
void setCollisionBitmask(int bitmask);
/** get the category bit mask */
inline int getCategoryBitmask() const { return _categoryBitmask; }
/** get the contact test bit mask */
inline int getContactTestBitmask() const { return _contactTestBitmask; }
/** get the collision bit mask */
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);
/** get the group of body */
inline int getGroup() const { return _group; }
/*
* @brief get the body position.
*/
/** get the body position. */
Point getPosition() const;
/*
* @brief get the body rotation.
*/
/** get the body rotation. */
float getRotation() const;
/*
/**
* @brief test the body is dynamic or not.
* a dynamic body will effect with gravity.
*/
inline bool isDynamic() const { return _dynamic; }
/*
/**
* @brief set dynamic to body.
* a dynamic body will effect with gravity.
*/
void setDynamic(bool dynamic);
/*
/**
* @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.
*/
void setMass(float mass);
/*
* @brief get the body mass.
*/
/** get the body mass. */
inline float getMass() const { return _mass; }
/*
/**
* @brief add mass to body.
* if _mass(mass of the body) == PHYSICS_INFINITY, it remains.
* if mass == PHYSICS_INFINITY, _mass will be PHYSICS_INFINITY.
@ -205,16 +220,14 @@ public:
*/
void addMass(float mass);
/*
/**
* @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.
*/
void setMoment(float moment);
/*
* @brief get the body moment of inertia.
*/
/** get the body moment of inertia. */
inline float getMoment(float moment) const { return _moment; }
/*
/**
* @brief add moment of inertia to body.
* if _moment(moment of the body) == PHYSICS_INFINITY, it remains.
* if moment == PHYSICS_INFINITY, _moment will be PHYSICS_INFINITY.
@ -223,35 +236,54 @@ public:
* other wise, moment = moment + _moment;
*/
void addMoment(float moment);
/*
* @brief set angular damping.
*/
//void setAngularDamping(float angularDamping);
/*
* @brief get angular damping.
*/
/** get linear damping. */
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; }
/** get angular damping. */
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; }
//virtual Clonable* clone() const override;
/** whether the body is at rest */
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; }
/**
* set the enable value.
* if the body it isn't enabled, it will not has simulation by world
*/
void setEnable(bool enable);
/** whether the body can rotation */
inline bool isRotationEnabled() const { return _rotationEnable; }
/** set the body is allow rotation or not */
void setRotationEnable(bool enable);
/** whether this physics body is affected by the physics worlds gravitational force. */
inline bool isGravityEnabled() const { return _gravityEnable; }
/** set the body is affected by the physics world's gravitational force or not. */
void setGravityEnable(bool enable);
/** get the body's tag */
inline int getTag() const { return _tag; }
/** set the body's tag */
inline void setTag(int tag) { _tag = tag; }
/** convert the world point to local */
Point world2Local(const Point& point);
/** convert the local point to world */
Point local2World(const Point& point);
protected:

View File

@ -130,7 +130,7 @@ float PhysicsContactPreSolve::getElasticity() const
return static_cast<cpArbiter*>(_contactInfo)->e;
}
float PhysicsContactPreSolve::getFriciton() const
float PhysicsContactPreSolve::getFriction() const
{
return static_cast<cpArbiter*>(_contactInfo)->u;
}
@ -177,7 +177,7 @@ float PhysicsContactPostSolve::getElasticity() const
return static_cast<cpArbiter*>(_contactInfo)->e;
}
float PhysicsContactPostSolve::getFriciton() const
float PhysicsContactPostSolve::getFriction() const
{
return static_cast<cpArbiter*>(_contactInfo)->u;
}
@ -216,7 +216,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
bool ret = true;
if (onContactBegin != nullptr
&& test(contact.getShapeA(), contact.getShapeB()))
&& hitTest(contact.getShapeA(), contact.getShapeB()))
{
contact._begin = true;
contact.generateContactData();
@ -240,7 +240,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
bool ret = true;
if (onContactPreSolve != nullptr
&& test(contact.getShapeA(), contact.getShapeB()))
&& hitTest(contact.getShapeA(), contact.getShapeB()))
{
PhysicsContactPreSolve solve(contact._begin ? nullptr : contact._contactData, contact._contactInfo);
contact._begin = false;
@ -255,7 +255,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
case PhysicsContact::EventCode::POSTSOLVE:
{
if (onContactPostSolve != nullptr
&& test(contact.getShapeA(), contact.getShapeB()))
&& hitTest(contact.getShapeA(), contact.getShapeB()))
{
PhysicsContactPostSolve solve(contact._contactInfo);
onContactPostSolve(event, contact, solve);
@ -265,7 +265,7 @@ void EventListenerPhysicsContact::onEvent(EventCustom* event)
case PhysicsContact::EventCode::SEPERATE:
{
if (onContactSeperate != nullptr
&& test(contact.getShapeA(), contact.getShapeB()))
&& hitTest(contact.getShapeA(), contact.getShapeB()))
{
onContactSeperate(event, contact);
}
@ -295,7 +295,7 @@ EventListenerPhysicsContact* EventListenerPhysicsContact::create()
return nullptr;
}
bool EventListenerPhysicsContact::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
bool EventListenerPhysicsContact::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
CC_UNUSED_PARAM(shapeA);
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)
|| (shapeA->getBody() == _b && shapeB->getBody() == _a))
@ -416,7 +416,7 @@ EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::cr
return nullptr;
}
bool EventListenerPhysicsContactWithShapes::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
if ((shapeA == _a && shapeB == _b)
|| (shapeA == _b && shapeB == _a))
@ -469,7 +469,7 @@ EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::crea
return nullptr;
}
bool EventListenerPhysicsContactWithGroup::test(PhysicsShape* shapeA, PhysicsShape* shapeB)
bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
{
if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
{

View File

@ -69,24 +69,19 @@ public:
SEPERATE
};
/*
* @brief get contact shape A.
*/
/** get contact shape A. */
inline PhysicsShape* getShapeA() const { return _shapeA; }
/*
* @brief get contact shape B.
*/
/** get contact shape B. */
inline PhysicsShape* getShapeB() const { return _shapeB; }
/** get contact data */
inline const PhysicsContactData* getContactData() const { return _contactData; }
/*
* @brief get data.
*/
/** get 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; }
/** get the event code */
EventCode getEventCode() const { return _eventCode; };
private:
@ -132,13 +127,19 @@ private:
class PhysicsContactPreSolve
{
public:
// getter/setter
/** get elasticity between two bodies*/
float getElasticity() const;
float getFriciton() const;
/** get friction between two bodies*/
float getFriction() const;
/** get surface velocity between two bodies*/
Point getSurfaceVelocity() const;
/** set the elasticity*/
void setElasticity(float elasticity);
/** set the friction*/
void setFriction(float friction);
/** set the surface velocity*/
void setSurfaceVelocity(const Vect& velocity);
/** ignore the rest of the contact presolve and postsolve callbacks */
void ignore();
private:
@ -161,9 +162,11 @@ private:
class PhysicsContactPostSolve
{
public:
// getter
/** get elasticity between two bodies*/
float getElasticity() const;
float getFriciton() const;
/** get friction between two bodies*/
float getFriction() const;
/** get surface velocity between two bodies*/
Point getSurfaceVelocity() const;
private:
@ -176,18 +179,22 @@ private:
friend class EventListenerPhysicsContact;
};
/*
* @brief contact listener.
*/
/* contact listener. it will recive all the contact callbacks. */
class EventListenerPhysicsContact : public EventListenerCustom
{
public:
/** create the listener */
static EventListenerPhysicsContact* create();
virtual bool test(PhysicsShape* shapeA, PhysicsShape* shapeB);
virtual bool checkAvailable() 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:
/*
* @brief it will called at two shapes start to contact, and only call it once.
@ -214,14 +221,17 @@ protected:
protected:
EventListenerPhysicsContact();
virtual ~EventListenerPhysicsContact();
friend class PhysicsWorld;
};
/** this event listener only be called when bodyA and bodyB have contacts */
class EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
{
public:
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;
protected:
@ -233,12 +243,13 @@ protected:
virtual ~EventListenerPhysicsContactWithBodies();
};
/** this event listener only be called when shapeA and shapeB have contacts */
class EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
{
public:
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;
protected:
@ -250,12 +261,13 @@ protected:
virtual ~EventListenerPhysicsContactWithShapes();
};
/** this event listener only be called when shapeA or shapeB is in the group your specified */
class EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
{
public:
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;
protected:

View File

@ -130,16 +130,6 @@ PhysicsJointFixed::~PhysicsJointFixed()
}
PhysicsJointSliding::PhysicsJointSliding()
{
}
PhysicsJointSliding::~PhysicsJointSliding()
{
}
PhysicsJointLimit::PhysicsJointLimit()
{
@ -296,41 +286,6 @@ float PhysicsJointPin::getMaxForce() const
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* joint = new PhysicsJointLimit();
@ -354,7 +309,7 @@ bool PhysicsJointLimit::init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2),
0,
PhysicsHelper::float2cpfloat(anchr1.getDistance(anchr2)));
PhysicsHelper::float2cpfloat(_bodyA->local2World(anchr1).getDistance(_bodyB->local2World(anchr2))));
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(),
getBodyInfo(b)->getBody(),
PhysicsHelper::point2cpv(anchr1), PhysicsHelper::point2cpv(anchr2));
PhysicsHelper::point2cpv(anchr1),
PhysicsHelper::point2cpv(anchr2));
CC_BREAK_IF(joint == nullptr);

View File

@ -81,6 +81,7 @@ protected:
friend class PhysicsBody;
friend class PhysicsWorld;
friend class PhysicsDebugDraw;
};
/*
@ -99,45 +100,13 @@ protected:
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.
*/
class PhysicsJointLimit : public PhysicsJoint
{
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;
void setMin(float min);
@ -173,7 +142,6 @@ protected:
class PhysicsJointDistance : public PhysicsJoint
{
public:
static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);

View File

@ -793,6 +793,8 @@ void PhysicsShape::setGroup(int group)
cpShapeSetGroup(shape, (cpGroup)group);
}
}
_group = group;
}
bool PhysicsShape::containsPoint(const Point& point) const

View File

@ -144,6 +144,7 @@ protected:
friend class PhysicsWorld;
friend class PhysicsBody;
friend class PhysicsJoint;
friend class PhysicsDebugDraw;
};
/** A circle shape */

View File

@ -85,6 +85,13 @@ namespace
PhysicsRectQueryCallbackFunc func;
void* data;
}RectQueryCallbackInfo;
typedef struct PointQueryCallbackInfo
{
PhysicsWorld* world;
PhysicsPointQueryCallbackFunc func;
void* data;
}PointQueryCallbackInfo;
}
class PhysicsWorldCallback
@ -95,8 +102,9 @@ public:
static void collisionPostSolveCallbackFunc(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 rectQueryCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info);
static void nearestPointQueryFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr);
static void queryRectCallbackFunc(cpShape *shape, RectQueryCallbackInfo *info);
static void queryPointFunc(cpShape *shape, cpFloat distance, cpVect point, PointQueryCallbackInfo *info);
static void getShapesAtPointFunc(cpShape *shape, cpFloat distance, cpVect point, Array *arr);
public:
static bool continues;
@ -161,7 +169,7 @@ void PhysicsWorldCallback::rayCastCallbackFunc(cpShape *shape, cpFloat t, cpVect
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);
@ -175,7 +183,7 @@ void PhysicsWorldCallback::rectQueryCallbackFunc(cpShape *shape, RectQueryCallba
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);
@ -184,6 +192,15 @@ void PhysicsWorldCallback::nearestPointQueryFunc(cpShape *shape, cpFloat distanc
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)
{
do
@ -219,7 +236,7 @@ bool PhysicsWorld::init(Scene& scene)
void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
{
if (_delayRemoveBodies->getIndexOfObject(body) != UINT_MAX)
if (_delayRemoveBodies->getIndexOfObject(body) != CC_INVALID_INDEX)
{
_delayRemoveBodies->removeObject(body);
return;
@ -227,7 +244,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
if (_info->getSpace()->locked_private)
{
if (_delayAddBodies->getIndexOfObject(body) == UINT_MAX)
if (_delayAddBodies->getIndexOfObject(body) == CC_INVALID_INDEX)
{
_delayAddBodies->addObject(body);
_delayDirty = true;
@ -240,7 +257,7 @@ void PhysicsWorld::addBodyOrDelay(PhysicsBody* body)
void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
{
if (_delayAddBodies->getIndexOfObject(body) != UINT_MAX)
if (_delayAddBodies->getIndexOfObject(body) != CC_INVALID_INDEX)
{
_delayAddBodies->removeObject(body);
return;
@ -248,7 +265,7 @@ void PhysicsWorld::removeBodyOrDelay(PhysicsBody* body)
if (_info->getSpace()->locked_private)
{
if (_delayRemoveBodies->getIndexOfObject(body) == UINT_MAX)
if (_delayRemoveBodies->getIndexOfObject(body) == CC_INVALID_INDEX)
{
_delayRemoveBodies->addObject(body);
_delayDirty = true;
@ -601,13 +618,7 @@ void PhysicsWorld::update(float delta)
cpSpaceStep(_info->getSpace(), delta);
if (_drawNode)
{
_drawNode->removeFromParent();
_drawNode = nullptr;
}
if (_debugDraw)
if (_debugDrawMask != DEBUGDRAW_NONE)
{
debugDraw();
}
@ -615,146 +626,51 @@ void PhysicsWorld::update(float delta)
void PhysicsWorld::debugDraw()
{
if (_debugDraw == nullptr)
{
_debugDraw = new PhysicsDebugDraw(*this);
}
if (_debugDraw && _bodies != nullptr)
{
_drawNode= DrawNode::create();
for (Object* obj : *_bodies)
if (_debugDraw->begin())
{
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
for (auto shape : *body->getShapes())
if (_debugDrawMask & DEBUGDRAW_SHAPE)
{
drawWithShape(_drawNode, dynamic_cast<PhysicsShape*>(shape));
}
}
for (auto joint : _joints)
{
drawWithJoint(_drawNode, joint);
}
if (_scene)
{
_scene->addChild(_drawNode);
}
}
}
void PhysicsWorld::drawWithJoint(DrawNode* node, 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));
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)
{
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)
for (Object* obj : *_bodies)
{
float angle = (float)i * M_PI / (float)CIRCLE_SEG_NUM * 2.0f;
Point d(radius * cosf(angle), radius * sinf(angle));
seg[i] = centre + d;
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
for (auto shape : *body->getShapes())
{
_debugDraw->drawShape(*dynamic_cast<PhysicsShape*>(shape));
}
}
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:
if (_debugDrawMask & DEBUGDRAW_JOINT)
{
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;
for (auto joint : _joints)
{
_debugDraw->drawJoint(*joint);
}
}
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;
_debugDraw->end();
}
}
}
void PhysicsWorld::setDebugDrawMask(int mask)
{
if (mask == DEBUGDRAW_NONE)
{
CC_SAFE_DELETE(_debugDraw);
}
_debugDrawMask = mask;
}
int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
{
bool ret = true;
@ -883,7 +799,7 @@ void PhysicsWorld::setGravity(const Vect& gravity)
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)
{
@ -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)
{
@ -914,11 +830,30 @@ void PhysicsWorld::rectQuery(PhysicsRectQueryCallbackFunc func, const Rect& rect
PhysicsHelper::rect2cpbb(rect),
CP_ALL_LAYERS,
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);
}
}
Array* PhysicsWorld::getShapes(const Point& point) const
{
Array* arr = Array::create();
@ -927,7 +862,7 @@ Array* PhysicsWorld::getShapes(const Point& point) const
0,
CP_ALL_LAYERS,
CP_NO_GROUP,
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::nearestPointQueryFunc,
(cpSpaceNearestPointQueryFunc)PhysicsWorldCallback::getShapesAtPointFunc,
arr);
return arr;
@ -963,11 +898,128 @@ PhysicsBody* PhysicsWorld::getBody(int tag) const
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)
#endif
PhysicsWorld* PhysicsWorld::create(Scene& scene)
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
{
PhysicsWorld * world = new PhysicsWorld();
if(world && world->init(scene))
@ -986,8 +1038,8 @@ PhysicsWorld::PhysicsWorld()
, _bodies(nullptr)
, _scene(nullptr)
, _delayDirty(false)
, _debugDraw(false)
, _drawNode(nullptr)
, _debugDraw(nullptr)
, _debugDrawMask(DEBUGDRAW_NONE)
, _delayAddBodies(nullptr)
, _delayRemoveBodies(nullptr)
{
@ -1001,6 +1053,48 @@ PhysicsWorld::~PhysicsWorld()
CC_SAFE_RELEASE(_delayRemoveBodies);
CC_SAFE_RELEASE(_delayAddBodies);
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

View File

@ -46,10 +46,10 @@ class Array;
class Sprite;
class Scene;
class DrawNode;
class PhysicsDebugDraw;
class PhysicsWorld;
typedef struct PhysicsRayCastInfo
{
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&, 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.
*/
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:
/** Adds a joint to the physics world.*/
virtual void addJoint(PhysicsJoint* joint);
@ -92,7 +100,8 @@ public:
virtual void removeAllBodies();
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;
PhysicsShape* getShape(const Point& point) const;
Array* getAllBodies() const;
@ -109,13 +118,12 @@ public:
/** set the gravity value */
void setGravity(const Vect& gravity);
/** test the debug draw is enabled */
inline bool isDebugDraw() const { return _debugDraw; }
/** set the debug draw */
inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; }
void setDebugDrawMask(int mask);
inline int getDebugDrawMask() { return _debugDrawMask; }
protected:
static PhysicsWorld* create(Scene& scene);
static PhysicsWorld* construct(Scene& scene);
bool init(Scene& scene);
virtual void addBody(PhysicsBody* body);
@ -124,8 +132,6 @@ protected:
virtual void update(float delta);
virtual void debugDraw();
virtual void drawWithShape(DrawNode* node, PhysicsShape* shape);
virtual void drawWithJoint(DrawNode* node, PhysicsJoint* joint);
virtual int collisionBeginCallback(PhysicsContact& contact);
virtual int collisionPreSolveCallback(PhysicsContact& contact);
@ -153,8 +159,9 @@ protected:
Scene* _scene;
bool _delayDirty;
bool _debugDraw;
DrawNode* _drawNode;
PhysicsDebugDraw* _debugDraw;
int _debugDrawMask;
Array* _delayAddBodies;
Array* _delayRemoveBodies;
@ -171,6 +178,28 @@ protected:
friend class PhysicsShape;
friend class PhysicsJoint;
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

View File

@ -44,7 +44,7 @@ public:
static float cpfloat2float(cpFloat 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 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)
{

View File

@ -7,7 +7,7 @@ namespace
{
static std::function<Layer*()> createFunctions[] = {
CL(PhysicsDemoLogoSmash),
CL(PhysicsDemoPlink),
CL(PhysicsDemoPyramidStack),
CL(PhysicsDemoClickAdd),
CL(PhysicsDemoRayCast),
CL(PhysicsDemoJoints),
@ -62,21 +62,6 @@ namespace
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()
{
#ifdef CC_USE_PHYSICS
@ -91,7 +76,7 @@ void PhysicsTestScene::runThisTest()
void PhysicsTestScene::toggleDebug()
{
_debugDraw = !_debugDraw;
getPhysicsWorld()->setDebugDraw(_debugDraw);
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
}
PhysicsDemo::PhysicsDemo()
@ -99,7 +84,6 @@ PhysicsDemo::PhysicsDemo()
, _spriteTexture(nullptr)
, _ball(nullptr)
{
}
PhysicsDemo::~PhysicsDemo()
@ -120,7 +104,6 @@ std::string PhysicsDemo::subtitle()
void PhysicsDemo::restartCallback(Object* sender)
{
auto s = new PhysicsTestScene();
s->initTest();
s->addChild( restart() );
Director::getInstance()->replaceScene(s);
s->release();
@ -129,7 +112,6 @@ void PhysicsDemo::restartCallback(Object* sender)
void PhysicsDemo::nextCallback(Object* sender)
{
auto s = new PhysicsTestScene();
s->initTest();
s->addChild( next() );
Director::getInstance()->replaceScene(s);
s->release();
@ -138,7 +120,6 @@ void PhysicsDemo::nextCallback(Object* sender)
void PhysicsDemo::backCallback(Object* sender)
{
auto s = new PhysicsTestScene();
s->initTest();
s->addChild( back() );
Director::getInstance()->replaceScene(s);
s->release();
@ -403,7 +384,6 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
if (shape != nullptr)
{
Node* mouse = Node::create();
mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY));
mouse->getPhysicsBody()->setDynamic(false);
@ -461,7 +441,7 @@ void PhysicsDemoLogoSmash::onEnter()
float y_jitter = 0.05*frand();
Node* ball = makeBall(Point(2*(x - logo_width/2 + x_jitter) + VisibleRect::getVisibleRect().size.width/2,
2*(logo_height-y + y_jitter) + VisibleRect::getVisibleRect().size.height/2 - logo_height/2),
2*(logo_height-y + y_jitter) + VisibleRect::getVisibleRect().size.height/2 - logo_height/2),
0.95f, PhysicsMaterial(0.01f, 0.0f, 0.0f));
ball->getPhysicsBody()->setMass(1.0);
@ -485,39 +465,38 @@ void PhysicsDemoLogoSmash::onEnter()
std::string PhysicsDemoLogoSmash::title()
{
return "Logo Smash";
}
void PhysicsDemoPlink::onEnter()
}void PhysicsDemoPyramidStack::onEnter()
{
PhysicsDemo::onEnter();
auto node = DrawNode::create();
auto body = PhysicsBody::create();
body->setDynamic(false);
node->setPhysicsBody(body);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchMoved, this);
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();
for (int i = 0; i < 9; ++i)
auto ball = Sprite::create("Images/ball.png");
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);
body->addShape(PhysicsShapePolygon::create(tris, 3, PHYSICSSHAPE_MATERIAL_DEFAULT, offset));
Point drawVec[] = {tris[0] + offset, tris[1] + offset, tris[2] + offset};
node->drawPolygon(drawVec, 3, STATIC_COLOR, 1, STATIC_COLOR);
auto sp = addGrossiniAtPosition(VisibleRect::bottom() + Point((i/2 - j) * 11, (14 - i) * 23 + 100), 0.2f);
sp->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
}
}
addChild(node);
}
std::string PhysicsDemoPlink::title()
std::string PhysicsDemoPyramidStack::title()
{
return "Plink";
return "Pyramid Stack";
}
PhysicsDemoRayCast::PhysicsDemoRayCast()
@ -757,6 +736,37 @@ void PhysicsDemoJoints::onEnter()
this->addChild(sp2);
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:
break;
}
@ -1111,4 +1121,4 @@ std::string PhysicsDemoSlice::title()
std::string PhysicsDemoSlice::subtitle()
{
return "click and drag to slice up the block, open debug to see it";
}
}

View File

@ -11,7 +11,9 @@
class PhysicsTestScene : public TestScene
{
public:
virtual bool initTest() override;
PhysicsTestScene():TestScene(false, true){}
public:
virtual void runThisTest();
void toggleDebug();
@ -69,7 +71,7 @@ public:
std::string title() override;
};
class PhysicsDemoPlink : public PhysicsDemo
class PhysicsDemoPyramidStack : public PhysicsDemo
{
public:
void onEnter() override;

View File

@ -158,7 +158,7 @@ void TestController::menuCallback(Object * sender)
// create the test scene and run it
auto scene = g_aTestNames[idx].callback();
if (scene && scene->initTest())
if (scene)
{
scene->runThisTest();
scene->release();

View File

@ -3,13 +3,20 @@
#include "extensions/cocos-ext.h"
#include "cocostudio/CocoStudio.h"
TestScene::TestScene(bool bPortrait)
TestScene::TestScene(bool bPortrait, bool physics/* = false*/)
{
}
bool TestScene::initTest()
{
return Scene::init();
if (physics)
{
#ifdef CC_USE_PHYSICS
TestScene::initWithPhysics();
#else
Scene::init();
#endif
}
else
{
Scene::init();
}
}
void TestScene::onEnter()

View File

@ -11,8 +11,7 @@ using namespace std;
class TestScene : public Scene
{
public:
TestScene(bool bPortrait = false);
virtual bool initTest();
TestScene(bool bPortrait = false, bool physics = false);
virtual void onEnter();
virtual void runThisTest() = 0;