Merge pull request #6288 from boyu0/bug4771_move_physicsworld_2_layer

closed #4771: Move PhysicsWorld to Layer
This commit is contained in:
James Chen 2014-04-25 11:52:32 +08:00
commit cd6fe294b8
19 changed files with 499 additions and 384 deletions

View File

@ -48,6 +48,10 @@ THE SOFTWARE.
#include "renderer/CCRenderer.h"
#include "deprecated/CCString.h"
#if CC_USE_PHYSICS
#include "physics/CCPhysicsBody.h"
#endif
NS_CC_BEGIN
// Layer
@ -60,6 +64,9 @@ Layer::Layer()
, _accelerationListener(nullptr)
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
, _swallowsTouches(true)
#if CC_USE_PHYSICS
, _physicsWorld(nullptr)
#endif
{
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Point(0.5f, 0.5f));
@ -67,7 +74,9 @@ Layer::Layer()
Layer::~Layer()
{
#if CC_USE_PHYSICS
CC_SAFE_DELETE(_physicsWorld);
#endif
}
bool Layer::init()
@ -431,6 +440,93 @@ std::string Layer::getDescription() const
return StringUtils::format("<Layer | Tag = %d>", _tag);
}
#if CC_USE_PHYSICS
void Layer::onEnter()
{
Node::onEnter();
if (_physicsWorld != nullptr)
{
this->schedule(schedule_selector(Layer::updatePhysics));
}
}
void Layer::onExit()
{
Node::onExit();
if (_physicsWorld != nullptr)
{
this->unschedule(schedule_selector(Layer::updatePhysics));
}
}
void Layer::updatePhysics(float delta)
{
if (nullptr != _physicsWorld)
{
_physicsWorld->update(delta);
}
}
Layer* Layer::createWithPhysics()
{
Layer *ret = new Layer();
if (ret && ret->initWithPhysics())
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
bool Layer::initWithPhysics()
{
bool ret = false;
do
{
Director * director;
CC_BREAK_IF( ! (director = Director::getInstance()) );
this->setContentSize(director->getWinSize());
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
// success
ret = true;
} while (0);
return ret;
}
void Layer::addChildToPhysicsWorld(Node* child)
{
if (_physicsWorld)
{
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
{
if (node->getPhysicsBody())
{
_physicsWorld->addBody(node->getPhysicsBody());
node->updatePhysicsBodyPosition(this);
node->updatePhysicsBodyRotation(this);
}
auto& children = node->getChildren();
for( const auto &n : children) {
addToPhysicsWorldFunc(n);
}
};
addToPhysicsWorldFunc(child);
}
}
#endif
__LayerRGBA::__LayerRGBA()
{
CCLOG("LayerRGBA deprecated.");

View File

@ -38,6 +38,8 @@ THE SOFTWARE.
#include "CCEventKeyboard.h"
#include "renderer/CCCustomCommand.h"
#include "CCPhysicsWorld.h"
NS_CC_BEGIN
/**
@ -169,6 +171,27 @@ CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
#if CC_USE_PHYSICS
public:
virtual void updatePhysics(float delta);
virtual void onEnter() override;
virtual void onExit() override;
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
static Layer *createWithPhysics();
CC_CONSTRUCTOR_ACCESS:
bool initWithPhysics();
protected:
void addChildToPhysicsWorld(Node* child);
PhysicsWorld* _physicsWorld;
friend class Node;
friend class ProtectedNode;
#endif // CC_USE_PHYSICS
protected:
//add the api for avoid use deprecated api
void _addTouchListener();

View File

@ -43,7 +43,7 @@ THE SOFTWARE.
#include "CCEventDispatcher.h"
#include "CCEvent.h"
#include "CCEventTouch.h"
#include "CCScene.h"
#include "CCLayer.h"
#if CC_USE_PHYSICS
#include "CCPhysicsBody.h"
@ -269,9 +269,10 @@ void Node::setRotation(float rotation)
_transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody)
if (_physicsBody && !_physicsBody->_rotationResetTag)
{
_physicsBody->setRotation(rotation);
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyRotation(layer);
}
#endif
}
@ -299,7 +300,8 @@ void Node::setRotation3D(const Vertex3F& rotation)
#if CC_USE_PHYSICS
if (_physicsBody)
{
_physicsBody->setRotation(_rotationZ_X);
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyRotation(layer);
}
#endif
}
@ -428,11 +430,10 @@ void Node::setPosition(const Point& position)
_transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody)
if (_physicsBody != nullptr && !_physicsBody->_positionResetTag)
{
Node* parent = getParent();
Point pos = parent != nullptr ? parent->convertToWorldSpace(getPosition()) : getPosition();
_physicsBody->setPosition(pos);
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyPosition(layer);
}
#endif
}
@ -727,27 +728,24 @@ void Node::addChild(Node *child, int zOrder, int tag)
}
this->insertChild(child, zOrder);
#if CC_USE_PHYSICS
if (child->getPhysicsBody() != nullptr)
{
child->getPhysicsBody()->setPosition(this->convertToWorldSpace(child->getPosition()));
}
for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
{
if (dynamic_cast<Scene*>(node) != nullptr)
{
(dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
break;
}
}
#endif
child->_tag = tag;
child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);
#if CC_USE_PHYSICS
// Recursive add children with which have physics body.
for (Node* node = this; node != nullptr; node = node->getParent())
{
Layer* layer = dynamic_cast<Layer*>(node);
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
layer->addChildToPhysicsWorld(child);
break;
}
}
#endif
if( _running )
{
@ -1587,6 +1585,43 @@ void Node::removeAllComponents()
}
#if CC_USE_PHYSICS
void Node::updatePhysicsBodyPosition(Layer* layer)
{
if (_physicsBody != nullptr)
{
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
Point pos = getParent() == layer ? getPosition() : layer->convertToNodeSpace(_parent->convertToWorldSpace(getPosition()));
_physicsBody->setPosition(pos);
}
else
{
_physicsBody->setPosition(getPosition());
}
}
}
void Node::updatePhysicsBodyRotation(Layer* layer)
{
if (_physicsBody != nullptr)
{
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
float rotation = _rotationZ_X;
for (Node* parent = _parent; parent != layer; parent = parent->getParent())
{
rotation += parent->getRotation();
}
_physicsBody->setRotation(rotation);
}
else
{
_physicsBody->setRotation(_rotationZ_X);
}
}
}
void Node::setPhysicsBody(PhysicsBody* body)
{
if (body != nullptr)
@ -1617,12 +1652,23 @@ void Node::setPhysicsBody(PhysicsBody* body)
}
_physicsBody = body;
if (body != nullptr)
{
Node* parent = getParent();
Point pos = parent != nullptr ? parent->convertToWorldSpace(getPosition()) : getPosition();
_physicsBody->setPosition(pos);
_physicsBody->setRotation(getRotation());
Node* node;
Layer* layer = nullptr;
for (node = this->getParent(); node != nullptr; node = node->getParent())
{
Layer* tmpLayer = dynamic_cast<Layer*>(node);
if (tmpLayer != nullptr && tmpLayer->getPhysicsWorld() != nullptr)
{
layer = tmpLayer;
break;
}
}
updatePhysicsBodyPosition(layer);
updatePhysicsBodyRotation(layer);
}
}

View File

@ -1364,6 +1364,11 @@ protected:
virtual void updateCascadeColor();
virtual void disableCascadeColor();
virtual void updateColor() {}
#if CC_USE_PHYSICS
virtual void updatePhysicsBodyPosition(Layer* layer);
virtual void updatePhysicsBodyRotation(Layer* layer);
#endif // CC_USE_PHYSICS
float _rotationX; ///< rotation on the X-axis
float _rotationY; ///< rotation on the Y-axis
@ -1455,6 +1460,10 @@ protected:
private:
CC_DISALLOW_COPY_AND_ASSIGN(Node);
#if CC_USE_PHYSICS
friend class Layer;
#endif //CC_USTPS
};
// NodeRGBA

View File

@ -36,9 +36,6 @@ THE SOFTWARE.
NS_CC_BEGIN
Scene::Scene()
#if CC_USE_PHYSICS
: _physicsWorld(nullptr)
#endif
{
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Point(0.5f, 0.5f));
@ -46,9 +43,6 @@ Scene::Scene()
Scene::~Scene()
{
#if CC_USE_PHYSICS
CC_SAFE_DELETE(_physicsWorld);
#endif
}
bool Scene::init()
@ -90,75 +84,4 @@ Scene* Scene::getScene()
return this;
}
#if CC_USE_PHYSICS
void Scene::addChild(Node* child, int zOrder, int tag)
{
Node::addChild(child, zOrder, tag);
addChildToPhysicsWorld(child);
}
void Scene::update(float delta)
{
Node::update(delta);
if (nullptr != _physicsWorld)
{
_physicsWorld->update(delta);
}
}
Scene *Scene::createWithPhysics()
{
Scene *ret = new Scene();
if (ret && ret->initWithPhysics())
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
bool Scene::initWithPhysics()
{
bool ret = false;
do
{
Director * director;
CC_BREAK_IF( ! (director = Director::getInstance()) );
this->setContentSize(director->getWinSize());
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
this->scheduleUpdate();
// success
ret = true;
} while (0);
return ret;
}
void Scene::addChildToPhysicsWorld(Node* child)
{
if (_physicsWorld)
{
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
{
if (node->getPhysicsBody())
{
_physicsWorld->addBody(node->getPhysicsBody());
}
auto& children = node->getChildren();
for( const auto &n : children) {
addToPhysicsWorldFunc(n);
}
};
addToPhysicsWorldFunc(child);
}
}
#endif
NS_CC_END

View File

@ -67,28 +67,10 @@ CC_CONSTRUCTOR_ACCESS:
virtual bool init() override;
protected:
friend class Node;
friend class ProtectedNode;
friend class SpriteBatchNode;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Scene);
#if CC_USE_PHYSICS
public:
virtual void addChild(Node* child, int zOrder, int tag) override;
virtual void update(float delta) override;
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
static Scene *createWithPhysics();
CC_CONSTRUCTOR_ACCESS:
bool initWithPhysics();
protected:
void addChildToPhysicsWorld(Node* child);
PhysicsWorld* _physicsWorld;
#endif // CC_USE_PHYSICS
};
// end of scene group

View File

@ -30,7 +30,7 @@
#include "chipmunk.h"
#include "CCNode.h"
#include "CCLayer.h"
#include "CCPhysicsShape.h"
#include "CCPhysicsJoint.h"
@ -347,18 +347,12 @@ void PhysicsBody::setGravityEnable(bool enable)
void PhysicsBody::setPosition(Point position)
{
if (!_positionResetTag)
{
cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset));
}
cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset));
}
void PhysicsBody::setRotation(float rotation)
{
if (!_rotationResetTag)
{
cpBodySetAngle(_info->getBody(), -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
}
cpBodySetAngle(_info->getBody(), -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
}
Point PhysicsBody::getPosition() const
@ -770,12 +764,19 @@ void PhysicsBody::update(float delta)
if (_node != nullptr)
{
Node* parent = _node->getParent();
Layer* layer = &_world->getLayer();
Point position = parent != layer ? parent->convertToNodeSpace(layer->convertToWorldSpace(getPosition())) : getPosition();
float rotation = getRotation();
for (; parent != layer; parent = parent->getParent())
{
rotation -= parent->getRotation();
}
Point position = parent != nullptr ? parent->convertToNodeSpace(getPosition()) : getPosition();
_positionResetTag = true;
_rotationResetTag = true;
_node->setPosition(position);
_node->setRotation(getRotation());
_node->setRotation(rotation);
_positionResetTag = false;
_rotationResetTag = false;

View File

@ -349,6 +349,7 @@ protected:
friend class PhysicsShape;
friend class PhysicsJoint;
friend class Node;
friend class Layer;
friend class ProtectedNode;
};

View File

@ -43,7 +43,7 @@
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
#include "CCDrawNode.h"
#include "CCScene.h"
#include "CCLayer.h"
#include "CCDirector.h"
#include "CCEventDispatcher.h"
#include "CCEventCustom.h"
@ -289,7 +289,7 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
{
contact.setEventCode(PhysicsContact::EventCode::BEGIN);
contact.setWorld(this);
_scene->getEventDispatcher()->dispatchEvent(&contact);
_layer->getEventDispatcher()->dispatchEvent(&contact);
}
return ret ? contact.resetResult() : false;
@ -305,7 +305,7 @@ int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact)
contact.setEventCode(PhysicsContact::EventCode::PRESOLVE);
contact.setWorld(this);
_scene->getEventDispatcher()->dispatchEvent(&contact);
_layer->getEventDispatcher()->dispatchEvent(&contact);
return contact.resetResult();
}
@ -319,7 +319,7 @@ void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact)
contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE);
contact.setWorld(this);
_scene->getEventDispatcher()->dispatchEvent(&contact);
_layer->getEventDispatcher()->dispatchEvent(&contact);
}
void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
@ -331,7 +331,7 @@ void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
contact.setEventCode(PhysicsContact::EventCode::SEPERATE);
contact.setWorld(this);
_scene->getEventDispatcher()->dispatchEvent(&contact);
_layer->getEventDispatcher()->dispatchEvent(&contact);
}
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data)
@ -416,10 +416,10 @@ PhysicsShape* PhysicsWorld::getShape(const Point& point) const
return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape();
}
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
PhysicsWorld* PhysicsWorld::construct(Layer& layer)
{
PhysicsWorld * world = new PhysicsWorld();
if(world && world->init(scene))
if(world && world->init(layer))
{
return world;
}
@ -428,14 +428,14 @@ PhysicsWorld* PhysicsWorld::construct(Scene& scene)
return nullptr;
}
bool PhysicsWorld::init(Scene& scene)
bool PhysicsWorld::init(Layer& layer)
{
do
{
_info = new PhysicsWorldInfo();
CC_BREAK_IF(_info == nullptr);
_scene = &scene;
_layer = &layer;
_info->setGravity(_gravity);
@ -867,8 +867,7 @@ void PhysicsWorld::setGravity(const Vect& gravity)
// reset gravity for body
if (!body->isGravityEnabled())
{
body->applyForce(_gravity * body->getMass());
body->applyForce(- gravity * body->getMass());
body->applyForce((_gravity - gravity) * body->getMass());
}
}
}
@ -912,7 +911,7 @@ PhysicsWorld::PhysicsWorld()
, _updateRateCount(0)
, _updateTime(0.0f)
, _info(nullptr)
, _scene(nullptr)
, _layer(nullptr)
, _delayDirty(false)
, _debugDraw(nullptr)
, _debugDrawMask(DEBUGDRAW_NONE)
@ -933,7 +932,7 @@ PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world)
, _world(world)
{
_drawNode = DrawNode::create();
_world.getScene().addChild(_drawNode);
_world.getLayer().addChild(_drawNode);
}
PhysicsDebugDraw::~PhysicsDebugDraw()

View File

@ -46,7 +46,7 @@ typedef Point Vect;
class Node;
class Sprite;
class Scene;
class Layer;
class DrawNode;
class PhysicsDebugDraw;
@ -119,8 +119,8 @@ public:
/** Get body by tag */
PhysicsBody* getBody(int tag) const;
/** Get scene contain this physics world */
inline Scene& getScene() const { return *_scene; }
/** Get layer contain this physics world */
inline Layer& getLayer() const { return *_layer; }
/** get the gravity value */
inline Vect getGravity() const { return _gravity; }
/** set the gravity value */
@ -144,8 +144,8 @@ public:
inline int getDebugDrawMask() { return _debugDrawMask; }
protected:
static PhysicsWorld* construct(Scene& scene);
bool init(Scene& scene);
static PhysicsWorld* construct(Layer& layer);
bool init(Layer& layer);
virtual void addBody(PhysicsBody* body);
virtual void addShape(PhysicsShape* shape);
@ -180,7 +180,7 @@ protected:
Vector<PhysicsBody*> _bodies;
std::list<PhysicsJoint*> _joints;
Scene* _scene;
Layer* _layer;
bool _delayDirty;
PhysicsDebugDraw* _debugDraw;
@ -198,7 +198,7 @@ protected:
friend class Node;
friend class Sprite;
friend class Scene;
friend class Layer;
friend class PhysicsBody;
friend class PhysicsShape;
friend class PhysicsJoint;

View File

@ -74,7 +74,7 @@ tolua_lerror:
return 0;
}
int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
int lua_cocos2dx_physics_PhysicsWorld_getLayer(lua_State* tolua_S)
{
int argc = 0;
cocos2d::PhysicsWorld* cobj = nullptr;
@ -93,7 +93,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'", NULL);
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_getLayer'", NULL);
return 0;
}
#endif
@ -103,7 +103,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
{
if(!ok)
return 0;
cocos2d::Scene& ret = cobj->getScene();
cocos2d::Layer& ret = cobj->getLayer();
do {
std::string hashName = typeid(ret).name();
@ -112,7 +112,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
if(iter != g_luaType.end()){
className = iter->second.c_str();
} else {
className = "cc.Scene";
className = "cc.Layer";
}
int ID = (int)(ret._ID);
@ -122,12 +122,12 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
}while (0);
return 1;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getScene",argc, 0);
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getLayer",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'.",&tolua_err);
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_getLayer'.",&tolua_err);
#endif
return 0;
@ -1573,8 +1573,8 @@ int register_all_cocos2dx_physics_manual(lua_State* tolua_S)
lua_rawget(tolua_S, LUA_REGISTRYINDEX);
if (lua_istable(tolua_S,-1))
{
lua_pushstring(tolua_S,"getScene");
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_getScene );
lua_pushstring(tolua_S,"getLayer");
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_getLayer );
lua_rawset(tolua_S,-3);
lua_pushstring(tolua_S,"queryPoint");
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_queryPoint );

