Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/physics/CCPhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ bool PhysicsBody::isResting() const
void PhysicsBody::update(float delta)
{
// damping compute
if (_dynamic)
if (_dynamic && !isResting())
{
_info->getBody()->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
_info->getBody()->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f);
Expand Down
14 changes: 14 additions & 0 deletions cocos/physics/CCPhysicsJoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ class PhysicsJoint
inline int getTag() const { return _tag; }
inline void setTag(int tag) { _tag = tag; }
inline bool isEnabled() const { return _enable; }
/** Enable/Disable the joint */
void setEnable(bool enable);
inline bool isCollisionEnabled() const { return _collisionEnable; }
/** Enable/disable the collision between two bodies */
void setCollisionEnable(bool enable);
/** Remove the joint from the world */
void removeFormWorld();
/** Distory the joint*/
static void destroy(PhysicsJoint* joint);

/** Set the max force between two bodies */
void setMaxForce(float force);
/** Get the max force setting */
float getMaxForce() const;

protected:
Expand Down Expand Up @@ -145,6 +151,7 @@ class PhysicsJointPin : public PhysicsJoint
virtual ~PhysicsJointPin() {}
};

/** Set the fixed distance with two bodies */
class PhysicsJointDistance : public PhysicsJoint
{
public:
Expand All @@ -161,6 +168,7 @@ class PhysicsJointDistance : public PhysicsJoint
virtual ~PhysicsJointDistance() {}
};

/** Connecting two physics bodies together with a spring. */
class PhysicsJointSpring : public PhysicsJoint
{
public:
Expand All @@ -184,6 +192,7 @@ class PhysicsJointSpring : public PhysicsJoint
virtual ~PhysicsJointSpring() {}
};

/** Attach body a to a line, and attach body b to a dot */
class PhysicsJointGroove : public PhysicsJoint
{
public:
Expand All @@ -204,6 +213,7 @@ class PhysicsJointGroove : public PhysicsJoint
virtual ~PhysicsJointGroove() {}
};

/** Likes a spring joint, but works with rotary */
class PhysicsJointRotarySpring : public PhysicsJoint
{
public:
Expand All @@ -224,6 +234,7 @@ class PhysicsJointRotarySpring : public PhysicsJoint
virtual ~PhysicsJointRotarySpring() {}
};

/** Likes a limit joint, but works with rotary */
class PhysicsJointRotaryLimit : public PhysicsJoint
{
public:
Expand All @@ -243,6 +254,7 @@ class PhysicsJointRotaryLimit : public PhysicsJoint
virtual ~PhysicsJointRotaryLimit() {}
};

/** Works like a socket wrench. */
class PhysicsJointRatchet : public PhysicsJoint
{
public:
Expand All @@ -263,6 +275,7 @@ class PhysicsJointRatchet : public PhysicsJoint
virtual ~PhysicsJointRatchet() {}
};

/** Keeps the angular velocity ratio of a pair of bodies constant. */
class PhysicsJointGear : public PhysicsJoint
{
public:
Expand All @@ -281,6 +294,7 @@ class PhysicsJointGear : public PhysicsJoint
virtual ~PhysicsJointGear() {}
};

