issue #2771: Change CC_USE_PHYSICS_ENGINE to CC_USE_PHYSICS. Add some implements.

This commit is contained in:
boyu0 2013-09-10 17:36:49 +08:00
parent bd10d92460
commit bcab90ddc4
8 changed files with 130 additions and 22 deletions

View File

@ -22,3 +22,99 @@
THE SOFTWARE. THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsBody.h" #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

View File

@ -23,7 +23,7 @@
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsSetting.h" #include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS_ENGINE #ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_BODY_H__ #ifndef __CCPHYSICS_BODY_H__
#define __CCPHYSICS_BODY_H__ #define __CCPHYSICS_BODY_H__
@ -37,6 +37,7 @@ class Sprite;
class PhysicsWorld; class PhysicsWorld;
class PhysicsJoint; class PhysicsJoint;
class PhysicsFixture; class PhysicsFixture;
class PhysicsBodyInfo;
class PhysicsBody : public Object, public Clonable class PhysicsBody : public Object, public Clonable
{ {
@ -63,7 +64,7 @@ public:
void removeFixture(PhysicsFixture* fixture); void removeFixture(PhysicsFixture* fixture);
void removeAllFixtures(); void removeAllFixtures();
inline PhysicsWorld* getWorld() const { return _physicsWorld; } inline PhysicsWorld* getWorld() const { return _world; }
inline const std::vector<PhysicsJoint*>* getJoints() const { return &_joints; } inline const std::vector<PhysicsJoint*>* getJoints() const { return &_joints; }
inline Sprite* getOwner() const { return _owner; } inline Sprite* getOwner() const { return _owner; }
@ -78,13 +79,14 @@ public:
virtual Clonable* clone() const override; virtual Clonable* clone() const override;
protected: protected:
static PhysicsBody* create();
bool init(); bool init();
protected: protected:
PhysicsBody(); PhysicsBody();
virtual ~PhysicsBody(); virtual ~PhysicsBody();
private: protected:
float _mass; float _mass;
float _density; float _density;
float _area; float _area;
@ -100,11 +102,12 @@ private:
std::vector<PhysicsJoint*> _joints; std::vector<PhysicsJoint*> _joints;
Array* _fixtures; Array* _fixtures;
PhysicsWorld* _physicsWorld; PhysicsWorld* _world;
PhysicsBodyInfo* _info;
}; };
NS_CC_END NS_CC_END
#endif // __CCPHYSICS_BODY_H__ #endif // __CCPHYSICS_BODY_H__
#endif // CC_USE_PHYSICS_ENGINE #endif // CC_USE_PHYSICS

View File

@ -23,7 +23,7 @@
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsSetting.h" #include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS_ENGINE #ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_CONTACTDELEGATE_H__ #ifndef __CCPHYSICS_CONTACTDELEGATE_H__
#define __CCPHYSICS_CONTACTDELEGATE_H__ #define __CCPHYSICS_CONTACTDELEGATE_H__
@ -49,4 +49,4 @@ public:
NS_CC_END NS_CC_END
#endif //__CCPHYSICS_CONTACTDELEGATE_H__ #endif //__CCPHYSICS_CONTACTDELEGATE_H__
#endif // CC_USE_PHYSICS_ENGINE #endif // CC_USE_PHYSICS

View File

@ -23,7 +23,7 @@
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsSetting.h" #include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS_ENGINE #ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_FIXTURE_H__ #ifndef __CCPHYSICS_FIXTURE_H__
#define __CCPHYSICS_FIXTURE_H__ #define __CCPHYSICS_FIXTURE_H__
@ -60,4 +60,4 @@ protected:
NS_CC_END NS_CC_END
#endif // __CCPHYSICS_FIXTURE_H__ #endif // __CCPHYSICS_FIXTURE_H__
#endif // CC_USE_PHYSICS_ENGINE #endif // CC_USE_PHYSICS

View File

@ -23,7 +23,7 @@
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsSetting.h" #include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS_ENGINE #ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_JOINT_H__ #ifndef __CCPHYSICS_JOINT_H__
#define __CCPHYSICS_JOINT_H__ #define __CCPHYSICS_JOINT_H__
@ -115,4 +115,4 @@ NS_CC_END
#endif // __CCPHYSICS_JOINT_H__ #endif // __CCPHYSICS_JOINT_H__
#endif // CC_USE_PHYSICS_ENGINE #endif // CC_USE_PHYSICS

View File

@ -32,7 +32,7 @@
#define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK #define CC_PHYSICS_ENGINE CC_PHYSICS_CHIPMUNK
#if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN) #if (CC_PHYSICS_ENGINE != CC_PHYSICS_UNKNOWN)
#define CC_USE_PHYSICS_ENGINE #define CC_USE_PHYSICS
#endif #endif
#if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) #if (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)

View File

@ -57,6 +57,11 @@ bool PhysicsWorld::init()
return true; return true;
} }
void PhysicsWorld::addChild(PhysicsBody* body)
{
}
#elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D) #elif (CC_PHYSICS_ENGINE == CC_PHYSICS_BOX2D)
struct PhysicsInfo struct PhysicsInfo
@ -67,19 +72,20 @@ struct PhysicsInfo
PhysicsWorld* PhysicsWorld::create() PhysicsWorld* PhysicsWorld::create()
{ {
PhysicsWorld * physicsWorld = new PhysicsWorld(); PhysicsWorld * world = new PhysicsWorld();
if(physicsWorld && physicsWorld->init()) if(world && world->init())
{ {
return physicsWorld; return world;
} }
CC_SAFE_DELETE(physicsWorld); CC_SAFE_DELETE(world);
return NULL; return nullptr;
} }
PhysicsWorld::PhysicsWorld() : PhysicsWorld::PhysicsWorld()
_gravity(Point(0, -9.8)), : _gravity(Point(0, -9.8))
_speed(1.0) , _speed(1.0)
, _worldInfo(nullptr)
{ {
} }

View File

@ -23,7 +23,7 @@
****************************************************************************/ ****************************************************************************/
#include "CCPhysicsSetting.h" #include "CCPhysicsSetting.h"
#ifdef CC_USE_PHYSICS_ENGINE #ifdef CC_USE_PHYSICS
#ifndef __CCPHYSICS_WORLD_H__ #ifndef __CCPHYSICS_WORLD_H__
#define __CCPHYSICS_WORLD_H__ #define __CCPHYSICS_WORLD_H__
@ -55,6 +55,9 @@ public:
void registerContactDelegate(PhysicsContactDelegate* delegate); void registerContactDelegate(PhysicsContactDelegate* delegate);
inline Point getGravity() { return _gravity; }
inline void setGravity(Point gravity) { _gravity = gravity; }
protected: protected:
static PhysicsWorld* create(); static PhysicsWorld* create();
bool init(); bool init();
@ -78,4 +81,4 @@ NS_CC_END
#endif // __CCPHYSICS_WORLD_H__ #endif // __CCPHYSICS_WORLD_H__
#endif // CC_USE_PHYSICS_ENGINE #endif // CC_USE_PHYSICS