issue #2771: delete some files added by mistake.

This commit is contained in:
boyu0 2013-10-18 17:56:22 +08:00
parent 5741a16620
commit 8fbfff97d9
15 changed files with 1 additions and 6636 deletions

2
.gitmodules vendored
View File

@ -3,7 +3,7 @@
url = git://github.com/cocos2d/bindings-generator.git
[submodule "cocos/scripting/auto-generated"]
path = cocos/scripting/auto-generated
url=git://github.com/cocos2d-x/bindings-auto-generated.git
url = git://github.com/cocos2d-x/bindings-auto-generated.git
[submodule "samples/Javascript/Shared"]
path = samples/Javascript/Shared
url = git://github.com/cocos2d/cocos2d-js-tests.git

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,584 +0,0 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCGEMETRY_H__
#define __CCGEMETRY_H__
#include <math.h>
#include <functional>
#include "platform/CCPlatformMacros.h"
#include "CCObject.h"
#include "ccMacros.h"
NS_CC_BEGIN
/** Clamp a value between from and to.
@since v0.99.1
*/
inline float clampf(float value, float min_inclusive, float max_inclusive)
{
if (min_inclusive > max_inclusive) {
CC_SWAP(min_inclusive, max_inclusive, float);
}
return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}
/**
* @addtogroup data_structures
* @{
*/
// for Point assignement operator and copy constructor
class CC_DLL Size;
class CC_DLL Point
{
public:
float x;
float y;
public:
/**
* @js NA
*/
Point();
/**
* @js NA
*/
Point(float x, float y);
/**
* @js NA
* @lua NA
*/
Point(const Point& other);
/**
* @js NA
* @lua NA
*/
explicit Point(const Size& size);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Point& other);
/**
* @js NA
* @lua NA
*/
Point& operator= (const Size& size);
/**
* @js NA
* @lua NA
*/
Point operator+(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point& operator+=(const Point& right);
/**
* @js NA
* @lua NA
*/
Point operator-(const Point& right) const;
/**
* @js NA
* @lua NA
*/
Point& operator-=(const Point& right);
/**
* @js NA
* @lua NA
*/
Point operator-() const;
/**
* @js NA
* @lua NA
*/
bool operator==(const Point& right);
/**
* @js NA
* @lua NA
*/
bool operator!=(const Point& right);
/**
* @js NA
* @lua NA
*/
Point operator*(float a) const;
/**
* @js NA
* @lua NA
*/
Point operator/(float a) const;
/**
* @js NA
* @lua NA
*/
void setPoint(float x, float y);
/**
* @js NA
*/
bool equals(const Point& target) const;
/** @returns if points have fuzzy equality which means equal with some degree of variance.
@since v2.1.4
* @js NA
* @lua NA
*/
bool fuzzyEquals(const Point& target, float variance) const;
/** Calculates distance between point an origin
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLength() const {
return sqrtf(x*x + y*y);
};
/** Calculates the square length of a Point (not calling sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getLengthSq() const {
return dot(*this); //x*x + y*y;
};
/** Calculates the square distance between two points (not calling sqrt() )
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistanceSq(const Point& other) const {
return (*this - other).getLengthSq();
};
/** Calculates the distance between two points
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getDistance(const Point& other) const {
return (*this - other).getLength();
};
/** @returns the angle in radians between this vector and the x axis
@since v2.1.4
* @js NA
* @lua NA
*/
inline float getAngle() const {
return atan2f(y, x);
};
/** @returns the angle in radians between two vector directions
@since v2.1.4
* @js NA
* @lua NA
*/
float getAngle(const Point& other) const;
/** Calculates dot product of two points.
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float dot(const Point& other) const {
return x*other.x + y*other.y;
};
/** Calculates cross product of two points.
@return float
@since v2.1.4
* @js NA
* @lua NA
*/
inline float cross(const Point& other) const {
return x*other.y - y*other.x;
};
/** Calculates perpendicular of v, rotated 90 degrees counter-clockwise -- cross(v, perp(v)) >= 0
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getPerp() const {
return Point(-y, x);
};
/** Calculates midpoint between two points.
@return Point
@since v3.0
* @js NA
* @lua NA
*/
inline Point getMidpoint(const Point& other) const
{
return Point((x + other.x) / 2.0f, (y + other.y) / 2.0f);
}
/** Clamp a point between from and to.
@since v3.0
* @js NA
* @lua NA
*/
inline Point getClampPoint(const Point& min_inclusive, const Point& max_inclusive) const
{
return Point(clampf(x,min_inclusive.x,max_inclusive.x), clampf(y, min_inclusive.y, max_inclusive.y));
}
/** Run a math operation function on each point component
* absf, fllorf, ceilf, roundf
* any function that has the signature: float func(float);
* For example: let's try to take the floor of x,y
* p.compOp(floorf);
@since v3.0
* @js NA
* @lua NA
*/
inline Point compOp(std::function<float(float)> function) const
{
return Point(function(x), function(y));
}
/** Calculates perpendicular of v, rotated 90 degrees clockwise -- cross(v, rperp(v)) <= 0
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point getRPerp() const {
return Point(y, -x);
};
/** Calculates the projection of this over other.
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point project(const Point& other) const {
return other * (dot(other)/other.dot(other));
};
/** Complex multiplication of two points ("rotates" two points).
@return Point vector with an angle of this.getAngle() + other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point rotate(const Point& other) const {
return Point(x*other.x - y*other.y, x*other.y + y*other.x);
};
/** Unrotates two points.
@return Point vector with an angle of this.getAngle() - other.getAngle(),
and a length of this.getLength() * other.getLength().
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point unrotate(const Point& other) const {
return Point(x*other.x + y*other.y, y*other.x - x*other.y);
};
/** Returns point multiplied to a length of 1.
* If the point is 0, it returns (1, 0)
@return Point
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point normalize() const {
float length = getLength();
if(length == 0.) return Point(1.f, 0);
return *this / getLength();
};
/** Linear Interpolation between two points a and b
@returns
alpha == 0 ? a
alpha == 1 ? b
otherwise a value between a..b
@since v2.1.4
* @js NA
* @lua NA
*/
inline Point lerp(const Point& other, float alpha) const {
return *this * (1.f - alpha) + other * alpha;
};
/** Rotates a point counter clockwise by the angle around a pivot
@param pivot is the pivot, naturally
@param angle is the angle of rotation ccw in radians
@returns the rotated point
@since v2.1.4
* @js NA
* @lua NA
*/
Point rotateByAngle(const Point& pivot, float angle) const;
/**
* @js NA
* @lua NA
*/
static inline Point forAngle(const float a)
{
return Point(cosf(a), sinf(a));
}
/** A general line-line intersection test
@param A the startpoint for the first line L1 = (A - B)
@param B the endpoint for the first line L1 = (A - B)
@param C the startpoint for the second line L2 = (C - D)
@param D the endpoint for the second line L2 = (C - D)
@param S the range for a hitpoint in L1 (p = A + S*(B - A))
@param T the range for a hitpoint in L2 (p = C + T*(D - C))
@returns whether these two lines interects.
Note that to truly test intersection for segments we have to make
sure that S & T lie within [0..1] and for rays, make sure S & T > 0
the hit point is C + T * (D - C);
the hit point also is A + S * (B - A);
@since 3.0
* @js NA
* @lua NA
*/
static bool isLineIntersect(const Point& A, const Point& B,
const Point& C, const Point& D,
float *S = nullptr, float *T = nullptr);
/**
returns true if Line A-B overlap with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineOverlap(const Point& A, const Point& B,
const Point& C, const Point& D);
/**
returns true if Line A-B parallel with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isLineParallel(const Point& A, const Point& B,
const Point& C, const Point& D);
/**
returns true if Segment A-B overlap with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentOverlap(const Point& A, const Point& B,
const Point& C, const Point& D,
Point* S = nullptr, Point* E = nullptr);
/**
returns true if Segment A-B intersects with segment C-D
@since v3.0
* @js NA
* @lua NA
*/
static bool isSegmentIntersect(const Point& A, const Point& B, const Point& C, const Point& D);
/**
returns the intersection point of line A-B, C-D
@since v3.0
* @js NA
* @lua NA
*/
static Point getIntersectPoint(const Point& A, const Point& B, const Point& C, const Point& D);
static const Point ZERO;
private:
// returns true if segment A-B intersects with segment C-D. S->E is the ovderlap part
static bool isOneDemensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E);
// cross procuct of 2 vector. A->B X C->D
static float crossProduct2Vector(const Point& A, const Point& B, const Point& C, const Point& D) { return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y); }
};
class CC_DLL Size
{
public:
float width;
float height;
public:
/**
* @js NA
*/
Size();
/**
* @js NA
*/
Size(float width, float height);
/**
* @js NA
* @lua NA
*/
Size(const Size& other);
/**
* @js NA
* @lua NA
*/
explicit Size(const Point& point);
/**
* @js NA
* @lua NA
*/
Size& operator= (const Size& other);
/**
* @js NA
* @lua NA
*/
Size& operator= (const Point& point);
/**
* @js NA
* @lua NA
*/
Size operator+(const Size& right) const;
/**
* @js NA
* @lua NA
*/
Size operator-(const Size& right) const;
/**
* @js NA
* @lua NA
*/
Size operator*(float a) const;
/**
* @js NA
* @lua NA
*/
Size operator/(float a) const;
/**
* @js NA
* @lua NA
*/
void setSize(float width, float height);
/**
* @js NA
*/
bool equals(const Size& target) const;
static const Size ZERO;
};
class CC_DLL Rect
{
public:
Point origin;
Size size;
public:
/**
* @js NA
*/
Rect();
/**
* @js NA
*/
Rect(float x, float y, float width, float height);
/**
* @js NA
* @lua NA
*/
Rect(const Rect& other);
/**
* @js NA
* @lua NA
*/
Rect& operator= (const Rect& other);
/**
* @js NA
* @lua NA
*/
void setRect(float x, float y, float width, float height);
/**
* @js NA
*/
float getMinX() const; /// return the leftmost x-value of current rect
/**
* @js NA
*/
float getMidX() const; /// return the midpoint x-value of current rect
/**
* @js NA
*/
float getMaxX() const; /// return the rightmost x-value of current rect
/**
* @js NA
*/
float getMinY() const; /// return the bottommost y-value of current rect
/**
* @js NA
*/
float getMidY() const; /// return the midpoint y-value of current rect
/**
* @js NA
*/
float getMaxY() const; /// return the topmost y-value of current rect
/**
* @js NA
*/
bool equals(const Rect& rect) const;
/**
* @js NA
*/
bool containsPoint(const Point& point) const;
/**
* @js NA
*/
bool intersectsRect(const Rect& rect) const;
/**
* @js NA
* @lua NA
*/
Rect unionWithRect(const Rect & rect) const;
static const Rect ZERO;
};
// end of data_structure group
/// @}
NS_CC_END
#endif // __CCGEMETRY_H__