View File

@ -33,7 +33,7 @@
#if CC_USE_PHYSICS
#include "CCPhysicsBody.h"
#endif
#include "CCScene.h"
#include "CCLayer.h"
NS_CC_BEGIN
@ -95,27 +95,24 @@ void ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag)
this->insertProtectedChild(child, zOrder);
#if CC_USE_PHYSICS
if (child->getPhysicsBody() != nullptr)
{
child->getPhysicsBody()->setPosition(this->convertToWorldSpace(child->getPosition()));
}
for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
{
if (dynamic_cast<Scene*>(node) != nullptr)
{
(dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
break;
}
}
#endif
child->setTag(tag);
child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);
#if CC_USE_PHYSICS
// Recursive add children with which have physics body.
for (Node* node = this; node != nullptr; node = node->getParent())
{
Layer* layer = dynamic_cast<Layer*>(node);
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
layer->addChildToPhysicsWorld(child);
break;
}
}
#endif
if( _running )
{
child->onEnter();

View File

@ -3,23 +3,27 @@
#include "../testResource.h"
USING_NS_CC;
#if CC_USE_PHYSICS
#define CLPHYSICS(__className__) [](){ return __className__::createWithPhysics();}
#endif
namespace
{
static std::function<Layer*()> createFunctions[] = {
#if CC_USE_PHYSICS
CL(PhysicsDemoLogoSmash),
CL(PhysicsDemoPyramidStack),
CL(PhysicsDemoClickAdd),
CL(PhysicsDemoRayCast),
CL(PhysicsDemoJoints),
CL(PhysicsDemoActions),
CL(PhysicsDemoPump),
CL(PhysicsDemoOneWayPlatform),
CL(PhysicsDemoSlice),
CL(PhysicsDemoBug3988),
CL(PhysicsContactTest),
CLPHYSICS(PhysicsDemoLogoSmash),
CLPHYSICS(PhysicsDemoPyramidStack),
CLPHYSICS(PhysicsDemoClickAdd),
CLPHYSICS(PhysicsDemoRayCast),
CLPHYSICS(PhysicsDemoJoints),
CLPHYSICS(PhysicsDemoActions),
CLPHYSICS(PhysicsDemoPump),
CLPHYSICS(PhysicsDemoOneWayPlatform),
CLPHYSICS(PhysicsDemoSlice),
CLPHYSICS(PhysicsDemoBug3988),
CLPHYSICS(PhysicsContactTest),
CL(PhysicsPositionRotationTest),
CL(PhysicsSetGravityEnableTest),
CLPHYSICS(PhysicsSetGravityEnableTest),
#else
CL(PhysicsDemoDisabled),
#endif
@ -59,12 +63,7 @@ namespace
}
PhysicsTestScene::PhysicsTestScene()
#if CC_USE_PHYSICS
: TestScene(false, true)
#else
: TestScene()
#endif
, _debugDraw(false)
{}
void PhysicsTestScene::runThisTest()
@ -75,14 +74,6 @@ void PhysicsTestScene::runThisTest()
Director::getInstance()->replaceScene(this);
}
void PhysicsTestScene::toggleDebug()
{
#if CC_USE_PHYSICS
_debugDraw = !_debugDraw;
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
#endif
}
#if CC_USE_PHYSICS == 0
void PhysicsDemoDisabled::onEnter()
{
@ -97,9 +88,10 @@ void PhysicsDemoDisabled::onEnter()
#else
PhysicsDemo::PhysicsDemo()
: _scene(nullptr)
, _spriteTexture(nullptr)
: _spriteTexture(nullptr)
, _ball(nullptr)
, _debugDraw(false)
, _isRoot(true)
{
}
@ -108,6 +100,12 @@ PhysicsDemo::~PhysicsDemo()
}
void PhysicsDemo::toggleDebug()
{
_debugDraw = !_debugDraw;
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
}
std::string PhysicsDemo::title() const
{
return "PhysicsTest";
@ -144,19 +142,26 @@ void PhysicsDemo::backCallback(Ref* sender)
void PhysicsDemo::onEnter()
{
BaseTest::onEnter();
_scene = dynamic_cast<PhysicsTestScene*>(this->getParent());
if (_isRoot)
{
BaseTest::onEnter();
}
else
{
Layer::onEnter();
}
_spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture();
// menu for debug layer
MenuItemFont::setFontSize(18);
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
auto menu = Menu::create(item, NULL);
this->addChild(menu);
menu->setPosition(Point(VisibleRect::right().x-50, VisibleRect::top().y-10));
if (this->_physicsWorld != nullptr)
{
// menu for debug layer
MenuItemFont::setFontSize(18);
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
auto menu = Menu::create(item, nullptr);
this->addChild(menu);
menu->setPosition(Point(getContentSize().width-50, getContentSize().height/2));
}
}
Sprite* PhysicsDemo::addGrossiniAtPosition(Point p, float scale/* = 1.0*/)
@ -183,10 +188,7 @@ Sprite* PhysicsDemo::addGrossiniAtPosition(Point p, float scale/* = 1.0*/)
void PhysicsDemo::toggleDebugCallback(Ref* sender)
{
if (_scene != nullptr)
{
_scene->toggleDebug();
}
toggleDebug();
}
PhysicsDemoClickAdd::~PhysicsDemoClickAdd()
@ -246,10 +248,7 @@ void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event)
auto v = Point( accelX, accelY);
v = v * 200;
if(_scene != nullptr)
{
_scene->getPhysicsWorld()->setGravity(v);
}
getPhysicsWorld()->setGravity(v);
}
namespace
@ -381,8 +380,8 @@ Sprite* PhysicsDemo::makeTriangle(Point point, Size size, int color, PhysicsMate
bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
{
auto location = touch->getLocation();
auto arr = _scene->getPhysicsWorld()->getShapes(location);
auto location = this->convertTouchToNodeSpace(touch);
auto arr = getPhysicsWorld()->getShapes(location);
PhysicsBody* body = nullptr;
for (auto& obj : arr)
@ -403,7 +402,7 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
this->addChild(mouse);
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
joint->setMaxForce(5000.0f * body->getMass());
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
return true;
@ -418,7 +417,7 @@ void PhysicsDemo::onTouchMoved(Touch* touch, Event* event)
if (it != _mouses.end())
{
it->second->setPosition(touch->getLocation());
it->second->setPosition(this->convertTouchToNodeSpace(touch));
}
}
@ -438,8 +437,8 @@ void PhysicsDemoLogoSmash::onEnter()
{
PhysicsDemo::onEnter();
_scene->getPhysicsWorld()->setGravity(Point(0, 0));
_scene->getPhysicsWorld()->setUpdateRate(5.0f);
getPhysicsWorld()->setGravity(Point(0, 0));
getPhysicsWorld()->setUpdateRate(5.0f);
_ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0]));
addChild(_ball);
@ -465,7 +464,6 @@ void PhysicsDemoLogoSmash::onEnter()
}
}
auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
bullet->getPhysicsBody()->setVelocity(Point(200, 0));
@ -527,7 +525,7 @@ std::string PhysicsDemoPyramidStack::title() const
}
PhysicsDemoRayCast::PhysicsDemoRayCast()
: _angle(0.0f)
: _angle(0.0f)
, _node(nullptr)
, _mode(0)
{}
@ -539,8 +537,9 @@ void PhysicsDemoRayCast::onEnter()
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
scheduleUpdate();
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
getPhysicsWorld()->setGravity(Point::ZERO);
auto node = DrawNode::create();
node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Point(0, 50), VisibleRect::rightBottom() + Point(0, 50)));
@ -553,8 +552,6 @@ void PhysicsDemoRayCast::onEnter()
auto menu = Menu::create(item, NULL);
this->addChild(menu);
menu->setPosition(Point(VisibleRect::left().x+100, VisibleRect::top().y-10));
scheduleUpdate();
}
void PhysicsDemoRayCast::changeModeCallback(Ref* sender)
@ -600,7 +597,7 @@ void PhysicsDemoRayCast::update(float delta)
Point point3 = point2;
auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this);
_scene->getPhysicsWorld()->rayCast(func, point1, point2, &point3);
getPhysicsWorld()->rayCast(func, point1, point2, &point3);
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
if (point2 != point3)
@ -626,7 +623,7 @@ void PhysicsDemoRayCast::update(float delta)
return true;
};
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
if (point2 != point3)
@ -653,7 +650,7 @@ void PhysicsDemoRayCast::update(float delta)
return true;
};
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
_node->drawSegment(point1, point2, 1, STATIC_COLOR);
@ -705,7 +702,7 @@ std::string PhysicsDemoRayCast::title() const
void PhysicsDemoJoints::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
toggleDebug();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this);
@ -713,8 +710,6 @@ void PhysicsDemoJoints::onEnter()
listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
//_scene->getPhysicsWorld()->setGravity(Point::ZERO);
float width = (VisibleRect::getVisibleRect().size.width - 10) / 4;
float height = (VisibleRect::getVisibleRect().size.height - 50) / 4;
@ -742,7 +737,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -757,7 +752,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -772,7 +767,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -786,7 +781,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 30.0f, 60.0f);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -800,7 +795,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 500.0f, 0.3f);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -814,7 +809,7 @@ void PhysicsDemoJoints::onEnter()
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point(30, 15), Point(30, -15), Point(-30, 0));
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -827,10 +822,10 @@ void PhysicsDemoJoints::onEnter()
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 3000.0f, 60.0f);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -843,10 +838,10 @@ void PhysicsDemoJoints::onEnter()
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f,(float) M_PI_2);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -859,10 +854,10 @@ void PhysicsDemoJoints::onEnter()
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, (float)M_PI_2);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -875,10 +870,10 @@ void PhysicsDemoJoints::onEnter()
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
PhysicsJointGear* joint = PhysicsJointGear::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, 2.0f);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -891,10 +886,10 @@ void PhysicsDemoJoints::onEnter()
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), (float)M_PI_2);
_scene->getPhysicsWorld()->addJoint(joint);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
@ -953,7 +948,7 @@ std::string PhysicsDemoActions::title() const
void PhysicsDemoPump::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
toggleDebug();
_distance = 0.0f;
_rotationV = 0.0f;
@ -998,7 +993,7 @@ void PhysicsDemoPump::onEnter()
VisibleRect::leftBottom() + Point(102, 20)
};
auto _world = _scene->getPhysicsWorld();
auto _world = getPhysicsWorld();
// small gear
auto sgear = Node::create();
@ -1054,7 +1049,7 @@ void PhysicsDemoPump::onEnter()
void PhysicsDemoPump::update(float delta)
{
for (const auto& body : _scene->getPhysicsWorld()->getAllBodies())
for (const auto& body : getPhysicsWorld()->getAllBodies())
{
if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f)
{
@ -1063,7 +1058,7 @@ void PhysicsDemoPump::update(float delta)
}
}
PhysicsBody* gear = _scene->getPhysicsWorld()->getBody(1);
PhysicsBody* gear = getPhysicsWorld()->getBody(1);
if (gear != nullptr)
{
@ -1157,7 +1152,7 @@ std::string PhysicsDemoOneWayPlatform::title() const
void PhysicsDemoSlice::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
toggleDebug();
_sliceTag = 1;
@ -1245,7 +1240,7 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Point normal, float
void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event)
{
auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this);
_scene->getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
}
std::string PhysicsDemoSlice::title() const
@ -1262,8 +1257,8 @@ std::string PhysicsDemoSlice::subtitle() const
void PhysicsDemoBug3988::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
toggleDebug();
getPhysicsWorld()->setGravity(Vect::ZERO);
auto ball = Sprite::create("Images/YellowSquare.png");
ball->setPosition(VisibleRect::center() - Point(100, 0));
@ -1288,7 +1283,7 @@ std::string PhysicsDemoBug3988::subtitle() const
void PhysicsContactTest::onEnter()
{
PhysicsDemo::onEnter();
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
getPhysicsWorld()->setGravity(Vect::ZERO);
auto s = VisibleRect::getVisibleRect().size;
_yellowBoxNum = 50;
@ -1550,36 +1545,51 @@ std::string PhysicsContactTest::subtitle() const
void PhysicsPositionRotationTest::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
PhysicsDemo* layers[2] = {PhysicsDemo::createWithPhysics(), PhysicsDemo::createWithPhysics()};
Size halfSize = VisibleRect::getVisibleRect().size;
halfSize.width /= 2;
halfSize.height -= 40;
auto wall = Node::create();
wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
wall->setPosition(VisibleRect::center());
addChild(wall);
for (int i = 0; i < sizeof(layers) / sizeof(layers[0]); ++i)
{
layers[i]->ignoreAnchorPointForPosition(false);
layers[i]->setContentSize(halfSize);
layers[i]->setRoot(false);
layers[i]->getPhysicsWorld()->setGravity(Point::ZERO);
layers[i]->toggleDebug();
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, layers[i]);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, layers[i]);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, layers[i]);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, layers[i]);
auto wall = Node::create();
wall->setPhysicsBody(PhysicsBody::createEdgeBox(layers[i]->getContentSize(), PhysicsMaterial(0.1f, 1, 0.0f)));
wall->setPosition(Point(halfSize/2));
layers[i]->addChild(wall);
this->addChild(layers[i]);
}
layers[0]->setPosition(Point(halfSize.width/2, halfSize.height/2 + 60));
layers[1]->setPosition(Point(halfSize.width/2*3, halfSize.height/2 + 60));
// anchor test
auto anchorNode = Sprite::create("Images/YellowSquare.png");
anchorNode->setAnchorPoint(Point(0.1f, 0.9f));
anchorNode->setPosition(100, 100);
anchorNode->setPosition(100, 50);
anchorNode->setScale(0.25);
anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale()));
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(anchorNode);
layers[0]->addChild(anchorNode);
//parent test
auto parent = Sprite::create("Images/YellowSquare.png");
parent->setPosition(200, 100);
parent->setPosition(160, 50);
parent->setScale(0.25);
parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale()));
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(parent);
layers[0]->addChild(parent);
auto leftBall = Sprite::create("Images/ball.png");
leftBall->setPosition(-30, 0);
@ -1590,12 +1600,37 @@ void PhysicsPositionRotationTest::onEnter()
// offset position rotation test
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
offsetPosNode->setPosition(100, 200);
offsetPosNode->setPosition(100, 100);
offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2));
offsetPosNode->getPhysicsBody()->setPositionOffset(-Point(offsetPosNode->getContentSize()/2));
offsetPosNode->getPhysicsBody()->setRotationOffset(45);
offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(offsetPosNode);
layers[0]->addChild(offsetPosNode);
for (int i = 0; i < 30; ++i)
{
Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10);
Point position = Point(halfSize.width, halfSize.height) - Point(size.width, size.height);
position.x = position.x * CCRANDOM_0_1();
position.y = position.y * CCRANDOM_0_1();
position = position + Point(size.width/2, size.height/2);
Vect velocity((CCRANDOM_0_1() - 0.5)*200, (CCRANDOM_0_1() - 0.5)*200);
auto box = makeBox(position, size, 0, PhysicsMaterial(0.1f, 1, 0.0f));
box->getPhysicsBody()->setVelocity(velocity);
box->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
layers[1]->addChild(box);
}
MoveTo* moveTo = MoveTo::create(1.0f, Point(halfSize.width/2, halfSize.height/2 + 60));
MoveTo* moveBack = MoveTo::create(1.0f, Point(halfSize.width/2*3, halfSize.height/2 + 60));
ScaleTo* thrink = ScaleTo::create(1.0f, 0.5f);
ScaleTo* amplify = ScaleTo::create(1.0f, 2.0f);
ScaleTo* scaleBack = ScaleTo::create(1.0f, 1.0f);
RotateTo* rotateTo = RotateTo::create(1.0f, 180.0f);
RotateTo* rotateBack = RotateTo::create(1.0f, 360.0f);
layers[1]->runAction(Sequence::create(moveTo, thrink, rotateTo, amplify, rotateBack, scaleBack, moveBack, nullptr));
return;
}
@ -1605,6 +1640,11 @@ std::string PhysicsPositionRotationTest::title() const
return "Position/Rotation Test";
}
std::string PhysicsPositionRotationTest::subtitle() const
{
return "Two Physics Worlds";
}
void PhysicsSetGravityEnableTest::onEnter()
{
@ -1647,7 +1687,7 @@ void PhysicsSetGravityEnableTest::onScheduleOnce(float delta)
auto ball = getChildByTag(2);
ball->getPhysicsBody()->setMass(200);
_scene->getPhysicsWorld()->setGravity(Vect(0, 98));
getPhysicsWorld()->setGravity(Vect(0, 98));
}
std::string PhysicsSetGravityEnableTest::title() const

