Collision without physics

Im having some trouble i cant figure out. Im trying to set up simple collision and it works but if they player continues to click into the collision radius they will be able to clip through. How can i stop this from happening? The code below has been edited so you can test it in simulator. I have already read over Robs post about this.

--Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Constants local centerXcord = display.contentCenterX local centerYcord = display.contentCenterY -----------------------------------------------Starts Map --Displays Map map = display.newRect(0,0,1024,1024 ) map:setFillColor(256,256,256) --Centers Map map.x = centerXcord map.y = centerYcord -- Variable used to move map along y axis motiony = 0; -- Variable used to move map along x axis motionx = 0; -- Walk Speed speed = 5; -- Move map function movemap (event) map.x = map.x + motionx; map.y = map.y + motiony; wall1.y = wall1.y + motiony; wall1.x = wall1.x + motionx; end Runtime:addEventListener("enterFrame", movemap) --Collision detection function hasCollided( animation, wall1 ) local left = animation.contentBounds.xMin \<= wall1.contentBounds.xMin and animation.contentBounds.xMax \>= wall1.contentBounds.xMin local right = animation.contentBounds.xMin \>= wall1.contentBounds.xMin and animation.contentBounds.xMin \<= wall1.contentBounds.xMax local up = animation.contentBounds.yMin \<= wall1.contentBounds.yMin and animation.contentBounds.yMax \>= wall1.contentBounds.yMin local down = animation.contentBounds.yMin \>= wall1.contentBounds.yMin and animation.contentBounds.yMin \<= wall1.contentBounds.yMax return (left or right) and (up or down) end --If collision has been detected then stop character movment function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop ) wall1 = display.newRect(250, 250, 32, 32) --------------------------------------------Start Character animation = display.newRect( 0,0,32,32 ) animation.x = centerXcord animation.y = centerYcord ---------------------------------Start Controls -- Add left button left = display.newRect( 0,0,32,32 ) left.x = 15; left.y = 270; -- Add right button right = display.newRect( 0,0,32,32 ) right.x = 65; right.y = 270; -- Add up button up = display.newRect( 0,0,32,32 ) up.x = 40; up.y = 245; -- Add down button down = display.newRect( 0,0,32,32 ) down.x = 40; down.y = 295; -- When left arrow is touched, move character left function left:touch(event) --Targets the left button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; end return true end left:addEventListener("touch",left) -- When right arrow is touched, move character right function right:touch(event) --Targets the right button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; end return true end right:addEventListener("touch",right) -- When up arrow is touched, move character up function up:touch(event) --Targets the up button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; end return true end up:addEventListener("touch",up) -- When down arrow is touched, move character down function down:touch(event) --Targets the down button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; end return true end down:addEventListener("touch",down)

Still having issues with this. I have a idea of how I can fix this if I use physics but that was the whole point of using this method of collision so I would not have to. Any help would be appreciated. Thanks.

So i decided to take a break from this issue for a few hours and start working on another portion of my code. When i went back to the issue i figured it out no problem. I don’t know if its the best method but it works for me. Sample code below.

MAIN.LUA

--Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Constants centerXcord = display.contentCenterX centerYcord = display.contentCenterY -----------------------------------------------Starts Map --Displays Map map = display.newRect(0,0,1024,1024 ) map:setFillColor(256,256,256) --Centers Map map.x = centerXcord map.y = centerYcord --Display Wall wall1 = display.newRect(250, 250, 32, 32) -- Variable used to move map along y axis motiony = 0; -- Variable used to move map along x axis motionx = 0; -- Walk Speed speed = 5; -- Move map function movemap (event) map.x = map.x + motionx; map.y = map.y + motiony; wall1.y = wall1.y + motiony; wall1.x = wall1.x + motionx; end Runtime:addEventListener("enterFrame", movemap) --Set boundary for character and wall function hasCollided( animation, wall1 ) local left = animation.contentBounds.xMin \<= wall1.contentBounds.xMin and animation.contentBounds.xMax \>= wall1.contentBounds.xMin local right = animation.contentBounds.xMin \>= wall1.contentBounds.xMin and animation.contentBounds.xMin \<= wall1.contentBounds.xMax local up = animation.contentBounds.yMin \<= wall1.contentBounds.yMin and animation.contentBounds.yMax \>= wall1.contentBounds.yMin local down = animation.contentBounds.yMin \>= wall1.contentBounds.yMin and animation.contentBounds.yMin \<= wall1.contentBounds.yMax return (left or right) and (up or down) end --If collision has been detected then stop character movment function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop ) --------------------------------------------Start Character --Display Character animation = display.newRect( 0,0,32,32 ) animation.x = centerXcord animation.y = centerYcord ---------------------------------Start Controls -- Add left button left = display.newRect( 0,0,32,32 ) left.x = 15; left.y = 270; -- Add right button right = display.newRect( 0,0,32,32 ) right.x = 65; right.y = 270; -- Add up button up = display.newRect( 0,0,32,32 ) up.x = 40; up.y = 245; -- Add down button down = display.newRect( 0,0,32,32 ) down.x = 40; down.y = 295; -- When left arrow is touched, move character left function left:touch(event) --Targets the left button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x + 5; wall1.x = wall1.x + 5; end end return true end left:addEventListener("touch",left) -- When right arrow is touched, move character right function right:touch(event) --Targets the right button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x - 5; wall1.x = wall1.x - 5; end end return true end right:addEventListener("touch",right) -- When up arrow is touched, move character up function up:touch(event) --Targets the up button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.y = map.y + 5; wall1.y = wall1.y + 5; end end return true end up:addEventListener("touch",up) -- When down arrow is touched, move character down function down:touch(event) --Targets the down button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.y = map.y - 5; wall1.y = wall1.y - 5; end end return true end down:addEventListener("touch",down)

