I’m having a major problem with non-physics collision detection and really hope someone can help. I’m going crazy here.
I have a scene in a game that mimics an unblock-style puzzle. Slide objects around to make a path, that sort of thing. I’m using code from the tutorials, and collision detection works - up to a point. If the object you’re sliding gives another object a tiny “love tap” then everything is good. But if you put any oomph behind it at all, basically flick the object, it either gets stuck on the second object or nearly sails over it. It looks horrible.
It seems like a simple solution would be to eliminate the ability for a player to flick. I have no idea how to do this, or even if it’s possible. Another thought was to greatly slow down the speed of the objects being slid around, but, again, I have no clue how. I also thought I could implement a more aggresive detector on movement, something that detects the position of the object being moved and holds it back. But, once more, I don’t know how to do this, despite hours of trying.
The code, as you can see, is very basic, straight from the tutorial on non-physics collisions.
local function metObstacle( obj1, obj2 ) local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \< obj2.contentBounds.yMin and obj1.contentBounds.yMax \> obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \> obj2.contentBounds.yMin and obj1.contentBounds.yMin \< obj2.contentBounds.yMax return (left or right) and (up or down) end local function pinSlide( item ) local target = item.target local phase = item.phase if item.phase == "began" then local parent = target.parent display.getCurrentStage():setFocus( target ) target.isFocus = true target.y0 = item.y - target.y target.yStart = target.y target:toFront() elseif ( target.isFocus ) then if ( phase == "moved" ) then if metObstacle( target, obstacle1 ) then target.y = target.y + 25 display.getCurrentStage():setFocus( nil ) target.isFocus = false else target.y = item.y - target.y0 end elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) target.isFocus = false end end return true end pin1:addEventListener( "touch", pinSlide )
For simplicity’s sake, this code just represents one object being dragged - slid along the y - and one obstacle. Also, for what it’s worth, I tried physics collisions, and the player can still fling an object over an obstacle.
I’m sure there’s something really simple I’m missing here.