diff --git a/cocos2dx/physics/CCPhysicsBody.cpp b/cocos2dx/physics/CCPhysicsBody.cpp index eb7296843e..0b2309a1c1 100644 --- a/cocos2dx/physics/CCPhysicsBody.cpp +++ b/cocos2dx/physics/CCPhysicsBody.cpp @@ -22,3 +22,99 @@ THE SOFTWARE. ****************************************************************************/ #include "CCPhysicsBody.h" + +#ifdef CC_USE_PHYSICS + +NS_CC_BEGIN + + +#if (CC_PHYSICS_ENGINE == CC_PHYSICS_CHIPMUNK) + +class PhysicsBodyInfo +{ +public: + PhysicsBodyInfo(); + ~PhysicsBodyInfo(); + +public: + cpBody* body; + cpShape* shape; +}; + +PhysicsBodyInfo::PhysicsBodyInfo() +: body(nullptr) +, shape(nullptr) +{ +} + +PhysicsBodyInfo::~PhysicsBodyInfo() +{ + if (body) cpBodyFree(body); + if (shape) cpShapeFree(shape); +} + +PhysicsBody::PhysicsBody() +{ +} + +PhysicsBody::~PhysicsBody() +{ + CC_SAFE_DELETE(_info); +} + +PhysicsBody* PhysicsBody::create() +{ + PhysicsBody * body = new PhysicsBody(); + if(body && body->init()) + { + return body; + } + + CC_SAFE_DELETE(body); + return nullptr; +} + +bool PhysicsBody::init() +{ + do + { + _info = new PhysicsBodyInfo(); + CC_BREAK_IF(_info == nullptr); + + _info->body = cpBodyNew(1.0, 1.0); + CC_BREAK_IF(_info->body == nullptr); + + return true; + } while (false); + + return false; +} + +PhysicsBody* PhysicsBody::createCircle(Point centre, float radius) +{ + PhysicsBody* body = PhysicsBody::create(); + + if (body == nullptr) + { + return nullptr; + } + + cpBodySetPos(body->_info->body, cpv(centre.x, centre.y)); + body->_info->shape = cpCircleShapeNew(body->_info->body, radius, cpvzero); + + if (body->_info->shape == nullptr) + { + return nullptr; + } + + return body; +} + +#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) + + +#endif + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsBody.h b/cocos2dx/physics/CCPhysicsBody.h index 1871565c3d..64df98ec3f 100644 --- a/cocos2dx/physics/CCPhysicsBody.h +++ b/cocos2dx/physics/CCPhysicsBody.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__ @@ -37,6 +37,7 @@ class Sprite; class PhysicsWorld; class PhysicsJoint; class PhysicsFixture; +class PhysicsBodyInfo; class PhysicsBody : public Object, public Clonable { @@ -63,7 +64,7 @@ public: void removeFixture(PhysicsFixture* fixture); void removeAllFixtures(); - inline PhysicsWorld* getWorld() const { return _physicsWorld; } + inline PhysicsWorld* getWorld() const { return _world; } inline const std::vector* getJoints() const { return &_joints; } inline Sprite* getOwner() const { return _owner; } @@ -78,13 +79,14 @@ public: virtual Clonable* clone() const override; protected: + static PhysicsBody* create(); bool init(); protected: PhysicsBody(); virtual ~PhysicsBody(); -private: +protected: float _mass; float _density; float _area; @@ -100,11 +102,12 @@ private: std::vector _joints; Array* _fixtures; - PhysicsWorld* _physicsWorld; + PhysicsWorld* _world; + PhysicsBodyInfo* _info; }; NS_CC_END #endif // __CCPHYSICS_BODY_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsContactDelegate.h b/cocos2dx/physics/CCPhysicsContactDelegate.h index e96e563139..a371b6e044 100644 --- a/cocos2dx/physics/CCPhysicsContactDelegate.h +++ b/cocos2dx/physics/CCPhysicsContactDelegate.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_CONTACTDELEGATE_H__ #define __CCPHYSICS_CONTACTDELEGATE_H__ @@ -49,4 +49,4 @@ public: NS_CC_END #endif //__CCPHYSICS_CONTACTDELEGATE_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsFixture.h b/cocos2dx/physics/CCPhysicsFixture.h index 210f58472d..e743ab3c44 100644 --- a/cocos2dx/physics/CCPhysicsFixture.h +++ b/cocos2dx/physics/CCPhysicsFixture.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_FIXTURE_H__ #define __CCPHYSICS_FIXTURE_H__ @@ -60,4 +60,4 @@ protected: NS_CC_END #endif // __CCPHYSICS_FIXTURE_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsJoint.h b/cocos2dx/physics/CCPhysicsJoint.h index 03e0515336..c7e659ab75 100644 --- a/cocos2dx/physics/CCPhysicsJoint.h +++ b/cocos2dx/physics/CCPhysicsJoint.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_JOINT_H__ #define __CCPHYSICS_JOINT_H__ @@ -115,4 +115,4 @@ NS_CC_END #endif // __CCPHYSICS_JOINT_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS diff --git a/cocos2dx/physics/CCPhysicsSetting.h b/cocos2dx/physics/CCPhysicsSetting.h index 4edd004e11..b68da7cf93 100644 --- a/cocos2dx/physics/CCPhysicsSetting.h +++ b/cocos2dx/physics/CCPhysicsSetting.h @@ -32,7 +32,7 @@ #define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK #if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN) -#define CC_USE_PHYSICS_ENGINE +#define CC_USE_PHYSICS #endif #if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) diff --git a/cocos2dx/physics/CCPhysicsWorld.cpp b/cocos2dx/physics/CCPhysicsWorld.cpp index 7ca586a2b1..ccfc1ea788 100644 --- a/cocos2dx/physics/CCPhysicsWorld.cpp +++ b/cocos2dx/physics/CCPhysicsWorld.cpp @@ -57,6 +57,11 @@ bool PhysicsWorld::init() return true; } +void PhysicsWorld::addChild(PhysicsBody* body) +{ + +} + #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) struct PhysicsInfo @@ -67,19 +72,20 @@ struct PhysicsInfo PhysicsWorld* PhysicsWorld::create() { - PhysicsWorld * physicsWorld = new PhysicsWorld(); - if(physicsWorld && physicsWorld->init()) + PhysicsWorld * world = new PhysicsWorld(); + if(world && world->init()) { - return physicsWorld; + return world; } - CC_SAFE_DELETE(physicsWorld); - return NULL; + CC_SAFE_DELETE(world); + return nullptr; } -PhysicsWorld::PhysicsWorld() : -_gravity(Point(0, -9.8)), -_speed(1.0) +PhysicsWorld::PhysicsWorld() +: _gravity(Point(0, -9.8)) +, _speed(1.0) +, _worldInfo(nullptr) { } diff --git a/cocos2dx/physics/CCPhysicsWorld.h b/cocos2dx/physics/CCPhysicsWorld.h index 2b81983f77..5bf807c159 100644 --- a/cocos2dx/physics/CCPhysicsWorld.h +++ b/cocos2dx/physics/CCPhysicsWorld.h @@ -23,7 +23,7 @@ ****************************************************************************/ #include "CCPhysicsSetting.h" -#ifdef CC_USE_PHYSICS_ENGINE +#ifdef CC_USE_PHYSICS #ifndef __CCPHYSICS_WORLD_H__ #define __CCPHYSICS_WORLD_H__ @@ -55,6 +55,9 @@ public: void registerContactDelegate(PhysicsContactDelegate* delegate); + inline Point getGravity() { return _gravity; } + inline void setGravity(Point gravity) { _gravity = gravity; } + protected: static PhysicsWorld* create(); bool init(); @@ -78,4 +81,4 @@ NS_CC_END #endif // __CCPHYSICS_WORLD_H__ -#endif // CC_USE_PHYSICS_ENGINE +#endif // CC_USE_PHYSICS