Basically i use the Runtime event to keep checking for collision and when collision exist i stop motion

--If collision has been detected then stop character movement function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop )

Then i use this if statement to move the map and wall away from the player when they press a control button in order to keep them out of each others bounds.

--Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x + 5; wall1.x = wall1.x + 5; end

Still having issues with this. I have a idea of how I can fix this if I use physics but that was the whole point of using this method of collision so I would not have to. Any help would be appreciated. Thanks.

So i decided to take a break from this issue for a few hours and start working on another portion of my code. When i went back to the issue i figured it out no problem. I don’t know if its the best method but it works for me. Sample code below.

MAIN.LUA

--Hide status bar from the beginning display.setStatusBar( display.HiddenStatusBar ) --Constants centerXcord = display.contentCenterX centerYcord = display.contentCenterY -----------------------------------------------Starts Map --Displays Map map = display.newRect(0,0,1024,1024 ) map:setFillColor(256,256,256) --Centers Map map.x = centerXcord map.y = centerYcord --Display Wall wall1 = display.newRect(250, 250, 32, 32) -- Variable used to move map along y axis motiony = 0; -- Variable used to move map along x axis motionx = 0; -- Walk Speed speed = 5; -- Move map function movemap (event) map.x = map.x + motionx; map.y = map.y + motiony; wall1.y = wall1.y + motiony; wall1.x = wall1.x + motionx; end Runtime:addEventListener("enterFrame", movemap) --Set boundary for character and wall function hasCollided( animation, wall1 ) local left = animation.contentBounds.xMin \<= wall1.contentBounds.xMin and animation.contentBounds.xMax \>= wall1.contentBounds.xMin local right = animation.contentBounds.xMin \>= wall1.contentBounds.xMin and animation.contentBounds.xMin \<= wall1.contentBounds.xMax local up = animation.contentBounds.yMin \<= wall1.contentBounds.yMin and animation.contentBounds.yMax \>= wall1.contentBounds.yMin local down = animation.contentBounds.yMin \>= wall1.contentBounds.yMin and animation.contentBounds.yMin \<= wall1.contentBounds.yMax return (left or right) and (up or down) end --If collision has been detected then stop character movment function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop ) --------------------------------------------Start Character --Display Character animation = display.newRect( 0,0,32,32 ) animation.x = centerXcord animation.y = centerYcord ---------------------------------Start Controls -- Add left button left = display.newRect( 0,0,32,32 ) left.x = 15; left.y = 270; -- Add right button right = display.newRect( 0,0,32,32 ) right.x = 65; right.y = 270; -- Add up button up = display.newRect( 0,0,32,32 ) up.x = 40; up.y = 245; -- Add down button down = display.newRect( 0,0,32,32 ) down.x = 40; down.y = 295; -- When left arrow is touched, move character left function left:touch(event) --Targets the left button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x + 5; wall1.x = wall1.x + 5; end end return true end left:addEventListener("touch",left) -- When right arrow is touched, move character right function right:touch(event) --Targets the right button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motionx = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on X axis to 0 motionx = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x - 5; wall1.x = wall1.x - 5; end end return true end right:addEventListener("touch",right) -- When up arrow is touched, move character up function up:touch(event) --Targets the up button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = -speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.y = map.y + 5; wall1.y = wall1.y + 5; end end return true end up:addEventListener("touch",up) -- When down arrow is touched, move character down function down:touch(event) --Targets the down button if touched if ( event.phase == "began" ) then display.getCurrentStage():setFocus( event.target ) --Starts motion and animation motiony = speed; --Removes the target if touch has ended or finger has moved off the button elseif ( event.phase == "ended" or event.phase == "moved" ) then display.getCurrentStage():setFocus( nil ) --If button is released sets motion on Y axis to 0 motiony = 0; --Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.y = map.y - 5; wall1.y = wall1.y - 5; end end return true end down:addEventListener("touch",down)

Basically i use the Runtime event to keep checking for collision and when collision exist i stop motion

--If collision has been detected then stop character movement function gameLoop( event ) if ( hasCollided( animation, wall1) ) then motionx = 0; motiony = 0; end return true end Runtime:addEventListener( "enterFrame", gameLoop )

Then i use this if statement to move the map and wall away from the player when they press a control button in order to keep them out of each others bounds.

--Move the map and wall 5 to push the character out of collision bounds if ( hasCollided( animation, wall1) ) then map.x = map.x + 5; wall1.x = wall1.x + 5; end