mirror of https://github.com/axmolengine/axmol.git
issue #2771: Change CC_USE_PHYSICS_ENGINE to CC_USE_PHYSICS. Add some implements.
This commit is contained in:
parent
bd10d92460
commit
bcab90ddc4
|
@ -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
|
||||
|
|
|
@ -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<PhysicsJoint*>* 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<PhysicsJoint*> _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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue