diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 42090c9be8..3f1361a1e8 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -1363,6 +1363,14 @@ 46C02E0818E91123004B7456 /* xxhash.c in Sources */ = {isa = PBXBuildFile; fileRef = 46C02E0518E91123004B7456 /* xxhash.c */; }; 46C02E0918E91123004B7456 /* xxhash.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C02E0618E91123004B7456 /* xxhash.h */; }; 46C02E0A18E91123004B7456 /* xxhash.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C02E0618E91123004B7456 /* xxhash.h */; }; + 46EE47511B817EFD00100730 /* CCComponentPhysics2d.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46EE474D1B817EFD00100730 /* CCComponentPhysics2d.cpp */; }; + 46EE47521B817EFD00100730 /* CCComponentPhysics2d.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46EE474D1B817EFD00100730 /* CCComponentPhysics2d.cpp */; }; + 46EE47531B817EFD00100730 /* CCComponentPhysics2d.h in Headers */ = {isa = PBXBuildFile; fileRef = 46EE474E1B817EFD00100730 /* CCComponentPhysics2d.h */; }; + 46EE47541B817EFD00100730 /* CCComponentPhysics2d.h in Headers */ = {isa = PBXBuildFile; fileRef = 46EE474E1B817EFD00100730 /* CCComponentPhysics2d.h */; }; + 46EE47551B817EFD00100730 /* CCPhysicsManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46EE474F1B817EFD00100730 /* CCPhysicsManager.cpp */; }; + 46EE47561B817EFD00100730 /* CCPhysicsManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46EE474F1B817EFD00100730 /* CCPhysicsManager.cpp */; }; + 46EE47571B817EFD00100730 /* CCPhysicsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 46EE47501B817EFD00100730 /* CCPhysicsManager.h */; }; + 46EE47581B817EFD00100730 /* CCPhysicsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 46EE47501B817EFD00100730 /* CCPhysicsManager.h */; }; 4D76BE3A1A4AAF0A00102962 /* CCActionTimelineNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D76BE381A4AAF0A00102962 /* CCActionTimelineNode.cpp */; }; 4D76BE3B1A4AAF0A00102962 /* CCActionTimelineNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4D76BE381A4AAF0A00102962 /* CCActionTimelineNode.cpp */; }; 4D76BE3C1A4AAF0A00102962 /* CCActionTimelineNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D76BE391A4AAF0A00102962 /* CCActionTimelineNode.h */; }; @@ -4354,6 +4362,10 @@ 46A170781807CE7A005B8026 /* CCPhysicsWorld.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CCPhysicsWorld.h; sourceTree = ""; }; 46C02E0518E91123004B7456 /* xxhash.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xxhash.c; sourceTree = ""; }; 46C02E0618E91123004B7456 /* xxhash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xxhash.h; sourceTree = ""; }; + 46EE474D1B817EFD00100730 /* CCComponentPhysics2d.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCComponentPhysics2d.cpp; sourceTree = ""; }; + 46EE474E1B817EFD00100730 /* CCComponentPhysics2d.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCComponentPhysics2d.h; sourceTree = ""; }; + 46EE474F1B817EFD00100730 /* CCPhysicsManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCPhysicsManager.cpp; sourceTree = ""; }; + 46EE47501B817EFD00100730 /* CCPhysicsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsManager.h; sourceTree = ""; }; 4D76BE381A4AAF0A00102962 /* CCActionTimelineNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCActionTimelineNode.cpp; sourceTree = ""; }; 4D76BE391A4AAF0A00102962 /* CCActionTimelineNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCActionTimelineNode.h; sourceTree = ""; }; 5012168C1AC47380009A4BEA /* CCRenderState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCRenderState.cpp; sourceTree = ""; }; @@ -7368,6 +7380,10 @@ 46A170611807CE7A005B8026 /* physics */ = { isa = PBXGroup; children = ( + 46EE474D1B817EFD00100730 /* CCComponentPhysics2d.cpp */, + 46EE474E1B817EFD00100730 /* CCComponentPhysics2d.h */, + 46EE474F1B817EFD00100730 /* CCPhysicsManager.cpp */, + 46EE47501B817EFD00100730 /* CCPhysicsManager.h */, ED74D7681A5B8A2600157FD4 /* CCPhysicsHelper.h */, 46A1706E1807CE7A005B8026 /* CCPhysicsBody.cpp */, 46A1706F1807CE7A005B8026 /* CCPhysicsBody.h */, @@ -9662,6 +9678,7 @@ B6CAB3351AF9AA1A00B9B856 /* btGImpactShape.h in Headers */, 15AE1A6F19AAD40300C27E9E /* b2CircleContact.h in Headers */, B6CAB3431AF9AA1A00B9B856 /* gim_box_collision.h in Headers */, + 46EE47531B817EFD00100730 /* CCComponentPhysics2d.h in Headers */, B6CAB39B1AF9AA1A00B9B856 /* btVoronoiSimplexSolver.h in Headers */, B6CAB1EB1AF9AA1A00B9B856 /* btBroadphaseInterface.h in Headers */, B665E3E81AA80A6600DDB1C5 /* CCPUSineForceAffectorTranslator.h in Headers */, @@ -9733,6 +9750,7 @@ 5034CA35191D591100CE6051 /* ccShader_PositionTexture.frag in Headers */, 15AE1BB219AADFEF00C27E9E /* HttpClient.h in Headers */, B6CAB3FD1AF9AA1A00B9B856 /* btMultiBodyConstraint.h in Headers */, + 46EE47571B817EFD00100730 /* CCPhysicsManager.h in Headers */, B6DD2FF31B04825B00E47F5F /* DetourTileCacheBuilder.h in Headers */, 15AE197619AAD35700C27E9E /* CCTimelineMacro.h in Headers */, 50ABBE6F1925AB6F00A911A9 /* CCEventListenerKeyboard.h in Headers */, @@ -10179,6 +10197,7 @@ 1A570084180BC5A10088DEC7 /* CCActionManager.h in Headers */, B665E3151AA80A6500DDB1C5 /* CCPUObserverManager.h in Headers */, 15AE18C619AAD33D00C27E9E /* CCLayerLoader.h in Headers */, + 46EE47541B817EFD00100730 /* CCComponentPhysics2d.h in Headers */, B6CAB4A41AF9AA1A00B9B856 /* PpuAddressSpace.h in Headers */, B6CAB2041AF9AA1A00B9B856 /* btMultiSapBroadphase.h in Headers */, B665E2C51AA80A6500DDB1C5 /* CCPUGeometryRotatorTranslator.h in Headers */, @@ -10952,6 +10971,7 @@ B6CAB3E01AF9AA1A00B9B856 /* btTypedConstraint.h in Headers */, 50ABBDAA1925AB4100A911A9 /* CCRenderCommand.h in Headers */, B29A7DF019EE1B7700872B35 /* SkeletonBounds.h in Headers */, + 46EE47581B817EFD00100730 /* CCPhysicsManager.h in Headers */, B6CAB3361AF9AA1A00B9B856 /* btGImpactShape.h in Headers */, 15AE1BE919AAE01E00C27E9E /* CCControl.h in Headers */, 15AE193719AAD35100C27E9E /* CCArmature.h in Headers */, @@ -11292,6 +11312,7 @@ 38B8E2E119E671D2002D7CE7 /* UILayoutComponent.cpp in Sources */, B665E3F61AA80A6600DDB1C5 /* CCPUSlaveEmitterTranslator.cpp in Sources */, 1A57019D180BCB590088DEC7 /* CCFont.cpp in Sources */, + 46EE47551B817EFD00100730 /* CCPhysicsManager.cpp in Sources */, B6CAB2D51AF9AA1A00B9B856 /* btOptimizedBvh.cpp in Sources */, 50CB247B19D9C5A100687767 /* AudioEngine-inl.mm in Sources */, 1A5701A1180BCB590088DEC7 /* CCFontAtlas.cpp in Sources */, @@ -11514,6 +11535,7 @@ B6CAB3631AF9AA1A00B9B856 /* btContinuousConvexCollision.cpp in Sources */, B665E3021AA80A6500DDB1C5 /* CCPUMeshSurfaceEmitter.cpp in Sources */, B665E3721AA80A6500DDB1C5 /* CCPUParticleFollower.cpp in Sources */, + 46EE47511B817EFD00100730 /* CCComponentPhysics2d.cpp in Sources */, B6CAB22B1AF9AA1A00B9B856 /* btCollisionObject.cpp in Sources */, 1A5702C8180BCE370088DEC7 /* CCTextFieldTTF.cpp in Sources */, B665E2861AA80A6500DDB1C5 /* CCPUDoStopSystemEventHandlerTranslator.cpp in Sources */, @@ -11977,6 +11999,7 @@ 1A57006E180BC5A10088DEC7 /* CCActionEase.cpp in Sources */, 50ABBD8C1925AB4100A911A9 /* CCGLProgram.cpp in Sources */, B665E2671AA80A6500DDB1C5 /* CCPUDoExpireEventHandlerTranslator.cpp in Sources */, + 46EE47561B817EFD00100730 /* CCPhysicsManager.cpp in Sources */, B6CAB4AA1AF9AA1A00B9B856 /* SpuCollisionObjectWrapper.cpp in Sources */, B6CAAFFF1AF9A9E100B9B856 /* CCPhysicsSprite3D.cpp in Sources */, 15AE196119AAD35100C27E9E /* CCSSceneReader.cpp in Sources */, @@ -12159,6 +12182,7 @@ B6CAB2201AF9AA1A00B9B856 /* btBoxBoxDetector.cpp in Sources */, 5E9F612B1A3FFE3D0038DE01 /* CCPlane.cpp in Sources */, 1A57019E180BCB590088DEC7 /* CCFont.cpp in Sources */, + 46EE47521B817EFD00100730 /* CCComponentPhysics2d.cpp in Sources */, 15AE1ACC19AAD40300C27E9E /* b2PrismaticJoint.cpp in Sources */, 15AE195F19AAD35100C27E9E /* CCSpriteFrameCacheHelper.cpp in Sources */, 15AE193019AAD35100C27E9E /* CCActionManagerEx.cpp in Sources */, diff --git a/cocos/2d/CCComponentContainer.h b/cocos/2d/CCComponentContainer.h index 357e082eb3..44939fbb82 100644 --- a/cocos/2d/CCComponentContainer.h +++ b/cocos/2d/CCComponentContainer.h @@ -49,6 +49,22 @@ public: * @lua NA */ virtual ~ComponentContainer(void); + + template + T* getComponent() const + { + if (_components) + { + for (const auto &iter : *_components) + { + if (dynamic_cast(iter.second) != nullptr) + return static_cast(iter.second); + } + } + + return nullptr; + } + /** * @js getComponent */ diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 8eaf23c10c..ca839dbb9c 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -44,10 +44,6 @@ THE SOFTWARE. #include "deprecated/CCString.h" -#if CC_USE_PHYSICS -#include "physics/CCPhysicsBody.h" -#endif - NS_CC_BEGIN // Layer diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 6f6a8b45f8..2f5e0f42a1 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -41,19 +41,12 @@ THE SOFTWARE. #include "2d/CCActionManager.h" #include "2d/CCScene.h" #include "2d/CCComponent.h" -#include "2d/CCComponentContainer.h" #include "renderer/CCGLProgram.h" #include "renderer/CCGLProgramState.h" #include "renderer/CCMaterial.h" #include "math/TransformUtils.h" - #include "deprecated/CCString.h" -#if CC_USE_PHYSICS -#include "physics/CCPhysicsBody.h" -#include "physics/CCPhysicsWorld.h" -#endif - #if CC_NODE_RENDER_SUBPIXEL #define RENDER_IN_SUBPIXEL @@ -118,17 +111,6 @@ Node::Node(void) , _updateScriptHandler(0) #endif , _componentContainer(nullptr) -#if CC_USE_PHYSICS -, _physicsBody(nullptr) -, _physicsScaleStartX(1.0f) -, _physicsScaleStartY(1.0f) -, _physicsRotation(0.0f) -, _physicsTransformDirty(true) -, _updateTransformFromPhysics(true) -, _physicsWorld(nullptr) -, _physicsBodyAssociatedWith(0) -, _physicsRotationOffset(0.0f) -#endif , _displayedOpacity(255) , _realOpacity(255) , _displayedColor(Color3B::WHITE) @@ -194,11 +176,6 @@ Node::~Node() CC_SAFE_DELETE(_componentContainer); -#if CC_USE_PHYSICS - setPhysicsBody(nullptr); - -#endif - stopAllActions(); unscheduleAllCallbacks(); CC_SAFE_RELEASE_NULL(_actionManager); @@ -259,13 +236,6 @@ void Node::setSkewX(float skewX) if (_skewX == skewX) return; -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setSkewX"); - } -#endif - _skewX = skewX; _transformUpdated = _transformDirty = _inverseDirty = true; } @@ -280,13 +250,6 @@ void Node::setSkewY(float skewY) if (_skewY == skewY) return; -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setSkewY"); - } -#endif - _skewY = skewY; _transformUpdated = _transformDirty = _inverseDirty = true; } @@ -336,12 +299,6 @@ void Node::setRotation(float rotation) _rotationZ_X = _rotationZ_Y = rotation; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif updateRotationQuat(); } @@ -367,13 +324,6 @@ void Node::setRotation3D(const Vec3& rotation) _rotationZ_Y = _rotationZ_X = rotation.z; updateRotationQuat(); - -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setRotation3D"); - } -#endif } Vec3 Node::getRotation3D() const @@ -429,13 +379,6 @@ void Node::setRotationSkewX(float rotationX) if (_rotationZ_X == rotationX) return; -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setRotationSkewX"); - } -#endif - _rotationZ_X = rotationX; _transformUpdated = _transformDirty = _inverseDirty = true; @@ -452,13 +395,6 @@ void Node::setRotationSkewY(float rotationY) if (_rotationZ_Y == rotationY) return; -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setRotationSkewY"); - } -#endif - _rotationZ_Y = rotationY; _transformUpdated = _transformDirty = _inverseDirty = true; @@ -480,12 +416,6 @@ void Node::setScale(float scale) _scaleX = _scaleY = _scaleZ = scale; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } /// scaleX getter @@ -503,12 +433,6 @@ void Node::setScale(float scaleX,float scaleY) _scaleX = scaleX; _scaleY = scaleY; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } /// scaleX setter @@ -519,12 +443,6 @@ void Node::setScaleX(float scaleX) _scaleX = scaleX; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } /// scaleY getter @@ -539,13 +457,6 @@ void Node::setScaleZ(float scaleZ) if (_scaleZ == scaleZ) return; -#if CC_USE_PHYSICS - if (_physicsBody != nullptr) - { - CCLOG("Node WARNING: PhysicsBody doesn't support setScaleZ"); - } -#endif - _scaleZ = scaleZ; _transformUpdated = _transformDirty = _inverseDirty = true; } @@ -564,12 +475,6 @@ void Node::setScaleY(float scaleY) _scaleY = scaleY; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } @@ -601,12 +506,6 @@ void Node::setPosition(float x, float y) _transformUpdated = _transformDirty = _inverseDirty = true; _usingNormalizedPosition = false; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } void Node::setPosition3D(const Vec3& position) @@ -671,12 +570,6 @@ void Node::setNormalizedPosition(const Vec2& position) _usingNormalizedPosition = true; _normalizedPositionDirty = true; _transformUpdated = _transformDirty = _inverseDirty = true; -#if CC_USE_PHYSICS - if (_physicsWorld && _physicsBodyAssociatedWith > 0) - { - _physicsWorld->_updateBodyTransform = true; - } -#endif } ssize_t Node::getChildrenCount() const @@ -1059,24 +952,6 @@ void Node::addChildHelper(Node* child, int localZOrder, int tag, const std::stri child->setParent(this); child->setOrderOfArrival(s_globalOrderOfArrival++); -#if CC_USE_PHYSICS - _physicsBodyAssociatedWith += child->_physicsBodyAssociatedWith; - auto parentNode = this; - while (parentNode->_parent) - { - parentNode = parentNode->_parent; - parentNode->_physicsBodyAssociatedWith += child->_physicsBodyAssociatedWith; - } - - auto scene = dynamic_cast(parentNode); - - // Recursive add children with which have physics body. - if (scene && scene->getPhysicsWorld()) - { - scene->addChildToPhysicsWorld(child); - } -#endif - if( _running ) { child->onEnter(); @@ -1177,21 +1052,6 @@ void Node::removeAllChildren() this->removeAllChildrenWithCleanup(true); } -#if CC_USE_PHYSICS -void Node::removeFromPhysicsWorld() -{ - if (_physicsBody != nullptr) - { - _physicsBody->removeFromWorld(); - } - - for (auto child : _children) - { - child->removeFromPhysicsWorld(); - } -} -#endif - void Node::removeAllChildrenWithCleanup(bool cleanup) { // not using detachChild improves speed here @@ -1206,10 +1066,6 @@ void Node::removeAllChildrenWithCleanup(bool cleanup) child->onExit(); } -#if CC_USE_PHYSICS - child->removeFromPhysicsWorld(); -#endif - if (cleanup) { child->cleanup(); @@ -1231,10 +1087,6 @@ void Node::detachChild(Node *child, ssize_t childIndex, bool doCleanup) child->onExitTransitionDidStart(); child->onExit(); } - -#if CC_USE_PHYSICS - child->removeFromPhysicsWorld(); -#endif // If you don't do cleanup, the child's actions will not get removed and the // its scheduledSelectors_ dict will not get released! @@ -1297,12 +1149,6 @@ void Node::visit() uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFlags) { -#if CC_USE_PHYSICS - if (_physicsBody && _updateTransformFromPhysics) - { - updateTransformFromPhysics(parentTransform, parentFlags); - } -#endif if(_usingNormalizedPosition) { CCASSERT(_parent, "setNormalizedPosition() doesn't work with orphan nodes"); @@ -1330,15 +1176,8 @@ uint32_t Node::processParentFlags(const Mat4& parentTransform, uint32_t parentFl if(flags & FLAGS_DIRTY_MASK) _modelViewTransform = this->transform(parentTransform); -#if CC_USE_PHYSICS - if (_updateTransformFromPhysics) { - _transformUpdated = false; - _contentSizeDirty = false; - } -#else _transformUpdated = false; _contentSizeDirty = false; -#endif return flags; } @@ -2065,120 +1904,6 @@ void Node::removeAllComponents() _componentContainer->removeAll(); } -#if CC_USE_PHYSICS - -// MARK: Physics - -void Node::setPhysicsBody(PhysicsBody* body) -{ - if (_physicsBody == body) - { - return; - } - - if (_physicsBody) - { - _physicsBody->removeFromWorld(); - _physicsBody->_node = nullptr; - _physicsBody->release(); - _physicsBody = nullptr; - - _physicsBodyAssociatedWith--; - auto parentNode = this; - while (parentNode->_parent) - { - parentNode = parentNode->_parent; - parentNode->_physicsBodyAssociatedWith--; - } - } - - if (body) - { - if (body->getNode()) - { - body->getNode()->setPhysicsBody(nullptr); - } - - body->_node = this; - body->retain(); - - _physicsBody = body; - _physicsScaleStartX = _scaleX; - _physicsScaleStartY = _scaleY; - _physicsRotationOffset = _rotationZ_X; - - _physicsBodyAssociatedWith++; - auto parentNode = this; - while (parentNode->_parent) - { - parentNode = parentNode->_parent; - parentNode->_physicsBodyAssociatedWith++; - } - - auto scene = dynamic_cast(parentNode); - if (scene && scene->getPhysicsWorld()) - { - _physicsTransformDirty = true; - scene->getPhysicsWorld()->addBody(body); - } - } -} - -void Node::updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY) -{ - _updateTransformFromPhysics = false; - auto flags = processParentFlags(parentTransform, parentFlags); - _updateTransformFromPhysics = true; - auto scaleX = parentScaleX * _scaleX; - auto scaleY = parentScaleY * _scaleY; - - if (_parent) - { - _physicsRotation = _parent->_physicsRotation + _rotationZ_X; - } - if (_physicsBody && ((flags & FLAGS_DIRTY_MASK) || _physicsTransformDirty)) - { - _physicsTransformDirty = false; - - Vec3 vec3(_contentSize.width * 0.5f, _contentSize.height * 0.5f, 0); - Vec3 ret; - _modelViewTransform.transformPoint(vec3, &ret); - _physicsBody->setPosition(Vec2(ret.x, ret.y)); - - parentTransform.getInversed().transformPoint(&ret); - _offsetX = ret.x - _position.x; - _offsetY = ret.y - _position.y; - - _physicsBody->setScale(scaleX / _physicsScaleStartX, scaleY / _physicsScaleStartY); - _physicsBody->setRotation(_physicsRotation - _physicsRotationOffset); - } - - for (auto node : _children) - { - node->updatePhysicsBodyTransform(_modelViewTransform, flags, scaleX, scaleY); - } -} - -void Node::updateTransformFromPhysics(const Mat4& parentTransform, uint32_t parentFlags) -{ - auto& newPosition = _physicsBody->getPosition(); - auto& recordedPosition = _physicsBody->_recordedPosition; - auto updateBodyTransform = _physicsWorld->_updateBodyTransform; - if (parentFlags || recordedPosition.x != newPosition.x || recordedPosition.y != newPosition.y) - { - recordedPosition = newPosition; - Vec3 vec3(newPosition.x, newPosition.y, 0); - Vec3 ret; - parentTransform.getInversed().transformPoint(vec3, &ret); - setPosition(ret.x - _offsetX, ret.y - _offsetY); - } - _physicsRotation = _physicsBody->getRotation(); - setRotation(_physicsRotation - _parent->_physicsRotation + _physicsRotationOffset); - _physicsWorld->_updateBodyTransform = updateBodyTransform; -} - -#endif //CC_USE_PHYSICS - // MARK: Opacity and Color GLubyte Node::getOpacity(void) const diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 2240eed040..2ce7811d91 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -35,6 +35,7 @@ #include "base/CCScriptSupport.h" #include "math/CCAffineTransform.h" #include "math/CCMath.h" +#include "2d/CCComponentContainer.h" NS_CC_BEGIN @@ -53,10 +54,6 @@ class Director; class GLProgram; class GLProgramState; class Material; -#if CC_USE_PHYSICS -class PhysicsBody; -class PhysicsWorld; -#endif class Camera; /** @@ -1560,7 +1557,6 @@ public: virtual Mat4 getWorldToNodeTransform() const; virtual AffineTransform getWorldToNodeAffineTransform() const; - /** @deprecated Use getWorldToNodeTransform() instead */ CC_DEPRECATED_ATTRIBUTE inline virtual AffineTransform worldToNodeTransform() const { return getWorldToNodeAffineTransform(); } @@ -1644,6 +1640,22 @@ public: * @return The Component by name. */ Component* getComponent(const std::string& name); + + /** + * Get a component by the type T. + * @lua NA + * @js NA + * + * @return The component that match the type T. + */ + template + T* getComponent() const + { + if (_componentContainer) + return _componentContainer->getComponent(); + else + return nullptr; + } /** * Adds a component. @@ -1673,39 +1685,6 @@ public: */ virtual void removeAllComponents(); /// @} end of component functions - - -#if CC_USE_PHYSICS - /** - * Set the PhysicsBody that let the sprite effect with physics. - * @note This method will set anchor point to Vec2::ANCHOR_MIDDLE if body not null, and you cann't change anchor point if node has a physics body. - * - * @param body A given physics body. - */ - void setPhysicsBody(PhysicsBody* body); - - /** - * Get the PhysicsBody the sprite have. - * - * @return The PhysicsBody the sprite have. - */ - PhysicsBody* getPhysicsBody() const { return _physicsBody; } - - /** - * Remove this node from physics world. it will remove all the physics bodies in it's children too. - */ - void removeFromPhysicsWorld(); - - /** - * Update the transform matrix from physics. - */ - void updateTransformFromPhysics(const Mat4& parentTransform, uint32_t parentFlags); - - /** - * Update physics body transform matrix. - */ - virtual void updatePhysicsBodyTransform(const Mat4& parentTransform, uint32_t parentFlags, float parentScaleX, float parentScaleY); -#endif // overrides virtual GLubyte getOpacity() const; @@ -1863,22 +1842,6 @@ protected: #endif ComponentContainer *_componentContainer; ///< Dictionary of components - -#if CC_USE_PHYSICS - PhysicsBody* _physicsBody; ///< the physicsBody the node have - float _physicsScaleStartX; ///< the scale x value when setPhysicsBody - float _physicsScaleStartY; ///< the scale y value when setPhysicsBody - float _physicsRotation; - bool _physicsTransformDirty; - bool _updateTransformFromPhysics; - - PhysicsWorld* _physicsWorld; /** The PhysicsWorld associated with the node.*/ - int _physicsBodyAssociatedWith; /** The count of PhysicsBody associated with the node and children.*/ - float _physicsRotationOffset; /** Record the rotation value when invoke Node::setPhysicsBody.*/ - - float _offsetX; - float _offsetY; -#endif // opacity controls GLubyte _displayedOpacity; @@ -1900,10 +1863,6 @@ protected: private: CC_DISALLOW_COPY_AND_ASSIGN(Node); - -#if CC_USE_PHYSICS - friend class Scene; -#endif //CC_USTPS }; diff --git a/cocos/2d/CCProtectedNode.cpp b/cocos/2d/CCProtectedNode.cpp index 1b831bd9f5..c248846047 100644 --- a/cocos/2d/CCProtectedNode.cpp +++ b/cocos/2d/CCProtectedNode.cpp @@ -29,10 +29,6 @@ #include "CCProtectedNode.h" #include "base/CCDirector.h" - -#if CC_USE_PHYSICS -#include "physics/CCPhysicsBody.h" -#endif #include "2d/CCScene.h" NS_CC_BEGIN @@ -108,19 +104,6 @@ void ProtectedNode::addProtectedChild(Node *child, int zOrder, int 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()) - { - Scene* scene = dynamic_cast(node); - if (scene != nullptr && scene->getPhysicsWorld() != nullptr) - { - scene->addChildToPhysicsWorld(child); - break; - } - } -#endif - if( _running ) { child->onEnter(); @@ -178,13 +161,6 @@ void ProtectedNode::removeProtectedChild(cocos2d::Node *child, bool cleanup) child->onExit(); } -#if CC_USE_PHYSICS - if (child->getPhysicsBody() != nullptr) - { - child->getPhysicsBody()->removeFromWorld(); - } - -#endif // If you don't do cleanup, the child's actions will not get removed and the // its scheduledSelectors_ dict will not get released! if (cleanup) @@ -218,13 +194,6 @@ void ProtectedNode::removeAllProtectedChildrenWithCleanup(bool cleanup) child->onExit(); } -#if CC_USE_PHYSICS - if (child->getPhysicsBody() != nullptr) - { - child->getPhysicsBody()->removeFromWorld(); - } -#endif - if (cleanup) { child->cleanup(); diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index 9dc81e8a2a..a2dee0aa9f 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -35,7 +35,7 @@ THE SOFTWARE. #include "deprecated/CCString.h" #if CC_USE_PHYSICS -#include "physics/CCPhysicsWorld.h" +#include "physics/CCPhysicsManager.h" #endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION @@ -50,9 +50,6 @@ THE SOFTWARE. NS_CC_BEGIN Scene::Scene() -#if CC_USE_PHYSICS -: _physicsWorld(nullptr) -#endif { #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION _physics3DWorld = nullptr; @@ -61,6 +58,9 @@ Scene::Scene() #if CC_USE_NAVMESH _navMesh = nullptr; _navMeshDebugCamera = nullptr; +#endif +#if CC_USE_PHYSICS + _physicsManager = nullptr; #endif _ignoreAnchorPointForPosition = true; setAnchorPoint(Vec2(0.5f, 0.5f)); @@ -79,9 +79,6 @@ Scene::Scene() Scene::~Scene() { -#if CC_USE_PHYSICS - CC_SAFE_DELETE(_physicsWorld); -#endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION CC_SAFE_RELEASE(_physics3DWorld); CC_SAFE_RELEASE(_physics3dDebugCamera); @@ -91,6 +88,10 @@ Scene::~Scene() #endif Director::getInstance()->getEventDispatcher()->removeEventListener(_event); CC_SAFE_RELEASE(_event); + +#if CC_USE_PHYSICS + delete _physicsManager; +#endif } #if CC_USE_NAVMESH @@ -260,21 +261,6 @@ void Scene::setNavMeshDebugCamera(Camera *camera) #endif #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION)) -void Scene::addChild(Node* child, int zOrder, int tag) -{ - Node::addChild(child, zOrder, tag); -#if CC_USE_PHYSICS - addChildToPhysicsWorld(child); -#endif -} - -void Scene::addChild(Node* child, int zOrder, const std::string &name) -{ - Node::addChild(child, zOrder, name); -#if CC_USE_PHYSICS - addChildToPhysicsWorld(child); -#endif -} Scene* Scene::createWithPhysics() { @@ -293,6 +279,9 @@ Scene* Scene::createWithPhysics() bool Scene::initWithPhysics() { + _physicsManager = new (std::nothrow) PhysicsManager(this); + _physicsWorld = _physicsManager->getPhysicsWorld(); + bool ret = false; do { @@ -300,9 +289,6 @@ bool Scene::initWithPhysics() CC_BREAK_IF( ! (director = Director::getInstance()) ); this->setContentSize(director->getWinSize()); -#if CC_USE_PHYSICS - CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this))); -#endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION Physics3DWorldDes info; @@ -316,43 +302,16 @@ bool Scene::initWithPhysics() return ret; } -void Scene::addChildToPhysicsWorld(Node* child) -{ -#if CC_USE_PHYSICS - if (_physicsWorld) - { - std::function addToPhysicsWorldFunc = nullptr; - addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void - { - node->_physicsWorld = _physicsWorld; - - if (node->getPhysicsBody()) - { - _physicsWorld->addBody(node->getPhysicsBody()); - } - - auto& children = node->getChildren(); - for( const auto &n : children) { - addToPhysicsWorldFunc(n); - } - }; - - addToPhysicsWorldFunc(child); - } -#endif -} - #endif #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH) void Scene::stepPhysicsAndNavigation(float deltaTime) { #if CC_USE_PHYSICS - if (_physicsWorld && _physicsWorld->isAutoStep()) - { - _physicsWorld->update(deltaTime, false); - } + if (_physicsManager) + _physicsManager->update(deltaTime); #endif + #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION if (_physics3DWorld) { diff --git a/cocos/2d/CCScene.h b/cocos/2d/CCScene.h index ae154083e4..6047977074 100644 --- a/cocos/2d/CCScene.h +++ b/cocos/2d/CCScene.h @@ -40,6 +40,7 @@ class EventListenerCustom; class EventCustom; #if CC_USE_PHYSICS class PhysicsWorld; +class PhysicsManager; #endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION class Physics3DWorld; @@ -145,15 +146,21 @@ private: #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION)) public: - virtual void addChild(Node* child, int zOrder, int tag) override; - virtual void addChild(Node* child, int zOrder, const std::string &name) override; #if CC_USE_PHYSICS /** Get the physics world of the scene. * @return The physics world of the scene. * @js NA */ - inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; } + inline PhysicsWorld* getPhysicsWorld() const { return _physicsWorld; } + + /** + * Get the `PhysicsManager` belongs to this `Scene`. + * + * @return PhysicsManager of the scene. + * @js NA + */ + PhysicsManager* getPhysicsManager() const { return _physicsManager; } #endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION @@ -183,6 +190,7 @@ protected: #if CC_USE_PHYSICS PhysicsWorld* _physicsWorld; + PhysicsManager* _physicsManager; #endif #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION @@ -203,8 +211,8 @@ public: void setNavMeshDebugCamera(Camera *camera); protected: - NavMesh* _navMesh; - Camera * _navMeshDebugCamera; + NavMesh* _navMesh; + Camera * _navMeshDebugCamera; #endif #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH) diff --git a/cocos/2d/CCSpriteBatchNode.cpp b/cocos/2d/CCSpriteBatchNode.cpp index fc927aa342..96a78e6d83 100644 --- a/cocos/2d/CCSpriteBatchNode.cpp +++ b/cocos/2d/CCSpriteBatchNode.cpp @@ -367,13 +367,6 @@ void SpriteBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t f for (const auto &child : _children) { -#if CC_USE_PHYSICS - auto physicsBody = child->getPhysicsBody(); - if (physicsBody) - { - child->updateTransformFromPhysics(transform, flags); - } -#endif child->updateTransform(); } diff --git a/cocos/2d/libcocos2d.vcxproj b/cocos/2d/libcocos2d.vcxproj index f62d0aeb5a..eec068403b 100644 --- a/cocos/2d/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d.vcxproj @@ -587,9 +587,11 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + + @@ -1195,10 +1197,12 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + + diff --git a/cocos/2d/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d.vcxproj.filters index eb2707d8f7..72850a6776 100644 --- a/cocos/2d/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d.vcxproj.filters @@ -285,6 +285,9 @@ + + physics + physics @@ -294,6 +297,9 @@ physics + + physics + physics @@ -1936,6 +1942,9 @@ + + physics + physics @@ -1945,6 +1954,9 @@ physics + + physics + physics diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems index 0933df8075..b3595dd31a 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems @@ -468,10 +468,12 @@ + + @@ -1071,9 +1073,11 @@ + + diff --git a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters index f9759f3410..27e9f827b9 100644 --- a/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters +++ b/cocos/2d/libcocos2d_8_1/libcocos2d_8_1/libcocos2d_8_1.Shared/libcocos2d_8_1.Shared.vcxitems.filters @@ -210,6 +210,12 @@ physics + + physics + + + physics + cocostudio @@ -2054,6 +2060,12 @@ physics + + physics + + + physics + cocostudio\action diff --git a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj index 547868ac0c..ad54fe8425 100644 --- a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj @@ -565,9 +565,11 @@ + + @@ -1179,10 +1181,12 @@ + + diff --git a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters index ecf6c1b2d4..d2e1c62e5a 100644 --- a/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d_win10/libcocos2d.vcxproj.filters @@ -1578,6 +1578,9 @@ network + + physics + physics @@ -1587,6 +1590,9 @@ physics + + physics + physics @@ -3389,6 +3395,9 @@ network + + physics + physics @@ -3401,6 +3410,9 @@ physics + + physics + physics diff --git a/cocos/Android.mk b/cocos/Android.mk index d2049c1e68..7a5dad7ec7 100644 --- a/cocos/Android.mk +++ b/cocos/Android.mk @@ -200,6 +200,8 @@ physics/CCPhysicsContact.cpp \ physics/CCPhysicsJoint.cpp \ physics/CCPhysicsShape.cpp \ physics/CCPhysicsWorld.cpp \ +physics/CCComponentPhysics2d.cpp \ +physics/CCPhysicsManager.cpp \ physics3d/CCPhysics3D.cpp \ physics3d/CCPhysics3DWorld.cpp \ physics3d/CCPhysics3DComponent.cpp \ diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 5494c67408..86a4498fd2 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -61,7 +61,6 @@ THE SOFTWARE. #include "base/CCConfiguration.h" #include "base/CCAsyncTaskPool.h" #include "platform/CCApplication.h" -//#include "platform/CCGLViewImpl.h" #if CC_ENABLE_SCRIPT_BINDING #include "CCScriptSupport.h" diff --git a/cocos/cocos2d.h b/cocos/cocos2d.h index 9dc4356af0..34070a9a70 100644 --- a/cocos/cocos2d.h +++ b/cocos/cocos2d.h @@ -186,6 +186,8 @@ THE SOFTWARE. #include "physics/CCPhysicsJoint.h" #include "physics/CCPhysicsShape.h" #include "physics/CCPhysicsWorld.h" +#include "physics/CCComponentPhysics2d.h" +#include "physics/CCPhysicsManager.h" // platform #include "platform/CCCommon.h" diff --git a/cocos/physics/CCComponentPhysics2d.cpp b/cocos/physics/CCComponentPhysics2d.cpp new file mode 100644 index 0000000000..08c4c9c3b8 --- /dev/null +++ b/cocos/physics/CCComponentPhysics2d.cpp @@ -0,0 +1,263 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "base/ccConfig.h" // to include defination of CC_USE_PHYSICS +#if CC_USE_PHYSICS + +#include "physics/CCComponentPhysics2d.h" +#include "physics/CCPhysicsBody.h" +#include "physics/CCPhysicsManager.h" +#include "2d/CCNode.h" +#include "base/CCDirector.h" + +NS_CC_BEGIN + +const std::string ComponentPhysics2d::COMPONENT_NAME = "physics2d"; + +ComponentPhysics2d* ComponentPhysics2d::create() +{ + return create(nullptr); +} + +ComponentPhysics2d* ComponentPhysics2d::create(PhysicsBody *physicsBody) +{ + auto ret = new (std::nothrow) ComponentPhysics2d(physicsBody); + if (ret) + ret->autorelease(); + + return ret; +} + +ComponentPhysics2d::ComponentPhysics2d() +: _physicsBody(nullptr) +, _physicsRotation(0.0f) +, _ownerOriginRotation(0.0f) +{ + _name = COMPONENT_NAME; +} + +ComponentPhysics2d::ComponentPhysics2d(PhysicsBody* physicsBody) +: _physicsRotation(0.0f) +, _ownerOriginRotation(0.0f) +, _physicsBody(nullptr) // should set to null to invoke setPhysicsBody() +{ + _name = COMPONENT_NAME; + setPhysicsBody(physicsBody); +} + +ComponentPhysics2d::~ComponentPhysics2d() +{ + removePhysicsBody(); +} + +// true if two Vec3 equals, false else +bool ComponentPhysics2d::isVec3Equal(const cocos2d::Vec3 &v1, const cocos2d::Vec3 &v2) const +{ + return fabs(v1.x - v2.x) < FLT_EPSILON && + fabs(v1.x - v2.y) < FLT_EPSILON; +} + +void ComponentPhysics2d::beforeSimulation() +{ + if (nullptr == _physicsBody) + return; + + _nodeToWorldTransform = _owner->getNodeToWorldTransform(); + + // set scale + _nodeToWorldTransform.getScale(&_scale); + if (! isVec3Equal(_scale, _recordScale)) + { + _physicsBody->setScale(_scale.x, _scale.y); + _recordScale = _scale; + } + + // set rotation + if (_owner->getParent()) + { + _physicsRotation = getPhysicsRotation(_owner->getParent()) + _owner->getRotation(); + } + _physicsBody->setRotation(_physicsRotation - _ownerOriginRotation); + + // set position + auto worldPosition = _ownerCenterOffset; + _nodeToWorldTransform.transformPoint(&worldPosition); + _physicsBody->setPosition(Vec2(worldPosition.x, worldPosition.y)); + + getParentToWorldTransform().getInversed().transformPoint(&worldPosition); + _offset.x = worldPosition.x - _owner->getPosition().x; + _offset.y = worldPosition.y - _owner->getPosition().y; +} + +void ComponentPhysics2d::afterSimulation() +{ + if (nullptr == _physicsBody) + return; + + // set Node position + auto worldPosition = _physicsBody->getPosition(); + Vec3 positionInParent(worldPosition.x, worldPosition.y, 0); + getParentToWorldTransform().getInversed().transformPoint(&positionInParent); + _owner->setPosition(positionInParent.x - _offset.x, positionInParent.y - _offset.y); + + // set Node rotation + _physicsRotation = _physicsBody->getRotation(); + _owner->setRotation(_physicsRotation - getPhysicsRotation(_owner->getParent()) + _ownerOriginRotation); +} + +void ComponentPhysics2d::setPhysicsBody(PhysicsBody *physicsBody) +{ + removeFromPhysicsManager(); + + if (physicsBody != _physicsBody) + { + if (nullptr != _physicsBody) + { + _physicsBody->release(); + _physicsBody->_componentBelongsTo = nullptr; + } + + // two components should not share the same physics body + if (physicsBody && physicsBody->_componentBelongsTo != nullptr) + physicsBody->_componentBelongsTo->_physicsBody = nullptr; + + _physicsBody = physicsBody; + + if (nullptr != _physicsBody) + { + _physicsBody->retain(); + _physicsBody->_componentBelongsTo = this; + } + } + + addToPhysicsManager(); +} + +PhysicsBody* ComponentPhysics2d::getPhysicsBody() const +{ + return _physicsBody; +} + +void ComponentPhysics2d::setEnabled(bool value) +{ + Component::setEnabled(value); + + if (value) + addToPhysicsManager(); + else + removeFromPhysicsManager(); +} + +void ComponentPhysics2d::onEnter() +{ + addToPhysicsManager(); +} + +void ComponentPhysics2d::onExit() +{ + removeFromPhysicsManager(); +} + +void ComponentPhysics2d::onAdd() +{ + auto contentSize = _owner->getContentSize(); + _ownerCenterOffset.x = 0.5 * contentSize.width; + _ownerCenterOffset.y = 0.5 * contentSize.height; + + _ownerOriginRotation = _owner->getRotation(); + + // component may be added after onEnter() has been invoked, so we should add + // this line to make sure physics body is added to physics world + addToPhysicsManager(); +} + +void ComponentPhysics2d::onRemove() +{ + removePhysicsBody(); +} + +void ComponentPhysics2d::removePhysicsBody() +{ + removeFromPhysicsManager(); + + if (_physicsBody) + { + _physicsBody->_componentBelongsTo = nullptr; + _physicsBody->release(); + _physicsBody = nullptr; + } +} + +void ComponentPhysics2d::addToPhysicsManager() +{ + if (_owner) + { + auto scene = _owner->getScene(); + if (scene) + scene->getPhysicsManager()->addPhysicsComponent(this); + } + +} + +void ComponentPhysics2d::removeFromPhysicsManager() +{ + if (_owner) + { + auto scene = _owner->getScene(); + if (scene) + scene->getPhysicsManager()->removePhysicsComponent(this); + } +} + +Mat4 ComponentPhysics2d::getParentToWorldTransform() const +{ + if (_owner->getParent()) + return _owner->getParent()->getNodeToWorldTransform(); + else + return _owner->getNodeToWorldTransform(); +} + +float ComponentPhysics2d::getPhysicsRotation(Node *node) const +{ + if (!node) + return 0.0f; + + auto physicsComponent = node->getComponent(); + if (physicsComponent) + { + return physicsComponent->_physicsRotation; + } + else + { + auto parent = node->getParent(); + if (parent) + return getPhysicsRotation(parent) + node->getRotation(); + else + return 0.0f; + } +} + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos/physics/CCComponentPhysics2d.h b/cocos/physics/CCComponentPhysics2d.h new file mode 100644 index 0000000000..6814b883ef --- /dev/null +++ b/cocos/physics/CCComponentPhysics2d.h @@ -0,0 +1,140 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#pragma once + +#include "base/ccConfig.h" // to include defination of CC_USE_PHYSICS +#if CC_USE_PHYSICS + +#include "platform/CCPlatformMacros.h" +#include "2d/CCComponent.h" + +NS_CC_BEGIN + +/** + * @addtogroup physics + * @{ + * @addtogroup physics_2d + * @{ + */ + +class PhysicsBody; + +class CC_DLL ComponentPhysics2d : public Component +{ +public: + /** + * Create a physics component without physics body. Can set phyiscs body later with setPhysicsBody(). + * I am not sure if we need this function, because i think physics body is needed when creating a + * physics component. But i think it is needed by editor, so i keep this function. + * + * @return A pointer of `ComponentPhysics2d` that is autoreleased. + */ + static ComponentPhysics2d* create(); + /** + * Create a physics component with physics body. + * + * @param physicsBody The physics body that belongs to this component. + * @return A pointer of `ComponentPhysics2d` that is autoreleased. + */ + static ComponentPhysics2d* create(PhysicsBody *physicsBody); + + virtual ~ComponentPhysics2d(); + + /// @cond DO_NOT_SHOW + + /** + * Synchronize Node's status(position, rotation, scale) to phyiscs body. + * It is invoked before physics simulation. + * + * @warning Don't invoke this funciton by yourself. + */ + void beforeSimulation(); + /** + * Synchronize physics body's status(position, rotation) to phyiscs body. + * It is invoked after physics simulation. + * + * @warning Don't invoke this funciton by yourself. + */ + void afterSimulation(); + + /// @endcond DO_NOT_SHOW + + /** + * Set physics body of this physics component. If the physics body is set to + * another physics component before, will set another physics component's physics + * body to null. + * + * @param physicsBody The physics body belongs to this component. + */ + void setPhysicsBody(PhysicsBody *physicsBody); + /** + * Get the physics body of this component. + * + * @return The physics body of this component. + */ + PhysicsBody* getPhysicsBody() const; + + virtual void setEnabled(bool value) override; + virtual void onEnter() override; + virtual void onExit() override; + virtual void onAdd() override; + virtual void onRemove() override; + +public: + const static std::string COMPONENT_NAME; + +CC_CONSTRUCTOR_ACCESS: + ComponentPhysics2d(); + ComponentPhysics2d(PhysicsBody *phsicsBody); + +private: + void removePhysicsBody(); + void addToPhysicsManager(); + void removeFromPhysicsManager(); + Mat4 getParentToWorldTransform() const; + float getPhysicsRotation(Node *node) const; + bool isVec3Equal(const Vec3 &v1, const Vec3 &v2) const; + +private: + // this physic body of this component + PhysicsBody *_physicsBody; + // offset between owner's center point and down left point + Vec3 _ownerCenterOffset; + Mat4 _nodeToWorldTransform; + // offset of owner's center point and anchor point in parent coordinate + Vec2 _offset; + float _physicsRotation; + // the rotation of owner when the component is added to, the value will not change + float _ownerOriginRotation; + Vec3 _recordScale; + Vec3 _scale; +}; + +/** @} */ +/** @} */ + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos/physics/CCPhysicsBody.cpp b/cocos/physics/CCPhysicsBody.cpp index 188a16e00b..563bb9973f 100644 --- a/cocos/physics/CCPhysicsBody.cpp +++ b/cocos/physics/CCPhysicsBody.cpp @@ -31,10 +31,11 @@ #include "chipmunk.h" #include "2d/CCScene.h" -#include "CCPhysicsShape.h" -#include "CCPhysicsJoint.h" -#include "CCPhysicsWorld.h" -#include "CCPhysicsHelper.h" +#include "physics/CCPhysicsShape.h" +#include "physics/CCPhysicsJoint.h" +#include "physics/CCPhysicsWorld.h" +#include "physics/CCPhysicsHelper.h" +#include "physics/CCComponentPhysics2d.h" static inline void cpBodyUpdateVelocityWithoutGravity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { @@ -51,8 +52,7 @@ namespace } PhysicsBody::PhysicsBody() -: _node(nullptr) -, _world(nullptr) +: _world(nullptr) , _cpBody(nullptr) , _dynamic(true) , _enabled(true) @@ -68,10 +68,12 @@ PhysicsBody::PhysicsBody() , _linearDamping(0.0f) , _angularDamping(0.0f) , _tag(0) -, _positionInitDirty(true) , _rotationOffset(0) , _recordedRotation(0.0f) , _recordedAngle(0.0) +, _componentBelongsTo(nullptr) +, _massSetByUser(false) +, _momentSetByUser(false) { } @@ -334,8 +336,6 @@ void PhysicsBody::setGravityEnable(bool enable) void PhysicsBody::setPosition(const Vec2& position) { - _positionInitDirty = false; - _recordedPosition = position; cpBodySetPos(_cpBody, PhysicsHelper::point2cpv(position + _positionOffset)); } @@ -350,25 +350,25 @@ void PhysicsBody::setScale(float scaleX, float scaleY) { for (auto shape : _shapes) { + _area -= shape->getArea(); + if (!_massSetByUser) + addMass(-shape->getMass()); + if (!_momentSetByUser) + addMoment(-shape->getMoment()); + shape->setScale(scaleX, scaleY); + + _area += shape->getArea(); + if (!_massSetByUser) + addMass(shape->getMass()); + if (!_momentSetByUser) + addMoment(shape->getMoment()); } } -const Vec2& PhysicsBody::getPosition() +Vec2 PhysicsBody::getPosition() const { - if (_positionInitDirty) { - if (_node) { - if (_node->getParent()) { - _latestPosition = _node->getParent()->convertToWorldSpace(_node->getPosition()); - } else { - _latestPosition = _node->getPosition(); - } - } - } else { - _latestPosition.x = _cpBody->p.x - _positionOffset.x; - _latestPosition.y = _cpBody->p.y - _positionOffset.y; - } - return _latestPosition; + return Vec2(_cpBody->p.x - _positionOffset.x, _cpBody->p.y - _positionOffset.y); } float PhysicsBody::getRotation() @@ -450,6 +450,7 @@ void PhysicsBody::setMass(float mass) } _mass = mass; _massDefault = false; + _massSetByUser = true; // update density if (_mass == PHYSICS_INFINITY) @@ -628,6 +629,7 @@ void PhysicsBody::setMoment(float moment) { _moment = moment; _momentDefault = false; + _momentSetByUser = true; // the static body's mass and moment is always infinity if (_rotationEnabled && _dynamic) @@ -723,6 +725,14 @@ void PhysicsBody::removeFromWorld() } } +Node* PhysicsBody::getNode() const +{ + if (_componentBelongsTo) + return _componentBelongsTo->getOwner(); + else + return nullptr; +} + void PhysicsBody::setEnable(bool enable) { if (_enabled != enable) @@ -760,15 +770,12 @@ void PhysicsBody::setResting(bool rest) const void PhysicsBody::update(float delta) { - if (_node) + // damping compute + if (_isDamping && _dynamic && !isResting()) { - // damping compute - if (_isDamping && _dynamic && !isResting()) - { - _cpBody->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); - _cpBody->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); - _cpBody->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f); - } + _cpBody->v.x *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); + _cpBody->v.y *= cpfclamp(1.0f - delta * _linearDamping, 0.0f, 1.0f); + _cpBody->w *= cpfclamp(1.0f - delta * _angularDamping, 0.0f, 1.0f); } } diff --git a/cocos/physics/CCPhysicsBody.h b/cocos/physics/CCPhysicsBody.h index a47fd564a8..ad0db3d1a5 100644 --- a/cocos/physics/CCPhysicsBody.h +++ b/cocos/physics/CCPhysicsBody.h @@ -41,6 +41,7 @@ class Node; class Sprite; class PhysicsWorld; class PhysicsJoint; +class ComponentPhysics2d; typedef Vec2 Vect; @@ -305,7 +306,7 @@ public: inline const std::vector& getJoints() const { return _joints; } /** get the sprite the body set to. */ - inline Node* getNode() const { return _node; } + Node* getNode() const; /** * A mask that defines which categories this physics body belongs to. @@ -368,7 +369,7 @@ public: int getGroup() const; /** get the body position. */ - const Vec2& getPosition(); + Vec2 getPosition() const; /** get the body rotation. */ float getRotation(); @@ -505,7 +506,7 @@ public: Vec2 local2World(const Vec2& point); /** Get the rigid body of chipmunk. */ - cpBody* getCPBody() { return _cpBody; } + cpBody* getCPBody() const { return _cpBody; } protected: @@ -525,10 +526,11 @@ protected: virtual ~PhysicsBody(); protected: - Node* _node; std::vector _joints; Vector _shapes; PhysicsWorld* _world; + // weak reference + ComponentPhysics2d *_componentBelongsTo; cpBody* _cpBody; bool _dynamic; bool _enabled; @@ -545,9 +547,11 @@ protected: float _angularDamping; int _tag; - bool _positionInitDirty; - Vec2 _recordedPosition; - Vec2 _latestPosition; + // when setMass() is invoked, it means body's mass is not calculated by shapes + bool _massSetByUser; + // when setMoment() is invoked, it means body's moment is not calculated by shapes + bool _momentSetByUser; + Vec2 _positionOffset; float _rotationOffset; float _recordedRotation; @@ -559,6 +563,7 @@ protected: friend class Node; friend class Layer; friend class ProtectedNode; + friend class ComponentPhysics2d; }; /** @} */ diff --git a/cocos/physics/CCPhysicsManager.cpp b/cocos/physics/CCPhysicsManager.cpp new file mode 100644 index 0000000000..15a4d40583 --- /dev/null +++ b/cocos/physics/CCPhysicsManager.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "base/ccConfig.h" // to include defination of CC_USE_PHYSICS +#if CC_USE_PHYSICS + +#include "physics/CCPhysicsManager.h" +#include "physics/CCComponentPhysics2d.h" +#include "physics/CCPhysicsWorld.h" +#include "2d/CCScene.h" + +NS_CC_BEGIN + +PhysicsManager::PhysicsManager(Scene *scene) +: _scene(scene) +{ + _physicsWorld = PhysicsWorld::construct(); +} + +PhysicsManager::~PhysicsManager() +{ + delete _physicsWorld; +} + +void PhysicsManager::update(float dt) +{ + // Update physics position, should loop as the same sequence as node tree. + // ComponentPhysics2d::beforeSimulation() will depend on the sequence. + beforeSimulation(_scene); + + // do simulation + _physicsWorld->update(dt, false); + + // Update physics position, should loop as the same sequence as node tree. + // ComponentPhysics2d::afterSimulation() will depend on the sequence. + afterSimulation(_scene); +} + +void PhysicsManager::beforeSimulation(Node *node) +{ + auto iter = _owners.find(node); + if (iter != _owners.end()) + { + auto component = iter->second; + component->beforeSimulation(); + } + + for (auto child : node->getChildren()) + beforeSimulation(child); +} + +void PhysicsManager::afterSimulation(Node *node) +{ + auto iter = _owners.find(node); + if (iter != _owners.end()) + { + auto component = iter->second; + component->afterSimulation(); + } + + for (auto child : node->getChildren()) + afterSimulation(child); +} + +void PhysicsManager::addPhysicsComponent(ComponentPhysics2d* componentPhsics2d) +{ + // don't add component again + if (std::find(_components.begin(), _components.end(), componentPhsics2d) != _components.end()) + return; + + _components.push_back(componentPhsics2d); + // Node::getComponent<>() is a time comsuming operation, so record data to avoid invoking it. + std::pair element(componentPhsics2d->getOwner(), componentPhsics2d); + _owners.insert(element); + + if (nullptr != componentPhsics2d->getPhysicsBody()) + _physicsWorld->addBody(componentPhsics2d->getPhysicsBody()); +} + +void PhysicsManager::removePhysicsComponent(ComponentPhysics2d* componentPhsics2d) +{ + auto iter = std::find(_components.begin(), _components.end(), componentPhsics2d); + if (iter != _components.end()) + { + removeElementFromMap(*iter); + + _components.erase(iter); + if (componentPhsics2d->getPhysicsBody()) + _physicsWorld->removeBody(componentPhsics2d->getPhysicsBody()); + } +} + +void PhysicsManager::removeElementFromMap(ComponentPhysics2d* component) +{ + for (auto element : _owners) + { + if (element.second == component) + { + _owners.erase(element.first); + break; + } + } +} + +PhysicsWorld* PhysicsManager::getPhysicsWorld() const +{ + return _physicsWorld; +} + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos/physics/CCPhysicsManager.h b/cocos/physics/CCPhysicsManager.h new file mode 100644 index 0000000000..5c1ecc0931 --- /dev/null +++ b/cocos/physics/CCPhysicsManager.h @@ -0,0 +1,124 @@ +/**************************************************************************** + Copyright (c) 2015 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#pragma once + +#include "base/ccConfig.h" // to include defination of CC_USE_PHYSICS +#if CC_USE_PHYSICS + +#include +#include + +#include "platform/CCPlatformMacros.h" + +NS_CC_BEGIN + +class ComponentPhysics2d; +class PhysicsWorld; +class Scene; +class Node; + +/** + * @addtogroup physics + * @{ + * @addtogroup physics_2d + * @{ + */ + +/** + * Now `PhysicsManager` belongs to a `Scene`. `Scene` will create `PhysicsManager` automatically. + * The reason to design like this are: + * - PhysicsManager keeps physics world, it is reasonale that a scene has its own physics world. + * It is useful that when a scene is active again, you don't have to create physics world and + * do all related things again. + * - Keep compatibility + */ +class CC_DLL PhysicsManager +{ +public: + + /// @cond DO_NOT_SHOW + + /** + * Create a PhysicsManager with `Scene`. `Scene` will create it automatically, + * so don't create it yourself. + * + * @scene The scene this `PhysicsManager` belongs to. + */ + PhysicsManager(Scene *scene); + /** + * Destructor. + */ + ~PhysicsManager(); + + /** + * Do physics simulation. It will do the following things: + * - synchronize Node's status(position, rotation, scale) to corresponding physics body + * - do physics simulation + * - synchronize physics body's status(position, rotation) to correspondind Node + * + * It is invoked by `Scene`, don't invoke it yourself. + */ + void update(float dt); + + /// @endcond DO_NOT_SHOW + + /** + * Get the physics world. + * + * @return Physics world managed by this `PhysicsManager`. + */ + PhysicsWorld* getPhysicsWorld() const; + + /** + * Add a physics component to be managed by the `PhysicsManager`. Will register physics + * component's physics body to physics world managed by this `PhysicsManager`. + * + * @param componentPhsics2d The physics component to be managed by this `PhysicsManager`. + */ + void addPhysicsComponent(ComponentPhysics2d* componentPhsics2d); + /** + * Remove a physics component from `PhysiscsManager`. Will remove physics component's physics + * body from the physics world managed by this `PhysicsManager`. + */ + void removePhysicsComponent(ComponentPhysics2d* componentPhsics2d); + +private: + void beforeSimulation(Node *node); + void afterSimulation(Node* node); + void removeElementFromMap(ComponentPhysics2d* component); +private: + std::vector _components; + // record the owners of components for performance + std::unordered_map _owners; + PhysicsWorld *_physicsWorld; + Scene *_scene; +}; + +/** @} */ +/** @} */ + +NS_CC_END + +#endif // CC_USE_PHYSICS diff --git a/cocos/physics/CCPhysicsShape.cpp b/cocos/physics/CCPhysicsShape.cpp index 538778e7d1..9f5116305e 100644 --- a/cocos/physics/CCPhysicsShape.cpp +++ b/cocos/physics/CCPhysicsShape.cpp @@ -114,7 +114,7 @@ void PhysicsShape::setMaterial(const PhysicsMaterial& material) void PhysicsShape::setScale(float scaleX, float scaleY) { - if (_scaleX != scaleX || _scaleY != scaleY) + if (fabs(_scaleX - scaleX) > FLT_EPSILON || fabs(_scaleY - scaleY) > FLT_EPSILON) { if (_type == Type::CIRCLE && scaleX != scaleY) { @@ -123,7 +123,13 @@ void PhysicsShape::setScale(float scaleX, float scaleY) } _newScaleX = scaleX; _newScaleY = scaleY; + updateScale(); + + // re-calculate area and mass + _area = calculateArea(); + _mass = _material.density * _area; + _moment = calculateDefaultMoment(); } } @@ -649,10 +655,14 @@ void PhysicsShapePolygon::updateScale() for (int i = 0; i < count; ++i) { - cpVect n = cpvnormalize(cpvperp(cpvsub(vects[i], vects[(i + 1) % count]))); - - planes[i].n = n; - planes[i].d = cpvdot(n, vects[i]); +// cpVect n = cpvnormalize(cpvperp(cpvsub(vects[i], vects[(i + 1) % count]))); +// +// planes[i].n = n; +// planes[i].d = cpvdot(n, vects[i]); + + // FIXED ME: if update 'planes[i]' as the above codes, then can not query polygon shape by PhysicsWorld::getShapes(). + // But modified like this, then ray test can not work correctly on some cases. + planes[i].d = cpvdot(planes[i].n, vects[i]); } PhysicsShape::updateScale(); diff --git a/cocos/physics/CCPhysicsWorld.cpp b/cocos/physics/CCPhysicsWorld.cpp index 117d4627c1..1ceb0824f4 100644 --- a/cocos/physics/CCPhysicsWorld.cpp +++ b/cocos/physics/CCPhysicsWorld.cpp @@ -281,7 +281,7 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact) { contact.setEventCode(PhysicsContact::EventCode::BEGIN); contact.setWorld(this); - _scene->getEventDispatcher()->dispatchEvent(&contact); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&contact); } return ret ? contact.resetResult() : false; @@ -296,7 +296,7 @@ int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact) contact.setEventCode(PhysicsContact::EventCode::PRESOLVE); contact.setWorld(this); - _scene->getEventDispatcher()->dispatchEvent(&contact); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&contact); return contact.resetResult(); } @@ -310,7 +310,7 @@ void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact) contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE); contact.setWorld(this); - _scene->getEventDispatcher()->dispatchEvent(&contact); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&contact); } void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact) @@ -322,7 +322,7 @@ void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact) contact.setEventCode(PhysicsContact::EventCode::SEPARATE); contact.setWorld(this); - _scene->getEventDispatcher()->dispatchEvent(&contact); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&contact); } void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vec2& point1, const Vec2& point2, void* data) @@ -333,7 +333,6 @@ void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vec2& point1, { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } RayCastCallbackInfo info = { this, func, point1, point2, data }; @@ -357,7 +356,6 @@ void PhysicsWorld::queryRect(PhysicsQueryRectCallbackFunc func, const Rect& rect { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } RectQueryCallbackInfo info = {this, func, data}; @@ -380,7 +378,6 @@ void PhysicsWorld::queryPoint(PhysicsQueryPointCallbackFunc func, const Vec2& po { if (!_delayAddBodies.empty() || !_delayRemoveBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); } PointQueryCallbackInfo info = {this, func, data}; @@ -422,10 +419,10 @@ PhysicsShape* PhysicsWorld::getShape(const Vec2& point) const return shape == nullptr ? nullptr : s_physicsShapeMap.find(shape)->second; } -PhysicsWorld* PhysicsWorld::construct(Scene& scene) +PhysicsWorld* PhysicsWorld::construct() { PhysicsWorld * world = new (std::nothrow) PhysicsWorld(); - if(world && world->init(scene)) + if(world && world->init()) { return world; } @@ -434,15 +431,13 @@ PhysicsWorld* PhysicsWorld::construct(Scene& scene) return nullptr; } -bool PhysicsWorld::init(Scene& scene) +bool PhysicsWorld::init() { do { _cpSpace = cpSpaceNew(); CC_BREAK_IF(_cpSpace == nullptr); - _scene = &scene; - cpSpaceSetGravity(_cpSpace, PhysicsHelper::point2cpv(_gravity)); cpSpaceSetDefaultCollisionHandler(_cpSpace, @@ -817,11 +812,9 @@ void PhysicsWorld::step(float delta) void PhysicsWorld::update(float delta, bool userCall/* = false*/) { - if(_updateBodyTransform || !_delayAddBodies.empty()) + if(!_delayAddBodies.empty()) { - _scene->updatePhysicsBodyTransform(_scene->getNodeToParentTransform(), 0, 1.0f, 1.0f); updateBodies(); - _updateBodyTransform = false; } else if (!_delayRemoveBodies.empty()) { @@ -904,7 +897,7 @@ PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world) , _world(world) { _drawNode = DrawNode::create(); - _world.getScene().addChild(_drawNode); + Director::getInstance()->getRunningScene()->addChild(_drawNode); } PhysicsDebugDraw::~PhysicsDebugDraw() diff --git a/cocos/physics/CCPhysicsWorld.h b/cocos/physics/CCPhysicsWorld.h index 4f45d6554e..107dcac932 100644 --- a/cocos/physics/CCPhysicsWorld.h +++ b/cocos/physics/CCPhysicsWorld.h @@ -329,8 +329,9 @@ public: void step(float delta); protected: - static PhysicsWorld* construct(Scene& scene); - bool init(Scene& scene); + static PhysicsWorld* construct(); + bool init(); + virtual void addBody(PhysicsBody* body); virtual void addShape(PhysicsShape* shape); @@ -389,6 +390,7 @@ protected: friend class PhysicsJoint; friend class PhysicsWorldCallback; friend class PhysicsDebugDraw; + friend class PhysicsManager; }; /** A physics helper class. Draw physics shape, joint in debug mode. diff --git a/cocos/physics/CMakeLists.txt b/cocos/physics/CMakeLists.txt index 22d5349bdf..4a2b9447d6 100644 --- a/cocos/physics/CMakeLists.txt +++ b/cocos/physics/CMakeLists.txt @@ -6,5 +6,6 @@ set(COCOS_PHYSICS_SRC physics/CCPhysicsJoint.cpp physics/CCPhysicsShape.cpp physics/CCPhysicsWorld.cpp - + physics/CCComponentPhysics2d.cpp + physics/CCPhysicsManager.cpp ) diff --git a/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp b/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp index 641413e080..cede2b32f3 100644 --- a/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp @@ -2379,157 +2379,6 @@ tolua_lerror: return 0; } -#if CC_USE_PHYSICS - -int lua_cocos2dx_Node_setPhysicsBody(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::Node* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.Node",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::Node*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_Node_setPhysicsBody'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 1) - { - cocos2d::PhysicsBody* arg0; - - ok &= luaval_to_object(tolua_S, 2, "cc.PhysicsBody",&arg0, "cc.Node:setPhysicsBody"); - if(!ok) - { - tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Node_setPhysicsBody'", nullptr); - return 0; - } - cobj->setPhysicsBody(arg0); - lua_settop(tolua_S, 1); - return 1; - } - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Node:setPhysicsBody",argc, 1); - return 0; - -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_Node_setPhysicsBody'.",&tolua_err); -#endif - - return 0; -} - -int lua_cocos2dx_Node_removeFromPhysicsWorld(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::Node* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.Node",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::Node*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_Node_removeFromPhysicsWorld'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 0) - { - if(!ok) - { - tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Node_removeFromPhysicsWorld'", nullptr); - return 0; - } - cobj->removeFromPhysicsWorld(); - lua_settop(tolua_S, 1); - return 1; - } - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Node:removeFromPhysicsWorld",argc, 0); - return 0; - -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_Node_removeFromPhysicsWorld'.",&tolua_err); -#endif - - return 0; -} - -int lua_cocos2dx_Node_getPhysicsBody(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::Node* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.Node",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::Node*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_Node_getPhysicsBody'", nullptr); - return 0; - } -#endif - - argc = lua_gettop(tolua_S)-1; - if (argc == 0) - { - if(!ok) - { - tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_Node_getPhysicsBody'", nullptr); - return 0; - } - cocos2d::PhysicsBody* ret = cobj->getPhysicsBody(); - object_to_luaval(tolua_S, "cc.PhysicsBody",(cocos2d::PhysicsBody*)ret); - return 1; - } - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.Node:getPhysicsBody",argc, 0); - return 0; - -#if COCOS2D_DEBUG >= 1 -tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_Node_getPhysicsBody'.",&tolua_err); -#endif - - return 0; -} - -#endif //CC_USE_PHYSICS - #if CC_USE_NAVMESH #include "navmesh/CCNavMesh.h" int lua_cocos2dx_Scene_setNavMeshDebugCamera(lua_State* tolua_S) @@ -5083,18 +4932,6 @@ static void extendNode(lua_State* tolua_S) lua_pushstring(tolua_S, "setRotationQuat"); lua_pushcfunction(tolua_S, lua_cocos2dx_Node_setRotationQuat); lua_rawset(tolua_S, -3); -#if CC_USE_PHYSICS - lua_pushstring(tolua_S, "setPhysicsBody"); - lua_pushcfunction(tolua_S, lua_cocos2dx_Node_setPhysicsBody); - lua_rawset(tolua_S, -3); - lua_pushstring(tolua_S, "removeFromPhysicsWorld"); - lua_pushcfunction(tolua_S, lua_cocos2dx_Node_removeFromPhysicsWorld); - lua_rawset(tolua_S, -3); - lua_pushstring(tolua_S, "getPhysicsBody"); - lua_pushcfunction(tolua_S, lua_cocos2dx_Node_getPhysicsBody); - lua_rawset(tolua_S, -3); -#endif //CC_USE_PHYSICS - } lua_pop(tolua_S, 1); } diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp index c4441e7099..b63d7b63fd 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp @@ -1,18 +1,22 @@ #include "PhysicsTest.h" + +#if CC_USE_PHYSICS + #include -#include "../testResource.h" #include "ui/CocosGUI.h" +#include "physics/CCComponentPhysics2d.h" +#include "../testResource.h" + USING_NS_CC; PhysicsTests::PhysicsTests() { -#if CC_USE_PHYSICS ADD_TEST_CASE(PhysicsDemoLogoSmash); ADD_TEST_CASE(PhysicsDemoPyramidStack); ADD_TEST_CASE(PhysicsDemoClickAdd); ADD_TEST_CASE(PhysicsDemoRayCast); - ADD_TEST_CASE(PhysicsDemoJoints); ADD_TEST_CASE(PhysicsDemoActions); + ADD_TEST_CASE(PhysicsDemoJoints); ADD_TEST_CASE(PhysicsDemoPump); ADD_TEST_CASE(PhysicsDemoOneWayPlatform); ADD_TEST_CASE(PhysicsDemoSlice); @@ -20,38 +24,26 @@ PhysicsTests::PhysicsTests() ADD_TEST_CASE(PhysicsContactTest); ADD_TEST_CASE(PhysicsPositionRotationTest); ADD_TEST_CASE(PhysicsSetGravityEnableTest); - ADD_TEST_CASE(Bug5482); + ADD_TEST_CASE(PhysicsDemoBug5482); ADD_TEST_CASE(PhysicsFixedUpdate); ADD_TEST_CASE(PhysicsTransformTest); ADD_TEST_CASE(PhysicsIssue9959); -#else - ADD_TEST_CASE(PhysicsDemoDisabled); -#endif } - -static const Color4F STATIC_COLOR(1.0f, 0.0f, 0.0f, 1.0f); -static const int DRAG_BODYS_TAG = 0x80; + +namespace +{ + Color4F STATIC_COLOR(1.0f, 0.0f, 0.0f, 1.0f); + const int DRAG_BODYS_TAG = 0x80; +} void PhysicsDemo::toggleDebug() { #if CC_USE_PHYSICS _debugDraw = !_debugDraw; - getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE); + _physicsWorld->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE); #endif } -#if CC_USE_PHYSICS == 0 -void PhysicsDemoDisabled::onEnter() -{ - auto label = Label::createWithTTF("Should define CC_USE_PHYSICS\n to run this test case", - "fonts/arial.ttf", - 18); - auto size = Director::getInstance()->getWinSize(); - label->setPosition(Vec2(size.width/2, size.height/2)); - - addChild(label); -} -#else PhysicsDemo::PhysicsDemo() : _spriteTexture(nullptr) @@ -60,22 +52,19 @@ PhysicsDemo::PhysicsDemo() { } -PhysicsDemo::~PhysicsDemo() -{ - -} - bool PhysicsDemo::init() { - if (TestCase::init()) - { - return initWithPhysics(); - } - return false; + TestCase::init(); + return initWithPhysics(); } + +PhysicsDemo::~PhysicsDemo() +{ +} + std::string PhysicsDemo::title() const { - return "PhysicsTest"; + return "PhysicsComponentTest"; } void PhysicsDemo::onEnter() @@ -90,12 +79,12 @@ void PhysicsDemo::onEnter() auto menu = Menu::create(item, nullptr); this->addChild(menu); - menu->setPosition(Vec2(VisibleRect::right().x-50, VisibleRect::top().y-10)); + menu->setPosition(Vec2(VisibleRect::right().x - item->getContentSize().width / 2 - 10, VisibleRect::top().y - item->getContentSize().height / 2 - 10)); } Sprite* PhysicsDemo::addGrossiniAtPosition(Vec2 p, float scale/* = 1.0*/) { - CCLOG("Add sprite %0.2f x %02.f",p.x,p.y); + CCLOG("Add sprite %0.2f x %02.f", p.x, p.y); int posx, posy; @@ -106,10 +95,12 @@ Sprite* PhysicsDemo::addGrossiniAtPosition(Vec2 p, float scale/* = 1.0*/) posy = (posy % 3) * 121; auto sp = Sprite::createWithTexture(_spriteTexture, Rect(posx, posy, 85, 121)); + sp->setScale(scale); - sp->setPhysicsBody(PhysicsBody::createBox(Size(48.0f * scale, 108.0f * scale))); - this->addChild(sp); sp->setPosition(p); + addPhysicsComponent(sp, PhysicsBody::createBox(Size(48.0f, 108.0f))); + //addPhysicsComponent(sp, PhysicsBody::createCircle(sp->getContentSize().height/2)); + this->addChild(sp); return sp; } @@ -119,116 +110,56 @@ void PhysicsDemo::toggleDebugCallback(Ref* sender) toggleDebug(); } -PhysicsDemoClickAdd::~PhysicsDemoClickAdd() -{ - Device::setAccelerometerEnabled(false); -} - -void PhysicsDemoClickAdd::onEnter() -{ - PhysicsDemo::onEnter(); - - auto touchListener = EventListenerTouchAllAtOnce::create(); - touchListener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoClickAdd::onTouchesEnded, this); - _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); - - Device::setAccelerometerEnabled(true); - auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this)); - _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this); - - auto node = Node::create(); - node->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); - node->setPosition(VisibleRect::center()); - this->addChild(node); - - addGrossiniAtPosition(VisibleRect::center()); -} - -std::string PhysicsDemoClickAdd::subtitle() const -{ - return "multi touch to add grossini"; -} - -void PhysicsDemoClickAdd::onTouchesEnded(const std::vector& touches, Event* event) -{ - //Add a new body/atlas sprite at the touched location - - for( auto &touch: touches) - { - auto location = touch->getLocation(); - - addGrossiniAtPosition( location ); - } -} - -void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event) -{ - static float prevX=0, prevY=0; - -#define kFilterFactor 0.05f - - float accelX = (float) acc->x * kFilterFactor + (1- kFilterFactor)*prevX; - float accelY = (float) acc->y * kFilterFactor + (1- kFilterFactor)*prevY; - - prevX = accelX; - prevY = accelY; - - auto v = Vec2( accelX, accelY); - v = v * 200; - - getPhysicsWorld()->setGravity(v); -} - namespace { - static const int logo_width = 188; - static const int logo_height = 35; - static const int logo_row_length = 24; - static const char logo_image[] = + const int LOGO_WIDTH = 188; + const int LOGO_HEIGHT = 35; + const int LOGO_RAW_LENGTH = 24; + const char LOGO_IMAGE[] = { - 15,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-64,15,63,-32,-2,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,0,31,-64,15,127,-125,-1,-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,0,127,-64,15,127,15,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-2, - 31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, - 1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32, - -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0, - 0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31, - -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, - -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, - -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, - -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, - -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, - 15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13, - -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, - -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, - -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, - -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, - -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, - -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, - -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, - 127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16, - 63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0 + 15, -16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, -64, 15, 63, -32, -2, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, -64, 15, 127, -125, -1, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 127, -64, 15, 127, 15, -1, -64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 15, -2, + 31, -1, -64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 0, -4, 63, -1, -32, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -64, 15, -8, 127, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, -1, -64, 0, -8, -15, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -31, -1, -64, 15, -8, -32, + -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, -15, -1, -64, 9, -15, -32, -1, -32, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, -15, -1, -64, 0, -15, -32, -1, -32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 63, -7, -1, -64, 9, -29, -32, 127, -61, -16, 63, 15, -61, -1, -8, 31, -16, 15, -8, 126, 7, -31, + -8, 31, -65, -7, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -2, 63, -8, 31, -4, -1, 15, -13, + -4, 63, -1, -3, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -2, 63, -8, 31, -4, -1, 15, -13, + -2, 63, -1, -3, -1, -64, 9, -29, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13, + -2, 63, -33, -1, -1, -32, 9, -25, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13, + -1, 63, -33, -1, -1, -16, 9, -25, -32, 0, 7, -8, 127, -97, -25, -1, -1, 63, -4, 63, -4, -1, 15, -13, + -1, 63, -49, -1, -1, -8, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13, + -1, -65, -49, -1, -1, -4, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13, + -1, -65, -57, -1, -1, -2, 9, -57, -32, 0, 7, -8, 127, -97, -25, -8, -1, 63, -2, 127, -4, -1, 15, -13, + -1, -1, -57, -1, -1, -1, 9, -57, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1, + -1, -61, -1, -1, -1, -119, -57, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1, + -1, -61, -1, -1, -1, -55, -49, -32, 0, 7, -1, -1, -97, -25, -8, -1, 63, -1, -1, -4, -1, 15, -13, -1, + -1, -63, -1, -1, -1, -23, -49, -32, 127, -57, -1, -1, -97, -25, -1, -1, 63, -1, -1, -4, -1, 15, -13, + -1, -1, -63, -1, -1, -1, -16, -49, -32, -1, -25, -1, -1, -97, -25, -1, -1, 63, -33, -5, -4, -1, 15, + -13, -1, -1, -64, -1, -9, -1, -7, -49, -32, -1, -25, -8, 127, -97, -25, -1, -1, 63, -33, -5, -4, -1, + 15, -13, -1, -1, -64, -1, -13, -1, -32, -49, -32, -1, -25, -8, 127, -97, -25, -1, -2, 63, -49, -13, + -4, -1, 15, -13, -1, -1, -64, 127, -7, -1, -119, -17, -15, -1, -25, -8, 127, -97, -25, -1, -2, 63, + -49, -13, -4, -1, 15, -13, -3, -1, -64, 127, -8, -2, 15, -17, -1, -1, -25, -8, 127, -97, -25, -1, + -8, 63, -49, -13, -4, -1, 15, -13, -3, -1, -64, 63, -4, 120, 0, -17, -1, -1, -25, -8, 127, -97, -25, + -8, 0, 63, -57, -29, -4, -1, 15, -13, -4, -1, -64, 63, -4, 0, 15, -17, -1, -1, -25, -8, 127, -97, + -25, -8, 0, 63, -57, -29, -4, -1, -1, -13, -4, -1, -64, 31, -2, 0, 0, 103, -1, -1, -57, -8, 127, -97, + -25, -8, 0, 63, -57, -29, -4, -1, -1, -13, -4, 127, -64, 31, -2, 0, 15, 103, -1, -1, -57, -8, 127, + -97, -25, -8, 0, 63, -61, -61, -4, 127, -1, -29, -4, 127, -64, 15, -8, 0, 0, 55, -1, -1, -121, -8, + 127, -97, -25, -8, 0, 63, -61, -61, -4, 127, -1, -29, -4, 63, -64, 15, -32, 0, 0, 23, -1, -2, 3, -16, + 63, 15, -61, -16, 0, 31, -127, -127, -8, 31, -1, -127, -8, 31, -128, 7, -128, 0, 0 }; - static inline int get_pixel(int x, int y) + int getPixel(int x, int y) { - return (logo_image[(x>>3) + y*logo_row_length]>>(~x&0x7)) & 1; + return (LOGO_IMAGE[(x >> 3) + y * LOGO_RAW_LENGTH] >> (~x & 0x7)) & 1; } - static inline float frand(void) + float frand(void) { - return rand()/RAND_MAX; + return rand() / RAND_MAX; } } @@ -236,17 +167,13 @@ Sprite* PhysicsDemo::makeBall(Vec2 point, float radius, PhysicsMaterial material { Sprite* ball = nullptr; if (_ball != nullptr) - { ball = Sprite::createWithTexture(_ball->getTexture()); - }else - { + else ball = Sprite::create("Images/ball.png"); - } ball->setScale(0.13f * radius); - auto body = PhysicsBody::createCircle(radius, material); - ball->setPhysicsBody(body); + addPhysicsComponent(ball, PhysicsBody::createCircle(ball->getContentSize().width / 2, material)); ball->setPosition(Vec2(point.x, point.y)); return ball; @@ -258,18 +185,18 @@ Sprite* PhysicsDemo::makeBox(Vec2 point, Size size, int color, PhysicsMaterial m if (color == 0) { yellow = CCRANDOM_0_1() > 0.5f; - }else + } + else { yellow = color == 1; } auto box = yellow ? Sprite::create("Images/YellowSquare.png") : Sprite::create("Images/CyanSquare.png"); + box->setScaleX(size.width / 100.0f); + box->setScaleY(size.height / 100.0f); - box->setScaleX(size.width/100.0f); - box->setScaleY(size.height/100.0f); + addPhysicsComponent(box, PhysicsBody::createBox(box->getContentSize(), material)); - auto body = PhysicsBody::createBox(size, material); - box->setPhysicsBody(body); box->setPosition(Vec2(point.x, point.y)); return box; @@ -281,26 +208,27 @@ Sprite* PhysicsDemo::makeTriangle(Vec2 point, Size size, int color, PhysicsMater if (color == 0) { yellow = CCRANDOM_0_1() > 0.5f; - }else + } + else { yellow = color == 1; } auto triangle = yellow ? Sprite::create("Images/YellowTriangle.png") : Sprite::create("Images/CyanTriangle.png"); - if(size.height == 0) + if (size.height == 0) { - triangle->setScale(size.width/100.0f); - }else + triangle->setScale(size.width / 100.0f); + } + else { - triangle->setScaleX(size.width/50.0f); - triangle->setScaleY(size.height/43.5f); + triangle->setScaleX(size.width / 50.0f); + triangle->setScaleY(size.height / 43.5f); } - Vec2 vers[] = { Vec2(0, size.height/2), Vec2(size.width/2, -size.height/2), Vec2(-size.width/2, -size.height/2)}; + Vec2 vers[] = { Vec2(0, triangle->getContentSize().height / 2), Vec2(triangle->getContentSize().width / 2, -triangle->getContentSize().height / 2), Vec2(-triangle->getContentSize().width / 2, -triangle->getContentSize().height / 2) }; - auto body = PhysicsBody::createPolygon(vers, 3, material); - triangle->setPhysicsBody(body); + addPhysicsComponent(triangle, PhysicsBody::createPolygon(vers, 3, material)); triangle->setPosition(Vec2(point.x, point.y)); return triangle; @@ -309,7 +237,7 @@ Sprite* PhysicsDemo::makeTriangle(Vec2 point, Size size, int color, PhysicsMater bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event) { auto location = touch->getLocation(); - auto arr = getPhysicsWorld()->getShapes(location); + auto arr = _physicsWorld->getShapes(location); PhysicsBody* body = nullptr; for (auto& obj : arr) @@ -324,13 +252,14 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event) if (body != nullptr) { Node* mouse = Node::create(); - mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY)); - mouse->getPhysicsBody()->setDynamic(false); + auto physicsBody = PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY); + physicsBody->setDynamic(false); + addPhysicsComponent(mouse, physicsBody); mouse->setPosition(location); this->addChild(mouse); - PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location); + PhysicsJointPin* joint = PhysicsJointPin::construct(physicsBody, body, location); joint->setMaxForce(5000.0f * body->getMass()); - getPhysicsWorld()->addJoint(joint); + _physicsWorld->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(), mouse)); return true; @@ -360,40 +289,49 @@ void PhysicsDemo::onTouchEnded(Touch* touch, Event* event) } } +void PhysicsDemo::addPhysicsComponent(Node *node, PhysicsBody *physicsBody) +{ + auto physicsComponent = ComponentPhysics2d::create(); + physicsComponent->setPhysicsBody(physicsBody); + node->addComponent(physicsComponent); +} + +// Implementation of PhysicsComponentDemoLogoSmash + void PhysicsDemoLogoSmash::onEnter() { PhysicsDemo::onEnter(); - getPhysicsWorld()->setGravity(Vec2(0, 0)); - getPhysicsWorld()->setUpdateRate(5.0f); + _physicsWorld->setGravity(Vec2(0, 0)); + _physicsWorld->setUpdateRate(5.0f); - _ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0])); + _ball = SpriteBatchNode::create("Images/ball.png", sizeof(LOGO_IMAGE)/sizeof(LOGO_IMAGE[0])); addChild(_ball); - for (int y = 0; y < logo_height; ++y) + for (int y = 0; y < LOGO_HEIGHT; ++y) { - for (int x = 0; x < logo_width; ++x) + for (int x = 0; x < LOGO_WIDTH; ++x) { - if (get_pixel(x, y)) + if (getPixel(x, y)) { - float x_jitter = 0.05*frand(); - float y_jitter = 0.05*frand(); + float xJitter = 0.05 * frand(); + float yJitter = 0.05 * frand(); - Node* ball = makeBall(Vec2(2*(x - logo_width/2 + x_jitter) + VisibleRect::getVisibleRect().size.width/2, - 2*(logo_height-y + y_jitter) + VisibleRect::getVisibleRect().size.height/2 - logo_height/2), + Node* ball = makeBall(Vec2(2*(x - LOGO_WIDTH/2 + xJitter) + VisibleRect::getVisibleRect().size.width/2, + 2*(LOGO_HEIGHT-y + yJitter) + VisibleRect::getVisibleRect().size.height/2 - LOGO_HEIGHT/2), 0.95f, PhysicsMaterial(0.01f, 0.0f, 0.0f)); - ball->getPhysicsBody()->setMass(1.0); - ball->getPhysicsBody()->setMoment(PHYSICS_INFINITY); + auto physicsBody = ball->getComponent()->getPhysicsBody(); + physicsBody->setMass(1.0); + physicsBody->setMoment(PHYSICS_INFINITY); _ball->addChild(ball); - } } } auto bullet = makeBall(Vec2(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); - bullet->getPhysicsBody()->setVelocity(Vec2(200, 0)); + bullet->getComponent()->getPhysicsBody()->setVelocity(Vec2(200, 0)); bullet->setPosition(Vec2(-500, VisibleRect::getVisibleRect().size.height/2)); @@ -405,13 +343,66 @@ std::string PhysicsDemoLogoSmash::title() const return "Logo Smash"; } -void PhysicsDemoPyramidStack::updateOnce(float delta) +// Implementation of PhysicsComponentDemoClickAdd + +PhysicsDemoClickAdd::~PhysicsDemoClickAdd() { - auto ball = getChildByTag(100); + Device::setAccelerometerEnabled(false); +} + +void PhysicsDemoClickAdd::onEnter() +{ + PhysicsDemo::onEnter(); - ball->setScale(ball->getScale() * 3); - ball->setPhysicsBody(PhysicsBody::createCircle(30)); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto touchListener = EventListenerTouchAllAtOnce::create(); + touchListener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoClickAdd::onTouchesEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + + Device::setAccelerometerEnabled(true); + auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this)); + _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this); + + auto node = Node::create(); + addPhysicsComponent(node, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); + node->setPosition(VisibleRect::center()); + this->addChild(node); + + addGrossiniAtPosition(VisibleRect::center()); +} + +std::string PhysicsDemoClickAdd::subtitle() const +{ + return "multi touch to add grossini"; +} + +void PhysicsDemoClickAdd::onTouchesEnded(const std::vector& touches, Event* event) +{ + //Add a new body/atlas sprite at the touched location + + for( auto touch: touches) + { + auto location = touch->getLocation(); + + addGrossiniAtPosition( location ); + } +} + +void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event) +{ + static float prevX=0, prevY=0; + +#define FILTER_FACTOR 0.05f + + float accelX = (float) acc->x * FILTER_FACTOR + (1- FILTER_FACTOR)*prevX; + float accelY = (float) acc->y * FILTER_FACTOR + (1- FILTER_FACTOR)*prevY; + + prevX = accelX; + prevY = accelY; + + auto v = Vec2( accelX, accelY); + v = v * 200; + + _physicsWorld->setGravity(v); } void PhysicsDemoPyramidStack::onEnter() @@ -425,28 +416,36 @@ void PhysicsDemoPyramidStack::onEnter() _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto node = Node::create(); - node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); + addPhysicsComponent(node, PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); this->addChild(node); auto ball = Sprite::create("Images/ball.png"); ball->setScale(1); ball->setTag(100); - ball->setPhysicsBody(PhysicsBody::createCircle(10)); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + addPhysicsComponent(ball, PhysicsBody::createCircle(10)); + ball->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); ball->setPosition(VisibleRect::bottom() + Vec2(0, 60)); this->addChild(ball); scheduleOnce(CC_SCHEDULE_SELECTOR(PhysicsDemoPyramidStack::updateOnce), 3.0); - - for(int i=0; i<14; i++) + + for (int i = 0; i < 14; i++) { - for(int j=0; j<=i; j++) + for (int j = 0; j <= i; j++) { - auto sp = addGrossiniAtPosition(VisibleRect::bottom() + Vec2((i/2 - j) * 11, (14 - i) * 23 + 100), 0.2f); - sp->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp = addGrossiniAtPosition(VisibleRect::bottom() + Vec2((i / 2 - j) * 11, (14 - i) * 23 + 100), 0.2f); + sp->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); } } } + +void PhysicsDemoPyramidStack::updateOnce(float delta) +{ + auto ball = getChildByTag(100); + if (ball) + ball->setScale(ball->getScale() * 3); +} + std::string PhysicsDemoPyramidStack::title() const { return "Pyramid Stack"; @@ -466,10 +465,10 @@ void PhysicsDemoRayCast::onEnter() listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); - getPhysicsWorld()->setGravity(Point::ZERO); + _physicsWorld->setGravity(Point::ZERO); auto node = DrawNode::create(); - node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); + addPhysicsComponent(node, PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); node->drawSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50), 1, STATIC_COLOR); this->addChild(node); @@ -478,7 +477,7 @@ void PhysicsDemoRayCast::onEnter() auto menu = Menu::create(item, nullptr); this->addChild(menu); - menu->setPosition(Vec2(VisibleRect::left().x+100, VisibleRect::top().y-10)); + menu->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::top().y - 10)); scheduleUpdate(); } @@ -526,7 +525,7 @@ void PhysicsDemoRayCast::update(float delta) Vec2 point3 = point2; auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this); - getPhysicsWorld()->rayCast(func, point1, point2, &point3); + _physicsWorld->rayCast(func, point1, point2, &point3); _node->drawSegment(point1, point3, 1, STATIC_COLOR); if (point2 != point3) @@ -552,7 +551,7 @@ void PhysicsDemoRayCast::update(float delta) return true; }; - getPhysicsWorld()->rayCast(func, point1, point2, nullptr); + _physicsWorld->rayCast(func, point1, point2, nullptr); _node->drawSegment(point1, point3, 1, STATIC_COLOR); if (point2 != point3) @@ -579,7 +578,7 @@ void PhysicsDemoRayCast::update(float delta) return true; }; - getPhysicsWorld()->rayCast(func, point1, point2, nullptr); + _physicsWorld->rayCast(func, point1, point2, nullptr); _node->drawSegment(point1, point2, 1, STATIC_COLOR); @@ -604,21 +603,23 @@ void PhysicsDemoRayCast::onTouchesEnded(const std::vector& touches, Even { //Add a new body/atlas sprite at the touched location - for( auto &touch: touches) + for (auto &touch : touches) { auto location = touch->getLocation(); float r = CCRANDOM_0_1(); - if (r < 1.0f/3.0f) + if (r < 1.0f / 3.0f) { - addChild(makeBall(location, 5 + CCRANDOM_0_1()*10)); - }else if(r < 2.0f/3.0f) + addChild(makeBall(location, 5 + CCRANDOM_0_1() * 10)); + } + else if (r < 2.0f / 3.0f) { - addChild(makeBox(location, Size(10 + CCRANDOM_0_1()*15, 10 + CCRANDOM_0_1()*15))); - }else + addChild(makeBox(location, Size(10 + CCRANDOM_0_1() * 15, 10 + CCRANDOM_0_1() * 15))); + } + else { - addChild(makeTriangle(location, Size(10 + CCRANDOM_0_1()*20, 10 + CCRANDOM_0_1()*20))); + addChild(makeTriangle(location, Size(10 + CCRANDOM_0_1() * 20, 10 + CCRANDOM_0_1() * 20))); } } } @@ -628,15 +629,63 @@ std::string PhysicsDemoRayCast::title() const return "Ray Cast"; } +void PhysicsDemoActions::onEnter() +{ + PhysicsDemo::onEnter(); + _physicsWorld->setGravity(Vect::ZERO); + + auto touchListener = EventListenerTouchOneByOne::create(); + touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoActions::onTouchBegan, this); + touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoActions::onTouchMoved, this); + touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoActions::onTouchEnded, this); + _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); + + auto node = Node::create(); + addPhysicsComponent(node, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); + node->setPosition(VisibleRect::center()); + this->addChild(node); + + Sprite* sp1 = addGrossiniAtPosition(VisibleRect::center()); + Sprite* sp2 = addGrossiniAtPosition(VisibleRect::left() + Vec2(50, 0)); + Sprite* sp3 = addGrossiniAtPosition(VisibleRect::right() - Vec2(20, 0)); + Sprite* sp4 = addGrossiniAtPosition(VisibleRect::leftTop() + Vec2(50, -50)); + sp4->getComponent()->getPhysicsBody()->setGravityEnable(false); + + sp1->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + sp2->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + sp3->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + sp4->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + + auto actionTo = JumpTo::create(2, Vec2(100, 100), 50, 4); + auto actionBy = JumpBy::create(2, Vec2(300, 0), 50, 4); + auto actionUp = JumpBy::create(2, Vec2(0, 50), 80, 4); + auto actionByBack = actionBy->reverse(); + auto rotateBy = RotateBy::create(2, 180); + auto rotateByBack = RotateBy::create(2, -180); + + sp1->runAction(RepeatForever::create(actionUp)); + sp2->runAction(RepeatForever::create(Sequence::create(actionBy, actionByBack, nullptr))); + sp3->runAction(actionTo); + sp4->runAction(RepeatForever::create(Sequence::create(rotateBy, rotateByBack, nullptr))); +} + +std::string PhysicsDemoActions::title() const +{ + return "Actions"; +} + +// implementation of PhysicsDemoJoints + + void PhysicsDemoJoints::onEnter() { PhysicsDemo::onEnter(); toggleDebug(); auto listener = EventListenerTouchOneByOne::create(); - listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this); - listener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoJoints::onTouchMoved, this); - listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchEnded, this); + listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this); + listener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this); + listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); float width = (VisibleRect::getVisibleRect().size.width - 10) / 4; @@ -644,7 +693,8 @@ void PhysicsDemoJoints::onEnter() Node* node = Node::create(); PhysicsBody* box = PhysicsBody::create(); - node->setPhysicsBody(box); + addPhysicsComponent(node, box); + box->setDynamic(false); node->setPosition(Point::ZERO); this->addChild(node); @@ -655,17 +705,20 @@ void PhysicsDemoJoints::onEnter() { Vec2 offset(VisibleRect::leftBottom().x + 5 + j * width + width/2, VisibleRect::leftBottom().y + 50 + i * height + height/2); box->addShape(PhysicsShapeEdgeBox::create(Size(width, height), PHYSICSSHAPE_MATERIAL_DEFAULT, 1, offset)); - + switch (i*4 + j) { case 0: { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBall(offset + Vec2(30, 0), 10); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset); + auto sp2 = makeBall(offset + Vec2(30, 0), 10); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointPin* joint = PhysicsJointPin::construct(sp1PhysicsBody, sp2PhysicsBody, offset); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -676,11 +729,14 @@ void PhysicsDemoJoints::onEnter() { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1PhysicsBody, sp2PhysicsBody, offset); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -690,11 +746,14 @@ void PhysicsDemoJoints::onEnter() case 2: { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO, Point::ZERO); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -704,11 +763,14 @@ void PhysicsDemoJoints::onEnter() case 3: { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 30.0f, 60.0f); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO, Point::ZERO, 30.0f, 60.0f); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -718,11 +780,14 @@ void PhysicsDemoJoints::onEnter() case 4: { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 500.0f, 0.3f); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO, Point::ZERO, 500.0f, 0.3f); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -732,11 +797,14 @@ void PhysicsDemoJoints::onEnter() case 5: { auto sp1 = makeBall(offset - Vec2(30, 0), 10); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Vec2(30, 15), Vec2(30, -15), Vec2(-30, 0)); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1PhysicsBody, sp2PhysicsBody, Vec2(30, 15), Vec2(30, -15), Vec2(-30, 0)); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -746,13 +814,16 @@ void PhysicsDemoJoints::onEnter() case 6: { auto sp1 = makeBox(offset - Vec2(30, 0), Size(30, 10)); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - 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); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition())); + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition())); + PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1PhysicsBody, sp2PhysicsBody, 3000.0f, 60.0f); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -762,13 +833,16 @@ void PhysicsDemoJoints::onEnter() case 7: { auto sp1 = makeBox(offset - Vec2(30, 0), Size(30, 10)); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - 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); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition())); + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition())); + PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f,(float) M_PI_2); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -778,13 +852,16 @@ void PhysicsDemoJoints::onEnter() case 8: { auto sp1 = makeBox(offset - Vec2(30, 0), Size(30, 10)); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - 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); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition())); + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition())); + PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f, (float)M_PI_2); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -794,13 +871,16 @@ void PhysicsDemoJoints::onEnter() case 9: { auto sp1 = makeBox(offset - Vec2(30, 0), Size(30, 10)); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - 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); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition())); + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition())); + PhysicsJointGear* joint = PhysicsJointGear::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f, 2.0f); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -810,13 +890,16 @@ void PhysicsDemoJoints::onEnter() case 10: { auto sp1 = makeBox(offset - Vec2(30, 0), Size(30, 10)); - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + auto sp1PhysicsBody = sp1->getComponent()->getPhysicsBody(); + sp1PhysicsBody->setTag(DRAG_BODYS_TAG); - 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); + auto sp2 = makeBox(offset + Vec2(30, 0), Size(30, 10)); + auto sp2PhysicsBody = sp2->getComponent()->getPhysicsBody(); + sp2PhysicsBody->setTag(DRAG_BODYS_TAG); + + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition())); + getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition())); + PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1PhysicsBody, sp2PhysicsBody, (float)M_PI_2); getPhysicsWorld()->addJoint(joint); this->addChild(sp1); @@ -835,51 +918,6 @@ std::string PhysicsDemoJoints::title() const return "Joints"; } -void PhysicsDemoActions::onEnter() -{ - PhysicsDemo::onEnter(); - getPhysicsWorld()->setGravity(Vect::ZERO); - - auto touchListener = EventListenerTouchOneByOne::create(); - touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoActions::onTouchBegan, this); - touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoActions::onTouchMoved, this); - touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoActions::onTouchEnded, this); - _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); - - auto node = Node::create(); - node->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); - node->setPosition(VisibleRect::center()); - this->addChild(node); - - Sprite* sp1 = addGrossiniAtPosition(VisibleRect::center()); - Sprite* sp2 = addGrossiniAtPosition(VisibleRect::left() + Vec2(50, 0)); - Sprite* sp3 = addGrossiniAtPosition(VisibleRect::right() - Vec2(20, 0)); - Sprite* sp4 = addGrossiniAtPosition(VisibleRect::leftTop() + Vec2(50, -50)); - sp4->getPhysicsBody()->setGravityEnable(false); - - sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - sp3->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - sp4->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - - auto actionTo = JumpTo::create(2, Vec2(100,100), 50, 4); - auto actionBy = JumpBy::create(2, Vec2(300,0), 50, 4); - auto actionUp = JumpBy::create(2, Vec2(0,50), 80, 4); - auto actionByBack = actionBy->reverse(); - auto rotateBy = RotateBy::create(2, 180); - auto rotateByBack = RotateBy::create(2, -180); - - sp1->runAction(RepeatForever::create(actionUp)); - sp2->runAction(RepeatForever::create(Sequence::create(actionBy, actionByBack, nullptr))); - sp3->runAction(actionTo); - sp4->runAction(RepeatForever::create(Sequence::create(rotateBy, rotateByBack, nullptr))); -} - -std::string PhysicsDemoActions::title() const -{ - return "Actions"; -} - void PhysicsDemoPump::onEnter() { PhysicsDemo::onEnter(); @@ -895,20 +933,19 @@ void PhysicsDemoPump::onEnter() scheduleUpdate(); auto node = Node::create(); - auto body = PhysicsBody::create(); - body->setDynamic(false); + addPhysicsComponent(node, PhysicsBody::create()); + node->getComponent()->getPhysicsBody()->setDynamic(false); PhysicsMaterial staticMaterial(PHYSICS_INFINITY, 0, 0.5f); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, 0), VisibleRect::leftTop() + Vec2(50, -130), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(190, 0), VisibleRect::leftTop() + Vec2(100, -50), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(100, -50), VisibleRect::leftTop() + Vec2(100, -90), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, -130), VisibleRect::leftTop() + Vec2(100, -145), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(100, -145), VisibleRect::leftBottom() + Vec2(100, 80), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::leftBottom() + Vec2(150, 80), staticMaterial, 2.0f)); - body->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::rightTop() + Vec2(-100, -150), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, 0), VisibleRect::leftTop() + Vec2(50, -130), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(190, 0), VisibleRect::leftTop() + Vec2(100, -50), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(100, -50), VisibleRect::leftTop() + Vec2(100, -90), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, -130), VisibleRect::leftTop() + Vec2(100, -145), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(100, -145), VisibleRect::leftBottom() + Vec2(100, 80), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::leftBottom() + Vec2(150, 80), staticMaterial, 2.0f)); + node->getComponent()->getPhysicsBody()->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::rightTop() + Vec2(-100, -150), staticMaterial, 2.0f)); - body->setCategoryBitmask(0x01); - node->setPhysicsBody(body); + node->getComponent()->getPhysicsBody()->setCategoryBitmask(0x01); node->setPosition(Vec2::ZERO); this->addChild(node); @@ -916,12 +953,10 @@ void PhysicsDemoPump::onEnter() for (int i = 0; i < 6; ++i) { auto ball = makeBall(VisibleRect::leftTop() + Vec2(75 + CCRANDOM_0_1() * 90, 0), 22, PhysicsMaterial(0.05f, 0.0f, 0.1f)); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + ball->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); addChild(ball); } - auto _world = getPhysicsWorld(); - Vec2 vec[4] = { VisibleRect::leftTop() + Vec2(102, -148), @@ -934,75 +969,73 @@ void PhysicsDemoPump::onEnter() auto pump = Node::create(); auto center = PhysicsShape::getPolyonCenter(vec, 4); pump->setPosition(center); - auto pumpB = PhysicsBody::createPolygon(vec, 4, PHYSICSBODY_MATERIAL_DEFAULT, -center); - pump->setPhysicsBody(pumpB); + addPhysicsComponent(pump, PhysicsBody::createPolygon(vec, 4, PHYSICSBODY_MATERIAL_DEFAULT, -center)); this->addChild(pump); - pumpB->setCategoryBitmask(0x02); - pumpB->setGravityEnable(false); + pump->getComponent()->getPhysicsBody()->setCategoryBitmask(0x02); + pump->getComponent()->getPhysicsBody()->setGravityEnable(false); // small gear auto sgear = Node::create(); - auto sgearB = PhysicsBody::createCircle(44); - sgear->setPhysicsBody(sgearB); + addPhysicsComponent(sgear, PhysicsBody::createCircle(44)); sgear->setPosition(VisibleRect::leftBottom() + Vec2(125, 0)); this->addChild(sgear); - sgearB->setCategoryBitmask(0x04); - sgearB->setCollisionBitmask(0x04); - sgearB->setTag(1); - - _world->addJoint(PhysicsJointPin::construct(body, sgearB, sgear->getPosition())); - _world->addJoint(PhysicsJointDistance::construct(pumpB, sgearB, Vec2(0, 0), Vec2(0, -44))); + sgear->getComponent()->getPhysicsBody()->setCategoryBitmask(0x04); + sgear->getComponent()->getPhysicsBody()->setCollisionBitmask(0x04); + sgear->getComponent()->getPhysicsBody()->setTag(1); + + _physicsWorld->addJoint(PhysicsJointPin::construct(node->getComponent()->getPhysicsBody(), sgear->getComponent()->getPhysicsBody(), sgear->getPosition())); + _physicsWorld->addJoint(PhysicsJointDistance::construct(pump->getComponent()->getPhysicsBody(), sgear->getComponent()->getPhysicsBody(), Vec2(0, 0), Vec2(0, -44))); // big gear auto bgear = Node::create(); - auto bgearB = PhysicsBody::createCircle(100); - bgear->setPhysicsBody(bgearB); + addPhysicsComponent(bgear, PhysicsBody::createCircle(100)); bgear->setPosition(VisibleRect::leftBottom() + Vec2(275, 0)); this->addChild(bgear); - bgearB->setCategoryBitmask(0x04); - - _world->addJoint(PhysicsJointPin::construct(bgearB, body, bgear->getPosition())); - _world->addJoint(PhysicsJointGear::construct(sgearB, bgearB, (float)-M_PI_2, -2.0f)); - + bgear->getComponent()->getPhysicsBody()->setCategoryBitmask(0x04); + + _physicsWorld->addJoint(PhysicsJointPin::construct(bgear->getComponent()->getPhysicsBody(), node->getComponent()->getPhysicsBody(), bgear->getPosition())); + _physicsWorld->addJoint(PhysicsJointGear::construct(sgear->getComponent()->getPhysicsBody(), bgear->getComponent()->getPhysicsBody(), (float)-M_PI_2, -2.0f)); + // plugger - Vec2 seg[] = {VisibleRect::leftTop() + Vec2(75, -120), VisibleRect::leftBottom() + Vec2(75, -100)}; - Vec2 segCenter = (seg[1] + seg[0])/2; + Vec2 seg[] = { VisibleRect::leftTop() + Vec2(75, -120), VisibleRect::leftBottom() + Vec2(75, -100) }; + Vec2 segCenter = (seg[1] + seg[0]) / 2; seg[1] -= segCenter; seg[0] -= segCenter; auto plugger = Node::create(); - auto pluggerB = PhysicsBody::createEdgeSegment(seg[0], seg[1], PhysicsMaterial(0.01f, 0.0f, 0.5f), 20); - pluggerB->setDynamic(true); - pluggerB->setMass(30); - pluggerB->setMoment(100000); - plugger->setPhysicsBody(pluggerB); + addPhysicsComponent(plugger, PhysicsBody::createEdgeSegment(seg[0], seg[1], PhysicsMaterial(0.01f, 0.0f, 0.5f), 20)); + plugger->getComponent()->getPhysicsBody()->setDynamic(true); + plugger->getComponent()->getPhysicsBody()->setMass(30); + plugger->getComponent()->getPhysicsBody()->setMoment(100000); plugger->setPosition(segCenter); this->addChild(plugger); - pluggerB->setCategoryBitmask(0x02); - sgearB->setCollisionBitmask(0x04 | 0x01); - _world->addJoint(PhysicsJointPin::construct(body, pluggerB, VisibleRect::leftBottom() + Vec2(75, -90))); - _world->addJoint(PhysicsJointDistance::construct(pluggerB, sgearB, Vec2::ZERO, Vec2(44, 0))); + plugger->getComponent()->getPhysicsBody()->setCategoryBitmask(0x02); + sgear->getComponent()->getPhysicsBody()->setCollisionBitmask(0x04 | 0x01); + _physicsWorld->addJoint(PhysicsJointPin::construct(node->getComponent()->getPhysicsBody(), plugger->getComponent()->getPhysicsBody(), VisibleRect::leftBottom() + Vec2(75, -90))); + _physicsWorld->addJoint(PhysicsJointDistance::construct(plugger->getComponent()->getPhysicsBody(), sgear->getComponent()->getPhysicsBody(), Vec2::ZERO, Vec2(44, 0))); } void PhysicsDemoPump::update(float delta) { - auto physicsWorld = getPhysicsWorld(); - - for (const auto& body : physicsWorld->getAllBodies()) + for (const auto& body : _physicsWorld->getAllBodies()) { if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f) { - body->getNode()->setPosition(VisibleRect::leftTop() + Vec2(75 + CCRANDOM_0_1() * 90, 0)); + if (body->getNode()!=nullptr) + { + body->getNode()->setPosition(VisibleRect::leftTop() + Vec2(75 + CCRANDOM_0_1() * 90, 0)); + } + body->setVelocity(Vec2(0, 0)); } } - PhysicsBody* gear = physicsWorld->getBody(1); + PhysicsBody* gear = _physicsWorld->getBody(1); if (gear != nullptr) { if (_distance != 0.0f) { - _rotationV += _distance/2500.0f; + _rotationV += _distance / 2500.0f; if (_rotationV > 30) _rotationV = 30.0f; if (_rotationV < -30) _rotationV = -30.0f; @@ -1057,22 +1090,22 @@ void PhysicsDemoOneWayPlatform::onEnter() _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto ground = Node::create(); - ground->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); + addPhysicsComponent(ground, PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); this->addChild(ground); auto platform = makeBox(VisibleRect::center(), Size(200, 50)); - platform->getPhysicsBody()->setDynamic(false); - platform->getPhysicsBody()->setContactTestBitmask(0xFFFFFFFF); + platform->getComponent()->getPhysicsBody()->setDynamic(false); + platform->getComponent()->getPhysicsBody()->setContactTestBitmask(0xFFFFFFFF); this->addChild(platform); auto ball = makeBall(VisibleRect::center() - Vec2(0, 50), 20); - ball->getPhysicsBody()->setVelocity(Vec2(0, 150)); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - ball->getPhysicsBody()->setMass(1.0f); - ball->getPhysicsBody()->setContactTestBitmask(0xFFFFFFFF); + ball->getComponent()->getPhysicsBody()->setVelocity(Vec2(0, 150)); + ball->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + ball->getComponent()->getPhysicsBody()->setMass(1.0f); + ball->getComponent()->getPhysicsBody()->setContactTestBitmask(0xFFFFFFFF); this->addChild(ball); - auto contactListener = EventListenerPhysicsContactWithBodies::create(platform->getPhysicsBody(), ball->getPhysicsBody()); + auto contactListener = EventListenerPhysicsContactWithBodies::create(platform->getComponent()->getPhysicsBody(), ball->getComponent()->getPhysicsBody()); contactListener->onContactBegin = CC_CALLBACK_1(PhysicsDemoOneWayPlatform::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); } @@ -1100,14 +1133,14 @@ void PhysicsDemoSlice::onEnter() _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto ground = Node::create(); - ground->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); + addPhysicsComponent(ground, PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50), VisibleRect::rightBottom() + Vec2(0, 50))); this->addChild(ground); auto box = Node::create(); Vec2 points[4] = {Vec2(-100, -100), Vec2(-100, 100), Vec2(100, 100), Vec2(100, -100)}; - box->setPhysicsBody(PhysicsBody::createPolygon(points, 4)); + addPhysicsComponent(box, PhysicsBody::createPolygon(points, 4)); box->setPosition(VisibleRect::center()); - box->getPhysicsBody()->setTag(_sliceTag); + box->getComponent()->getPhysicsBody()->setTag(_sliceTag); addChild(box); } @@ -1166,7 +1199,7 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Vec2 normal, float d Node* node = Node::create(); PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center); node->setPosition(center); - node->setPhysicsBody(polyon); + addPhysicsComponent(node, polyon); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); @@ -1191,21 +1224,20 @@ std::string PhysicsDemoSlice::subtitle() const return "click and drag to slice up the block"; } - void PhysicsDemoBug3988::onEnter() { PhysicsDemo::onEnter(); toggleDebug(); - getPhysicsWorld()->setGravity(Vect::ZERO); - - auto ball = Sprite::create("Images/YellowSquare.png"); - ball->setPosition(VisibleRect::center() - Vec2(100, 0)); - ball->setRotation(30.0f); - this->addChild(ball); - - auto physicsBall = makeBox(VisibleRect::center() + Vec2(100, 0), Size(100, 100)); - physicsBall->setRotation(30.0f); - this->addChild(physicsBall); + _physicsWorld->setGravity(Vect::ZERO); + + auto ball = Sprite::create("Images/YellowSquare.png"); + ball->setPosition(VisibleRect::center() - Vec2(100, 0)); + ball->setRotation(30.0f); + this->addChild(ball); + + auto physicsBall = makeBox(VisibleRect::center() + Vec2(100, 0), Size(100, 100)); + physicsBall->setRotation(30.0f); + this->addChild(physicsBall); } std::string PhysicsDemoBug3988::title() const @@ -1221,7 +1253,7 @@ std::string PhysicsDemoBug3988::subtitle() const void PhysicsContactTest::onEnter() { PhysicsDemo::onEnter(); - getPhysicsWorld()->setGravity(Vect::ZERO); + _physicsWorld->setGravity(Vect::ZERO); auto s = VisibleRect::getVisibleRect().size; _yellowBoxNum = 50; @@ -1232,69 +1264,69 @@ void PhysicsContactTest::onEnter() MenuItemFont::setFontSize(65); auto decrease1 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this)); - decrease1->setColor(Color3B(0,200,20)); + decrease1->setColor(Color3B(0, 200, 20)); auto increase1 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this)); - increase1->setColor(Color3B(0,200,20)); + increase1->setColor(Color3B(0, 200, 20)); decrease1->setTag(1); increase1->setTag(1); auto menu1 = Menu::create(decrease1, increase1, nullptr); menu1->alignItemsHorizontally(); - menu1->setPosition(Vec2(s.width/2, s.height-50)); + menu1->setPosition(Vec2(s.width / 2, s.height - 50)); addChild(menu1, 1); auto label = Label::createWithTTF("yellow box", "fonts/arial.ttf", 32); addChild(label, 1); - label->setPosition(Vec2(s.width/2 - 150, s.height-50)); + label->setPosition(Vec2(s.width / 2 - 150, s.height - 50)); auto decrease2 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this)); - decrease2->setColor(Color3B(0,200,20)); + decrease2->setColor(Color3B(0, 200, 20)); auto increase2 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this)); - increase2->setColor(Color3B(0,200,20)); + increase2->setColor(Color3B(0, 200, 20)); decrease2->setTag(2); increase2->setTag(2); auto menu2 = Menu::create(decrease2, increase2, nullptr); menu2->alignItemsHorizontally(); - menu2->setPosition(Vec2(s.width/2, s.height-90)); + menu2->setPosition(Vec2(s.width / 2, s.height - 90)); addChild(menu2, 1); label = Label::createWithTTF("blue box", "fonts/arial.ttf", 32); addChild(label, 1); - label->setPosition(Vec2(s.width/2 - 150, s.height-90)); + label->setPosition(Vec2(s.width / 2 - 150, s.height - 90)); auto decrease3 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this)); - decrease3->setColor(Color3B(0,200,20)); + decrease3->setColor(Color3B(0, 200, 20)); auto increase3 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this)); - increase3->setColor(Color3B(0,200,20)); + increase3->setColor(Color3B(0, 200, 20)); decrease3->setTag(3); increase3->setTag(3); auto menu3 = Menu::create(decrease3, increase3, nullptr); menu3->alignItemsHorizontally(); - menu3->setPosition(Vec2(s.width/2, s.height-130)); + menu3->setPosition(Vec2(s.width / 2, s.height - 130)); addChild(menu3, 1); label = Label::createWithTTF("yellow triangle", "fonts/arial.ttf", 32); addChild(label, 1); - label->setPosition(Vec2(s.width/2 - 150, s.height-130)); + label->setPosition(Vec2(s.width / 2 - 150, s.height - 130)); auto decrease4 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this)); - decrease4->setColor(Color3B(0,200,20)); + decrease4->setColor(Color3B(0, 200, 20)); auto increase4 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this)); - increase4->setColor(Color3B(0,200,20)); + increase4->setColor(Color3B(0, 200, 20)); decrease4->setTag(4); increase4->setTag(4); auto menu4 = Menu::create(decrease4, increase4, nullptr); menu4->alignItemsHorizontally(); - menu4->setPosition(Vec2(s.width/2, s.height-170)); + menu4->setPosition(Vec2(s.width / 2, s.height - 170)); addChild(menu4, 1); label = Label::createWithTTF("blue triangle", "fonts/arial.ttf", 32); addChild(label, 1); - label->setPosition(Vec2(s.width/2 - 150, s.height-170)); - + label->setPosition(Vec2(s.width / 2 - 150, s.height - 170)); + resetTest(); } @@ -1303,16 +1335,16 @@ void PhysicsContactTest::onDecrease(Ref* sender) switch (dynamic_cast(sender)->getTag()) { case 1: - if(_yellowBoxNum > 0) _yellowBoxNum -= 50; + if (_yellowBoxNum > 0) _yellowBoxNum -= 50; break; case 2: - if(_blueBoxNum > 0) _blueBoxNum -= 50; + if (_blueBoxNum > 0) _blueBoxNum -= 50; break; case 3: - if(_yellowTriangleNum > 0) _yellowTriangleNum -= 50; + if (_yellowTriangleNum > 0) _yellowTriangleNum -= 50; break; case 4: - if(_blueTriangleNum > 0) _blueTriangleNum -= 50; + if (_blueTriangleNum > 0) _blueTriangleNum -= 50; break; default: @@ -1360,25 +1392,25 @@ void PhysicsContactTest::resetTest() sprintf(buffer, "%d", _yellowBoxNum); auto label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32); root->addChild(label, 1); - label->setPosition(Vec2(s.width/2, s.height-50)); + label->setPosition(Vec2(s.width / 2, s.height - 50)); sprintf(buffer, "%d", _blueBoxNum); label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32); root->addChild(label, 1); - label->setPosition(Vec2(s.width/2, s.height-90)); + label->setPosition(Vec2(s.width / 2, s.height - 90)); sprintf(buffer, "%d", _yellowTriangleNum); label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32); root->addChild(label, 1); - label->setPosition(Vec2(s.width/2, s.height-130)); + label->setPosition(Vec2(s.width / 2, s.height - 130)); sprintf(buffer, "%d", _blueTriangleNum); label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32); root->addChild(label, 1); - label->setPosition(Vec2(s.width/2, s.height-170)); + label->setPosition(Vec2(s.width / 2, s.height - 170)); auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f))); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f))); wall->setPosition(VisibleRect::center()); root->addChild(wall); @@ -1389,72 +1421,72 @@ void PhysicsContactTest::resetTest() // yellow box, will collide with itself and blue box. for (int i = 0; i < _yellowBoxNum; ++i) { - Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10); + Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10); Size winSize = VisibleRect::getVisibleRect().size; Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height); position.x = position.x * CCRANDOM_0_1(); position.y = position.y * CCRANDOM_0_1(); - position = VisibleRect::leftBottom() + position + Vec2(size.width/2, size.height/2); - Vect velocity((CCRANDOM_0_1() - 0.5)*200, (CCRANDOM_0_1() - 0.5)*200); + position = VisibleRect::leftBottom() + position + Vec2(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, 1, PhysicsMaterial(0.1f, 1, 0.0f)); - box->getPhysicsBody()->setVelocity(velocity); - box->getPhysicsBody()->setCategoryBitmask(0x01); // 0001 - box->getPhysicsBody()->setContactTestBitmask(0x04); // 0100 - box->getPhysicsBody()->setCollisionBitmask(0x03); // 0011 + box->getComponent()->getPhysicsBody()->setVelocity(velocity); + box->getComponent()->getPhysicsBody()->setCategoryBitmask(0x01); // 0001 + box->getComponent()->getPhysicsBody()->setContactTestBitmask(0x04); // 0100 + box->getComponent()->getPhysicsBody()->setCollisionBitmask(0x03); // 0011 root->addChild(box); } // blue box, will collide with blue box. for (int i = 0; i < _blueBoxNum; ++i) { - Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10); + Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10); Size winSize = VisibleRect::getVisibleRect().size; Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height); position.x = position.x * CCRANDOM_0_1(); position.y = position.y * CCRANDOM_0_1(); - position = VisibleRect::leftBottom() + position + Vec2(size.width/2, size.height/2); - Vect velocity((CCRANDOM_0_1() - 0.5)*200, (CCRANDOM_0_1() - 0.5)*200); + position = VisibleRect::leftBottom() + position + Vec2(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, 2, PhysicsMaterial(0.1f, 1, 0.0f)); - box->getPhysicsBody()->setVelocity(velocity); - box->getPhysicsBody()->setCategoryBitmask(0x02); // 0010 - box->getPhysicsBody()->setContactTestBitmask(0x08); // 1000 - box->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 + box->getComponent()->getPhysicsBody()->setVelocity(velocity); + box->getComponent()->getPhysicsBody()->setCategoryBitmask(0x02); // 0010 + box->getComponent()->getPhysicsBody()->setContactTestBitmask(0x08); // 1000 + box->getComponent()->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 root->addChild(box); } // yellow triangle, will collide with itself and blue box. for (int i = 0; i < _yellowTriangleNum; ++i) { - Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10); + Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10); Size winSize = VisibleRect::getVisibleRect().size; Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height); position.x = position.x * CCRANDOM_0_1(); position.y = position.y * CCRANDOM_0_1(); - position = VisibleRect::leftBottom() + position + Vec2(size.width/2, size.height/2); - Vect velocity((CCRANDOM_0_1() - 0.5)*300, (CCRANDOM_0_1() - 0.5)*300); + position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2); + Vect velocity((CCRANDOM_0_1() - 0.5) * 300, (CCRANDOM_0_1() - 0.5) * 300); auto triangle = makeTriangle(position, size, 1, PhysicsMaterial(0.1f, 1, 0.0f)); - triangle->getPhysicsBody()->setVelocity(velocity); - triangle->getPhysicsBody()->setCategoryBitmask(0x04); // 0100 - triangle->getPhysicsBody()->setContactTestBitmask(0x01); // 0001 - triangle->getPhysicsBody()->setCollisionBitmask(0x06); // 0110 + triangle->getComponent()->getPhysicsBody()->setVelocity(velocity); + triangle->getComponent()->getPhysicsBody()->setCategoryBitmask(0x04); // 0100 + triangle->getComponent()->getPhysicsBody()->setContactTestBitmask(0x01); // 0001 + triangle->getComponent()->getPhysicsBody()->setCollisionBitmask(0x06); // 0110 root->addChild(triangle); } // blue triangle, will collide with yellow box. for (int i = 0; i < _blueTriangleNum; ++i) { - Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10); + Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10); Size winSize = VisibleRect::getVisibleRect().size; Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height); position.x = position.x * CCRANDOM_0_1(); position.y = position.y * CCRANDOM_0_1(); - position = VisibleRect::leftBottom() + position + Vec2(size.width/2, size.height/2); - Vect velocity((CCRANDOM_0_1() - 0.5)*300, (CCRANDOM_0_1() - 0.5)*300); + position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2); + Vect velocity((CCRANDOM_0_1() - 0.5) * 300, (CCRANDOM_0_1() - 0.5) * 300); auto triangle = makeTriangle(position, size, 2, PhysicsMaterial(0.1f, 1, 0.0f)); - triangle->getPhysicsBody()->setVelocity(velocity); - triangle->getPhysicsBody()->setCategoryBitmask(0x08); // 1000 - triangle->getPhysicsBody()->setContactTestBitmask(0x02); // 0010 - triangle->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 + triangle->getComponent()->getPhysicsBody()->setVelocity(velocity); + triangle->getComponent()->getPhysicsBody()->setCategoryBitmask(0x08); // 1000 + triangle->getComponent()->getPhysicsBody()->setContactTestBitmask(0x02); // 0010 + triangle->getComponent()->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 root->addChild(triangle); } } @@ -1484,7 +1516,7 @@ void PhysicsPositionRotationTest::onEnter() { PhysicsDemo::onEnter(); toggleDebug(); - getPhysicsWorld()->setGravity(Point::ZERO); + _physicsWorld->setGravity(Point::ZERO); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this); @@ -1493,41 +1525,41 @@ void PhysicsPositionRotationTest::onEnter() _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size)); wall->setPosition(VisibleRect::center()); addChild(wall); // anchor test auto anchorNode = Sprite::create("Images/YellowSquare.png"); - anchorNode->setAnchorPoint(Vec2(0.1f, 0.9f)); + //anchorNode->setAnchorPoint(Vec2(0.1f, 0.9f)); anchorNode->setPosition(100, 100); anchorNode->setScale(0.25); + addPhysicsComponent(anchorNode, PhysicsBody::createBox(anchorNode->getContentSize())); + anchorNode->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); addChild(anchorNode); - anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale())); - anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG); //parent test auto parent = Sprite::create("Images/YellowSquare.png"); parent->setPosition(200, 100); parent->setScale(0.25); - parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale())); - parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + addPhysicsComponent(parent,PhysicsBody::createBox(parent->getContentSize())); + parent->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); addChild(parent); auto leftBall = Sprite::create("Images/ball.png"); leftBall->setPosition(-30, 0); - leftBall->cocos2d::Node::setScale(2); - leftBall->setPhysicsBody(PhysicsBody::createCircle(leftBall->getContentSize().width)); - leftBall->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + leftBall->Node::setScale(2); + addPhysicsComponent(leftBall, PhysicsBody::createCircle(leftBall->getContentSize().width/2)); + leftBall->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); parent->addChild(leftBall); // offset position rotation test auto offsetPosNode = Sprite::create("Images/YellowSquare.png"); offsetPosNode->setPosition(100, 200); - offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2)); - offsetPosNode->getPhysicsBody()->setPositionOffset(-Vec2(offsetPosNode->getContentSize()/2)); - offsetPosNode->getPhysicsBody()->setRotationOffset(45); - offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + addPhysicsComponent(offsetPosNode, PhysicsBody::createBox(offsetPosNode->getContentSize()/2)); + offsetPosNode->getComponent()->getPhysicsBody()->setPositionOffset(-Vec2(offsetPosNode->getContentSize() / 2)); + offsetPosNode->getComponent()->getPhysicsBody()->setRotationOffset(45); + offsetPosNode->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); addChild(offsetPosNode); return; @@ -1538,7 +1570,6 @@ std::string PhysicsPositionRotationTest::title() const return "Position/Rotation Test"; } - void PhysicsSetGravityEnableTest::onEnter() { PhysicsDemo::onEnter(); @@ -1551,36 +1582,36 @@ void PhysicsSetGravityEnableTest::onEnter() // wall auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); wall->setPosition(VisibleRect::center()); addChild(wall); // common box auto commonBox = makeBox(Vec2(100, 100), Size(50, 50), 1); - commonBox->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + commonBox->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); addChild(commonBox); auto box = makeBox(Vec2(200, 100), Size(50, 50), 2); - box->getPhysicsBody()->setMass(20); - box->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - box->getPhysicsBody()->setGravityEnable(false); + box->getComponent()->getPhysicsBody()->setMass(20); + box->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + box->getComponent()->getPhysicsBody()->setGravityEnable(false); addChild(box); auto ball = makeBall(Vec2(200, 200), 50); ball->setTag(2); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - ball->getPhysicsBody()->setGravityEnable(false); + ball->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + ball->getComponent()->getPhysicsBody()->setGravityEnable(false); addChild(ball); - ball->getPhysicsBody()->setMass(50); + ball->getComponent()->getPhysicsBody()->setMass(50); scheduleOnce(CC_SCHEDULE_SELECTOR(PhysicsSetGravityEnableTest::onScheduleOnce), 1.0); } void PhysicsSetGravityEnableTest::onScheduleOnce(float delta) { auto ball = getChildByTag(2); - ball->getPhysicsBody()->setMass(200); + ball->getComponent()->getPhysicsBody()->setMass(200); - getPhysicsWorld()->setGravity(Vect(0, 98)); + _physicsWorld->setGravity(Vect(0, 98)); } std::string PhysicsSetGravityEnableTest::title() const @@ -1593,7 +1624,7 @@ std::string PhysicsSetGravityEnableTest::subtitle() const return "only yellow box drop down"; } -void Bug5482::onEnter() +void PhysicsDemoBug5482::onEnter() { PhysicsDemo::onEnter(); @@ -1609,23 +1640,25 @@ void Bug5482::onEnter() // wall auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); wall->setPosition(VisibleRect::center()); addChild(wall); //button MenuItemFont::setFontSize(18); - _button = MenuItemFont::create("Set Body To A", CC_CALLBACK_1(Bug5482::changeBodyCallback, this)); + _button = MenuItemFont::create("Set Body To A", CC_CALLBACK_1(PhysicsDemoBug5482::changeBodyCallback, this)); auto menu = Menu::create(_button, nullptr); this->addChild(menu); _nodeA = Sprite::create("Images/YellowSquare.png"); _nodeA->setPosition(VisibleRect::center().x - 150, 100); + _nodeA->addComponent(ComponentPhysics2d::create()); this->addChild(_nodeA); _nodeB = Sprite::create("Images/YellowSquare.png"); _nodeB->setPosition(VisibleRect::center().x + 150, 100); + _nodeB->addComponent(ComponentPhysics2d::create()); this->addChild(_nodeB); _body = PhysicsBody::createBox(_nodeA->getContentSize()); @@ -1633,27 +1666,25 @@ void Bug5482::onEnter() _body->retain(); } -void Bug5482::onExit() +void PhysicsDemoBug5482::onExit() { PhysicsDemo::onExit(); _body->release(); } -void Bug5482::changeBodyCallback(Ref* sender) +void PhysicsDemoBug5482::changeBodyCallback(Ref* sender) { Sprite* node = _bodyInA ? _nodeB : _nodeA; - - node->setPhysicsBody(_body); - + node->getComponent()->setPhysicsBody(_body); _bodyInA = !_bodyInA; } -std::string Bug5482::title() const +std::string PhysicsDemoBug5482::title() const { return "bug 5482: setPhysicsBodyTest"; } -std::string Bug5482::subtitle() const +std::string PhysicsDemoBug5482::subtitle() const { return "change physics body to the other."; } @@ -1662,12 +1693,12 @@ void PhysicsFixedUpdate::onEnter() { PhysicsDemo::onEnter(); - getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); - getPhysicsWorld()->setGravity(Point::ZERO); + _physicsWorld->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); + _physicsWorld->setGravity(Point::ZERO); // wall auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f))); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f))); wall->setPosition(VisibleRect::center()); this->addChild(wall); @@ -1680,9 +1711,9 @@ void PhysicsFixedUpdate::addBall() { auto ball = Sprite::create("Images/ball.png"); ball->setPosition(100, 100); - ball->setPhysicsBody(PhysicsBody::createCircle(ball->getContentSize().width/2, PhysicsMaterial(0.1f, 1, 0.0f))); - ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG); - ball->getPhysicsBody()->setVelocity(Point(1000, 20)); + addPhysicsComponent(ball, PhysicsBody::createCircle(ball->getContentSize().width/2, PhysicsMaterial(0.1f, 1, 0.0f))); + ball->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + ball->getComponent()->getPhysicsBody()->setVelocity(Point(1000, 20)); this->addChild(ball); } @@ -1690,7 +1721,7 @@ void PhysicsFixedUpdate::updateStart(float delta) { addBall(); - getPhysicsWorld()->setAutoStep(false); + _physicsWorld->setAutoStep(false); scheduleUpdate(); } @@ -1700,7 +1731,7 @@ void PhysicsFixedUpdate::update(float delta) // use fixed time and calculate 3 times per frame makes physics simulate more precisely. for (int i = 0; i < 3; ++i) { - getPhysicsWorld()->step(1/180.0f); + _physicsWorld->step(1/180.0f); } } @@ -1724,7 +1755,7 @@ void PhysicsTransformTest::onEnter() { PhysicsDemo::onEnter(); toggleDebug(); - getPhysicsWorld()->setGravity(Point::ZERO); + _physicsWorld->setGravity(Point::ZERO); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsTransformTest::onTouchBegan, this); @@ -1734,7 +1765,7 @@ void PhysicsTransformTest::onEnter() addChild(_rootLayer); auto wall = Node::create(); - wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); + addPhysicsComponent(wall, PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); wall->setPosition(VisibleRect::center()); _rootLayer->addChild(wall); @@ -1742,16 +1773,16 @@ void PhysicsTransformTest::onEnter() _parentSprite = Sprite::create("Images/YellowSquare.png"); _parentSprite->setPosition(200, 100); _parentSprite->setScale(0.25); - _parentSprite->setPhysicsBody(PhysicsBody::createBox(_parentSprite->getContentSize()*_parentSprite->getScale(), PhysicsMaterial(0.1f, 1.0f, 0.0f))); - _parentSprite->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + addPhysicsComponent(_parentSprite, PhysicsBody::createBox(_parentSprite->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f))); + _parentSprite->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); _parentSprite->setTag(1); _rootLayer->addChild(_parentSprite); auto leftBall = Sprite::create("Images/ball.png"); leftBall->setPosition(-30, 0); - leftBall->cocos2d::Node::setScale(2); - leftBall->setPhysicsBody(PhysicsBody::createCircle(leftBall->getContentSize().width, PhysicsMaterial(0.1f, 1.0f, 0.0f))); - leftBall->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + leftBall->setScale(2); + addPhysicsComponent(leftBall, PhysicsBody::createCircle(leftBall->getContentSize().width/2, PhysicsMaterial(0.1f, 1.0f, 0.0f))); + leftBall->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); _parentSprite->addChild(leftBall); ScaleTo* scaleTo = ScaleTo::create(2.0, 0.5); @@ -1761,20 +1792,16 @@ void PhysicsTransformTest::onEnter() auto normal = Sprite::create("Images/YellowSquare.png"); normal->setPosition(300, 100); normal->setScale(0.25, 0.5); - auto size = _parentSprite->getContentSize(); - size.width *= normal->getScaleX(); - size.height *= normal->getScaleY(); - normal->setPhysicsBody(PhysicsBody::createBox(size, PhysicsMaterial(0.1f, 1.0f, 0.0f))); - normal->getPhysicsBody()->setTag(DRAG_BODYS_TAG); + addPhysicsComponent(normal,PhysicsBody::createBox(normal->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f))); + normal->getComponent()->getPhysicsBody()->setTag(DRAG_BODYS_TAG); _rootLayer->addChild(normal); auto bullet = Sprite::create("Images/ball.png"); bullet->setPosition(200, 200); - bullet->setPhysicsBody(PhysicsBody::createCircle(bullet->getContentSize().width/2, PhysicsMaterial(0.1f, 1.0f, 0.0f))); - bullet->getPhysicsBody()->setVelocity(Vect(100, 100)); + addPhysicsComponent(bullet,PhysicsBody::createCircle(bullet->getContentSize().width/2, PhysicsMaterial(0.1f, 1.0f, 0.0f))); + bullet->getComponent()->getPhysicsBody()->setVelocity(Vect(100, 100)); _rootLayer->addChild(bullet); - MoveBy* move = MoveBy::create(2.0f, Vec2(100, 100)); MoveBy* move2 = MoveBy::create(2.0f, Vec2(-200, 0)); MoveBy* move3 = MoveBy::create(2.0f, Vec2(100, -100)); @@ -1826,4 +1853,4 @@ std::string PhysicsIssue9959::subtitle() const return "Test Scale9Sprite run scale/move/rotation action in physics scene"; } -#endif // ifndef CC_USE_PHYSICS +#endif \ No newline at end of file diff --git a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h index 0e3646c9fd..4163e1d13b 100644 --- a/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h +++ b/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.h @@ -1,22 +1,13 @@ -#ifndef _PHYSICS_TEST_H_ -#define _PHYSICS_TEST_H_ - -#include "cocos2d.h" -#include "../BaseTest.h" +#pragma once #include -DEFINE_TEST_SUITE(PhysicsTests); +#include "../BaseTest.h" -#if CC_USE_PHYSICS == 0 -class PhysicsDemoDisabled : public TestCase -{ -public: - CREATE_FUNC(PhysicsDemoDisabled); - - virtual void onEnter() override; -}; -#else +#if CC_USE_PHYSICS + + +DEFINE_TEST_SUITE(PhysicsTests); class PhysicsDemo : public TestCase { @@ -38,20 +29,32 @@ public: bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); - + void toggleDebug(); + protected: + void addPhysicsComponent(cocos2d::Node *node, cocos2d::PhysicsBody *physicsBody); + cocos2d::Texture2D* _spriteTexture; cocos2d::SpriteBatchNode* _ball; std::unordered_map _mouses; bool _debugDraw; }; +class PhysicsDemoLogoSmash : public PhysicsDemo +{ +public: + CREATE_FUNC(PhysicsDemoLogoSmash); + + void onEnter() override; + virtual std::string title() const override; +}; + class PhysicsDemoClickAdd : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoClickAdd); - + virtual ~PhysicsDemoClickAdd(); void onEnter() override; virtual std::string subtitle() const override; @@ -60,20 +63,11 @@ public: void onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* event); }; -class PhysicsDemoLogoSmash : public PhysicsDemo -{ -public: - CREATE_FUNC(PhysicsDemoLogoSmash); - - void onEnter() override; - virtual std::string title() const override; -}; - class PhysicsDemoPyramidStack : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoPyramidStack); - + void onEnter() override; void updateOnce(float delta); virtual std::string title() const override; @@ -83,9 +77,9 @@ class PhysicsDemoRayCast : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoRayCast); - + PhysicsDemoRayCast(); - + void onEnter() override; virtual std::string title() const override; void update(float delta) override; @@ -101,20 +95,20 @@ private: int _mode; }; -class PhysicsDemoJoints : public PhysicsDemo -{ -public: - CREATE_FUNC(PhysicsDemoJoints); - - void onEnter() override; - virtual std::string title() const override; -}; - class PhysicsDemoActions : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoActions); + + void onEnter() override; + virtual std::string title() const override; +}; +class PhysicsDemoJoints : public PhysicsDemo +{ +public: + CREATE_FUNC(PhysicsDemoJoints); + void onEnter() override; virtual std::string title() const override; }; @@ -123,7 +117,7 @@ class PhysicsDemoPump : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoPump); - + void onEnter() override; void update(float delta) override; virtual std::string title() const override; @@ -142,7 +136,7 @@ class PhysicsDemoOneWayPlatform : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoOneWayPlatform); - + void onEnter() override; virtual std::string title() const override; @@ -153,7 +147,7 @@ class PhysicsDemoSlice : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoSlice); - + void onEnter() override; virtual std::string title() const override; virtual std::string subtitle() const override; @@ -171,7 +165,7 @@ class PhysicsDemoBug3988 : public PhysicsDemo { public: CREATE_FUNC(PhysicsDemoBug3988); - + void onEnter() override; virtual std::string title() const override; virtual std::string subtitle() const override; @@ -218,10 +212,10 @@ public: virtual std::string subtitle() const override; }; -class Bug5482 : public PhysicsDemo +class PhysicsDemoBug5482 : public PhysicsDemo { public: - CREATE_FUNC(Bug5482); + CREATE_FUNC(PhysicsDemoBug5482); void onEnter() override; void onExit() override; @@ -274,5 +268,4 @@ public: virtual std::string subtitle() const override; }; -#endif -#endif +#endif // #if CC_USE_PHYSICS \ No newline at end of file diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index c18faa8e07..8d8595eec5 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -71,7 +71,9 @@ public: addTest("Node: Parallax", [](){return new ParallaxTests(); }); addTest("Node: Particles", [](){return new ParticleTests(); }); addTest("Node: Particle3D (PU)", [](){return new Particle3DTests(); }); +#if CC_USE_PHYSICS addTest("Node: Physics", []() { return new PhysicsTests(); }); +#endif addTest("Node: Physics3D", []() { return new Physics3DTests(); } ); addTest("Node: RenderTexture", [](){return new RenderTextureTests(); }); addTest("Node: Scene", [](){return new SceneTests(); }); diff --git a/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua b/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua index 59d00d315f..54bcb41b9a 100644 --- a/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua +++ b/tests/lua-tests/src/PhysicsTest/PhysicsTest.lua @@ -3,6 +3,7 @@ local MATERIAL_DEFAULT = cc.PhysicsMaterial(0.1, 0.5, 0.5) local curLayer = nil local STATIC_COLOR = cc.c4f(1.0, 0.0, 0.0, 1.0) local DRAG_BODYS_TAG = 0x80 +local PHYSICS_COMPONENT_NAME = "physics_component" local function range(from, to, step) step = step or 1 @@ -26,7 +27,7 @@ local function initWithLayer(layer, callback) cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE) end - layer.toggleDebug = function(self) toggleDebugCallback(nil) end; + layer.toggleDebug = function(self) toggleDebugCallback(nil) end cc.MenuItemFont:setFontSize(18) local item = cc.MenuItemFont:create("Toogle debug") item:registerScriptTapHandler(toggleDebugCallback) @@ -43,6 +44,12 @@ local function initWithLayer(layer, callback) layer:registerScriptHandler(onNodeEvent) end +local function addPhysicsComponent(node, physicsBody) + local component = cc.ComponentPhysics2d:create(physicsBody) + component:setName(PHYSICS_COMPONENT_NAME) + node:addComponent(component) +end + local function addGrossiniAtPosition(layer, p, scale) scale = scale or 1.0 @@ -53,7 +60,8 @@ local function addGrossiniAtPosition(layer, p, scale) local sp = cc.Sprite:createWithTexture(layer.spriteTexture, cc.rect(posx, posy, 85, 121)) sp:setScale(scale) - sp:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(48.0*scale, 108.0*scale))) + -- sp:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(48.0*scale, 108.0*scale))) + addPhysicsComponent(sp, cc.PhysicsBody:createBox(cc.size(48.0, 108.0))) layer:addChild(sp) sp:setPosition(p) return sp @@ -66,31 +74,32 @@ local function onTouchBegan(touch, event) local body for _, obj in ipairs(arr) do if bit.band(obj:getBody():getTag(), DRAG_BODYS_TAG) ~= 0 then - body = obj:getBody(); - break; + body = obj:getBody() + break end end if body then - local mouse = cc.Node:create(); - mouse:setPhysicsBody(cc.PhysicsBody:create(PHYSICS_INFINITY, PHYSICS_INFINITY)); - mouse:getPhysicsBody():setDynamic(false); - mouse:setPosition(location); - 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); + local mouse = cc.Node:create() + local physicsBody = cc.PhysicsBody:create(PHYSICS_INFINITY, PHYSICS_INFINITY) + addPhysicsComponent(mouse, physicsBody) + physicsBody:setDynamic(false) + mouse:setPosition(location) + curLayer:addChild(mouse) + local joint = cc.PhysicsJointPin:construct(physicsBody, body, location) + joint:setMaxForce(5000.0 * body:getMass()) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint) touch.mouse = mouse - return true; + return true end - return false; + return false end local function onTouchMoved(touch, event) if touch.mouse then - touch.mouse:setPosition(touch:getLocation()); + touch.mouse:setPosition(touch:getLocation()) end end @@ -113,8 +122,9 @@ local function makeBall(layer, point, radius, material) ball:setScale(0.13 * radius) - local body = cc.PhysicsBody:createCircle(radius, material) - ball:setPhysicsBody(body) + local body = cc.PhysicsBody:createCircle(ball:getContentSize().width / 2, material) + -- ball:setPhysicsBody(body) + addPhysicsComponent(ball, body) ball:setPosition(point) return ball @@ -123,58 +133,61 @@ end local function makeBox(point, size, color, material) material = material or MATERIAL_DEFAULT - local yellow = false; + local yellow = false if color == 0 then - yellow = math.random() > 0.5; + yellow = math.random() > 0.5 else - yellow = color == 1; + yellow = color == 1 end - local box = yellow and cc.Sprite:create("Images/YellowSquare.png") or cc.Sprite:create("Images/CyanSquare.png"); + local box = yellow and cc.Sprite:create("Images/YellowSquare.png") or cc.Sprite:create("Images/CyanSquare.png") - box:setScaleX(size.width/100.0); - box:setScaleY(size.height/100.0); + box:setScaleX(size.width/100.0) + box:setScaleY(size.height/100.0) - local body = cc.PhysicsBody:createBox(size, material); - box:setPhysicsBody(body); - box:setPosition(cc.p(point.x, point.y)); + local body = cc.PhysicsBody:createBox(box:getContentSize(), material) + addPhysicsComponent(box, body) + box:setPosition(cc.p(point.x, point.y)) - return box; + return box end local function makeTriangle(point, size, color, material) material = material or MATERIAL_DEFAULT - local yellow = false; + local yellow = false if color == 0 then - yellow = math.random() > 0.5; + yellow = math.random() > 0.5 else - yellow = color == 1; + yellow = color == 1 end - local triangle = yellow and cc.Sprite:create("Images/YellowTriangle.png") or cc.Sprite:create("Images/CyanTriangle.png"); + local triangle = yellow and cc.Sprite:create("Images/YellowTriangle.png") or cc.Sprite:create("Images/CyanTriangle.png") if size.height == 0 then - triangle:setScale(size.width/100.0); + triangle:setScale(size.width/100.0) else triangle:setScaleX(size.width/50.0) triangle:setScaleY(size.height/43.5) end + + vers = { cc.p(0, triangle:getContentSize().height/2), + cc.p(triangle:getContentSize().width/2, -triangle:getContentSize().height/2), + cc.p(-triangle:getContentSize().width/2, -triangle:getContentSize().height/2) + } + + local body = cc.PhysicsBody:createPolygon(vers, material) + addPhysicsComponent(triangle, body) + triangle:setPosition(point) - vers = { cc.p(0, size.height/2), cc.p(size.width/2, -size.height/2), cc.p(-size.width/2, -size.height/2)}; - - local body = cc.PhysicsBody:createPolygon(vers, material); - triangle:setPhysicsBody(body); - triangle:setPosition(point); - - return triangle; + return triangle end local function PhysicsDemoClickAdd() local layer = cc.Layer:create() local function onEnter() local function onTouchEnded(touch, event) - local location = touch:getLocation(); - addGrossiniAtPosition(layer, location) + local location = touch:getLocation() + addGrossiniAtPosition(layer, location) end local touchListener = cc.EventListenerTouchOneByOne:create() @@ -186,7 +199,12 @@ local function PhysicsDemoClickAdd() addGrossiniAtPosition(layer, VisibleRect:center()) local node = cc.Node:create() - node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))) + addPhysicsComponent(node, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height + ) + ) + ) node:setPosition(VisibleRect:center()) layer:addChild(node) end @@ -211,66 +229,69 @@ local function PhysicsDemoLogoSmash() 31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32, - -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, + -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31, - -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, - -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, - -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, - -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, - -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, - -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, - -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, - -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, + -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, + -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, + -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, 15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13, - -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, - -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, - -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, - -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, - -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, - -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, - -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, + -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, + -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, + -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, + -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, + -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, 127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16, 63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0 - }; + } local function get_pixel(x, y) - return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1) + 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); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setUpdateRate(5.0) - layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image); - layer:addChild(layer.ball); + layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image) + layer:addChild(layer.ball) for y in range(0, logo_height-1) do - for x in range(0, logo_width-1) do - if get_pixel(x, y) == 1 then - local x_jitter = 0.05*math.random(); - local y_jitter = 0.05*math.random(); + for x in range(0, logo_width-1) do + if get_pixel(x, y) == 1 then + local x_jitter = 0.05*math.random() + local y_jitter = 0.05*math.random() - local ball = makeBall(layer, cc.p(2*(x - logo_width/2 + x_jitter) + VisibleRect:getVisibleRect().width/2, - 2*(logo_height-y + y_jitter) + VisibleRect:getVisibleRect().height/2 - logo_height/2), - 0.95, cc.PhysicsMaterial(0.01, 0.0, 0.0)); - - ball:getPhysicsBody():setMass(1.0); - ball:getPhysicsBody():setMoment(PHYSICS_INFINITY); + local ball = makeBall(layer, + cc.p(2*(x - logo_width/2 + x_jitter) + VisibleRect:getVisibleRect().width/2, + 2*(logo_height-y + y_jitter) + VisibleRect:getVisibleRect().height/2 - logo_height/2), + 0.95, + cc.PhysicsMaterial(0.01, 0.0, 0.0)) + local physicsBody = ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + physicsBody:setMass(1.0) + physicsBody:setMoment(PHYSICS_INFINITY) - layer.ball:addChild(ball); - end - end + layer.ball:addChild(ball) + end + end end - local bullet = makeBall(layer, cc.p(400, 0), 10, cc.PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); - bullet:getPhysicsBody():setVelocity(cc.p(200, 0)); + local bullet = makeBall(layer, cc.p(400, 0), 10, cc.PhysicsMaterial(PHYSICS_INFINITY, 0, 0)) + + bullet:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setVelocity(cc.p(200, 0)) bullet:setPosition(cc.p(-500, VisibleRect:getVisibleRect().height/2)) - layer.ball:addChild(bullet); + layer.ball:addChild(bullet) end initWithLayer(layer, onEnter) @@ -282,7 +303,7 @@ end local function PhysicsDemoJoints() local layer = cc.Layer:create() local function onEnter() - layer:toggleDebug(); + layer:toggleDebug() local touchListener = cc.EventListenerTouchOneByOne:create() touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) @@ -291,153 +312,239 @@ local function PhysicsDemoJoints() local eventDispatcher = layer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local width = (VisibleRect:getVisibleRect().width - 10) / 4; - local height = (VisibleRect:getVisibleRect().height - 50) / 4; + local width = (VisibleRect:getVisibleRect().width - 10) / 4 + local height = (VisibleRect:getVisibleRect().height - 50) / 4 - local node = cc.Node:create(); - local box = cc.PhysicsBody:create(); - node:setPhysicsBody(box); - box:setDynamic(false); - node:setPosition(cc.p(0, 0)); - layer:addChild(node); + local node = cc.Node:create() + local box = cc.PhysicsBody:create() + addPhysicsComponent(node, box) + box:setDynamic(false) + node:setPosition(cc.p(0, 0)) + layer:addChild(node) - local scene = cc.Director:getInstance():getRunningScene(); + 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); - box:addShape(cc.PhysicsShapeEdgeBox:create(cc.size(width, height), cc.PHYSICSSHAPE_MATERIAL_DEFAULT, 1, offset)); + local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, + VisibleRect:leftBottom().y + 50 + i * height + height/2) + box:addShape(cc.PhysicsShapeEdgeBox:create(cc.size(width, height), + cc.PHYSICSSHAPE_MATERIAL_DEFAULT, + 1, + offset) + ) local index = i*4 + j + if index == 0 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); - local sp2 = makeBall(layer, cc.p(offset.x + 30, offset.y), 10); - sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBall(layer, cc.p(offset.x + 30, offset.y), 10) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody:setTag(DRAG_BODYS_TAG) - local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); - cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint); + local joint = cc.PhysicsJointPin:construct(sp1PhysicsBody, sp2PhysicsBody, offset) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); + layer:addChild(sp1) + layer:addChild(sp2) elseif index == 1 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - 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); + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody:setTag(DRAG_BODYS_TAG) - local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); - scene:getPhysicsWorld():addJoint(joint); + local joint = cc.PhysicsJointFixed:construct(sp1PhysicsBody, sp2PhysicsBody, offset) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); + layer:addChild(sp1) + layer:addChild(sp2) elseif index == 2 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - 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); + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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); + local joint = cc.PhysicsJointDistance:construct(sp1PhysicsBody, + sp2PhysicsBody, + cc.p(0, 0), + cc.p(0, 0)) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 3 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 3 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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); + local joint = cc.PhysicsJointLimit:construct(sp1PhysicsBody, + sp2PhysicsBody, + cc.p(0, 0), + cc.p(0, 0), + 30.0, + 60.0) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 4 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 4 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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); + local joint = cc.PhysicsJointSpring:construct(sp1PhysicsBody, + sp2PhysicsBody, + cc.p(0, 0), + cc.p(0, 0), + 500.0, + 0.3) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 5 then - local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 5 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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); + local joint = cc.PhysicsJointGroove:construct(sp1PhysicsBody, + sp2PhysicsBody, + cc.p(30, 15), + cc.p(30, -15), + cc.p(-30, 0)) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 6 then - local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); - 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()))); - local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0); - scene:getPhysicsWorld():addJoint(joint); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 6 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody:setTag(DRAG_BODYS_TAG) + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1PhysicsBody, + box, + cc.p(sp1:getPosition()))) + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2PhysicsBody, + box, + cc.p(sp2:getPosition()))) + local joint = cc.PhysicsJointRotarySpring:construct(sp1PhysicsBody, + sp2PhysicsBody, + 3000.0, + 60.0) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 7 then - local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 7 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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()))); - local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); - scene:getPhysicsWorld():addJoint(joint); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1PhysicsBody, + box, + cc.p(sp1:getPosition()))) + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2PhysicsBody, + box, + cc.p(sp2:getPosition()))) + local joint = cc.PhysicsJointRotaryLimit:construct(sp1PhysicsBody, + sp2PhysicsBody, + 0.0, + math.pi/2) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 8 then - local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 8 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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()))); - local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); - scene:getPhysicsWorld():addJoint(joint); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1PhysicsBody, + box, + cc.p(sp1:getPosition()))) + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2PhysicsBody, + box, + cc.p(sp2:getPosition()))) + local joint = cc.PhysicsJointRatchet:construct(sp1PhysicsBody, + sp2PhysicsBody, + 0.0, + math.pi/2) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 9 then - local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 9 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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()))); - local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0); - scene:getPhysicsWorld():addJoint(joint); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1PhysicsBody, + box, + cc.p(sp1:getPosition()))) + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2PhysicsBody, + box, + cc.p(sp2:getPosition()))) + local joint = cc.PhysicsJointGear:construct(sp1PhysicsBody, sp2PhysicsBody, 0.0, 2.0) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - elseif index == 10 then - local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); - 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); + layer:addChild(sp1) + layer:addChild(sp2) + elseif index == 10 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)) + local sp1PhysicsBody = sp1:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp1PhysicsBody:setTag(DRAG_BODYS_TAG) + + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)) + local sp2PhysicsBody = sp2:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + sp2PhysicsBody: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()))); - local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2); - scene:getPhysicsWorld():addJoint(joint); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1PhysicsBody, + box, + cc.p(sp1:getPosition()))) + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2PhysicsBody, + box, + cc.p(sp2:getPosition()))) + local joint = cc.PhysicsJointMotor:construct(sp1PhysicsBody, sp2PhysicsBody, math.pi/2) + scene:getPhysicsWorld():addJoint(joint) - layer:addChild(sp1); - layer:addChild(sp2); - end + layer:addChild(sp1) + layer:addChild(sp2) + end end end end @@ -451,31 +558,38 @@ local function PhysicsDemoPyramidStack() local layer = cc.Layer:create() local function onEnter() - local touchListener = cc.EventListenerTouchOneByOne:create(); - touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN); - touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED); - touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = layer:getEventDispatcher() - eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local node = cc.Node:create(); - node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); - layer:addChild(node); + local node = cc.Node:create() + addPhysicsComponent(node, + cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, + VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, + VisibleRect:rightBottom().y + 50))) + layer:addChild(node) - local ball = cc.Sprite:create("Images/ball.png"); - ball:setScale(1); - ball:setPhysicsBody(cc.PhysicsBody:createCircle(10)); - ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); - ball:setPosition(cc.p(VisibleRect:bottom().x, VisibleRect:bottom().y + 60)); - layer:addChild(ball); + local ball = cc.Sprite:create("Images/ball.png") + ball:setScale(1) + addPhysicsComponent(ball, cc.PhysicsBody:createCircle(10)) + ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + ball:setPosition(cc.p(VisibleRect:bottom().x, VisibleRect:bottom().y + 60)) + layer:addChild(ball) + ball:runAction(cc.Sequence:create(cc.DelayTime:create(3), + cc.ScaleTo:create(0, 3))) + for i in range(0, 13) do - for j in range(0, i) do - local x = VisibleRect:bottom().x + (i/2 - j) * 11 - local y = VisibleRect:bottom().y + (14 - i) * 23 + 100 - local sp = addGrossiniAtPosition(layer, cc.p(x, y), 0.2); - sp:getPhysicsBody():setTag(DRAG_BODYS_TAG); - end + for j in range(0, i) do + local x = VisibleRect:bottom().x + (i/2 - j) * 11 + local y = VisibleRect:bottom().y + (14 - i) * 23 + 100 + local sp = addGrossiniAtPosition(layer, cc.p(x, y), 0.2) + sp:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + end end end @@ -490,117 +604,124 @@ local function PhysicsDemoRayCast() local function onEnter() local function onTouchEnded(touch, event) - local location = touch:getLocation(); + local location = touch:getLocation() - local r = math.random(3); - if r ==1 then - layer:addChild(makeBall(layer, location, 5 + math.random()*10)); - elseif r == 2 then - layer:addChild(makeBox(location, cc.size(10 + math.random()*15, 10 + math.random()*15))); - elseif r == 3 then - layer:addChild(makeTriangle(location, cc.size(10 + math.random()*20, 10 + math.random()*20))); - end + local r = math.random(3) + if r ==1 then + layer:addChild(makeBall(layer, location, 5 + math.random()*10)) + elseif r == 2 then + layer:addChild(makeBox(location, cc.size(10 + math.random()*15, 10 + math.random()*15))) + elseif r == 3 then + layer:addChild(makeTriangle(location, cc.size(10 + math.random()*20, 10 + math.random()*20))) + end end - local touchListener = cc.EventListenerTouchOneByOne:create(); - touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN); - touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = layer:getEventDispatcher() - eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0)); + cc.Director:getInstance():getRunningScene():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))) - node:drawSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50), 1, STATIC_COLOR); - layer:addChild(node); + local node = cc.DrawNode:create() + addPhysicsComponent(node, + cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, + VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, + VisibleRect:rightBottom().y + 50))) + node:drawSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50), + 1, + STATIC_COLOR) + layer:addChild(node) local mode = 0 - cc.MenuItemFont:setFontSize(18); + cc.MenuItemFont:setFontSize(18) local item = cc.MenuItemFont:create("Toogle debugChange Mode(any)") local function changeModeCallback(sender) - mode = (mode + 1) % 3; + mode = (mode + 1) % 3 - if mode == 0 then - item:setString("Change Mode(any)"); - elseif mode == 1 then - item:setString("Change Mode(nearest)"); - elseif mode == 2 then - item:setString("Change Mode(multiple)"); - end + if mode == 0 then + item:setString("Change Mode(any)") + elseif mode == 1 then + item:setString("Change Mode(nearest)") + elseif mode == 2 then + item:setString("Change Mode(multiple)") + end end item:registerScriptTapHandler(changeModeCallback) - local menu = cc.Menu:create(item); - layer:addChild(menu); - menu:setPosition(cc.p(VisibleRect:left().x+100, VisibleRect:top().y-10)); + local menu = cc.Menu:create(item) + layer:addChild(menu) + menu:setPosition(cc.p(VisibleRect:left().x+100, VisibleRect:top().y-10)) local angle = 0 local drawNode = nil local function update(delta) - local L = 150.0; - local point1 = VisibleRect:center() - local d = cc.p(L * math.cos(angle), L * math.sin(angle)); - local point2 = cc.p(point1.x + d.x, point1.y + d.y) + local L = 150.0 + local point1 = VisibleRect:center() + local d = cc.p(L * math.cos(angle), L * math.sin(angle)) + local point2 = cc.p(point1.x + d.x, point1.y + d.y) - if drawNode then layer:removeChild(drawNode); end - drawNode = cc.DrawNode:create(); + if drawNode then layer:removeChild(drawNode) end + drawNode = cc.DrawNode:create() if mode == 0 then - local point3 = cc.p(point2.x, point2.y) - local function func(world, info) - point3 = info.contact - return false - end + local point3 = cc.p(point2.x, point2.y) + local function func(world, info) + point3 = info.contact + return false + end - cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); - drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2) + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR) - if point2.x ~= point3.x or point2.y ~= point3.y then - drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); - end - layer:addChild(drawNode); - elseif mode == 1 then - local point3 = cc.p(point2.x, point2.y) - local friction = 1.0; - local function func(world, info) - if friction > info.fraction then - point3 = info.contact; - friction = info.fraction; - end - return true; - end + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)) + end + layer:addChild(drawNode) + elseif mode == 1 then + local point3 = cc.p(point2.x, point2.y) + local friction = 1.0 + local function func(world, info) + if friction > info.fraction then + point3 = info.contact + friction = info.fraction + end + return true + end - cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); - drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2) + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR) - if point2.x ~= point3.x or point2.y ~= point3.y then - drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); - end - layer:addChild(drawNode); + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)) + end + layer:addChild(drawNode) elseif mode == 2 then - local points = {} + local points = {} - local function func(world, info) - points[#points + 1] = info.contact; - return true; - end + local function func(world, info) + points[#points + 1] = info.contact + return true + end - cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); - drawNode:drawSegment(point1, point2, 1, STATIC_COLOR); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2) + drawNode:drawSegment(point1, point2, 1, STATIC_COLOR) - for _, p in ipairs(points) do - drawNode:drawDot(p, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); - end + for _, p in ipairs(points) do + drawNode:drawDot(p, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)) + end - layer:addChild(drawNode); + layer:addChild(drawNode) end - angle = angle + 0.25 * math.pi / 180.0; + angle = angle + 0.25 * math.pi / 180.0 - end + end - layer:scheduleUpdateWithPriorityLua(update, 0); + layer:scheduleUpdateWithPriorityLua(update, 0) end initWithLayer(layer, onEnter) @@ -613,35 +734,41 @@ local function PhysicsDemoOneWayPlatform() local layer = cc.Layer:create() local function onEnter() - local touchListener = cc.EventListenerTouchOneByOne:create(); - touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN); - touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED); - touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = layer:getEventDispatcher() - eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local ground = cc.Node:create(); - ground:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); - layer:addChild(ground); + local ground = cc.Node:create() + addPhysicsComponent(ground, + cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, + VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, + VisibleRect:rightBottom().y + 50))) + layer:addChild(ground) - local platform = makeBox(VisibleRect:center(), cc.size(200, 50)); - platform:getPhysicsBody():setDynamic(false); - platform:getPhysicsBody():setContactTestBitmask(1); - layer:addChild(platform); + local platform = makeBox(VisibleRect:center(), cc.size(200, 50)) + local platformPhysicsBody = platform:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + platformPhysicsBody:setDynamic(false) + platformPhysicsBody:setContactTestBitmask(0xFFFFFFFF) + layer:addChild(platform) - local ball = makeBall(layer, cc.p(VisibleRect:center().x, VisibleRect:center().y - 50), 20); - ball:getPhysicsBody():setVelocity(cc.p(0, 150)); - ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); - ball:getPhysicsBody():setMass(1.0); - ball:getPhysicsBody():setContactTestBitmask(1); - layer:addChild(ball); + local ball = makeBall(layer, cc.p(VisibleRect:center().x, VisibleRect:center().y - 50), 20) + local ballPhysicsBody = ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + ballPhysicsBody:setVelocity(cc.p(0, 150)) + ballPhysicsBody:setTag(DRAG_BODYS_TAG) + ballPhysicsBody:setMass(1.0) + ballPhysicsBody:setContactTestBitmask(0xFFFFFFFF) + layer:addChild(ball) local function onContactBegin(contact) - return contact:getContactData().normal.y < 0; + return contact:getContactData().normal.y < 0 end - local contactListener = cc.EventListenerPhysicsContactWithBodies:create(platform:getPhysicsBody(), ball:getPhysicsBody()); - contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN); - eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer); + local contactListener = cc.EventListenerPhysicsContactWithBodies:create(platformPhysicsBody, ballPhysicsBody) + contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN) + eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer) end initWithLayer(layer, onEnter) @@ -660,27 +787,31 @@ local function PhysicsDemoActions() local eventDispatcher = layer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local node = cc.Node:create(); - node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))); - node:setPosition(VisibleRect:center()); - layer:addChild(node); + local node = cc.Node:create() + addPhysicsComponent(node, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height))) + node:setPosition(VisibleRect:center()) + layer:addChild(node) - local sp1 = addGrossiniAtPosition(layer, VisibleRect:center()); - local sp2 = addGrossiniAtPosition(layer, cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)); - local sp3 = addGrossiniAtPosition(layer, cc.p(VisibleRect:right().x - 20, VisibleRect:right().y)); - local sp4 = addGrossiniAtPosition(layer, cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-50)); - sp4:getPhysicsBody():setGravityEnable(false); + local sp1 = addGrossiniAtPosition(layer, VisibleRect:center()) + local sp2 = addGrossiniAtPosition(layer, cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)) + local sp3 = addGrossiniAtPosition(layer, cc.p(VisibleRect:right().x - 20, VisibleRect:right().y)) + local sp4 = addGrossiniAtPosition(layer, cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-50)) + sp4:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setGravityEnable(false) - local actionTo = cc.JumpTo:create(2, cc.p(100,100), 50, 4); - local actionBy = cc.JumpBy:create(2, cc.p(300,0), 50, 4); - local actionUp = cc.JumpBy:create(2, cc.p(0,50), 80, 4); - local actionByBack = actionBy:reverse(); + local actionTo = cc.JumpTo:create(2, cc.p(100,100), 50, 4) + local actionBy = cc.JumpBy:create(2, cc.p(300,0), 50, 4) + local actionUp = cc.JumpBy:create(2, cc.p(0,50), 80, 4) + local actionByBack = actionBy:reverse() + local rotateBy = cc.RotateBy:create(2, 180) + local rotateByBack = cc.RotateBy:create(2, -180) - sp1:runAction(cc.RepeatForever:create(actionUp)); - sp2:runAction(cc.RepeatForever:create(cc.Sequence:create(actionBy, actionByBack))); - sp3:runAction(actionTo); - sp4:runAction(cc.RepeatForever:create(cc.Sequence:create(actionBy:clone(), actionByBack:clone()))); + sp1:runAction(cc.RepeatForever:create(actionUp)) + sp2:runAction(cc.RepeatForever:create(cc.Sequence:create(actionBy, actionByBack))) + sp3:runAction(actionTo) + sp4:runAction(cc.RepeatForever:create(cc.Sequence:create(rotateBy, rotateByBack))) end initWithLayer(layer, onEnter) @@ -692,24 +823,24 @@ end local function PhysicsDemoPump() local layer = cc.Layer:create() local function onEnter() - layer:toggleDebug(); + layer:toggleDebug() - local distance = 0.0; - local rotationV = 0.0; + local distance = 0.0 + local rotationV = 0.0 local function onTouchBeganEx(touch, event) onTouchBegan(touch, event) - distance = touch:getLocation().x - VisibleRect:center().x; - return true; + distance = touch:getLocation().x - VisibleRect:center().x + return true end local function onTouchMovedEx(touch, event) - onTouchMoved(touch, event); - distance = touch:getLocation().x - VisibleRect:center().x; + onTouchMoved(touch, event) + distance = touch:getLocation().x - VisibleRect:center().x end local function onTouchEndedEx(touch, event) onTouchEnded(touch, event) - distance = 0; + distance = 0 end local touchListener = cc.EventListenerTouchOneByOne:create() @@ -722,63 +853,89 @@ local function PhysicsDemoPump() local function update() for _, body in ipairs(cc.Director:getInstance():getRunningScene():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)); + body:getNode():setPosition(cc.p(VisibleRect:leftTop().x + 75, + VisibleRect:leftTop().y + math.random() * 90)) + body:setVelocity(cc.p(0, 0)) end end - local gear = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getBody(1); + local gear = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getBody(1) if gear then if distance ~= 0.0 then - rotationV = rotationV + distance/2500.0; + rotationV = rotationV + distance/2500.0 end if rotationV > 30 then rotationV = 30.0 end if rotationV < -30 then rotationV = -30.0 end - gear:setAngularVelocity(rotationV); - rotationV = rotationV*0.995; + gear:setAngularVelocity(rotationV) + rotationV = rotationV * 0.995 end end - layer:scheduleUpdateWithPriorityLua(update, 0); + layer:scheduleUpdateWithPriorityLua(update, 0) - local node = cc.Node:create(); - local body = cc.PhysicsBody:create(); - body:setDynamic(false); + local node = cc.Node:create() + local body = cc.PhysicsBody:create() + body:setDynamic(false) - local staticMaterial = cc.PhysicsMaterial(cc.PHYSICS_INFINITY, 0, 0.5); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y), - cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-130), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 190, VisibleRect:leftTop().y), - cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-50), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-50), - cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-90), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-130), - cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-145), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-145), - cc.p(VisibleRect:leftBottom().x + 100, VisibleRect:leftBottom().y + 80), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 150, VisibleRect:leftTop().y-80), - cc.p(VisibleRect:leftBottom().x + 150, VisibleRect:leftBottom().y + 80), staticMaterial, 2.0)); - body:addShape(cc.PhysicsShapeEdgeSegment:create( - cc.p(VisibleRect:leftTop().x + 150, VisibleRect:leftTop().y-80), - cc.p(VisibleRect:rightTop().x -100, VisibleRect:rightTop().y-150), staticMaterial, 2.0)); + local staticMaterial = cc.PhysicsMaterial(cc.PHYSICS_INFINITY, 0, 0.5) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 50, + VisibleRect:leftTop().y), + cc.p(VisibleRect:leftTop().x + 50, + VisibleRect:leftTop().y-130), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 190, + VisibleRect:leftTop().y), + cc.p(VisibleRect:leftTop().x + 100, + VisibleRect:leftTop().y-50), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 100, + VisibleRect:leftTop().y-50), + cc.p(VisibleRect:leftTop().x + 100, + VisibleRect:leftTop().y-90), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 50, + VisibleRect:leftTop().y-130), + cc.p(VisibleRect:leftTop().x + 100, + VisibleRect:leftTop().y-145), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 100, + VisibleRect:leftTop().y-145), + cc.p(VisibleRect:leftBottom().x + 100, + VisibleRect:leftBottom().y + 80), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 150, + VisibleRect:leftTop().y-80), + cc.p(VisibleRect:leftBottom().x + 150, + VisibleRect:leftBottom().y + 80), + staticMaterial, + 2.0)) + body:addShape(cc.PhysicsShapeEdgeSegment:create(cc.p(VisibleRect:leftTop().x + 150, + VisibleRect:leftTop().y-80), + cc.p(VisibleRect:rightTop().x -100, + VisibleRect:rightTop().y-150), + staticMaterial, + 2.0)) - body:setCategoryBitmask(1); + body:setCategoryBitmask(1) for _ in range(1, 6) do - local ball = makeBall(layer, cc.p(VisibleRect:leftTop().x + 75 + math.random() * 90, VisibleRect:leftTop().y), 22, cc.PhysicsMaterial(0.05, 0.0, 0.1)); - ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); - layer:addChild(ball); + local ball = makeBall(layer, + cc.p(VisibleRect:leftTop().x + 75 + math.random() * 90, + VisibleRect:leftTop().y), + 22, + cc.PhysicsMaterial(0.05, 0.0, 0.1)) + ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + layer:addChild(ball) end - node:setPhysicsBody(body); - layer:addChild(node); + addPhysicsComponent(node, body) + layer:addChild(node) local vec = { @@ -786,61 +943,72 @@ local function PhysicsDemoPump() cc.p(VisibleRect:leftTop().x + 148, VisibleRect:leftTop().y-161), cc.p(VisibleRect:leftBottom().x + 148, VisibleRect:leftBottom().y + 20), cc.p(VisibleRect:leftBottom().x + 102, VisibleRect:leftBottom().y + 20) - }; + } - local world = cc.Director:getInstance():getRunningScene():getPhysicsWorld(); + local world = cc.Director:getInstance():getRunningScene():getPhysicsWorld() -- small gear - local sgear = cc.Node:create(); - local sgearB = cc.PhysicsBody:createCircle(44); - sgear:setPhysicsBody(sgearB); - sgear:setPosition(cc.p(VisibleRect:leftBottom().x + 125, VisibleRect:leftBottom().y)); - layer:addChild(sgear); - sgearB:setCategoryBitmask(4); - sgearB:setCollisionBitmask(4); - sgearB:setTag(1); - world:addJoint(cc.PhysicsJointPin:construct(body, sgearB, cc.p(sgear:getPosition()))); + local sgear = cc.Node:create() + local sgearB = cc.PhysicsBody:createCircle(44) + addPhysicsComponent(sgear, sgearB) + sgear:setPosition(cc.p(VisibleRect:leftBottom().x + 125, VisibleRect:leftBottom().y)) + layer:addChild(sgear) + sgearB:setCategoryBitmask(4) + sgearB:setCollisionBitmask(4) + sgearB:setTag(1) + world:addJoint(cc.PhysicsJointPin:construct(body, sgearB, cc.p(sgear:getPosition()))) -- big gear - local bgear = cc.Node:create(); - local bgearB = cc.PhysicsBody:createCircle(100); - bgear:setPhysicsBody(bgearB); - bgear:setPosition(cc.p(VisibleRect:leftBottom().x + 275, VisibleRect:leftBottom().y)); - layer:addChild(bgear); - bgearB:setCategoryBitmask(4); - world:addJoint(cc.PhysicsJointPin:construct(body, bgearB, cc.p(bgear:getPosition()))); + local bgear = cc.Node:create() + local bgearB = cc.PhysicsBody:createCircle(100) + addPhysicsComponent(bgear, bgearB) + bgear:setPosition(cc.p(VisibleRect:leftBottom().x + 275, VisibleRect:leftBottom().y)) + layer:addChild(bgear) + bgearB:setCategoryBitmask(4) + world:addJoint(cc.PhysicsJointPin:construct(body, bgearB, cc.p(bgear:getPosition()))) -- pump - local pump = cc.Node:create(); - local center = cc.PhysicsShape:getPolyonCenter(vec); - pump:setPosition(center); - local pumpB = cc.PhysicsBody:createPolygon(vec, cc.PHYSICSBODY_MATERIAL_DEFAULT, cc.p(-center.x, -center.y)); - pump:setPhysicsBody(pumpB); - layer:addChild(pump); - pumpB:setCategoryBitmask(2); - pumpB:setGravityEnable(false); - world:addJoint(cc.PhysicsJointDistance:construct(pumpB, sgearB, cc.p(0, 0), cc.p(0, -44))); + local pump = cc.Node:create() + local center = cc.PhysicsShape:getPolyonCenter(vec) + pump:setPosition(center) + local pumpB = cc.PhysicsBody:createPolygon(vec, + cc.PHYSICSBODY_MATERIAL_DEFAULT, + cc.p(-center.x, -center.y)) + addPhysicsComponent(pump, pumpB) + layer:addChild(pump) + pumpB:setCategoryBitmask(2) + pumpB:setGravityEnable(false) + world:addJoint(cc.PhysicsJointDistance:construct(pumpB, sgearB, cc.p(0, 0), cc.p(0, -44))) -- plugger - local seg = {cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y-120), cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y-100)}; - local segCenter = cc.p((seg[2].x + seg[1].x)/2, (seg[2].y + seg[1].y)/2); - seg[2] = cc.p(seg[2].x - segCenter.x, seg[2].y - segCenter.y); - seg[1] = cc.p(seg[1].x - segCenter.x, seg[1].y - segCenter.y); - local plugger = cc.Node:create(); - local pluggerB = cc.PhysicsBody:createEdgeSegment(seg[1], seg[2], cc.PhysicsMaterial(0.01, 0.0, 0.5), 20); - pluggerB:setDynamic(true); - pluggerB:setMass(30); - pluggerB:setMoment(100000); - plugger:setPhysicsBody(pluggerB); - plugger:setPosition(segCenter); - layer:addChild(plugger); - pluggerB:setCategoryBitmask(2); - sgearB:setCollisionBitmask(5); - world:addJoint(cc.PhysicsJointPin:construct(body, pluggerB, cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y-90))); - world:addJoint(cc.PhysicsJointDistance:construct(pluggerB, sgearB, - pluggerB:world2Local(cc.p(0,0)), cc.p(44, 0))); + local seg = {cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y-120), + cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y-100)} + local segCenter = cc.p((seg[2].x + seg[1].x)/2, (seg[2].y + seg[1].y)/2) + seg[2] = cc.p(seg[2].x - segCenter.x, seg[2].y - segCenter.y) + seg[1] = cc.p(seg[1].x - segCenter.x, seg[1].y - segCenter.y) + local plugger = cc.Node:create() + local pluggerB = cc.PhysicsBody:createEdgeSegment(seg[1], + seg[2], + cc.PhysicsMaterial(0.01, 0.0, 0.5), + 20) + pluggerB:setDynamic(true) + pluggerB:setMass(30) + pluggerB:setMoment(100000) + addPhysicsComponent(plugger, pluggerB) + plugger:setPosition(segCenter) + layer:addChild(plugger) + pluggerB:setCategoryBitmask(2) + sgearB:setCollisionBitmask(5) + world:addJoint(cc.PhysicsJointPin:construct(body, + pluggerB, + cc.p(VisibleRect:leftBottom().x + 75, + VisibleRect:leftBottom().y-90))) + world:addJoint(cc.PhysicsJointDistance:construct(pluggerB, + sgearB, + pluggerB:world2Local(cc.p(0,0)), + cc.p(44, 0))) end initWithLayer(layer, onEnter) @@ -854,83 +1022,90 @@ local function PhysicsDemoSlice() local layer = cc.Layer:create() local function onEnter() layer:toggleDebug() - local sliceTag = 1; + local sliceTag = 1 local function clipPoly(shape, normal, distance) - local body = shape:getBody(); - local count = shape:getPointsCount(); + local body = shape:getBody() + local count = shape:getPointsCount() local points = {} local j = count - 1 for i in range(0, count-1) do - local a = body:local2World(shape:getPoint(j)); - local aDist = cc.pDot(a, normal) - distance; + local a = body:local2World(shape:getPoint(j)) + local aDist = cc.pDot(a, normal) - distance if aDist < 0.0 then - points[#points + 1] = a; + points[#points + 1] = a end - local b = body:local2World(shape:getPoint(i)); - local bDist = cc.pDot(b, normal) - distance; + local b = body:local2World(shape:getPoint(i)) + local bDist = cc.pDot(b, normal) - distance if aDist*bDist < 0.0 then - local t = math.abs(aDist)/(math.abs(aDist) + math.abs(bDist)); - points[#points + 1] = cc.pLerp(a, b, t); + local t = math.abs(aDist)/(math.abs(aDist) + math.abs(bDist)) + points[#points + 1] = cc.pLerp(a, b, t) end j = i end - local center = cc.PhysicsShape:getPolyonCenter(points); - local node = cc.Node:create(); - local polyon = cc.PhysicsBody:createPolygon(points, cc.PHYSICSBODY_MATERIAL_DEFAULT, cc.p(-center.x, -center.y)); - node:setPosition(center); - node:setPhysicsBody(polyon); - polyon:setVelocity(body:getVelocityAtWorldPoint(center)); - polyon:setAngularVelocity(body:getAngularVelocity()); - polyon.tag = sliceTag; - layer:addChild(node); + local center = cc.PhysicsShape:getPolyonCenter(points) + local node = cc.Node:create() + local polyon = cc.PhysicsBody:createPolygon(points, + cc.PHYSICSBODY_MATERIAL_DEFAULT, + cc.p(-center.x, -center.y)) + node:setPosition(center) + addPhysicsComponent(node, polyon) + polyon:setVelocity(body:getVelocityAtWorldPoint(center)) + polyon:setAngularVelocity(body:getAngularVelocity()) + polyon.tag = sliceTag + layer:addChild(node) end local function slice(world, info) if info.shape:getBody().tag ~= sliceTag then - return true; + return true end if not info.shape:containsPoint(info.start) and not info.shape:containsPoint(info.ended) then - local normal = cc.p(info.ended.x - info.start.x, info.ended.y - info.start.y); - normal = cc.pNormalize(cc.pPerp(normal)); - local dist = cc.pDot(info.start, normal); + local normal = cc.p(info.ended.x - info.start.x, info.ended.y - info.start.y) + normal = cc.pNormalize(cc.pPerp(normal)) + local dist = cc.pDot(info.start, normal) - clipPoly(info.shape, normal, dist); - clipPoly(info.shape, cc.p(-normal.x, -normal.y), -dist); + clipPoly(info.shape, normal, dist) + clipPoly(info.shape, cc.p(-normal.x, -normal.y), -dist) - info.shape:getBody():removeFromWorld(); + info.shape:getBody():removeFromWorld() end - return true; + return true end local function onTouchEnded(touch, event) - cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation()); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(slice, + touch:getStartLocation(), + touch:getLocation()) end - local touchListener = cc.EventListenerTouchOneByOne:create(); - touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN); - touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = layer:getEventDispatcher() - eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local ground = cc.Node:create(); - ground:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), - cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); - layer:addChild(ground); + local ground = cc.Node:create() + addPhysicsComponent(ground, + cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, + VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, + VisibleRect:rightBottom().y + 50))) + layer:addChild(ground) - local box = cc.Node:create(); - local points = {cc.p(-100, -100), cc.p(-100, 100), cc.p(100, 100), cc.p(100, -100)}; - box:setPhysicsBody(cc.PhysicsBody:createPolygon(points)); - box:setPosition(VisibleRect:center()); - box:getPhysicsBody().tag = sliceTag; - layer:addChild(box); + local box = cc.Node:create() + local points = {cc.p(-100, -100), cc.p(-100, 100), cc.p(100, 100), cc.p(100, -100)} + addPhysicsComponent(box, cc.PhysicsBody:createPolygon(points)) + box:setPosition(VisibleRect:center()) + box:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody().tag = sliceTag + layer:addChild(box) end initWithLayer(layer, onEnter) @@ -944,17 +1119,19 @@ end local function PhysicsDemoBug3988() local layer = cc.Layer:create() local function onEnter() - layer:toggleDebug(); - cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + layer:toggleDebug() + cc.Director:getInstance():getRunningScene():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)); - ball:setRotation(30.0); - layer:addChild(ball); + local ball = cc.Sprite:create("Images/YellowSquare.png") + ball:setPosition(cc.p(VisibleRect:center().x-100, VisibleRect:center().y)) + ball:setRotation(30.0) + layer:addChild(ball) - local physicsBall = makeBox(cc.p(VisibleRect:center().x+100, VisibleRect:center().y), cc.size(100, 100)); - physicsBall:setRotation(30.0); - layer:addChild(physicsBall); + local physicsBall = makeBox(cc.p(VisibleRect:center().x+100, + VisibleRect:center().y), + cc.size(100, 100)) + physicsBall:setRotation(30.0) + layer:addChild(physicsBall) end initWithLayer(layer, onEnter) @@ -967,123 +1144,134 @@ end local function PhysicsContactTest() local layer = cc.Layer:create() local function onEnter() - cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); - local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)) + local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height) - layer.yellowBoxNum = 50; - layer.blueBoxNum = 50; - layer.yellowTriangleNum = 50; - layer.blueTriangleNum = 50; + layer.yellowBoxNum = 50 + layer.blueBoxNum = 50 + layer.yellowTriangleNum = 50 + layer.blueTriangleNum = 50 local function onContactBegin(contact) - local a = contact:getShapeA():getBody(); - local b = contact:getShapeB():getBody(); - local body = (a:getCategoryBitmask() == 4 or a:getCategoryBitmask() == 8) and a or b; + local a = contact:getShapeA():getBody() + local b = contact:getShapeB():getBody() + local body = (a:getCategoryBitmask() == 4 or a:getCategoryBitmask() == 8) and a or b - assert(body:getCategoryBitmask() == 4 or body:getCategoryBitmask() == 8, "physics contact fail"); + assert(body:getCategoryBitmask() == 4 or body:getCategoryBitmask() == 8, "physics contact fail") - return true; + return true end local function resetTest() - layer:removeChildByTag(10); - local root = cc.Node:create(); - root:setTag(10); - layer:addChild(root); + layer:removeChildByTag(10) + local root = cc.Node:create() + root:setTag(10) + layer:addChild(root) - local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height) - local label = cc.Label:createWithTTF(tostring(layer.yellowBoxNum), s_arialPath, 32); - root:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2, s.height-50)); + local label = cc.Label:createWithTTF(tostring(layer.yellowBoxNum), s_arialPath, 32) + root:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2, s.height-50)) - label = cc.Label:createWithTTF(tostring(layer.blueBoxNum), s_arialPath, 32); - root:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2, s.height-90)); + label = cc.Label:createWithTTF(tostring(layer.blueBoxNum), s_arialPath, 32) + root:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2, s.height-90)) - label = cc.Label:createWithTTF(tostring(layer.yellowTriangleNum), s_arialPath, 32); - root:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2, s.height-130)); + label = cc.Label:createWithTTF(tostring(layer.yellowTriangleNum), s_arialPath, 32) + root:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2, s.height-130)) - label = cc.Label:createWithTTF(tostring(layer.blueTriangleNum), s_arialPath, 32); - root:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2, s.height-170)); + label = cc.Label:createWithTTF(tostring(layer.blueTriangleNum), s_arialPath, 32) + root:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2, s.height-170)) - local wall = cc.Node:create(); - wall:setPhysicsBody(cc.PhysicsBody:createEdgeBox(s, cc.PhysicsMaterial(0.1, 1, 0.0))); - wall:setPosition(VisibleRect:center()); - root:addChild(wall); + local wall = cc.Node:create() + addPhysicsComponent(wall, cc.PhysicsBody:createEdgeBox(s, cc.PhysicsMaterial(0.1, 1, 0.0))) + wall:setPosition(VisibleRect:center()) + root:addChild(wall) -- yellow box, will collide with itself and blue box. for i = 1, layer.yellowBoxNum do - local size = cc.size(10 + math.random()*10, 10 + math.random()*10); - local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); - local position = cc.p(winSize.width - size.width, winSize.height - size.height); - position.x = position.x * math.random(); - position.y = position.y * math.random(); - position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); - local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); - local box = makeBox(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)); - box:getPhysicsBody():setVelocity(velocity); - box:getPhysicsBody():setCategoryBitmask(1); -- 0001 - box:getPhysicsBody():setContactTestBitmask(4); -- 0100 - box:getPhysicsBody():setCollisionBitmask(3); -- 0011 - root:addChild(box); + local size = cc.size(10 + math.random()*10, 10 + math.random()*10) + local winSize = cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height) + local position = cc.p(winSize.width - size.width, winSize.height - size.height) + position.x = position.x * math.random() + position.y = position.y * math.random() + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, + VisibleRect:leftBottom().y + position.y + size.height/2) + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200) + local box = makeBox(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)) + local boxPhysicsBody = box:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + boxPhysicsBody:setVelocity(velocity) + boxPhysicsBody:setCategoryBitmask(1) -- 0001 + boxPhysicsBody:setContactTestBitmask(4) -- 0100 + boxPhysicsBody:setCollisionBitmask(3) -- 0011 + root:addChild(box) end -- blue box, will collide with blue box. for i = 1, layer.blueBoxNum do - local size = cc.size(10 + math.random()*10, 10 + math.random()*10); - local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); - local position = cc.p(winSize.width - size.width, winSize.height - size.height); - position.x = position.x * math.random(); - position.y = position.y * math.random(); - position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); - local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); - local box = makeBox(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)); - box:getPhysicsBody():setVelocity(velocity); - box:getPhysicsBody():setCategoryBitmask(2); -- 0010 - box:getPhysicsBody():setContactTestBitmask(8); -- 1000 - box:getPhysicsBody():setCollisionBitmask(1); -- 0001 - root:addChild(box); + local size = cc.size(10 + math.random()*10, 10 + math.random()*10) + local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height) + local position = cc.p(winSize.width - size.width, winSize.height - size.height) + position.x = position.x * math.random() + position.y = position.y * math.random() + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, + VisibleRect:leftBottom().y + position.y + size.height/2) + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200) + local box = makeBox(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)) + local boxPhysicsBody = box:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + boxPhysicsBody:setVelocity(velocity) + boxPhysicsBody:setCategoryBitmask(2) -- 0010 + boxPhysicsBody:setContactTestBitmask(8) -- 1000 + boxPhysicsBody:setCollisionBitmask(1) -- 0001 + root:addChild(box) end -- yellow triangle, will collide with itself and blue box. for i = 1, layer.yellowTriangleNum do - local size = cc.size(10 + math.random()*10, 10 + math.random()*10); - local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); - local position = cc.p(winSize.width - size.width, winSize.height - size.height); - position.x = position.x * math.random(); - position.y = position.y * math.random(); - position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); - local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); - local triangle = makeTriangle(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)); - triangle:getPhysicsBody():setVelocity(velocity); - triangle:getPhysicsBody():setCategoryBitmask(4); -- 0100 - triangle:getPhysicsBody():setContactTestBitmask(1); -- 0001 - triangle:getPhysicsBody():setCollisionBitmask(6); -- 0110 - root:addChild(triangle); + local size = cc.size(10 + math.random()*10, 10 + math.random()*10) + local winSize = cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height) + local position = cc.p(winSize.width - size.width, winSize.height - size.height) + position.x = position.x * math.random() + position.y = position.y * math.random() + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, + VisibleRect:leftBottom().y + position.y + size.height/2) + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200) + local triangle = makeTriangle(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)) + local trianglePhysicsBody = triangle:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + trianglePhysicsBody:setVelocity(velocity) + trianglePhysicsBody:setCategoryBitmask(4) -- 0100 + trianglePhysicsBody:setContactTestBitmask(1) -- 0001 + trianglePhysicsBody:setCollisionBitmask(6) -- 0110 + root:addChild(triangle) end -- blue triangle, will collide with yellow box. for i = 1, layer.blueTriangleNum do - local size = cc.size(10 + math.random()*10, 10 + math.random()*10); - local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); - local position = cc.p(winSize.width - size.width, winSize.height - size.height); - position.x = position.x * math.random(); - position.y = position.y * math.random(); - position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); - local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); - local triangle = makeTriangle(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)); - triangle:getPhysicsBody():setVelocity(velocity); - triangle:getPhysicsBody():setCategoryBitmask(8); -- 1000 - triangle:getPhysicsBody():setContactTestBitmask(2); -- 0010 - triangle:getPhysicsBody():setCollisionBitmask(1); -- 0001 - root:addChild(triangle); + local size = cc.size(10 + math.random()*10, 10 + math.random()*10) + local winSize = cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height) + local position = cc.p(winSize.width - size.width, winSize.height - size.height) + position.x = position.x * math.random() + position.y = position.y * math.random() + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, + VisibleRect:leftBottom().y + position.y + size.height/2) + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200) + local triangle = makeTriangle(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)) + local trianglePhysicsBody = triangle:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + trianglePhysicsBody:setVelocity(velocity) + trianglePhysicsBody:setCategoryBitmask(8) -- 1000 + trianglePhysicsBody:setContactTestBitmask(2) -- 0010 + trianglePhysicsBody:setCollisionBitmask(1) -- 0001 + root:addChild(triangle) end end @@ -1112,93 +1300,93 @@ local function PhysicsContactTest() layer.blueTriangleNum = layer.blueTriangleNum + 50 end - resetTest(); + resetTest() end - cc.MenuItemFont:setFontSize(65); - local decrease1 = cc.MenuItemFont:create(" - "); - decrease1:setColor(cc.c3b(0,200,20)); - local increase1 = cc.MenuItemFont:create(" + "); - increase1:setColor(cc.c3b(0,200,20)); - decrease1:setTag(1); - increase1:setTag(1); - decrease1:registerScriptTapHandler(onDecrease); - increase1:registerScriptTapHandler(onIncrease); + cc.MenuItemFont:setFontSize(65) + local decrease1 = cc.MenuItemFont:create(" - ") + decrease1:setColor(cc.c3b(0,200,20)) + local increase1 = cc.MenuItemFont:create(" + ") + increase1:setColor(cc.c3b(0,200,20)) + decrease1:setTag(1) + increase1:setTag(1) + decrease1:registerScriptTapHandler(onDecrease) + increase1:registerScriptTapHandler(onIncrease) - local menu1 = cc.Menu:create(decrease1, increase1); - menu1:alignItemsHorizontally(); - menu1:setPosition(cc.p(s.width/2, s.height-50)); - layer:addChild(menu1, 1); + local menu1 = cc.Menu:create(decrease1, increase1) + menu1:alignItemsHorizontally() + menu1:setPosition(cc.p(s.width/2, s.height-50)) + layer:addChild(menu1, 1) - local label = cc.Label:createWithTTF("yellow box", s_arialPath, 32); - layer:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2 - 150, s.height-50)); + local label = cc.Label:createWithTTF("yellow box", s_arialPath, 32) + layer:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2 - 150, s.height-50)) - local decrease2 = cc.MenuItemFont:create(" - "); - decrease2:setColor(cc.c3b(0,200,20)); - local increase2 = cc.MenuItemFont:create(" + "); - increase2:setColor(cc.c3b(0,200,20)); - decrease2:setTag(2); - increase2:setTag(2); - decrease2:registerScriptTapHandler(onDecrease); - increase2:registerScriptTapHandler(onIncrease); + local decrease2 = cc.MenuItemFont:create(" - ") + decrease2:setColor(cc.c3b(0,200,20)) + local increase2 = cc.MenuItemFont:create(" + ") + increase2:setColor(cc.c3b(0,200,20)) + decrease2:setTag(2) + increase2:setTag(2) + decrease2:registerScriptTapHandler(onDecrease) + increase2:registerScriptTapHandler(onIncrease) - local menu2 = cc.Menu:create(decrease2, increase2); - menu2:alignItemsHorizontally(); - menu2:setPosition(cc.p(s.width/2, s.height-90)); - layer:addChild(menu2, 1); + local menu2 = cc.Menu:create(decrease2, increase2) + menu2:alignItemsHorizontally() + menu2:setPosition(cc.p(s.width/2, s.height-90)) + layer:addChild(menu2, 1) - label = cc.Label:createWithTTF("blue box", s_arialPath, 32); - layer:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2 - 150, s.height-90)); + label = cc.Label:createWithTTF("blue box", s_arialPath, 32) + layer:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2 - 150, s.height-90)) - local decrease3 = cc.MenuItemFont:create(" - "); - decrease3:setColor(cc.c3b(0,200,20)); - local increase3 = cc.MenuItemFont:create(" + "); - increase3:setColor(cc.c3b(0,200,20)); - decrease3:setTag(3); - increase3:setTag(3); - decrease3:registerScriptTapHandler(onDecrease); - increase3:registerScriptTapHandler(onIncrease); + local decrease3 = cc.MenuItemFont:create(" - ") + decrease3:setColor(cc.c3b(0,200,20)) + local increase3 = cc.MenuItemFont:create(" + ") + increase3:setColor(cc.c3b(0,200,20)) + decrease3:setTag(3) + increase3:setTag(3) + decrease3:registerScriptTapHandler(onDecrease) + increase3:registerScriptTapHandler(onIncrease) - local menu3 = cc.Menu:create(decrease3, increase3); - menu3:alignItemsHorizontally(); - menu3:setPosition(cc.p(s.width/2, s.height-130)); - layer:addChild(menu3, 1); + local menu3 = cc.Menu:create(decrease3, increase3) + menu3:alignItemsHorizontally() + menu3:setPosition(cc.p(s.width/2, s.height-130)) + layer:addChild(menu3, 1) - label = cc.Label:createWithTTF("yellow triangle", s_arialPath, 32); - layer:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2 - 150, s.height-130)); + label = cc.Label:createWithTTF("yellow triangle", s_arialPath, 32) + layer:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2 - 150, s.height-130)) - local decrease4 = cc.MenuItemFont:create(" - "); - decrease4:setColor(cc.c3b(0,200,20)); - local increase4 = cc.MenuItemFont:create(" + "); - increase4:setColor(cc.c3b(0,200,20)); - decrease4:setTag(4); - increase4:setTag(4); - decrease4:registerScriptTapHandler(onDecrease); - increase4:registerScriptTapHandler(onIncrease); + local decrease4 = cc.MenuItemFont:create(" - ") + decrease4:setColor(cc.c3b(0,200,20)) + local increase4 = cc.MenuItemFont:create(" + ") + increase4:setColor(cc.c3b(0,200,20)) + decrease4:setTag(4) + increase4:setTag(4) + decrease4:registerScriptTapHandler(onDecrease) + increase4:registerScriptTapHandler(onIncrease) - local menu4 = cc.Menu:create(decrease4, increase4); - menu4:alignItemsHorizontally(); - menu4:setPosition(cc.p(s.width/2, s.height-170)); - layer:addChild(menu4, 1); + local menu4 = cc.Menu:create(decrease4, increase4) + menu4:alignItemsHorizontally() + menu4:setPosition(cc.p(s.width/2, s.height-170)) + layer:addChild(menu4, 1) - label = cc.Label:createWithTTF("blue triangle", s_arialPath, 32); - layer:addChild(label, 1); - label:setAnchorPoint(cc.p(0.5, 0.5)); - label:setPosition(cc.p(s.width/2 - 150, s.height-170)); + label = cc.Label:createWithTTF("blue triangle", s_arialPath, 32) + layer:addChild(label, 1) + label:setAnchorPoint(cc.p(0.5, 0.5)) + label:setPosition(cc.p(s.width/2 - 150, s.height-170)) - local contactListener = cc.EventListenerPhysicsContact:create(); - contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN); + local contactListener = cc.EventListenerPhysicsContact:create() + contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN) local eventDispatcher = layer:getEventDispatcher() - eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer); + eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer) - resetTest(); + resetTest() end initWithLayer(layer, onEnter) @@ -1212,7 +1400,7 @@ local function PhysicsPositionRotationTest() local function onEnter() layer:toggleDebug() - cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)) local touchListener = cc.EventListenerTouchOneByOne:create() touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) @@ -1221,43 +1409,49 @@ local function PhysicsPositionRotationTest() local eventDispatcher = layer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) - local wall = cc.Node:create(); - wall:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))); - wall:setPosition(VisibleRect:center()); - layer:addChild(wall); + local wall = cc.Node:create() + addPhysicsComponent(wall, + cc.PhysicsBody:createEdgeBox(VisibleRect:getVisibleRect())) + wall:setPosition(VisibleRect:center()) + layer:addChild(wall) -- anchor test - local anchorNode = cc.Sprite:create("Images/YellowSquare.png"); - anchorNode:setAnchorPoint(cc.p(0.1, 0.9)); - anchorNode:setPosition(100, 100); - anchorNode:setScale(0.25); - anchorNode:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(anchorNode:getContentSize().width*anchorNode:getScale(), anchorNode:getContentSize().height*anchorNode:getScale()))); - anchorNode:getPhysicsBody():setTag(DRAG_BODYS_TAG); - layer:addChild(anchorNode); + local anchorNode = cc.Sprite:create("Images/YellowSquare.png") + anchorNode:setAnchorPoint(cc.p(0.1, 0.9)) + anchorNode:setPosition(100, 100) + anchorNode:setScale(0.25) + addPhysicsComponent(anchorNode, + cc.PhysicsBody:createBox(anchorNode:getContentSize())) + anchorNode:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + layer:addChild(anchorNode) --parent test - local parent = cc.Sprite:create("Images/YellowSquare.png"); - parent:setPosition(200, 100); - parent:setScale(0.25); - parent:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(anchorNode:getContentSize().width*anchorNode:getScale(), anchorNode:getContentSize().height*anchorNode:getScale()))); - parent:getPhysicsBody():setTag(DRAG_BODYS_TAG); - layer:addChild(parent); + local parent = cc.Sprite:create("Images/YellowSquare.png") + parent:setPosition(200, 100) + parent:setScale(0.25) + local parentPhysicsBody = cc.PhysicsBody:createBox(anchorNode:getContentSize()) + addPhysicsComponent(parent, parentPhysicsBody) + parentPhysicsBody:setTag(DRAG_BODYS_TAG) + layer:addChild(parent) - local leftBall = cc.Sprite:create("Images/ball.png"); - leftBall:setPosition(-30, 0); - leftBall:setScale(2); - leftBall:setPhysicsBody(cc.PhysicsBody:createCircle(leftBall:getContentSize().width)); - leftBall:getPhysicsBody():setTag(DRAG_BODYS_TAG); - parent:addChild(leftBall); + local leftBall = cc.Sprite:create("Images/ball.png") + leftBall:setPosition(-30, 0) + leftBall:setScale(2) + addPhysicsComponent(leftBall, cc.PhysicsBody:createCircle(leftBall:getContentSize().width / 2)) + leftBall:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + parent:addChild(leftBall) -- offset position rotation test - local offsetPosNode = cc.Sprite:create("Images/YellowSquare.png"); - offsetPosNode:setPosition(100, 200); - offsetPosNode:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(offsetPosNode:getContentSize().width/2, offsetPosNode:getContentSize().height/2))); - offsetPosNode:getPhysicsBody():setPositionOffset(cc.p(-offsetPosNode:getContentSize().width/2, -offsetPosNode:getContentSize().height/2)); - offsetPosNode:getPhysicsBody():setRotationOffset(45); - offsetPosNode:getPhysicsBody():setTag(DRAG_BODYS_TAG); - layer:addChild(offsetPosNode); + local offsetPosNode = cc.Sprite:create("Images/YellowSquare.png") + offsetPosNode:setPosition(100, 200) + local offsetPosNodePhysicsBody = cc.PhysicsBody:createBox(cc.size(offsetPosNode:getContentSize().width/2, + offsetPosNode:getContentSize().height/2)) + addPhysicsComponent(offsetPosNode, offsetPosNodePhysicsBody) + offsetPosNodePhysicsBody:setPositionOffset(cc.p(-offsetPosNode:getContentSize().width/2, + -offsetPosNode:getContentSize().height/2)) + offsetPosNodePhysicsBody:setRotationOffset(45) + offsetPosNodePhysicsBody:setTag(DRAG_BODYS_TAG) + layer:addChild(offsetPosNode) end initWithLayer(layer, onEnter) @@ -1266,6 +1460,274 @@ local function PhysicsPositionRotationTest() return layer end +local function PhysicsSetGravityEnableTest() + local layer = cc.Layer:create() + local function onEnter() + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local wall = cc.Node:create() + addPhysicsComponent(wall, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height), + cc.PhysicsMaterial(0.1, 1.0, 0.0))) + wall:setPosition(VisibleRect:center()); + layer:addChild(wall) + + local commonBox = makeBox(cc.p(100, 100), cc.size(50, 50), 1) + commonBox:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + layer:addChild(commonBox) + + local box = makeBox(cc.p(200, 100), cc.size(50, 50), 2) + local boxBody = box:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + boxBody:setMass(20) + boxBody:setTag(DRAG_BODYS_TAG) + boxBody:setGravityEnable(false) + layer:addChild(box) + + local ball = makeBall(layer,cc.p(200,200),50) + ball:setTag(2) + local ballBody = ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody() + ballBody:setTag(DRAG_BODYS_TAG) + ballBody:setGravityEnable(false) + ballBody:setMass(50) + layer:addChild(ball) + + local function onScheduleOnce() + cclog("onScheduleOnce") + local ball = layer:getChildByTag(2) + ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setMass(200) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 98)) + end + --layer:scheduleOnce(onScheduleOnce,1.0) + local action = cc.Sequence:create(cc.DelayTime:create(1.0), + cc.CallFunc:create(onScheduleOnce)) + layer:runAction(action) + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Set Gravity Enable Test") + Helper.subtitleLabel:setString("only yellow box drop down") + return layer +end + +local function PhysicsDemoBug5482() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug() + local _bodyInA = false + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + -- wall + local wall = cc.Node:create() + addPhysicsComponent(wall, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height), + cc.PhysicsMaterial(0.1, 1.0, 0.0))) + wall:setPosition(VisibleRect:center()); + layer:addChild(wall) + + local _nodeA = cc.Sprite:create("Images/YellowSquare.png") + _nodeA:setPosition(cc.p(VisibleRect:center().x-150,100)) + addPhysicsComponent(_nodeA,nil) + layer:addChild(_nodeA) + + local _nodeB = cc.Sprite:create("Images/YellowSquare.png") + _nodeB:setPosition(cc.p(VisibleRect:center().x+150,100)) + addPhysicsComponent(_nodeB,nil) + layer:addChild(_nodeB) + + local _body = cc.PhysicsBody:createBox(_nodeA:getContentSize()) + _body:setTag(DRAG_BODYS_TAG) + _body:retain() + + local function changeBodyCallback(sender) + local node = nil + if _bodyInA then + node = _nodeB + cclog("_nodeB") + else + node = _nodeA + cclog("_nodeA") + end + node:getComponent(PHYSICS_COMPONENT_NAME):setPhysicsBody(_body) + _bodyInA = not _bodyInA + end + + cc.MenuItemFont:setFontSize(18) + local _button = cc.MenuItemFont:create("Set Body To A"); + _button:registerScriptTapHandler(changeBodyCallback) + + local menu = cc.Menu:create(_button) + layer:addChild(menu) + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("bug 5482: setPhysicsBodyTest") + Helper.subtitleLabel:setString("change physics body to the other.") + return layer +end + +local function PhysicsDemoBug5482() + local layer = cc.Layer:create() + local function onEnter() + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)) + + local function addBall() + local ball = cc.Sprite:create("Images/ball.png") + ball:setPosition(cc.p(100,100)) + addPhysicsComponent(ball,cc.PhysicsBody:createCircle(ball:getContentSize().width/2, cc.PhysicsMaterial(0.1, 1, 0.0))) + ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + ball:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setVelocity(cc.p(1000,20)) + layer:addChild(ball) + end + + local function update(delta) + for i=1,3 do + cc.Director:getInstance():getRunningScene():getPhysicsWorld():step(1/180.0) + end + end + + local function updateStart(delta) + addBall() + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setAutoStep(false) + layer:scheduleUpdateWithPriorityLua(update, 0) + end + + -- wall + local wall = cc.Node:create() + addPhysicsComponent(wall, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height), + cc.PhysicsMaterial(0.1, 1.0, 0.0))) + wall:setPosition(VisibleRect:center()); + layer:addChild(wall) + + addBall() + layer:runAction(cc.Sequence:create(cc.DelayTime:create(2.0), + cc.CallFunc:create(updateStart))) + + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Fixed Update Test") + Helper.subtitleLabel:setString("The secend ball should not run across the wall") + return layer +end + +local function PhysicsTransformTest() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug() + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0)) + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local _rootLayer = cc.Layer:create() + layer:addChild(_rootLayer) + + local wall = cc.Node:create() + addPhysicsComponent(wall, + cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, + VisibleRect:getVisibleRect().height), + cc.PhysicsMaterial(0.1, 1.0, 0.0))) + wall:setPosition(VisibleRect:center()); + _rootLayer:addChild(wall) + + local _parentSprite = cc.Sprite:create("Images/YellowSquare.png") + _parentSprite:setPosition(cc.p(200,100)) + _parentSprite:setScale(0.25) + addPhysicsComponent(_parentSprite,cc.PhysicsBody:createBox(_parentSprite:getContentSize(),cc.PhysicsMaterial(0.1, 1.0, 0.0))) + _parentSprite:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + _parentSprite:setTag(1) + _rootLayer:addChild(_parentSprite) + + local leftBall = cc.Sprite:create("Images/ball.png") + leftBall:setPosition(cc.p(-30,0)) + leftBall:setScale(2) + addPhysicsComponent(leftBall,cc.PhysicsBody:createCircle(leftBall:getContentSize().width/2,cc.PhysicsMaterial(0.1,1.0,0.0))) + leftBall:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + _parentSprite:addChild(leftBall) + + local scaleTo = cc.ScaleTo:create(2.0,0.5) + local scaleBack = cc.ScaleTo:create(2.0,1.0) + _parentSprite:runAction(cc.RepeatForever:create(cc.Sequence:create(scaleTo,scaleBack))) + + local normal = cc.Sprite:create("Images/YellowSquare.png") + normal:setPosition(cc.p(300,100)) + normal:setScale(0.25,0.5) + addPhysicsComponent(normal,cc.PhysicsBody:createBox(normal:getContentSize()),cc.PhysicsMaterial(0.1,1.0,0.0)) + normal:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setTag(DRAG_BODYS_TAG) + _rootLayer:addChild(normal) + + local bullet = cc.Sprite:create("Images/ball.png") + bullet:setPosition(cc.p(200,200)) + addPhysicsComponent(bullet,cc.PhysicsBody:createCircle(bullet:getContentSize().width/2,cc.PhysicsMaterial(0.1,1.0,0.0))) + bullet:getComponent(PHYSICS_COMPONENT_NAME):getPhysicsBody():setVelocity(cc.p(100,100)) + _rootLayer:addChild(bullet) + + local move = cc.MoveBy:create(2.0,cc.p(100,100)) + local move2 = cc.MoveBy:create(2.0,cc.p(-200,0)) + local move3 = cc.MoveBy:create(2.0,cc.p(100,-100)) + local scale = cc.ScaleTo:create(3.0,0.3) + local scale2 = cc.ScaleTo:create(3.0,1.0) + local rotate = cc.RotateBy:create(6.0,360) + + _rootLayer:runAction(cc.RepeatForever:create(cc.Sequence:create(move,move2,move3))) + _rootLayer:runAction(cc.RepeatForever:create(cc.Sequence:create(scale,scale2))) + _rootLayer:runAction(cc.RepeatForever:create(cc.Sequence:create(rotate))) + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Reorder issue #9959") + Helper.subtitleLabel:setString("Test Scale9Sprite run scale/move/rotation action in physics scene") + return layer +end + +local function PhysicsIssue9959() + local layer = cc.Layer:create() + local function onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local visibleSize = cc.Director:getInstance():getVisibleSize() + + local scale9Sprite1 = ccui.Scale9Sprite:create("Images/ball.png") + scale9Sprite1:setPosition(cc.p(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2)) + layer:addChild(scale9Sprite1) + scale9Sprite1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.MoveBy:create(2.0,cc.p(100.0,0.0)), + cc.MoveBy:create(2.0,cc.p(-100,0.0))))) + + local scale9Sprite2 = ccui.Scale9Sprite:create("Images/ball.png") + scale9Sprite2:setPosition(cc.p(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2+50)) + layer:addChild(scale9Sprite2) + scale9Sprite2:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.ScaleTo:create(2.0,1.5), + cc.ScaleTo:create(2.0,1.0)))) + + local scale9Sprite3 = ccui.Scale9Sprite:create("Images/ball.png") + scale9Sprite3:setPosition(cc.p(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2-50)) + layer:addChild(scale9Sprite3) + scale9Sprite3:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.RotateBy:create(2.0,360)))) + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Physics transform test") + return layer +end + function PhysicsTest() cclog("PhysicsTest") local scene = cc.Scene:createWithPhysics() @@ -1285,6 +1747,11 @@ function PhysicsTest() PhysicsDemoBug3988, PhysicsContactTest, PhysicsPositionRotationTest, + PhysicsSetGravityEnableTest, + PhysicsDemoBug5482, + PhysicsFixedUpdate, + PhysicsTransformTest, + PhysicsIssue9959 } scene:addChild(Helper.createFunctionTable[1]()) diff --git a/tools/tolua/cocos2dx_physics.ini b/tools/tolua/cocos2dx_physics.ini index 6a763b9d9d..ef1bede544 100644 --- a/tools/tolua/cocos2dx_physics.ini +++ b/tools/tolua/cocos2dx_physics.ini @@ -29,7 +29,7 @@ headers = %(cocosdir)s/cocos/cocos2d.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = Event(.*(Physics).*) Physics.* +classes = Event(.*(Physics).*) Physics.* ComponentPhysics2d # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also @@ -46,7 +46,9 @@ skip = PhysicsBody::[getJoints createPolygon createEdgeChain createEdgePolygon], PhysicsShapeEdgePolygon::[create ^getPoints$], PhysicsShapeEdgeChain::[create ^getPoints$], PhysicsWorld::[getScene queryPoint queryRect rayCast], - PhysicsContact::[getData setData] + PhysicsContact::[getData setData], + PhysicsManager::[PhysicsManager update], + ComponentPhysics2d::[beforeSimulation afterSimulation] rename_functions =