{"_id":"56570c764d1c720d001f51bb","version":{"_id":"5463e333447e0a100024a08a","project":"5463e333447e0a100024a087","__v":6,"createdAt":"2014-11-12T22:46:11.486Z","releaseDate":"2014-11-12T22:46:11.486Z","categories":["5463e333447e0a100024a08b","54645c365871e90800f50151","547b1c5a4366a708001b3b50","547b1cdf4366a708001b3b5a","54e3bef8464a9c3700f7ca67","54ec651d7ab3172d00c01ab7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":2,"category":{"_id":"5463e333447e0a100024a08b","project":"5463e333447e0a100024a087","pages":["5463e334447e0a100024a08d","54646a285871e90800f50174","54667e7af2b6390800dfd82a","54679c8c98b6840800268982","54679d8498b6840800268987","546a2ed0394c861a00c3a413","546b6a4862515a14007ebbba","547b1c2e7eec0f080099e1dd","55d33d90b9ef4435006dec70","56570c764d1c720d001f51bb"],"version":"5463e333447e0a100024a08a","__v":10,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-12T22:46:11.951Z","from_sync":false,"order":1,"slug":"documentation","title":"Documentation"},"project":"5463e333447e0a100024a087","user":"54639d06447e0a1000249f7f","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-11-26T13:43:18.595Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Detect Collision Between Two Nodes\"\n}\n[/block]\nRect collision is a really easy way of detecting when two objects have overlapped each other.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Rect rect1 = node1->getBoundingBox( );\\nRect rect2 = node2->getBoundingBox( );\\n\\nif ( rect1.intersectsRect( rect2 ) )\\n{\\n    log( \\\"Collided\\\" );\\n}\\nelse\\n{\\n    log( \\\"Not collided\\\" );\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"var rect1 = node1.getBoundingBox( );\\nvar rect2 = node2.getBoundingBox( );\\n\\nif ( cc.rectIntersectsRect( rect1, rect2 ) )\\n{\\n    cc.log( \\\"Collided\\\" );\\n}\\nelse\\n{\\n    cc.log( \\\"Not collided\\\" );\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Detect When A Node Has Been Touched\"\n}\n[/block]\nRects can be used to easily detect when a node has been touched by the user.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)\\n{\\n    Rect rect1 = node->getBoundingBox( );\\n    Point touchPoint = touch->getLocation( );\\n    \\n    if ( rect1.containsPoint( touchPoint ) )\\n    {\\n        log( \\\"Touched\\\" );\\n    }\\n    else\\n    {\\n        log( \\\"Not touched\\\" );\\n    }\\n    \\n    return true; // true if the function wants to swallow the touch\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"onTouchBegan:function( touch, event )\\n{\\n    var rect1 = node1.getBoundingBox( );\\n    var touchPoint = touch.getLocation( );\\n    if ( cc.rectContainsPoint( rect1, touchPoint ) )\\n    {\\n        cc.log( \\\"Touched\\\" );\\n    }\\n    else\\n    {\\n        cc.log( \\\"Not touched\\\" );\\n    }\\n    \\n    return true;\\n},\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: ***This example uses onTouchBegan but any interaction method can be used.***","excerpt":"","slug":"rect-collision","type":"basic","title":"Rect/Bounding Box Collision"}

Rect/Bounding Box Collision


[block:api-header] { "type": "basic", "title": "Detect Collision Between Two Nodes" } [/block] Rect collision is a really easy way of detecting when two objects have overlapped each other. [block:code] { "codes": [ { "code": "Rect rect1 = node1->getBoundingBox( );\nRect rect2 = node2->getBoundingBox( );\n\nif ( rect1.intersectsRect( rect2 ) )\n{\n log( \"Collided\" );\n}\nelse\n{\n log( \"Not collided\" );\n}", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "var rect1 = node1.getBoundingBox( );\nvar rect2 = node2.getBoundingBox( );\n\nif ( cc.rectIntersectsRect( rect1, rect2 ) )\n{\n cc.log( \"Collided\" );\n}\nelse\n{\n cc.log( \"Not collided\" );\n}", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] [block:api-header] { "type": "basic", "title": "Detect When A Node Has Been Touched" } [/block] Rects can be used to easily detect when a node has been touched by the user. [block:code] { "codes": [ { "code": "bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)\n{\n Rect rect1 = node->getBoundingBox( );\n Point touchPoint = touch->getLocation( );\n \n if ( rect1.containsPoint( touchPoint ) )\n {\n log( \"Touched\" );\n }\n else\n {\n log( \"Not touched\" );\n }\n \n return true; // true if the function wants to swallow the touch\n}", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "onTouchBegan:function( touch, event )\n{\n var rect1 = node1.getBoundingBox( );\n var touchPoint = touch.getLocation( );\n if ( cc.rectContainsPoint( rect1, touchPoint ) )\n {\n cc.log( \"Touched\" );\n }\n else\n {\n cc.log( \"Not touched\" );\n }\n \n return true;\n},", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: ***This example uses onTouchBegan but any interaction method can be used.***