View File

@ -15,11 +15,6 @@ public:
public:
virtual void runThisTest();
void toggleDebug();
private:
bool _debugDraw;
};
#if CC_USE_PHYSICS == 0
@ -32,11 +27,26 @@ public:
};
#else
#define CREATE_WITH_PHYSICS_FUNC(__TYPE__) \
static __TYPE__* createWithPhysics() \
{ \
__TYPE__ *ret = new __TYPE__(); \
if (ret && ret->initWithPhysics()) \
{ \
ret->autorelease(); \
return ret; \
} \
else \
{ \
delete ret; \
ret = nullptr; \
return nullptr; \
} \
}
class PhysicsDemo : public BaseTest
{
public:
CREATE_FUNC(PhysicsDemo);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemo);
PhysicsDemo();
virtual ~PhysicsDemo();
@ -58,17 +68,23 @@ public:
void onTouchMoved(Touch* touch, Event* event);
void onTouchEnded(Touch* touch, Event* event);
void toggleDebug();
inline void setRoot(bool isRoot) { _isRoot = isRoot; }
protected:
PhysicsTestScene* _scene;
Texture2D* _spriteTexture; // weak ref
SpriteBatchNode* _ball;
std::unordered_map<int, Node*> _mouses;
private:
bool _debugDraw;
bool _isRoot;
};
class PhysicsDemoClickAdd : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoClickAdd);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoClickAdd);
virtual ~PhysicsDemoClickAdd();
void onEnter() override;
@ -81,7 +97,7 @@ public:
class PhysicsDemoLogoSmash : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoLogoSmash);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoLogoSmash);
void onEnter() override;
virtual std::string title() const override;
@ -90,7 +106,7 @@ public:
class PhysicsDemoPyramidStack : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoPyramidStack);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPyramidStack);
void onEnter() override;
void updateOnce(float delta);
@ -100,7 +116,7 @@ public:
class PhysicsDemoRayCast : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoRayCast);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoRayCast);
PhysicsDemoRayCast();
@ -122,7 +138,7 @@ private:
class PhysicsDemoJoints : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoJoints);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoJoints);
void onEnter() override;
virtual std::string title() const override;
@ -131,7 +147,7 @@ public:
class PhysicsDemoActions : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoActions);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoActions);
void onEnter() override;
virtual std::string title() const override;
@ -140,7 +156,7 @@ public:
class PhysicsDemoPump : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoPump);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPump);
void onEnter() override;
void update(float delta) override;
@ -159,7 +175,7 @@ private:
class PhysicsDemoOneWayPlatform : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoOneWayPlatform);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoOneWayPlatform);
void onEnter() override;
virtual std::string title() const override;
@ -170,7 +186,7 @@ public:
class PhysicsDemoSlice : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoSlice);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoSlice);
void onEnter() override;
virtual std::string title() const override;
@ -188,7 +204,7 @@ private:
class PhysicsDemoBug3988 : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsDemoBug3988);
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoBug3988);
void onEnter() override;
virtual std::string title() const override;
@ -198,7 +214,7 @@ public:
class PhysicsContactTest : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsContactTest);
CREATE_WITH_PHYSICS_FUNC(PhysicsContactTest);
void onEnter() override;
void resetTest();
@ -223,12 +239,13 @@ public:
void onEnter() override;
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
class PhysicsSetGravityEnableTest : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsSetGravityEnableTest);
CREATE_WITH_PHYSICS_FUNC(PhysicsSetGravityEnableTest);
void onEnter() override;
void onScheduleOnce(float delta);