View File

@ -1,292 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_BODY_H__
#define __CCPHYSICS_BODY_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCGeometry.h"
#include "CCPhysicsShape.h"
#include <vector>
NS_CC_BEGIN
class Sprite;
class PhysicsWorld;
class PhysicsJoint;
class PhysicsBodyInfo;
const PhysicsMaterial PHYSICSBODY_MATERIAL_DEFAULT = {0.0f, 1.0f, 1.0f};
/**
* A body affect by physics.
* it can attach one or more shapes.
*/
class PhysicsBody : public Object//, public Clonable
{
public:
static PhysicsBody* create();
/**
* @brief Create a body contains a circle shape.
*/
static PhysicsBody* createCircle(float radius, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
/**
* @brief Create a body contains a box shape.
*/
static PhysicsBody* createBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
/**
* @brief Create a body contains a polygon shape.
* points is an array of Point structs defining a convex hull with a clockwise winding.
*/
static PhysicsBody* createPolygon(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT);
/**
* @brief Create a body contains a EdgeSegment shape.
*/
static PhysicsBody* createEdgeSegment(Point a, Point b, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
/**
* @brief Create a body contains a EdgeBox shape.
*/
static PhysicsBody* createEdgeBox(Size size, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
/**
* @brief Create a body contains a EdgePolygon shape.
*/
static PhysicsBody* createEdgePolygon(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
/**
* @brief Create a body contains a EdgeChain shape.
*/
static PhysicsBody* createEdgeChain(Point* points, int count, PhysicsMaterial material = PHYSICSBODY_MATERIAL_DEFAULT, float border = 1);
virtual void addShape(PhysicsShape* shape);
/**
* @brief Applies a immediate force to body.
*/
virtual void applyForce(Point force);
/**
* @brief Applies a immediate force to body.
*/
virtual void applyForce(Point force, Point offset);
/**
* @brief Applies a continuous force to body.
*/
virtual void applyImpulse(Point impulse);
/**
* @brief Applies a continuous force to body.
*/
virtual void applyImpulse(Point impulse, Point offset);
/**
* @brief Applies a torque force to body.
*/
virtual void applyTorque(float torque);
virtual void setVelocity(Point velocity);
virtual Point getVelocity();
virtual void setAngularVelocity(float velocity);
virtual float getAngularVelocity();
virtual void setVelocityLimit(float limit);
virtual float getVelocityLimit();
virtual void setAngularVelocityLimit(float limit);
virtual float getAngularVelocityLimit();
/*
* @brief get the body shapes.
*/
inline std::vector<PhysicsShape*>& getShapes() { return _shapes; }
/*
* @brief get the first body shapes.
*/
inline PhysicsShape* getShape() { return _shapes.size() >= 1 ? _shapes.front() : nullptr; }
PhysicsShape* getShapeByTag(int tag);
/*
* @brief remove a shape from body
*/
void removeShape(PhysicsShape* shape);
void removeShapeByTag(int tag);
/*
* @brief remove all shapes
*/
void removeAllShapes();
/*
* @brief get the world body added to.
*/
inline PhysicsWorld* getWorld() const { return _world; }
/*
* @brief get all joints the body have
*/
inline const std::vector<PhysicsJoint*>* getJoints() const { return &_joints; }
/*
* @brief get the sprite the body set to.
*/
inline Sprite* getOwner() const { return _owner; }
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; }
inline int getCategoryBitmask() const { return _categoryBitmask; }
inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; }
inline int getContactTestBitmask() const { return _contactTestBitmask; }
inline void setCollisionBitmask(int bitmask) { _collisionBitmask = bitmask; }
inline int getCollisionBitmask() const { return _collisionBitmask; }
/*
* @brief get the body position.
*/
Point getPosition() const;
/*
* @brief 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() { 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.
*/
inline float getMass() { 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.
* if mass == -PHYSICS_INFINITY, _mass will not change.
* if mass + _mass <= 0, _mass will equal to MASS_DEFAULT(1.0)
* other wise, mass = mass + _mass;
*/
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.
*/
inline float getMoment(float moment) { 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.
* if moment == -PHYSICS_INFINITY, _moment will not change.
* if moment + _moment <= 0, _moment will equal to MASS_DEFAULT(1.0)
* other wise, moment = moment + _moment;
*/
void addMoment(float moment);
/*
* @brief set angular damping.
*/
//void setAngularDamping(float angularDamping);
/*
* @brief get angular damping.
*/
inline float getLinearDamping() { return _linearDamping; }
inline void setLinearDamping(float damping) { _linearDamping = damping; }
inline float getAngularDamping() { return _angularDamping; }
inline void setAngularDamping(float damping) { _angularDamping = damping; }
//virtual Clonable* clone() const override;
bool isResting();
inline bool isEnable() { return _enable; }
void setEnable(bool enable);
inline bool isRotationEnable() { return _rotationEnable; }
void setRotationEnable(bool enable);
inline bool isGravityEnable() { return _gravityEnable; }
void setGravityEnable(bool enable);
inline int getTag() { return _tag; }
inline void setTag(int tag) { _tag = tag; }
protected:
bool init();
virtual void setPosition(Point position);
virtual void setRotation(float rotation);
virtual void update(float delta) override;
protected:
PhysicsBody();
virtual ~PhysicsBody();
protected:
Sprite* _owner;
std::vector<PhysicsJoint*> _joints;
std::vector<PhysicsShape*> _shapes;
PhysicsWorld* _world;
PhysicsBodyInfo* _info;
bool _dynamic;
bool _enable;
bool _rotationEnable;
bool _gravityEnable;
bool _massDefault;
bool _momentDefault;
float _mass;
float _area;
float _density;
float _moment;
float _linearDamping;
float _angularDamping;
int _tag;
int _categoryBitmask;
int _contactTestBitmask;
int _collisionBitmask;
friend class PhysicsWorld;
friend class PhysicsShape;
friend class PhysicsJoint;
friend class Node;
};
NS_CC_END
#endif // __CCPHYSICS_BODY_H__
#endif // CC_USE_PHYSICS

View File

@ -1,147 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_CONTACT_H__
#define __CCPHYSICS_CONTACT_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCGeometry.h"
NS_CC_BEGIN
class PhysicsShape;
class PhysicsWorld;
class PhysicsContactInfo;
/**
* @brief Contact infomation. it will created automatically when two shape contact with each other. and it will destoried automatically when two shape separated.
*/
class PhysicsContact
{
public:
/*
* @brief get contact shape A.
*/
inline PhysicsShape* getShapeA() const { return _shapeA; }
/*
* @brief get contact shape B.
*/
inline PhysicsShape* getShapeB() const { return _shapeB; }
/*
* @brief get data.
*/
inline void* getData() { 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.
*/
inline void setData(void* data) { _data = data; }
private:
static PhysicsContact* create(PhysicsShape* a, PhysicsShape* b);
bool init(PhysicsShape* a, PhysicsShape* b);
inline bool getNotify() { return _notify; }
inline void setNotify(bool notify) { _notify = notify; }
private:
PhysicsContact();
~PhysicsContact();
private:
PhysicsShape* _shapeA;
PhysicsShape* _shapeB;
PhysicsContactInfo* _info;
void* _data;
bool _notify;
friend class PhysicsWorld;
};
/*
* @brief presolve value generated when onContactPreSolve called.
*/
class PhysicsContactPreSolve
{
private:
PhysicsContactPreSolve();
~PhysicsContactPreSolve();
static PhysicsContactPreSolve* create();
bool init();
friend class PhysicsWorld;
};
/*
* @brief postsolve value generated when onContactPostSolve called.
*/
class PhysicsContactPostSolve
{
private:
PhysicsContactPostSolve();
~PhysicsContactPostSolve();
static PhysicsContactPostSolve* create();
bool init();
friend class PhysicsWorld;
};
/*
* @brief contact listener.
*/
class PhysicsContactListener
{
public:
PhysicsContactListener();
virtual ~PhysicsContactListener();
public:
/*
* @brief it will called at two shapes start to contact, and only call it once.
*/
std::function<bool(const PhysicsContact& contact)> onContactBegin;
/*
* @brief Two shapes are touching during this step. Return false from the callback to make world ignore the collision this step or true to process it normally. Additionally, you may override collision values, elasticity, or surface velocity values.
*/
std::function<bool(const PhysicsContact& contact, const PhysicsContactPreSolve& solve)> onContactPreSolve;
/*
* @brief Two shapes are touching and their collision response has been processed. You can retrieve the collision impulse or kinetic energy at this time if you want to use it to calculate sound volumes or damage amounts. See cpArbiter for more info
*/
std::function<void(const PhysicsContact& contact, const PhysicsContactPostSolve& solve)> onContactPostSolve;
/*
* @brief it will called at two shapes separated, and only call it once.
* onContactBegin and onContactEnd will called in pairs.
*/
std::function<void(const PhysicsContact& contact)> onContactEnd;
};
NS_CC_END
#endif //__CCPHYSICS_CONTACT_H__
#endif // CC_USE_PHYSICS

View File

@ -1,168 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_JOINT_H__
#define __CCPHYSICS_JOINT_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCGeometry.h"
NS_CC_BEGIN
class PhysicsBody;
class PhysicsJointInfo;
class PhysicsBodyInfo;
/*
* @brief An PhysicsJoint object connects two physics bodies together.
*/
class PhysicsJoint : public Object
{
protected:
PhysicsJoint();
virtual ~PhysicsJoint() = 0;
public:
PhysicsBody* getBodyA() { return _bodyA; }
PhysicsBody* getBodyB() { return _bodyB; }
inline int getTag() { return _tag; }
inline void setTag(int tag) { _tag = tag; }
inline bool isEnable() { return _enable; }
void setEnable(bool enable);
inline bool isCollisionEnable() { return _collisionEnable; }
void setCollisionEnable(bool enable);
protected:
bool init(PhysicsBody* a, PhysicsBody* b);
/**
* PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody.
*/
PhysicsBodyInfo* bodyInfo(PhysicsBody* body) const;
protected:
PhysicsBody* _bodyA;
PhysicsBody* _bodyB;
PhysicsJointInfo* _info;
bool _enable;
bool _collisionEnable;
int _tag;
friend class PhysicsBody;
friend class PhysicsWorld;
};
/*
* @brief A fixed joint fuses the two bodies together at a reference point. Fixed joints are useful for creating complex shapes that can be broken apart later.
*/
class PhysicsJointFixed : public PhysicsJoint
{
public:
PhysicsJointFixed* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
protected:
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
protected:
PhysicsJointFixed();
virtual ~PhysicsJointFixed();
};
/*
* @brief A sliding joint allows the two bodies to slide along a chosen axis.
*/
class PhysicsJointSliding : public PhysicsJoint
{
public:
PhysicsJointSliding* create(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* create();
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* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
float getMin();
void setMin(float min);
float getMax();
void setMax(float max);
protected:
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr1, const Point& anchr2);
protected:
PhysicsJointLimit();
virtual ~PhysicsJointLimit();
};
/*
* @brief A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together.
*/
class PhysicsJointPin : public PhysicsJoint
{
public:
static PhysicsJointPin* create(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
protected:
bool init(PhysicsBody* a, PhysicsBody* b, const Point& anchr);
protected:
PhysicsJointPin();
virtual ~PhysicsJointPin();
};
NS_CC_END
#endif // __CCPHYSICS_JOINT_H__
#endif // CC_USE_PHYSICS

View File

@ -1,296 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_SHAPE_H__
#define __CCPHYSICS_SHAPE_H__
#include "cocoa/CCObject.h"
#include "cocoa/CCGeometry.h"
NS_CC_BEGIN
class PhysicsShapeInfo;
class PhysicsBody;
class PhysicsBodyInfo;
typedef struct PhysicsMaterial
{
float density;
float restitution;
float friction;
PhysicsMaterial()
: density(0.0f)
, restitution(0.0f)
, friction(0.0f){}
PhysicsMaterial(float density, float restitution, float friction)
: density(density)
, restitution(restitution)
, friction(friction){}
}PhysicsMaterial;
const PhysicsMaterial PHYSICSSHAPE_MATERIAL_DEFAULT = {0.0f, 1.0f, 1.0f};
/**
* @brief A shape for body. You do not create PhysicsWorld objects directly, instead, you can view PhysicsBody to see how to create it.
*/
class PhysicsShape : public Object
{
public:
enum class Type
{
UNKNOWN,
CIRCLE,
BOX,
POLYGEN,
EDGESEGMENT,
EDGEBOX,
EDGEPOLYGEN,
EDGECHAIN,
};
public:
inline PhysicsBody* getBody() const { return _body; }
inline Type getType() const { return _type; }
inline float getArea() const { return _area; }
inline float getMoment() const { return _moment; }
void setMoment(float moment);
inline void setTag(int tag) { _tag = tag; }
inline int getTag() const { return _tag; }
inline float getMass() const { return _mass; }
void setMass(float mass);
inline float getDensity() const { return _material.density; }
void setDensity(float density);
void setRestitution(float restitution);
void setFriction(float friction);
void setMaterial(PhysicsMaterial material);
virtual float calculateDefaultMoment() { return 0; }
virtual float calculateDefaultArea() { return 0; }
virtual Point getOffset() { return Point::ZERO; }
virtual Point getCenter() { return getOffset(); }
static Point* recenterPoints(Point* points, int count, Point center);
static Point getPolyonCenter(Point* points, int count);
protected:
bool init(Type type);
/**
* @brief PhysicsShape is PhysicsBody's friend class, but all the subclasses isn't. so this method is use for subclasses to catch the bodyInfo from PhysicsBody.
*/
PhysicsBodyInfo* bodyInfo() const;
void setBody(PhysicsBody* body);
protected:
PhysicsShape();
virtual ~PhysicsShape() = 0;
protected:
PhysicsBody* _body;
PhysicsShapeInfo* _info;
Type _type;
float _area;
float _mass;
float _moment;
PhysicsMaterial _material;
int _tag;
friend class PhysicsWorld;
friend class PhysicsBody;
friend class PhysicsJoint;
};
/** A circle shape */
class PhysicsShapeCircle : public PhysicsShape
{
public:
static PhysicsShapeCircle* create(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
static float calculateArea(float radius);
static float calculateMoment(float mass, float radius, Point offset = Point(0, 0));
float calculateDefaultArea() override;
float calculateDefaultMoment() override;
float getRadius();
Point getOffset();
protected:
bool init(float radius, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
protected:
PhysicsShapeCircle();
~PhysicsShapeCircle();
};
/** A box shape */
class PhysicsShapeBox : public PhysicsShape
{
public:
static PhysicsShapeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
static float calculateArea(Size size);
static float calculateMoment(float mass, Size size, Point offset = Point(0, 0));
float calculateDefaultArea() override;
float calculateDefaultMoment() override;
Point* getPoints(Point* points);
Size getSize();
Point getOffset() override { return _offset; }
protected:
bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
protected:
PhysicsShapeBox();
virtual ~PhysicsShapeBox();
protected:
Point _offset;
};
/** A polygon shape */
class PhysicsShapePolygon : public PhysicsShape
{
public:
static PhysicsShapePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
static float calculateArea(Point* points, int count);
static float calculateMoment(float mass, Point* points, int count, Point offset = Point(0, 0));
float calculateDefaultArea() override;
float calculateDefaultMoment() override;
Point* getPoints(Point* points);
int getPointsCount();
Point getCenter() override;
protected:
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, Point offset = Point(0, 0));
protected:
PhysicsShapePolygon();
virtual ~PhysicsShapePolygon();
protected:
Point _center;
};
/** A segment shape */
class PhysicsShapeEdgeSegment : public PhysicsShape
{
public:
static PhysicsShapeEdgeSegment* create(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
Point getPointA();
Point getPointB();
Point getCenter() override;
protected:
bool init(Point a, Point b, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
protected:
PhysicsShapeEdgeSegment();
virtual ~PhysicsShapeEdgeSegment();
protected:
Point _center;
friend class PhysicsBody;
};
/** An edge box shape */
class PhysicsShapeEdgeBox : public PhysicsShape
{
public:
static PhysicsShapeEdgeBox* create(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 0, Point offset = Point(0, 0));
Point getOffset() override { return _offset; }
Point* getPoints(Point* points);
int getPointsCount();
protected:
bool init(Size size, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1, Point offset = Point(0, 0));
protected:
PhysicsShapeEdgeBox();
virtual ~PhysicsShapeEdgeBox();
protected:
Point _offset;
friend class PhysicsBody;
};
/** An edge polygon shape */
class PhysicsShapeEdgePolygon : public PhysicsShape
{
public:
static PhysicsShapeEdgePolygon* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
Point getCenter() override;
Point* getPoints(Point* points);
int getPointsCount();
protected:
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
protected:
PhysicsShapeEdgePolygon();
virtual ~PhysicsShapeEdgePolygon();
friend class PhysicsBody;
protected:
Point _center;
};
/** a chain shape */
class PhysicsShapeEdgeChain : public PhysicsShape
{
public:
static PhysicsShapeEdgeChain* create(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
Point getCenter() override;
Point* getPoints(Point* points);
int getPointsCount();
protected:
bool init(Point* points, int count, PhysicsMaterial material = PHYSICSSHAPE_MATERIAL_DEFAULT, float border = 1);
protected:
PhysicsShapeEdgeChain();
virtual ~PhysicsShapeEdgeChain();
protected:
Point _center;
friend class PhysicsBody;
};
NS_CC_END
#endif // __CCPHYSICS_FIXTURE_H__
#endif // CC_USE_PHYSICS

View File

@ -1,496 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsWorld.h"
#ifdef CC_USE_PHYSICS
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
#include "chipmunk.h"
#elif (CC_PHYSICS_ENGINE == CCPHYSICS_BOX2D)
#include "Box2D.h"
#endif
#include "CCPhysicsBody.h"
#include "CCPhysicsShape.h"
#include "CCPhysicsContact.h"
#include "CCPhysicsJoint.h"
#include "CCPhysicsContact.h"
#include "chipmunk/CCPhysicsWorldInfo.h"
#include "Box2D/CCPhysicsWorldInfo.h"
#include "chipmunk/CCPhysicsBodyInfo.h"
#include "Box2D/CCPhysicsBodyInfo.h"
#include "chipmunk/CCPhysicsShapeInfo.h"
#include "Box2D/CCPhysicsShapeInfo.h"
#include "chipmunk/CCPhysicsContactInfo.h"
#include "Box2D/CCPhysicsContactInfo.h"
#include "chipmunk/CCPhysicsJointInfo.h"
#include "Box2D/CCPhysicsJointInfo.h"
#include "chipmunk/CCPhysicsHelper.h"
#include "draw_nodes/CCDrawNode.h"
#include "cocoa/CCArray.h"
#include "layers_scenes_transitions_nodes/CCScene.h"
#include "CCDirector.h"
#include <algorithm>
NS_CC_BEGIN
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
const float PHYSICS_INFINITY = INFINITY;
int PhysicsWorld::collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data)
{
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
CP_ARBITER_GET_SHAPES(arb, a, b);
auto ita = PhysicsShapeInfo::map.find(a);
auto itb = PhysicsShapeInfo::map.find(b);
CC_ASSERT(ita != PhysicsShapeInfo::map.end() && itb != PhysicsShapeInfo::map.end());
PhysicsContact* contact = PhysicsContact::create(ita->second->shape, itb->second->shape);
arb->data = contact;
return world->collisionBeginCallback(*static_cast<PhysicsContact*>(arb->data));
}
int PhysicsWorld::collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
{
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
return world->collisionPreSolveCallback(*static_cast<PhysicsContact*>(arb->data),
PhysicsContactPreSolve());
}
void PhysicsWorld::collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
{
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
world->collisionPostSolveCallback(*static_cast<PhysicsContact*>(arb->data),
PhysicsContactPostSolve());
}
void PhysicsWorld::collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data)
{
PhysicsWorld* world = static_cast<PhysicsWorld*>(data);
PhysicsContact* contact = static_cast<PhysicsContact*>(arb->data);
world->collisionSeparateCallback(*contact);
delete contact;
}
bool PhysicsWorld::init()
{
_info = new PhysicsWorldInfo();
cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(_gravity));
cpSpaceSetDefaultCollisionHandler(_info->space,
PhysicsWorld::collisionBeginCallbackFunc,
PhysicsWorld::collisionPreSolveCallbackFunc,
PhysicsWorld::collisionPostSolveCallbackFunc,
PhysicsWorld::collisionSeparateCallbackFunc,
this);
return true;
}
void PhysicsWorld::addJoint(PhysicsJoint* joint)
{
auto it = std::find(_joints.begin(), _joints.end(), joint);
if (it == _joints.end())
{
_joints.push_back(joint);
for (auto subjoint : joint->_info->joints)
{
if (!cpSpaceContainsConstraint(_info->space, subjoint))
{
cpSpaceAddConstraint(_info->space, subjoint);
}
}
}
}
void PhysicsWorld::removeJoint(PhysicsJoint* joint)
{
}
void PhysicsWorld::removeAllJoints()
{
}
void PhysicsWorld::addShape(PhysicsShape* shape)
{
for (auto cps : shape->_info->shapes)
{
if (cpSpaceContainsShape(_info->space, cps))
{
continue;
}
if (cpBodyIsStatic(shape->getBody()->_info->body))
{
cpSpaceAddStaticShape(_info->space, cps);
}else
{
cpSpaceAddShape(_info->space, cps);
}
}
}
void PhysicsWorld::addBody(PhysicsBody* body)
{
CCASSERT(body != nullptr, "the body can not be nullptr");
if (body->isEnable())
{
//is gravity enable
if (!body->isGravityEnable())
{
body->applyForce(-_gravity);
}
// add body to space
if (body->isDynamic())
{
cpSpaceAddBody(_info->space, body->_info->body);
}
// add shapes to space
for (auto shape : body->getShapes())
{
addShape(shape);
}
}
if (_bodys == nullptr)
{
_bodys = Array::create(body, NULL);
_bodys->retain();
}else
{
_bodys->addObject(body);
}
}
void PhysicsWorld::removeBody(PhysicsBody* body)
{
CCASSERT(body != nullptr, "the body can not be nullptr");
if (body->getWorld() == this)
{
// reset the gravity
if (!body->isGravityEnable())
{
body->applyForce(-_gravity);
}
}
// remove shaps
for (auto shape : body->getShapes())
{
for (auto cps : shape->_info->shapes)
{
if (cpSpaceContainsShape(_info->space, cps))
{
cpSpaceRemoveShape(_info->space, cps);
}
}
}
// remove body
if (cpSpaceContainsBody(_info->space, body->_info->body))
{
cpSpaceRemoveBody(_info->space, body->_info->body);
}
if (_bodys != nullptr)
{
_bodys->removeObject(body);
}
}
void PhysicsWorld::removeBodyByTag(int tag)
{
for (Object* obj : *_bodys)
{
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
if (body->getTag() == tag)
{
removeBody(body);
return;
}
}
}
void PhysicsWorld::removeShape(PhysicsShape* shape)
{
for (auto cps : shape->_info->shapes)
{
if (cpSpaceContainsShape(_info->space, cps))
{
cpSpaceRemoveShape(_info->space, cps);
}
}
}
void PhysicsWorld::update(float delta)
{
for (auto body : *_bodys)
{
body->update(delta);
}
cpSpaceStep(_info->space, delta);
if (_drawNode)
{
_drawNode->removeFromParent();
_drawNode = nullptr;
}
if (_debugDraw)
{
debugDraw();
}
}
void PhysicsWorld::debugDraw()
{
if (_debugDraw && _bodys != nullptr)
{
_drawNode= DrawNode::create();
for (Object* obj : *_bodys)
{
PhysicsBody* body = dynamic_cast<PhysicsBody*>(obj);
std::vector<PhysicsShape*> shapes = body->getShapes();
for (auto shape : shapes)
{
drawWithShape(_drawNode, shape);
}
}
if (_scene)
{
_scene->addChild(_drawNode);
}
}
}
void PhysicsWorld::setScene(Scene *scene)
{
_scene = scene;
}
void PhysicsWorld::drawWithShape(DrawNode* node, PhysicsShape* shape)
{
for (auto it = shape->_info->shapes.begin(); it != shape->_info->shapes.end(); ++it)
{
cpShape *shape = *it;
switch ((*it)->klass_private->type)
{
case CP_CIRCLE_SHAPE:
{
float radius = PhysicsHelper::cpfloat2float(cpCircleShapeGetRadius(shape));
Point centre = PhysicsHelper::cpv2point(cpBodyGetPos(cpShapeGetBody(shape)))
+ PhysicsHelper::cpv2point(cpCircleShapeGetOffset(shape));
Point seg[4] = {};
seg[0] = Point(centre.x - radius, centre.y - radius);
seg[1] = Point(centre.x - radius, centre.y + radius);
seg[2] = Point(centre.x + radius, centre.y + radius);
seg[3] = Point(centre.x + radius, centre.y - radius);
node->drawPolygon(seg, 4, Color4F(), 1, Color4F(1, 0, 0, 1));
break;
}
case CP_SEGMENT_SHAPE:
{
cpSegmentShape *seg = (cpSegmentShape *)shape;
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*)shape;
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)
{
bool ret = true;
PhysicsBody* bodyA = contact.getShapeA()->getBody();
PhysicsBody* bodyB = contact.getShapeB()->getBody();
if ((bodyA->getCategoryBitmask() & bodyB->getContactTestBitmask()) == 0)
{
contact.setNotify(false);
}
if ((bodyA->getCategoryBitmask() & bodyB->getCollisionBitmask()) == 0)
{
ret = false;
}
if (contact.getNotify() && _listener && _listener->onContactBegin)
{
// the mask has high priority than _listener->onContactBegin.
// so if the mask test is false, the two bodies won't have collision.
if (ret)
{
ret = _listener->onContactBegin(contact);
}else
{
_listener->onContactBegin(contact);
}
}
return ret;
}
int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact, const PhysicsContactPreSolve& solve)
{
if (!contact.getNotify())
{
return true;
}
if (_listener && _listener->onContactPreSolve)
{
return _listener->onContactPreSolve(contact, solve);
}
return true;
}
void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact, const PhysicsContactPostSolve& solve)
{
if (!contact.getNotify())
{
return;
}
if (_listener && _listener->onContactPreSolve)
{
_listener->onContactPostSolve(contact, solve);
}
}
void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
{
if (!contact.getNotify())
{
return;
}
if (_listener && _listener->onContactEnd)
{
_listener->onContactEnd(contact);
}
}
void PhysicsWorld::setGravity(Point gravity)
{
if (_bodys != nullptr)
{
for (auto child : *_bodys)
{
PhysicsBody* body = dynamic_cast<PhysicsBody*>(child);
// reset gravity for body
if (!body->isGravityEnable())
{
body->applyForce(-_gravity);
body->applyForce(gravity);
}
}
}
_gravity = gravity;
cpSpaceSetGravity(_info->space, PhysicsHelper::point2cpv(gravity));
}
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
#endif
PhysicsWorld* PhysicsWorld::create()
{
PhysicsWorld * world = new PhysicsWorld();
if(world && world->init())
{
return world;
}
CC_SAFE_DELETE(world);
return nullptr;
}
PhysicsWorld::PhysicsWorld()
: _gravity(Point(0.0f, -98.0f))
, _speed(1.0f)
, _info(nullptr)
, _listener(nullptr)
, _bodys(nullptr)
, _scene(nullptr)
, _debugDraw(false)
, _drawNode(nullptr)
{
}
PhysicsWorld::~PhysicsWorld()
{
CC_SAFE_DELETE(_info);
CC_SAFE_RELEASE(_bodys);
}
NS_CC_END
#endif // CC_USE_PHYSICS

View File

@ -1,149 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_WORLD_H__
#define __CCPHYSICS_WORLD_H__
#include <list>
#include "cocoa/CCObject.h"
#include "cocoa/CCGeometry.h"
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
typedef struct cpArbiter cpArbiter;
typedef struct cpSpace cpSpace;
#endif
NS_CC_BEGIN
class PhysicsBody;
class PhysicsJoint;
class PhysicsWorldInfo;
class PhysicsShape;
class PhysicsContact;
class PhysicsContactPreSolve;
class PhysicsContactPostSolve;
class PhysicsContactListener;
class Array;
class Sprite;
class Scene;
class DrawNode;
/**
* @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:
/** Adds a joint to the physics world.*/
void addJoint(PhysicsJoint* joint);
/** Removes a joint from the physics world.*/
void removeJoint(PhysicsJoint* joint);
/** Remove all joints from the physics world.*/
void removeAllJoints();
Array* getBodysAlongRay(Point start, Point end) const;
Array* getBodysAtPoint(Point point) const;
Array* getBodysInRect(Rect rect) const;
Array* getAllBody() const;
/** Register a listener to receive contact callbacks*/
inline void registerContactListener(PhysicsContactListener* delegate) { _listener = delegate; }
/** Unregister a listener. */
inline void unregisterContactListener() { _listener = nullptr; }
/** get the gravity value */
inline Point getGravity() { return _gravity; }
/** set the gravity value */
void setGravity(Point gravity);
/** test the debug draw is enabled */
inline bool isDebugDraw() { return _debugDraw; }
/** set the debug draw */
inline void setDebugDraw(bool debugDraw) { _debugDraw = debugDraw; }
virtual void removeBody(PhysicsBody* body);
virtual void removeBodyByTag(int tag);
protected:
static PhysicsWorld* create();
bool init();
void setScene(Scene* scene);
virtual void addBody(PhysicsBody* body);
virtual void addShape(PhysicsShape* shape);
virtual void removeShape(PhysicsShape* shape);
virtual void update(float delta);
virtual void debugDraw();
virtual void drawWithShape(DrawNode* node, PhysicsShape* shape);
virtual int collisionBeginCallback(PhysicsContact& contact);
virtual int collisionPreSolveCallback(PhysicsContact& contact, const PhysicsContactPreSolve& solve);
virtual void collisionPostSolveCallback(PhysicsContact& contact, const PhysicsContactPostSolve& solve);
virtual void collisionSeparateCallback(PhysicsContact& contact);
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
static int collisionBeginCallbackFunc(cpArbiter *arb, struct cpSpace *space, void *data);
static int collisionPreSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
static void collisionPostSolveCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
static void collisionSeparateCallbackFunc(cpArbiter *arb, cpSpace *space, void *data);
#endif
protected:
Point _gravity;
float _speed;
PhysicsWorldInfo* _info;
PhysicsContactListener* _listener;
Array* _bodys;
std::list<PhysicsJoint*> _joints;
Scene* _scene;
bool _debugDraw;
DrawNode* _drawNode;
protected:
PhysicsWorld();
virtual ~PhysicsWorld();
friend class Sprite;
friend class Scene;
friend class PhysicsBody;
friend class PhysicsShape;
};
NS_CC_END
#endif // __CCPHYSICS_WORLD_H__
#endif // CC_USE_PHYSICS

View File

@ -1,54 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "../CCPhysicsSetting.h"
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
#ifndef __CCPHYSICS_BODY_INFO_H__
#define __CCPHYSICS_BODY_INFO_H__
#include "chipmunk.h"
#include "platform/CCPlatformMacros.h"
#include "cocoa/CCObject.h"
NS_CC_BEGIN
class PhysicsBodyInfo : public Clonable
{
public:
cpBody* body;
cpGroup group;
private:
PhysicsBodyInfo();
~PhysicsBodyInfo();
Clonable* clone() const override;
friend class PhysicsBody;
};
NS_CC_END
#endif // __CCPHYSICS_BODY_INFO_H__
#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK

View File

@ -1,71 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "../CCPhysicsSetting.h"
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
#ifndef __CCPHYSICS_HELPER_H__
#define __CCPHYSICS_HELPER_H__
#include "chipmunk.h"
#include "platform/CCPlatformMacros.h"
#include "cocoa/CCGeometry.h"
NS_CC_BEGIN
class PhysicsHelper
{
public:
static Point cpv2point(const cpVect& vec) { return Point(vec.x, vec.y); }
static cpVect point2cpv(const Point& point) { return cpv(point.x, point.y); }
static Size cpv2size(const cpVect& vec) { return Size(vec.x, vec.y); }
static cpVect size2cpv(const Size& size) { return cpv(size.width, size.height); }
static float cpfloat2float(cpFloat f) { return f; }
static cpFloat float2cpfloat(float f) { return f; }
static Point* cpvs2points(const cpVect* cpvs, Point* points, int count)
{
for (int i = 0; i < count; ++i)
{
points[i] = cpv2point(cpvs[i]);
}
return points;
}
static cpVect* points2cpvs(const Point* points, cpVect* cpvs, int count)
{
for (int i = 0; i < count; ++i)
{
cpvs[i] = point2cpv(points[i]);
}
return cpvs;
}
};
NS_CC_END
#endif // __CCPHYSICS_HELPER_H__
#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK

View File

@ -1,60 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "../CCPhysicsSetting.h"
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
#ifndef __CCPHYSICS_JOINT_INFO_H__
#define __CCPHYSICS_JOINT_INFO_H__
#include "chipmunk.h"
#include "platform/CCPlatformMacros.h"
#include <vector>
#include <map>
NS_CC_BEGIN
class PhysicsJoint;
class PhysicsJointInfo
{
public:
void add(cpConstraint* shape);
void remove(cpConstraint* shape);
void removeAll();
public:
std::vector<cpConstraint*> joints;
PhysicsJoint* joint;
static std::map<cpConstraint*, PhysicsJointInfo*> map;
private:
PhysicsJointInfo(PhysicsJoint* joint);
~PhysicsJointInfo();
friend class PhysicsJoint;
};
NS_CC_END
#endif // __CCPHYSICS_SHAPE_INFO_H__
#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK

View File

@ -1,67 +0,0 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "../CCPhysicsSetting.h"
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK)
#ifndef __CCPHYSICS_SHAPE_INFO_H__
#define __CCPHYSICS_SHAPE_INFO_H__
#include <vector>
#include <map>
#include "chipmunk.h"
#include "platform/CCPlatformMacros.h"
NS_CC_BEGIN
class PhysicsShape;
class PhysicsShapeInfo
{
public:
void add(cpShape* shape);
void remove(cpShape* shape);
void removeAll();
void setGroup(cpGroup group);
void setBody(cpBody* body);
public:
std::vector<cpShape*> shapes;
PhysicsShape* shape;
cpBody* body;
cpGroup group;
static std::map<cpShape*, PhysicsShapeInfo*> map;
static cpBody* shareBody;
private:
PhysicsShapeInfo(PhysicsShape* shape);
~PhysicsShapeInfo();
friend class PhysicsShape;
};
NS_CC_END
#endif // __CCPHYSICS_SHAPE_INFO_H__
#endif // CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK

File diff suppressed because it is too large Load Diff