diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index fef5d2dc7a..cc6990c0e7 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -65,7 +65,7 @@ Layer::Layer() , _swallowsTouches(true) { _ignoreAnchorPointForPosition = true; - setAnchorPoint(Point(0.5f, 0.5f)); + setAnchorPoint(Vector2(0.5f, 0.5f)); } Layer::~Layer() diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp index a2485fff81..8240019fed 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp @@ -3,27 +3,23 @@ #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 - CLPHYSICS(PhysicsDemoLogoSmash), - CLPHYSICS(PhysicsDemoPyramidStack), - CLPHYSICS(PhysicsDemoClickAdd), - CLPHYSICS(PhysicsDemoRayCast), - CLPHYSICS(PhysicsDemoJoints), - CLPHYSICS(PhysicsDemoActions), - CLPHYSICS(PhysicsDemoPump), - CLPHYSICS(PhysicsDemoOneWayPlatform), - CLPHYSICS(PhysicsDemoSlice), - CLPHYSICS(PhysicsDemoBug3988), - CLPHYSICS(PhysicsContactTest), + CL(PhysicsDemoLogoSmash), + CL(PhysicsDemoPyramidStack), + CL(PhysicsDemoClickAdd), + CL(PhysicsDemoRayCast), + CL(PhysicsDemoJoints), + CL(PhysicsDemoActions), + CL(PhysicsDemoPump), + CL(PhysicsDemoOneWayPlatform), + CL(PhysicsDemoSlice), + CL(PhysicsDemoBug3988), + CL(PhysicsContactTest), CL(PhysicsPositionRotationTest), - CLPHYSICS(PhysicsSetGravityEnableTest), + CL(PhysicsSetGravityEnableTest), #else CL(PhysicsDemoDisabled), #endif @@ -63,7 +59,12 @@ namespace } PhysicsTestScene::PhysicsTestScene() +#if CC_USE_PHYSICS +: TestScene(false, true) +#else : TestScene() +#endif +, _debugDraw(false) {} void PhysicsTestScene::runThisTest() @@ -74,6 +75,14 @@ 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() { @@ -88,10 +97,9 @@ void PhysicsDemoDisabled::onEnter() #else PhysicsDemo::PhysicsDemo() -: _spriteTexture(nullptr) +: _scene(nullptr) +, _spriteTexture(nullptr) , _ball(nullptr) -, _debugDraw(false) -, _isRoot(true) { } @@ -100,12 +108,6 @@ PhysicsDemo::~PhysicsDemo() } -void PhysicsDemo::toggleDebug() -{ - _debugDraw = !_debugDraw; - getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE); -} - std::string PhysicsDemo::title() const { return "PhysicsTest"; @@ -142,26 +144,19 @@ void PhysicsDemo::backCallback(Ref* sender) void PhysicsDemo::onEnter() { - if (_isRoot) - { - BaseTest::onEnter(); - } - else - { - Layer::onEnter(); - } + BaseTest::onEnter(); + + _scene = dynamic_cast(this->getParent()); _spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture(); - 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(Vector2(getContentSize().width-50, getContentSize().height/2)); - } + // 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(Vector2(VisibleRect::right().x-50, VisibleRect::top().y-10)); } Sprite* PhysicsDemo::addGrossiniAtPosition(Vector2 p, float scale/* = 1.0*/) @@ -188,7 +183,10 @@ Sprite* PhysicsDemo::addGrossiniAtPosition(Vector2 p, float scale/* = 1.0*/) void PhysicsDemo::toggleDebugCallback(Ref* sender) { - toggleDebug(); + if (_scene != nullptr) + { + _scene->toggleDebug(); + } } PhysicsDemoClickAdd::~PhysicsDemoClickAdd() @@ -248,7 +246,10 @@ void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event) auto v = Vector2( accelX, accelY); v = v * 200; - getPhysicsWorld()->setGravity(v); + if(_scene != nullptr) + { + _scene->getPhysicsWorld()->setGravity(v); + } } namespace @@ -380,8 +381,8 @@ Sprite* PhysicsDemo::makeTriangle(Vector2 point, Size size, int color, PhysicsMa bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event) { - auto location = this->convertTouchToNodeSpace(touch); - auto arr = getPhysicsWorld()->getShapes(location); + auto location = touch->getLocation(); + auto arr = _scene->getPhysicsWorld()->getShapes(location); PhysicsBody* body = nullptr; for (auto& obj : arr) @@ -402,7 +403,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()); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(), mouse)); return true; @@ -417,7 +418,7 @@ void PhysicsDemo::onTouchMoved(Touch* touch, Event* event) if (it != _mouses.end()) { - it->second->setPosition(this->convertTouchToNodeSpace(touch)); + it->second->setPosition(touch->getLocation()); } } @@ -436,8 +437,9 @@ void PhysicsDemo::onTouchEnded(Touch* touch, Event* event) void PhysicsDemoLogoSmash::onEnter() { PhysicsDemo::onEnter(); - getPhysicsWorld()->setGravity(Vector2::ZERO); - getPhysicsWorld()->setUpdateRate(5.0f); + + _scene->getPhysicsWorld()->setGravity(Vector2(0, 0)); + _scene->getPhysicsWorld()->setUpdateRate(5.0f); _ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0])); addChild(_ball); @@ -463,6 +465,7 @@ void PhysicsDemoLogoSmash::onEnter() } } + auto bullet = makeBall(Vector2(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); bullet->getPhysicsBody()->setVelocity(Vector2(200, 0)); @@ -524,7 +527,7 @@ std::string PhysicsDemoPyramidStack::title() const } PhysicsDemoRayCast::PhysicsDemoRayCast() -: _angle(0.0f) +: _angle(0.0f) , _node(nullptr) , _mode(0) {} @@ -536,9 +539,8 @@ void PhysicsDemoRayCast::onEnter() auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - scheduleUpdate(); - getPhysicsWorld()->setGravity(Point::ZERO); + _scene->getPhysicsWorld()->setGravity(Point::ZERO); auto node = DrawNode::create(); node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vector2(0, 50), VisibleRect::rightBottom() + Vector2(0, 50))); @@ -551,6 +553,8 @@ void PhysicsDemoRayCast::onEnter() auto menu = Menu::create(item, NULL); this->addChild(menu); menu->setPosition(Vector2(VisibleRect::left().x+100, VisibleRect::top().y-10)); + + scheduleUpdate(); } void PhysicsDemoRayCast::changeModeCallback(Ref* sender) @@ -596,7 +600,7 @@ void PhysicsDemoRayCast::update(float delta) Vector2 point3 = point2; auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this); - getPhysicsWorld()->rayCast(func, point1, point2, &point3); + _scene->getPhysicsWorld()->rayCast(func, point1, point2, &point3); _node->drawSegment(point1, point3, 1, STATIC_COLOR); if (point2 != point3) @@ -622,7 +626,7 @@ void PhysicsDemoRayCast::update(float delta) return true; }; - getPhysicsWorld()->rayCast(func, point1, point2, nullptr); + _scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr); _node->drawSegment(point1, point3, 1, STATIC_COLOR); if (point2 != point3) @@ -649,7 +653,7 @@ void PhysicsDemoRayCast::update(float delta) return true; }; - getPhysicsWorld()->rayCast(func, point1, point2, nullptr); + _scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr); _node->drawSegment(point1, point2, 1, STATIC_COLOR); @@ -701,7 +705,7 @@ std::string PhysicsDemoRayCast::title() const void PhysicsDemoJoints::onEnter() { PhysicsDemo::onEnter(); - toggleDebug(); + _scene->toggleDebug(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this); @@ -709,6 +713,8 @@ 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; @@ -716,7 +722,7 @@ void PhysicsDemoJoints::onEnter() PhysicsBody* box = PhysicsBody::create(); node->setPhysicsBody(box); box->setDynamic(false); - node->setPosition(Vector2::ZERO); + node->setPosition(Point::ZERO); this->addChild(node); for (int i = 0; i < 4; ++i) @@ -736,7 +742,7 @@ void PhysicsDemoJoints::onEnter() sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -751,7 +757,7 @@ void PhysicsDemoJoints::onEnter() sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -766,7 +772,7 @@ void PhysicsDemoJoints::onEnter() sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -780,7 +786,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); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -794,7 +800,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); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -808,7 +814,7 @@ void PhysicsDemoJoints::onEnter() sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Vector2(30, 15), Vector2(30, -15), Vector2(-30, 0)); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -821,10 +827,10 @@ void PhysicsDemoJoints::onEnter() auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10)); sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 3000.0f, 60.0f); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -837,10 +843,10 @@ void PhysicsDemoJoints::onEnter() auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10)); sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f,(float) M_PI_2); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -853,10 +859,10 @@ void PhysicsDemoJoints::onEnter() auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10)); sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, (float)M_PI_2); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -869,10 +875,10 @@ void PhysicsDemoJoints::onEnter() auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10)); sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); PhysicsJointGear* joint = PhysicsJointGear::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, 2.0f); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -885,10 +891,10 @@ void PhysicsDemoJoints::onEnter() auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10)); sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); - getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition())); + _scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition())); PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), (float)M_PI_2); - getPhysicsWorld()->addJoint(joint); + _scene->getPhysicsWorld()->addJoint(joint); this->addChild(sp1); this->addChild(sp2); @@ -947,7 +953,7 @@ std::string PhysicsDemoActions::title() const void PhysicsDemoPump::onEnter() { PhysicsDemo::onEnter(); - toggleDebug(); + _scene->toggleDebug(); _distance = 0.0f; _rotationV = 0.0f; @@ -992,7 +998,7 @@ void PhysicsDemoPump::onEnter() VisibleRect::leftBottom() + Vector2(102, 20) }; - auto _world = getPhysicsWorld(); + auto _world = _scene->getPhysicsWorld(); // small gear auto sgear = Node::create(); @@ -1048,7 +1054,7 @@ void PhysicsDemoPump::onEnter() void PhysicsDemoPump::update(float delta) { - for (const auto& body : getPhysicsWorld()->getAllBodies()) + for (const auto& body : _scene->getPhysicsWorld()->getAllBodies()) { if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f) { @@ -1057,7 +1063,7 @@ void PhysicsDemoPump::update(float delta) } } - PhysicsBody* gear = getPhysicsWorld()->getBody(1); + PhysicsBody* gear = _scene->getPhysicsWorld()->getBody(1); if (gear != nullptr) { @@ -1151,7 +1157,7 @@ std::string PhysicsDemoOneWayPlatform::title() const void PhysicsDemoSlice::onEnter() { PhysicsDemo::onEnter(); - toggleDebug(); + _scene->toggleDebug(); _sliceTag = 1; @@ -1239,7 +1245,7 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Vector2 normal, floa void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event) { auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this); - getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr); + _scene->getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr); } std::string PhysicsDemoSlice::title() const @@ -1256,8 +1262,8 @@ std::string PhysicsDemoSlice::subtitle() const void PhysicsDemoBug3988::onEnter() { PhysicsDemo::onEnter(); - toggleDebug(); - getPhysicsWorld()->setGravity(Vect::ZERO); + _scene->toggleDebug(); + _scene->getPhysicsWorld()->setGravity(Vect::ZERO); auto ball = Sprite::create("Images/YellowSquare.png"); ball->setPosition(VisibleRect::center() - Vector2(100, 0)); @@ -1282,7 +1288,7 @@ std::string PhysicsDemoBug3988::subtitle() const void PhysicsContactTest::onEnter() { PhysicsDemo::onEnter(); - getPhysicsWorld()->setGravity(Vect::ZERO); + _scene->getPhysicsWorld()->setGravity(Vect::ZERO); auto s = VisibleRect::getVisibleRect().size; _yellowBoxNum = 50; @@ -1544,51 +1550,36 @@ std::string PhysicsContactTest::subtitle() const void PhysicsPositionRotationTest::onEnter() { PhysicsDemo::onEnter(); + _scene->toggleDebug(); + _scene->getPhysicsWorld()->setGravity(Point::ZERO); - PhysicsDemo* layers[2] = {PhysicsDemo::createWithPhysics(), PhysicsDemo::createWithPhysics()}; - Size halfSize = VisibleRect::getVisibleRect().size; - halfSize.width /= 2; - halfSize.height -= 40; + 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); - 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(Vector2(halfSize/2)); - layers[i]->addChild(wall); - this->addChild(layers[i]); - } - layers[0]->setPosition(Vector2(halfSize.width/2, halfSize.height/2 + 60)); - layers[1]->setPosition(Vector2(halfSize.width/2*3, halfSize.height/2 + 60)); + auto wall = Node::create(); + wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); + wall->setPosition(VisibleRect::center()); + addChild(wall); // anchor test auto anchorNode = Sprite::create("Images/YellowSquare.png"); anchorNode->setAnchorPoint(Vector2(0.1f, 0.9f)); - anchorNode->setPosition(100, 50); + anchorNode->setPosition(100, 100); anchorNode->setScale(0.25); anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale())); anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - layers[0]->addChild(anchorNode); + addChild(anchorNode); //parent test auto parent = Sprite::create("Images/YellowSquare.png"); - parent->setPosition(160, 50); + parent->setPosition(200, 100); parent->setScale(0.25); parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale())); parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - layers[0]->addChild(parent); + addChild(parent); auto leftBall = Sprite::create("Images/ball.png"); leftBall->setPosition(-30, 0); @@ -1599,37 +1590,12 @@ void PhysicsPositionRotationTest::onEnter() // offset position rotation test auto offsetPosNode = Sprite::create("Images/YellowSquare.png"); - offsetPosNode->setPosition(100, 100); + offsetPosNode->setPosition(100, 200); offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2)); offsetPosNode->getPhysicsBody()->setPositionOffset(-Vector2(offsetPosNode->getContentSize()/2)); offsetPosNode->getPhysicsBody()->setRotationOffset(45); offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - layers[0]->addChild(offsetPosNode); - - - for (int i = 0; i < 30; ++i) - { - Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10); - Vector2 position = Vector2(halfSize.width, halfSize.height) - Vector2(size.width, size.height); - position.x = position.x * CCRANDOM_0_1(); - position.y = position.y * CCRANDOM_0_1(); - position = position + Vector2(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, Vector2(halfSize.width/2, halfSize.height/2 + 60)); - MoveTo* moveBack = MoveTo::create(1.0f, Vector2(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)); + addChild(offsetPosNode); return; } @@ -1639,11 +1605,6 @@ std::string PhysicsPositionRotationTest::title() const return "Position/Rotation Test"; } -std::string PhysicsPositionRotationTest::subtitle() const -{ - return "Two Physics Worlds"; -} - void PhysicsSetGravityEnableTest::onEnter() { @@ -1686,7 +1647,7 @@ void PhysicsSetGravityEnableTest::onScheduleOnce(float delta) auto ball = getChildByTag(2); ball->getPhysicsBody()->setMass(200); - getPhysicsWorld()->setGravity(Vect(0, 98)); + _scene->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 c2df19971f..78e26297e6 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h @@ -15,6 +15,11 @@ public: public: virtual void runThisTest(); + + void toggleDebug(); + +private: + bool _debugDraw; }; #if CC_USE_PHYSICS == 0 @@ -27,26 +32,11 @@ 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_WITH_PHYSICS_FUNC(PhysicsDemo); + CREATE_FUNC(PhysicsDemo); + PhysicsDemo(); virtual ~PhysicsDemo(); @@ -68,23 +58,17 @@ 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 _mouses; - -private: - bool _debugDraw; - bool _isRoot; }; class PhysicsDemoClickAdd : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoClickAdd); + CREATE_FUNC(PhysicsDemoClickAdd); virtual ~PhysicsDemoClickAdd(); void onEnter() override; @@ -97,7 +81,7 @@ public: class PhysicsDemoLogoSmash : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoLogoSmash); + CREATE_FUNC(PhysicsDemoLogoSmash); void onEnter() override; virtual std::string title() const override; @@ -106,7 +90,7 @@ public: class PhysicsDemoPyramidStack : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPyramidStack); + CREATE_FUNC(PhysicsDemoPyramidStack); void onEnter() override; void updateOnce(float delta); @@ -116,7 +100,7 @@ public: class PhysicsDemoRayCast : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoRayCast); + CREATE_FUNC(PhysicsDemoRayCast); PhysicsDemoRayCast(); @@ -138,7 +122,7 @@ private: class PhysicsDemoJoints : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoJoints); + CREATE_FUNC(PhysicsDemoJoints); void onEnter() override; virtual std::string title() const override; @@ -147,7 +131,7 @@ public: class PhysicsDemoActions : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoActions); + CREATE_FUNC(PhysicsDemoActions); void onEnter() override; virtual std::string title() const override; @@ -156,7 +140,7 @@ public: class PhysicsDemoPump : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPump); + CREATE_FUNC(PhysicsDemoPump); void onEnter() override; void update(float delta) override; @@ -175,7 +159,7 @@ private: class PhysicsDemoOneWayPlatform : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoOneWayPlatform); + CREATE_FUNC(PhysicsDemoOneWayPlatform); void onEnter() override; virtual std::string title() const override; @@ -186,7 +170,7 @@ public: class PhysicsDemoSlice : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoSlice); + CREATE_FUNC(PhysicsDemoSlice); void onEnter() override; virtual std::string title() const override; @@ -204,7 +188,7 @@ private: class PhysicsDemoBug3988 : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsDemoBug3988); + CREATE_FUNC(PhysicsDemoBug3988); void onEnter() override; virtual std::string title() const override; @@ -214,7 +198,7 @@ public: class PhysicsContactTest : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsContactTest); + CREATE_FUNC(PhysicsContactTest); void onEnter() override; void resetTest(); @@ -239,13 +223,12 @@ public: void onEnter() override; virtual std::string title() const override; - virtual std::string subtitle() const override; }; class PhysicsSetGravityEnableTest : public PhysicsDemo { public: - CREATE_WITH_PHYSICS_FUNC(PhysicsSetGravityEnableTest); + CREATE_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 3215484b65..05f46a7fc9 100644 --- a/tests/cpp-tests/Classes/testBasic.cpp +++ b/tests/cpp-tests/Classes/testBasic.cpp @@ -3,9 +3,20 @@ #include "extensions/cocos-ext.h" #include "cocostudio/CocoStudio.h" -TestScene::TestScene(bool bPortrait) +TestScene::TestScene(bool bPortrait, bool physics/* = false*/) { - Scene::init(); + if (physics) + { +#if CC_USE_PHYSICS + TestScene::initWithPhysics(); +#else + Scene::init(); +#endif + } + else + { + Scene::init(); + } } void testScene_callback(Ref *sender ) diff --git a/tests/cpp-tests/Classes/testBasic.h b/tests/cpp-tests/Classes/testBasic.h index 8a5583994a..d86aed6e26 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); + TestScene(bool bPortrait = false, bool physics = false); virtual void onEnter() override; virtual void runThisTest() = 0;