View File

@ -3,20 +3,9 @@
#include "extensions/cocos-ext.h"
#include "cocostudio/CocoStudio.h"
TestScene::TestScene(bool bPortrait, bool physics/* = false*/)
TestScene::TestScene(bool bPortrait)
{
if (physics)
{
#if CC_USE_PHYSICS
TestScene::initWithPhysics();
#else
Scene::init();
#endif
}
else
{
Scene::init();
}
Scene::init();
}
void testScene_callback(Ref *sender )

View File

@ -9,7 +9,7 @@ USING_NS_CC;
class TestScene : public Scene
{
public:
TestScene(bool bPortrait = false, bool physics = false);
TestScene(bool bPortrait = false);
virtual void onEnter() override;
virtual void runThisTest() = 0;

View File

@ -106,7 +106,7 @@ local function initWithLayer(layer, callback)
local debug = false
local function toggleDebugCallback(sender)
debug = not debug
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
layer:getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
end
layer.toggleDebug = function(self) toggleDebugCallback(nil) end;
@ -144,7 +144,7 @@ end
local function onTouchBegan(touch, event)
local location = touch:getLocation()
local arr = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getShapes(location)
local arr = curLayer:getPhysicsWorld():getShapes(location)
local body
for _, obj in ipairs(arr) do
@ -162,7 +162,7 @@ local function onTouchBegan(touch, event)
curLayer:addChild(mouse);
local joint = cc.PhysicsJointPin:construct(mouse:getPhysicsBody(), body, location);
joint:setMaxForce(5000.0 * body:getMass());
cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
touch.mouse = mouse
return true;
@ -253,7 +253,7 @@ local function makeTriangle(point, size, color, material)
end
local function PhysicsDemoClickAdd()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local function onTouchEnded(touch, event)
local location = touch:getLocation();
@ -281,7 +281,7 @@ local function PhysicsDemoClickAdd()
end
local function PhysicsDemoLogoSmash()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local logo_width = 188.0
@ -327,8 +327,8 @@ local function PhysicsDemoLogoSmash()
return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1)
end
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setUpdateRate(5.0);
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
curLayer:getPhysicsWorld():setUpdateRate(5.0);
layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image);
layer:addChild(layer.ball);
@ -363,7 +363,7 @@ local function PhysicsDemoLogoSmash()
end
local function PhysicsDemoJoints()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
layer:toggleDebug();
@ -384,7 +384,6 @@ local function PhysicsDemoJoints()
node:setPosition(cc.p(0, 0));
layer:addChild(node);
local scene = cc.Director:getInstance():getRunningScene();
for i in range(0, 3) do
for j in range(0, 3) do
local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, VisibleRect:leftBottom().y + 50 + i * height + height/2);
@ -397,7 +396,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -408,7 +407,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -419,7 +418,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointDistance:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0));
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -430,7 +429,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 30.0, 60.0);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -441,7 +440,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointSpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 500.0, 0.3);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -452,7 +451,7 @@ local function PhysicsDemoJoints()
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local joint = cc.PhysicsJointGroove:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(30, 15), cc.p(30, -15), cc.p(-30, 0))
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -461,10 +460,10 @@ local function PhysicsDemoJoints()
sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG);
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -474,10 +473,10 @@ local function PhysicsDemoJoints()
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -487,10 +486,10 @@ local function PhysicsDemoJoints()
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -500,10 +499,10 @@ local function PhysicsDemoJoints()
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -513,10 +512,10 @@ local function PhysicsDemoJoints()
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2);
scene:getPhysicsWorld():addJoint(joint);
curLayer:getPhysicsWorld():addJoint(joint);
layer:addChild(sp1);
layer:addChild(sp2);
@ -531,7 +530,7 @@ local function PhysicsDemoJoints()
end
local function PhysicsDemoPyramidStack()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local touchListener = cc.EventListenerTouchOneByOne:create();
@ -569,7 +568,7 @@ local function PhysicsDemoPyramidStack()
end
local function PhysicsDemoRayCast()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local function onTouchEnded(touch, event)
@ -591,7 +590,7 @@ local function PhysicsDemoRayCast()
local eventDispatcher = layer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer);
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0));
curLayer:getPhysicsWorld():setGravity(cc.p(0,0));
local node = cc.DrawNode:create();
node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50)))
@ -636,7 +635,7 @@ local function PhysicsDemoRayCast()
return false
end
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
if point2.x ~= point3.x or point2.y ~= point3.y then
@ -654,7 +653,7 @@ local function PhysicsDemoRayCast()
return true;
end
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
if point2.x ~= point3.x or point2.y ~= point3.y then
@ -669,7 +668,7 @@ local function PhysicsDemoRayCast()
return true;
end
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
drawNode:drawSegment(point1, point2, 1, STATIC_COLOR);
for _, p in ipairs(points) do
@ -693,7 +692,7 @@ local function PhysicsDemoRayCast()
end
local function PhysicsDemoOneWayPlatform()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local touchListener = cc.EventListenerTouchOneByOne:create();
@ -734,7 +733,7 @@ local function PhysicsDemoOneWayPlatform()
end
local function PhysicsDemoActions()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
local touchListener = cc.EventListenerTouchOneByOne:create()
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
@ -773,7 +772,7 @@ local function PhysicsDemoActions()
end
local function PhysicsDemoPump()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
layer:toggleDebug();
@ -803,14 +802,14 @@ local function PhysicsDemoPump()
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer)
local function update()
for _, body in ipairs(cc.Director:getInstance():getRunningScene():getPhysicsWorld():getAllBodies()) do
for _, body in ipairs(curLayer:getPhysicsWorld():getAllBodies()) do
if body:getTag() == DRAG_BODYS_TAG and body:getPosition().y < 0.0 then
body:getNode():setPosition(cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y + math.random() * 90, 0));
body:setVelocity(cc.p(0, 0));
end
end
local gear = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getBody(1);
local gear = curLayer:getPhysicsWorld():getBody(1);
if gear then
if distance ~= 0.0 then
rotationV = rotationV + distance/2500.0;
@ -871,7 +870,7 @@ local function PhysicsDemoPump()
cc.p(VisibleRect:leftBottom().x + 102, VisibleRect:leftBottom().y + 20)
};
local world = cc.Director:getInstance():getRunningScene():getPhysicsWorld();
local world = curLayer:getPhysicsWorld();
-- small gear
local sgear = cc.Node:create();
@ -934,7 +933,7 @@ local function PhysicsDemoPump()
end
local function PhysicsDemoSlice()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
layer:toggleDebug()
local sliceTag = 1;
@ -994,7 +993,7 @@ local function PhysicsDemoSlice()
end
local function onTouchEnded(touch, event)
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation());
curLayer:getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation());
end
local touchListener = cc.EventListenerTouchOneByOne:create();
@ -1025,10 +1024,10 @@ end
local function PhysicsDemoBug3988()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
layer:toggleDebug();
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
local ball = cc.Sprite:create("Images/YellowSquare.png");
ball:setPosition(cc.p(VisibleRect:center().x-100, VisibleRect:center().y));
@ -1048,9 +1047,9 @@ local function PhysicsDemoBug3988()
end
local function PhysicsContactTest()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height);
layer.yellowBoxNum = 50;
@ -1291,11 +1290,11 @@ local function PhysicsContactTest()
end
local function PhysicsPositionRotationTest()
local layer = cc.Layer:create()
local layer = cc.Layer:createWithPhysics()
local function onEnter()
layer:toggleDebug()
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
local touchListener = cc.EventListenerTouchOneByOne:create()
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
@ -1351,10 +1350,9 @@ end
function PhysicsTest()
cclog("PhysicsTest")
local scene = cc.Scene:createWithPhysics()
local scene = cc.Scene:create()
Helper.usePhysics = true
Helper.createFunctionTable = {
PhysicsDemoLogoSmash,
PhysicsDemoPyramidStack,

View File

@ -31,7 +31,6 @@ end
-- back menu callback
local function MainMenuCallback()
Helper.usePhysics = false
local scene = cc.Scene:create()
scene:addChild(CreateTestMenu())
@ -84,12 +83,7 @@ function Helper.restartAction()
end
function Helper.newScene()
local scene
if Helper.usePhysics then
scene = cc.Scene:createWithPhysics()
else
scene = cc.Scene:create()
end
local scene = cc.Scene:create()
Helper.currentLayer = Helper.createFunctionTable[Helper.index]()
scene:addChild(Helper.currentLayer)
scene:addChild(CreateBackMenuItem())

View File

@ -42,7 +42,7 @@ skip = PhysicsBody::[getJoints createPolygon createEdgeChain createEdgePolygon],
PhysicsShapePolygon::[create calculateArea calculateMoment ^getPoints$],
PhysicsShapeEdgePolygon::[create ^getPoints$],
PhysicsShapeEdgeChain::[create ^getPoints$],
PhysicsWorld::[getScene queryPoint queryRect rayCast],
PhysicsWorld::[getLayer queryPoint queryRect rayCast],
PhysicsContact::[getData setData]