Created
May 7, 2016 04:26
-
-
Save phpedinei/0f86903711f2472cf0f6875145aedb76 to your computer and use it in GitHub Desktop.
Physics Body Flipper Function | Corona SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local physics = require( "physics" ) ; physics.start() ; physics.setDrawMode( "hybrid" ) | |
| local function flipBody( thisBody ) | |
| local flippedBodyShapes = {} | |
| for i=1,#thisBody do | |
| local thisShape = thisBody[i] | |
| local flippedShape = {} | |
| for k=#thisShape,1,-2 do | |
| local pairY = thisShape[k] | |
| local pairX = thisShape[k-1] | |
| flippedShape[#flippedShape+1] = pairX*-1 | |
| flippedShape[#flippedShape+1] = pairY | |
| end | |
| flippedBodyShapes[#flippedBodyShapes+1] = { shape=flippedShape } | |
| end | |
| return flippedBodyShapes | |
| end | |
| --This is the body shape declaration to flip. Multi-element bodies are allowed. | |
| local normalBody = { | |
| { -26,-82, -26,-52, -36,-62, -36,-92 }, | |
| { 61,-80, 51,15, 43,9, 43,-96, 50,-96 }, | |
| { -38,69, -39,23, -31,27, -15,77 }, | |
| { -52,68, -50,65, 44,97, 47,101, 47,128, -43,128 } | |
| } | |
| --Pass the normal body shape to the function and get the flipped body in return | |
| local flippedBody = flipBody( normalBody ) | |
| --Display test physics bodies on screen... | |
| local normalBodyTable = {} | |
| for i=1,#normalBody do normalBodyTable[#normalBodyTable+1] = { shape=normalBody[i] } end | |
| local normalTest = display.newRect( 0,0,10,10 ) ; normalTest.isVisible = false | |
| physics.addBody( normalTest, "kinematic", unpack(normalBodyTable) ) | |
| normalTest.isSensor = true | |
| normalTest.x = display.contentCenterX | |
| normalTest.y = display.contentCenterY-(display.contentHeight/4) | |
| local flippedTest = display.newRect( 0,0,10,10 ) ; flippedTest.isVisible = false | |
| physics.addBody( flippedTest, "kinematic", unpack(flippedBody) ) | |
| flippedTest.isSensor = true | |
| flippedTest.x = display.contentCenterX | |
| flippedTest.y = display.contentCenterY+(display.contentHeight/4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment