From c98f50026fc4d7b23f36e61afa457b805a3a3e7d Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 15 Apr 2014 03:21:18 +0800 Subject: [PATCH 1/3] issue $4771: move PhysicsWorld to layer. --- cocos/2d/CCLayer.cpp | 106 +++++++++++- cocos/2d/CCLayer.h | 26 +++ cocos/2d/CCNode.cpp | 99 ++++++++--- cocos/2d/CCNode.h | 9 + cocos/2d/CCScene.cpp | 77 --------- cocos/2d/CCScene.h | 18 -- cocos/physics/CCPhysicsBody.cpp | 20 +-- cocos/physics/CCPhysicsBody.h | 1 + cocos/physics/CCPhysicsWorld.cpp | 22 +-- cocos/physics/CCPhysicsWorld.h | 14 +- cocos/ui/CCProtectedNode.cpp | 12 +- .../Classes/PhysicsTest/PhysicsTest.cpp | 157 ++++++++---------- .../Classes/PhysicsTest/PhysicsTest.h | 55 +++--- tests/cpp-tests/Classes/testBasic.cpp | 15 +- tests/cpp-tests/Classes/testBasic.h | 2 +- 15 files changed, 355 insertions(+), 278 deletions(-) diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 98c0ee6edc..c760c7ccae 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -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,101 @@ std::string Layer::getDescription() const return StringUtils::format("", _tag); } +#if CC_USE_PHYSICS +void Layer::addChild(Node* child) +{ + Node::addChild(child); +} +void Layer::addChild(Node* child, int zOrder) +{ + Node::addChild(child, zOrder); +} + +void Layer::addChild(Node* child, int zOrder, int tag) +{ + Node::addChild(child, zOrder, tag); + //addChildToPhysicsWorld(child); +} + +void Layer::onEnter() +{ + Node::onEnter(); + this->scheduleUpdate(); +} + +void Layer::onExit() +{ + Node::onExit(); + this->unscheduleUpdate(); +} + +void Layer::update(float delta) +{ + Node::update(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 addToPhysicsWorldFunc = nullptr; + addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void + { + if (node->getPhysicsBody()) + { + _physicsWorld->addBody(node->getPhysicsBody()); + + node->transformPhysicsBodyPosition(this); + node->transformPhysicsBodyRotation(this); + } + + auto& children = node->getChildren(); + for( const auto &n : children) { + addToPhysicsWorldFunc(n); + } + }; + + addToPhysicsWorldFunc(child); + } +} +#endif + + __LayerRGBA::__LayerRGBA() { CCLOG("LayerRGBA deprecated."); diff --git a/cocos/2d/CCLayer.h b/cocos/2d/CCLayer.h index a97f4771a5..192cf3f937 100644 --- a/cocos/2d/CCLayer.h +++ b/cocos/2d/CCLayer.h @@ -38,6 +38,8 @@ THE SOFTWARE. #include "CCEventKeyboard.h" #include "renderer/CCCustomCommand.h" +#include "CCPhysicsWorld.h" + NS_CC_BEGIN /** @@ -169,6 +171,30 @@ CC_CONSTRUCTOR_ACCESS: virtual bool init() override; +#if CC_USE_PHYSICS +public: + virtual void addChild(Node* child) override; + virtual void addChild(Node* child, int zOrder) override; + virtual void addChild(Node* child, int zOrder, int tag) override; + virtual void update(float delta) override; + 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(); diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 5970b52c8a..57eb68ceff 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -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; + transformPhysicsBodyRotation(layer); } #endif } @@ -428,11 +429,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; + transformPhysicsBodyPosition(layer); } #endif } @@ -727,27 +727,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(node) != nullptr) - { - (dynamic_cast(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(node); + if (layer != nullptr && layer->getPhysicsWorld() != nullptr) + { + layer->addChildToPhysicsWorld(child); + break; + } + } +#endif if( _running ) { @@ -1587,6 +1584,43 @@ void Node::removeAllComponents() } #if CC_USE_PHYSICS + +void Node::transformPhysicsBodyPosition(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::transformPhysicsBodyRotation(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 +1651,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(node); + if (tmpLayer != nullptr && tmpLayer->getPhysicsWorld() != nullptr) + { + layer = tmpLayer; + break; + } + } + + transformPhysicsBodyPosition(layer); + transformPhysicsBodyRotation(layer); } } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 743c4f338a..7955c443c0 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1364,6 +1364,11 @@ protected: virtual void updateCascadeColor(); virtual void disableCascadeColor(); virtual void updateColor() {} + +#if CC_USE_PHYSICS + virtual void transformPhysicsBodyPosition(Layer* layer); + virtual void transformPhysicsBodyRotation(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 diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 9b5069d5f8..bc4e41121c 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -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 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 diff --git a/cocos/2d/CCScene.h b/cocos/2d/CCScene.h index 45d35540d9..e1f6037fb1 100644 --- a/cocos/2d/CCScene.h +++ b/cocos/2d/CCScene.h @@ -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 diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 3d0203e8f5..191895cf13 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -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 @@ -772,10 +766,16 @@ void PhysicsBody::update(float delta) Node* parent = _node->getParent(); Point position = parent != nullptr ? parent->convertToNodeSpace(getPosition()) : getPosition(); + float rotation = getRotation(); + for (; parent != &_world->getLayer(); parent = parent->getParent()) + { + rotation -= parent->getRotation(); + } + _positionResetTag = true; _rotationResetTag = true; _node->setPosition(position); - _node->setRotation(getRotation()); + _node->setRotation(rotation); _positionResetTag = false; _rotationResetTag = false; diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index d9ac83a12a..eefbfe4c81 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -349,6 +349,7 @@ protected: friend class PhysicsShape; friend class PhysicsJoint; friend class Node; + friend class Layer; friend class ProtectedNode; }; diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 192167d68a..234b9cd34b 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -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); @@ -912,7 +912,7 @@ PhysicsWorld::PhysicsWorld() , _updateRateCount(0) , _updateTime(0.0f) , _info(nullptr) -, _scene(nullptr) +, _layer(nullptr) , _delayDirty(false) , _debugDraw(nullptr) , _debugDrawMask(DEBUGDRAW_NONE) @@ -933,7 +933,7 @@ PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world) , _world(world) { _drawNode = DrawNode::create(); - _world.getScene().addChild(_drawNode); + _world.getLayer().addChild(_drawNode); } PhysicsDebugDraw::~PhysicsDebugDraw() diff --git a/cocos/physics/CCPhysicsWorld.h b/cocos/physics/CCPhysicsWorld.h index e597531240..a948a5e015 100644 --- a/cocos/physics/CCPhysicsWorld.h +++ b/cocos/physics/CCPhysicsWorld.h @@ -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 _bodies; std::list _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; diff --git a/cocos/ui/CCProtectedNode.cpp b/cocos/ui/CCProtectedNode.cpp index d86292e6f9..2f756ac498 100644 --- a/cocos/ui/CCProtectedNode.cpp +++ b/cocos/ui/CCProtectedNode.cpp @@ -33,7 +33,7 @@ #if CC_USE_PHYSICS #include "CCPhysicsBody.h" #endif -#include "CCScene.h" +#include "CCLayer.h" NS_CC_BEGIN @@ -96,16 +96,12 @@ 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(node) != nullptr) + Layer* layer = dynamic_cast(node); + if (layer != nullptr && layer->getPhysicsWorld() != nullptr) { - (dynamic_cast(node))->addChildToPhysicsWorld(child); + (dynamic_cast(node))->addChildToPhysicsWorld(child); break; } } diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp index 9013f42930..b021a00b37 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp @@ -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 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), - CL(PhysicsPositionRotationTest), - CL(PhysicsSetGravityEnableTest), + CLPHYSICS(PhysicsDemoLogoSmash), + CLPHYSICS(PhysicsDemoPyramidStack), + CLPHYSICS(PhysicsDemoClickAdd), + CLPHYSICS(PhysicsDemoRayCast), + CLPHYSICS(PhysicsDemoJoints), + CLPHYSICS(PhysicsDemoActions), + CLPHYSICS(PhysicsDemoPump), + CLPHYSICS(PhysicsDemoOneWayPlatform), + CLPHYSICS(PhysicsDemoSlice), + CLPHYSICS(PhysicsDemoBug3988), + CLPHYSICS(PhysicsContactTest), + CLPHYSICS(PhysicsPositionRotationTest), + 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,9 @@ void PhysicsDemoDisabled::onEnter() #else PhysicsDemo::PhysicsDemo() -: _scene(nullptr) -, _spriteTexture(nullptr) +: _spriteTexture(nullptr) , _ball(nullptr) +, _debugDraw(false) { } @@ -108,6 +99,12 @@ PhysicsDemo::~PhysicsDemo() } +void PhysicsDemo::toggleDebug() +{ + _debugDraw = !_debugDraw; + getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE); +} + std::string PhysicsDemo::title() const { return "PhysicsTest"; @@ -146,8 +143,6 @@ void PhysicsDemo::onEnter() { BaseTest::onEnter(); - _scene = dynamic_cast(this->getParent()); - _spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture(); // menu for debug layer @@ -183,10 +178,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 +238,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 @@ -382,7 +371,7 @@ 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 arr = getPhysicsWorld()->getShapes(location); PhysicsBody* body = nullptr; for (auto& obj : arr) @@ -403,7 +392,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; @@ -438,8 +427,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); @@ -540,7 +529,7 @@ void PhysicsDemoRayCast::onEnter() listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - _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 +542,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) @@ -586,6 +573,8 @@ bool PhysicsDemoRayCast::anyRay(PhysicsWorld& world, const PhysicsRayCastInfo& i void PhysicsDemoRayCast::update(float delta) { + Layer::update(delta); + float L = 150.0f; Point point1 = VisibleRect::center(); Point d(L * cosf(_angle), L * sinf(_angle)); @@ -600,7 +589,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 +615,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 +642,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 +694,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 +702,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 +729,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 +744,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 +759,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 +773,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 +787,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 +801,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 +814,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 +830,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 +846,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 +862,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 +878,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 +940,7 @@ std::string PhysicsDemoActions::title() const void PhysicsDemoPump::onEnter() { PhysicsDemo::onEnter(); - _scene->toggleDebug(); + toggleDebug(); _distance = 0.0f; _rotationV = 0.0f; @@ -962,7 +949,6 @@ void PhysicsDemoPump::onEnter() touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPump::onTouchMoved, this); touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoPump::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); - scheduleUpdate(); auto node = Node::create(); auto body = PhysicsBody::create(); @@ -998,7 +984,7 @@ void PhysicsDemoPump::onEnter() VisibleRect::leftBottom() + Point(102, 20) }; - auto _world = _scene->getPhysicsWorld(); + auto _world = getPhysicsWorld(); // small gear auto sgear = Node::create(); @@ -1054,7 +1040,8 @@ void PhysicsDemoPump::onEnter() void PhysicsDemoPump::update(float delta) { - for (const auto& body : _scene->getPhysicsWorld()->getAllBodies()) + Layer::update(delta); + for (const auto& body : getPhysicsWorld()->getAllBodies()) { if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f) { @@ -1063,7 +1050,7 @@ void PhysicsDemoPump::update(float delta) } } - PhysicsBody* gear = _scene->getPhysicsWorld()->getBody(1); + PhysicsBody* gear = getPhysicsWorld()->getBody(1); if (gear != nullptr) { @@ -1157,7 +1144,7 @@ std::string PhysicsDemoOneWayPlatform::title() const void PhysicsDemoSlice::onEnter() { PhysicsDemo::onEnter(); - _scene->toggleDebug(); + toggleDebug(); _sliceTag = 1; @@ -1245,7 +1232,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 +1249,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 +1275,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,8 +1537,8 @@ std::string PhysicsContactTest::subtitle() const void PhysicsPositionRotationTest::onEnter() { PhysicsDemo::onEnter(); - _scene->toggleDebug(); - _scene->getPhysicsWorld()->setGravity(Point::ZERO); + toggleDebug(); + getPhysicsWorld()->setGravity(Point::ZERO); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this); @@ -1647,7 +1634,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 diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h index c487c54f6f..81faa70e15 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h @@ -15,11 +15,6 @@ public: public: virtual void runThisTest(); - - void toggleDebug(); - -private: - bool _debugDraw; }; #if CC_USE_PHYSICS == 0 @@ -32,10 +27,26 @@ public: }; #else +#define CREATE_WITH_PHYSICS_FUNC(__TYPE__) \ +static __TYPE__* createWithPhysics() \ +{ \ +__TYPE__ *pRet = new __TYPE__(); \ +if (pRet && pRet->initWithPhysics()) \ +{ \ +pRet->autorelease(); \ +return pRet; \ +} \ +else \ +{ \ +delete pRet; \ +pRet = NULL; \ +return NULL; \ +} \ +} class PhysicsDemo : public BaseTest { public: - CREATE_FUNC(PhysicsDemo); + CREATE_WITH_PHYSICS_FUNC(PhysicsDemo); PhysicsDemo(); virtual ~PhysicsDemo(); @@ -58,17 +69,21 @@ public: void onTouchMoved(Touch* touch, Event* event); void onTouchEnded(Touch* touch, Event* event); + void toggleDebug(); + protected: - PhysicsTestScene* _scene; Texture2D* _spriteTexture; // weak ref SpriteBatchNode* _ball; std::unordered_map _mouses; + +private: + bool _debugDraw; }; class PhysicsDemoClickAdd : public PhysicsDemo { public: - CREATE_FUNC(PhysicsDemoClickAdd); + CREATE_WITH_PHYSICS_FUNC(PhysicsDemoClickAdd); virtual ~PhysicsDemoClickAdd(); void onEnter() override; @@ -81,7 +96,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 +105,7 @@ public: class PhysicsDemoPyramidStack : public PhysicsDemo { public: - CREATE_FUNC(PhysicsDemoPyramidStack); + CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPyramidStack); void onEnter() override; void updateOnce(float delta); @@ -100,7 +115,7 @@ public: class PhysicsDemoRayCast : public PhysicsDemo { public: - CREATE_FUNC(PhysicsDemoRayCast); + CREATE_WITH_PHYSICS_FUNC(PhysicsDemoRayCast); PhysicsDemoRayCast(); @@ -122,7 +137,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 +146,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 +155,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 +174,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 +185,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 +203,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 +213,7 @@ public: class PhysicsContactTest : public PhysicsDemo { public: - CREATE_FUNC(PhysicsContactTest); + CREATE_WITH_PHYSICS_FUNC(PhysicsContactTest); void onEnter() override; void resetTest(); @@ -219,7 +234,7 @@ private: class PhysicsPositionRotationTest : public PhysicsDemo { public: - CREATE_FUNC(PhysicsPositionRotationTest); + CREATE_WITH_PHYSICS_FUNC(PhysicsPositionRotationTest); void onEnter() override; virtual std::string title() const override; @@ -228,7 +243,7 @@ public: class PhysicsSetGravityEnableTest : public PhysicsDemo { public: - CREATE_FUNC(PhysicsSetGravityEnableTest); + CREATE_WITH_PHYSICS_FUNC(PhysicsSetGravityEnableTest); void onEnter() override; void onScheduleOnce(float delta); diff --git a/tests/cpp-tests/Classes/testBasic.cpp b/tests/cpp-tests/Classes/testBasic.cpp index 2fc38644c1..04efe6fad6 100644 --- a/tests/cpp-tests/Classes/testBasic.cpp +++ b/tests/cpp-tests/Classes/testBasic.cpp @@ -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 ) diff --git a/tests/cpp-tests/Classes/testBasic.h b/tests/cpp-tests/Classes/testBasic.h index d86aed6e26..8a5583994a 100644 --- a/tests/cpp-tests/Classes/testBasic.h +++ b/tests/cpp-tests/Classes/testBasic.h @@ -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; From b53961b78d934078651d4a7c6728e24453580a52 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Tue, 15 Apr 2014 13:40:44 +0800 Subject: [PATCH 2/3] issue $4771: fix lua test and fix some bugs --- cocos/2d/CCLayer.cpp | 34 +++--- cocos/2d/CCLayer.h | 5 +- cocos/2d/CCNode.cpp | 12 +-- cocos/2d/CCNode.h | 4 +- cocos/physics/CCPhysicsBody.cpp | 5 +- cocos/physics/CCPhysicsWorld.cpp | 3 +- .../manual/lua_cocos2dx_physics_manual.cpp | 16 +-- cocos/ui/CCProtectedNode.cpp | 25 ++--- .../Classes/PhysicsTest/PhysicsTest.cpp | 7 +- .../lua-tests/src/PhysicsTest/PhysicsTest.lua | 102 +++++++++--------- tests/lua-tests/src/helper.lua | 8 +- tools/tolua/cocos2dx_physics.ini | 2 +- 12 files changed, 102 insertions(+), 121 deletions(-) diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index c760c7ccae..65f4cf1f4d 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -441,36 +441,28 @@ std::string Layer::getDescription() const } #if CC_USE_PHYSICS -void Layer::addChild(Node* child) -{ - Node::addChild(child); -} -void Layer::addChild(Node* child, int zOrder) -{ - Node::addChild(child, zOrder); -} - -void Layer::addChild(Node* child, int zOrder, int tag) -{ - Node::addChild(child, zOrder, tag); - //addChildToPhysicsWorld(child); -} - void Layer::onEnter() { Node::onEnter(); - this->scheduleUpdate(); + + if (_physicsWorld != nullptr) + { + this->schedule(schedule_selector(Layer::updatePhysics)); + } } void Layer::onExit() { Node::onExit(); - this->unscheduleUpdate(); + + if (_physicsWorld != nullptr) + { + this->unschedule(schedule_selector(Layer::updatePhysics)); + } } -void Layer::update(float delta) +void Layer::updatePhysics(float delta) { - Node::update(delta); if (nullptr != _physicsWorld) { _physicsWorld->update(delta); @@ -519,8 +511,8 @@ void Layer::addChildToPhysicsWorld(Node* child) { _physicsWorld->addBody(node->getPhysicsBody()); - node->transformPhysicsBodyPosition(this); - node->transformPhysicsBodyRotation(this); + node->updatePhysicsBodyPosition(this); + node->updatePhysicsBodyRotation(this); } auto& children = node->getChildren(); diff --git a/cocos/2d/CCLayer.h b/cocos/2d/CCLayer.h index 192cf3f937..4016f17a72 100644 --- a/cocos/2d/CCLayer.h +++ b/cocos/2d/CCLayer.h @@ -173,10 +173,7 @@ CC_CONSTRUCTOR_ACCESS: #if CC_USE_PHYSICS public: - virtual void addChild(Node* child) override; - virtual void addChild(Node* child, int zOrder) override; - virtual void addChild(Node* child, int zOrder, int tag) override; - virtual void update(float delta) override; + virtual void updatePhysics(float delta); virtual void onEnter() override; virtual void onExit() override; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 57eb68ceff..a2ad78046f 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -272,7 +272,7 @@ void Node::setRotation(float rotation) if (_physicsBody && !_physicsBody->_rotationResetTag) { Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr; - transformPhysicsBodyRotation(layer); + updatePhysicsBodyRotation(layer); } #endif } @@ -432,7 +432,7 @@ void Node::setPosition(const Point& position) if (_physicsBody != nullptr && !_physicsBody->_positionResetTag) { Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr; - transformPhysicsBodyPosition(layer); + updatePhysicsBodyPosition(layer); } #endif } @@ -1585,7 +1585,7 @@ void Node::removeAllComponents() #if CC_USE_PHYSICS -void Node::transformPhysicsBodyPosition(Layer* layer) +void Node::updatePhysicsBodyPosition(Layer* layer) { if (_physicsBody != nullptr) { @@ -1601,7 +1601,7 @@ void Node::transformPhysicsBodyPosition(Layer* layer) } } -void Node::transformPhysicsBodyRotation(Layer* layer) +void Node::updatePhysicsBodyRotation(Layer* layer) { if (_physicsBody != nullptr) { @@ -1666,8 +1666,8 @@ void Node::setPhysicsBody(PhysicsBody* body) } } - transformPhysicsBodyPosition(layer); - transformPhysicsBodyRotation(layer); + updatePhysicsBodyPosition(layer); + updatePhysicsBodyRotation(layer); } } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 7955c443c0..f70d08d0db 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1366,8 +1366,8 @@ protected: virtual void updateColor() {} #if CC_USE_PHYSICS - virtual void transformPhysicsBodyPosition(Layer* layer); - virtual void transformPhysicsBodyRotation(Layer* layer); + virtual void updatePhysicsBodyPosition(Layer* layer); + virtual void updatePhysicsBodyRotation(Layer* layer); #endif // CC_USE_PHYSICS float _rotationX; ///< rotation on the X-axis diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 191895cf13..d21ca867c2 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -764,10 +764,11 @@ void PhysicsBody::update(float delta) if (_node != nullptr) { Node* parent = _node->getParent(); + Layer* layer = &_world->getLayer(); - Point position = parent != nullptr ? parent->convertToNodeSpace(getPosition()) : getPosition(); + Point position = parent != layer ? parent->convertToNodeSpace(layer->convertToWorldSpace(getPosition())) : getPosition(); float rotation = getRotation(); - for (; parent != &_world->getLayer(); parent = parent->getParent()) + for (; parent != layer; parent = parent->getParent()) { rotation -= parent->getRotation(); } diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 234b9cd34b..a7e596a3d3 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -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()); } } } diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp index b5792b3444..d3edce6966 100644 --- a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp @@ -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 ); diff --git a/cocos/ui/CCProtectedNode.cpp b/cocos/ui/CCProtectedNode.cpp index 2f756ac498..496f1ff478 100644 --- a/cocos/ui/CCProtectedNode.cpp +++ b/cocos/ui/CCProtectedNode.cpp @@ -95,23 +95,24 @@ void ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag) this->insertProtectedChild(child, zOrder); -#if CC_USE_PHYSICS - for (Node* node = this->getParent(); node != nullptr; node = node->getParent()) - { - Layer* layer = dynamic_cast(node); - if (layer != nullptr && layer->getPhysicsWorld() != nullptr) - { - (dynamic_cast(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(node); + if (layer != nullptr && layer->getPhysicsWorld() != nullptr) + { + layer->addChildToPhysicsWorld(child); + break; + } + } +#endif + if( _running ) { child->onEnter(); diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp index b021a00b37..62765834c3 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp @@ -516,7 +516,7 @@ std::string PhysicsDemoPyramidStack::title() const } PhysicsDemoRayCast::PhysicsDemoRayCast() -: _angle(0.0f) +: _angle(0.0f) , _node(nullptr) , _mode(0) {} @@ -528,6 +528,7 @@ void PhysicsDemoRayCast::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); + scheduleUpdate(); getPhysicsWorld()->setGravity(Point::ZERO); @@ -573,8 +574,6 @@ bool PhysicsDemoRayCast::anyRay(PhysicsWorld& world, const PhysicsRayCastInfo& i void PhysicsDemoRayCast::update(float delta) { - Layer::update(delta); - float L = 150.0f; Point point1 = VisibleRect::center(); Point d(L * cosf(_angle), L * sinf(_angle)); @@ -949,6 +948,7 @@ void PhysicsDemoPump::onEnter() touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPump::onTouchMoved, this); touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoPump::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + scheduleUpdate(); auto node = Node::create(); auto body = PhysicsBody::create(); @@ -1040,7 +1040,6 @@ void PhysicsDemoPump::onEnter() void PhysicsDemoPump::update(float delta) { - Layer::update(delta); for (const auto& body : getPhysicsWorld()->getAllBodies()) { if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f) diff --git a/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua b/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua index 9043a42c8d..7b9d9d715e 100644 --- a/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua +++ b/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua @@ -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, diff --git a/tests/lua-tests/src/helper.lua b/tests/lua-tests/src/helper.lua index 25cec85d3e..265cc8bce3 100644 --- a/tests/lua-tests/src/helper.lua +++ b/tests/lua-tests/src/helper.lua @@ -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()) diff --git a/tools/tolua/cocos2dx_physics.ini b/tools/tolua/cocos2dx_physics.ini index 85bf2b2643..0f5c5f1355 100644 --- a/tools/tolua/cocos2dx_physics.ini +++ b/tools/tolua/cocos2dx_physics.ini @@ -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] From 710fe9d940a748328d1d502386f0e4d0b24e3cb0 Mon Sep 17 00:00:00 2001 From: boyu0 Date: Mon, 21 Apr 2014 13:32:47 +0800 Subject: [PATCH 3/3] closed #4771: modify test. --- cocos/2d/CCNode.cpp | 3 +- .../Classes/PhysicsTest/PhysicsTest.cpp | 112 +++++++++++++----- .../Classes/PhysicsTest/PhysicsTest.h | 20 ++-- 3 files changed, 96 insertions(+), 39 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index a2ad78046f..b98d365c8c 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -300,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 } diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp index 62765834c3..7cfd997c94 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp @@ -22,7 +22,7 @@ namespace CLPHYSICS(PhysicsDemoSlice), CLPHYSICS(PhysicsDemoBug3988), CLPHYSICS(PhysicsContactTest), - CLPHYSICS(PhysicsPositionRotationTest), + CL(PhysicsPositionRotationTest), CLPHYSICS(PhysicsSetGravityEnableTest), #else CL(PhysicsDemoDisabled), @@ -91,6 +91,7 @@ PhysicsDemo::PhysicsDemo() : _spriteTexture(nullptr) , _ball(nullptr) , _debugDraw(false) +, _isRoot(true) { } @@ -141,17 +142,26 @@ void PhysicsDemo::backCallback(Ref* sender) void PhysicsDemo::onEnter() { - BaseTest::onEnter(); + 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*/) @@ -370,7 +380,7 @@ Sprite* PhysicsDemo::makeTriangle(Point point, Size size, int color, PhysicsMate bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event) { - auto location = touch->getLocation(); + auto location = this->convertTouchToNodeSpace(touch); auto arr = getPhysicsWorld()->getShapes(location); PhysicsBody* body = nullptr; @@ -407,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)); } } @@ -454,7 +464,6 @@ void PhysicsDemoLogoSmash::onEnter() } } - auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); bullet->getPhysicsBody()->setVelocity(Point(200, 0)); @@ -1536,36 +1545,51 @@ std::string PhysicsContactTest::subtitle() const void PhysicsPositionRotationTest::onEnter() { PhysicsDemo::onEnter(); - toggleDebug(); - 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); @@ -1576,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; } @@ -1591,6 +1640,11 @@ std::string PhysicsPositionRotationTest::title() const return "Position/Rotation Test"; } +std::string PhysicsPositionRotationTest::subtitle() const +{ + return "Two Physics Worlds"; +} + void PhysicsSetGravityEnableTest::onEnter() { diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h index 81faa70e15..1ec4e09b9b 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h @@ -30,24 +30,23 @@ public: #define CREATE_WITH_PHYSICS_FUNC(__TYPE__) \ static __TYPE__* createWithPhysics() \ { \ -__TYPE__ *pRet = new __TYPE__(); \ -if (pRet && pRet->initWithPhysics()) \ +__TYPE__ *ret = new __TYPE__(); \ +if (ret && ret->initWithPhysics()) \ { \ -pRet->autorelease(); \ -return pRet; \ +ret->autorelease(); \ +return ret; \ } \ else \ { \ -delete pRet; \ -pRet = NULL; \ -return NULL; \ +delete ret; \ +ret = nullptr; \ +return nullptr; \ } \ } class PhysicsDemo : public BaseTest { public: CREATE_WITH_PHYSICS_FUNC(PhysicsDemo); - PhysicsDemo(); virtual ~PhysicsDemo(); @@ -70,6 +69,7 @@ public: void onTouchEnded(Touch* touch, Event* event); void toggleDebug(); + inline void setRoot(bool isRoot) { _isRoot = isRoot; } protected: Texture2D* _spriteTexture; // weak ref @@ -78,6 +78,7 @@ protected: private: bool _debugDraw; + bool _isRoot; }; class PhysicsDemoClickAdd : public PhysicsDemo @@ -234,10 +235,11 @@ private: class PhysicsPositionRotationTest : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsPositionRotationTest); + CREATE_FUNC(PhysicsPositionRotationTest); void onEnter() override; virtual std::string title() const override; + virtual std::string subtitle() const override; }; class PhysicsSetGravityEnableTest : public PhysicsDemo