/** Keeps the relative angular velocity of a pair of bodies constant */
class PhysicsJointMotor : public PhysicsJoint
{
public:
Expand Down
12 changes: 6 additions & 6 deletions cocos/physics/CCPhysicsShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ bool PhysicsShapeCircle::init(float radius, const PhysicsMaterial& material/* =

_info->add(shape);

_area = calculateDefaultArea();
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_moment = calculateDefaultMoment();

Expand All @@ -331,7 +331,7 @@ float PhysicsShapeCircle::calculateMoment(float mass, float radius, const Point&
PhysicsHelper::point2cpv(offset)));
}

float PhysicsShapeCircle::calculateDefaultArea()
float PhysicsShapeCircle::calculateArea()
{
return PhysicsHelper::cpfloat2float(cpAreaForCircle(0, cpCircleShapeGetRadius(_info->getShapes().front())));
}
Expand Down Expand Up @@ -447,7 +447,7 @@ bool PhysicsShapeBox::init(const Size& size, const PhysicsMaterial& material/* =
_info->add(shape);

_offset = offset;
_area = calculateDefaultArea();
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_moment = calculateDefaultMoment();

Expand Down Expand Up @@ -484,7 +484,7 @@ float PhysicsShapeBox::calculateMoment(float mass, const Size& size, const Point
PhysicsHelper::point2cpv(offset)));
}

float PhysicsShapeBox::calculateDefaultArea()
float PhysicsShapeBox::calculateArea()
{
cpShape* shape = _info->getShapes().front();
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
Expand Down Expand Up @@ -539,7 +539,7 @@ bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMate

_info->add(shape);

_area = calculateDefaultArea();
_area = calculateArea();
_mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area;
_moment = calculateDefaultMoment();
_center = PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
Expand Down Expand Up @@ -573,7 +573,7 @@ float PhysicsShapePolygon::calculateMoment(float mass, const Point* points, int
return moment;
}

float PhysicsShapePolygon::calculateDefaultArea()
float PhysicsShapePolygon::calculateArea()
{
cpShape* shape = _info->getShapes().front();
return PhysicsHelper::cpfloat2float(cpAreaForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts));
Expand Down
49 changes: 41 additions & 8 deletions cocos/physics/CCPhysicsShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class PhysicsBodyInfo;

typedef struct PhysicsMaterial
{
float density;
float restitution;
float friction;
float density; ///< The density of the object.
float restitution; ///< The bounciness of the physics body.
float friction; ///< The roughness of the surface of a shape.

PhysicsMaterial()
: density(0.0f)
Expand Down Expand Up @@ -78,35 +78,65 @@ class PhysicsShape : public Object
};

public:
/** Get the body that this shape attaches */
inline PhysicsBody* getBody() const { return _body; }
/** Return the type of this shape */
inline Type getType() const { return _type; }
/** return the area of this shape */
inline float getArea() const { return _area; }
/** get moment */
inline float getMoment() const { return _moment; }
/** Set moment, it will change the body's moment this shape attaches */
void setMoment(float moment);
inline void setTag(int tag) { _tag = tag; }
inline int getTag() const { return _tag; }

/** get mass */
inline float getMass() const { return _mass; }
/** Set mass, it will change the body's mass this shape attaches */
void setMass(float mass);
inline float getDensity() const { return _material.density; }
void setDensity(float density);
inline float getRestitution() const { return _material.restitution; }
void setRestitution(float restitution);
inline float getFriction() const { return _material.friction; }
void setFriction(float friction);
const PhysicsMaterial& getMaterial() const { return _material; }
void setMaterial(const PhysicsMaterial& material);

virtual float calculateDefaultMoment() { return 0; }
virtual float calculateDefaultArea() { return 0; }
/** Calculate the default moment value */
virtual float calculateDefaultMoment() { return 0.0f; }
/** Get offset */
virtual Point getOffset() { return Point::ZERO; }
/** Get center of this shape */
virtual Point getCenter() { return getOffset(); }
/** Test point is in shape or not */
bool containsPoint(const Point& point) const;

/** move the points to the center */
static Point* recenterPoints(Point* points, int count, const Point& center = Point::ZERO);
/** get center of the polyon points */
static Point getPolyonCenter(const Point* points, int count);

/**
* 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).
*/
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
inline int getCategoryBitmask() const { return _categoryBitmask; }
/**
* 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).
*/
inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
inline int getContactTestBitmask() const { return _contactTestBitmask; }
/**
* 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).
*/
inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
inline int getCollisionBitmask() const { return _collisionBitmask; }

Expand All @@ -123,6 +153,9 @@ class PhysicsShape : public Object

void setBody(PhysicsBody* body);

/** calculate the area of this shape */
virtual float calculateArea() { return 0.0f; }

protected:
PhysicsShape();
virtual ~PhysicsShape() = 0;
Expand Down Expand Up @@ -155,13 +188,13 @@ class PhysicsShapeCircle : public PhysicsShape
static float calculateArea(float radius);
static float calculateMoment(float mass, float radius, const Point& offset = Point::ZERO);

virtual float calculateDefaultArea() override;
virtual float calculateDefaultMoment() override;

float getRadius() const;
virtual Point getOffset() override;
protected:
bool init(float radius, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
virtual float calculateArea() override;

protected:
PhysicsShapeCircle();
Expand All @@ -176,7 +209,6 @@ class PhysicsShapeBox : public PhysicsShape
static float calculateArea(const Size& size);
static float calculateMoment(float mass, const Size& size, const Point& offset = Point::ZERO);

virtual float calculateDefaultArea() override;
virtual float calculateDefaultMoment() override;

void getPoints(Point* outPoints) const;
Expand All @@ -185,6 +217,7 @@ class PhysicsShapeBox : public PhysicsShape

protected:
bool init(const Size& size, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
virtual float calculateArea() override;

protected:
PhysicsShapeBox();
Expand All @@ -202,7 +235,6 @@ class PhysicsShapePolygon : public PhysicsShape
static float calculateArea(const Point* points, int count);
static float calculateMoment(float mass, const Point* points, int count, const Point& offset = Point::ZERO);

float calculateDefaultArea() override;
float calculateDefaultMoment() override;

Point getPoint(int i) const;
Expand All @@ -211,6 +243,7 @@ class PhysicsShapePolygon : public PhysicsShape
virtual Point getCenter() override;
protected:
bool init(const Point* points, int count, const PhysicsMaterial& material = PHYSICSSHAPE_MATERIAL_DEFAULT, const Point& offset = Point::ZERO);
float calculateArea() override;

protected:
PhysicsShapePolygon();
Expand Down
19 changes: 14 additions & 5 deletions cocos/physics/CCPhysicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ namespace
typedef struct RectQueryCallbackInfo
{
PhysicsWorld* world;
PhysicsRectQueryCallbackFunc func;
PhysicsQueryRectCallbackFunc func;
void* data;
}RectQueryCallbackInfo;

typedef struct PointQueryCallbackInfo
{
PhysicsWorld* world;
PhysicsPointQueryCallbackFunc func;
PhysicsQueryPointCallbackFunc func;
void* data;
}PointQueryCallbackInfo;
}
Expand Down Expand Up @@ -355,7 +355,7 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1,
}


void PhysicsWorld::queryRect(PhysicsRectQueryCallbackFunc func, const Rect& rect, void* data)
void PhysicsWorld::queryRect(PhysicsQueryRectCallbackFunc func, const Rect& rect, void* data)
{
CCASSERT(func != nullptr, "func shouldn't be nullptr");

Expand All @@ -373,7 +373,7 @@ void PhysicsWorld::queryRect(PhysicsRectQueryCallbackFunc func, const Rect& rect
}
}

void PhysicsWorld::queryPoint(PhysicsPointQueryCallbackFunc func, const Point& point, void* data)
void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Point& point, void* data)
{
CCASSERT(func != nullptr, "func shouldn't be nullptr");

Expand Down Expand Up @@ -1022,7 +1022,13 @@ void PhysicsWorld::update(float delta)
body->update(delta);
}

_info->step(delta);
_updateTime += delta;
if (++_updateRateCount >= _updateRate)
{
_info->step(_updateTime * _speed);
_updateRateCount = 0;
_updateTime = 0.0f;
}

if (_debugDrawMask != DEBUGDRAW_NONE)
{
Expand All @@ -1033,6 +1039,9 @@ void PhysicsWorld::update(float delta)
PhysicsWorld::PhysicsWorld()
: _gravity(Point(0.0f, -98.0f))
, _speed(1.0f)
, _updateRate(1)
, _updateRateCount(0)
, _updateTime(0.0f)
, _info(nullptr)
, _scene(nullptr)
, _delayDirty(false)
Expand Down
Loading