[+code, +gif] Issues with custom collision detection

Hi everyone,
 
I’m trying to make a simple collision detection system to handle my “rpg top-down” game. This applied physics are pretty simple and based on AABB routine.
 
Then the character moves around four ways (up, down, left and right) and if collides with something added on the system, he stop the movement and waits for user’s input.
 
I’m bit close to do what I want to, however some blocks I’ve added just ignore hero’s collision and make it pass through.
 
NOTE: The graphics are pretty simple (rectangles) because this is a separated file just to study this case.
 
collision_Issues.gif
 
Like you see above, purple blocks are ignored after some hits. I’m not sure why this is happening. It supposed to hit and collide with all blocks (green, blue and purple) all the time, but it don’t work as expected.
 
The full code follows below:
 

local isMoving = false local direction = "none" local collision = false local blockIndex = 1 local blocks = {} local runtime = 0 display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local function addBlock(obj, r, g, b) obj:setFillColor( r, g, b ) blocks[blockIndex] = obj blockIndex = blockIndex + 1 end addBlock(display.newRect( 0, 0, 1024, 32 ), 0, 100, 0) addBlock(display.newRect( 0, 288, 1024, 32 ), 0, 100, 0) addBlock(display.newRect( 0, 0, 32, 332 ), 0, 100, 100) addBlock(display.newRect( 492, 0, 32, 330 ), 0, 100, 100) addBlock(display.newRect( 32, 64, 200, 32 ), 30, 0, 100) addBlock(display.newRect( 256, 256, 32, 32 ), 30, 0, 100) local player = display.newRect( 128, 128, 32, 32 ) player:setFillColor( 39, 10, 10 ) local function onUpdate() if isMoving == true then for i=1, #blocks do if hasCollided(blocks[i], player) then isMoving = false collision = direction if direction == "up" then player.y = player.y + math.abs(player.contentBounds.yMin - blocks[i].contentBounds.yMax) elseif direction == "down" then player.y = player.y - math.abs(player.contentBounds.yMax - blocks[i].contentBounds.yMin) elseif direction == "left" then player.x = player.x + math.abs(player.contentBounds.xMin - blocks[i].contentBounds.xMax) elseif direction == "right" then player.x = player.x - math.abs(player.contentBounds.xMax - blocks[i].contentBounds.xMin) end print("COLLIDE: " .. direction) break end end local velocity = 10 if direction == "up" and collision ~= "up" then player.y = player.y - velocity elseif direction == "down" and collision ~= "down" then player.y = player.y + velocity elseif direction == "left" and collision ~= "left" then player.x = player.x - velocity elseif direction == "right" and collision ~= "right" then player.x = player.x + velocity end end end local function onKey(event) if isMoving == false then if event.keyName == "up" and event.phase == "down" then direction = "up" isMoving = true elseif event.keyName == "down" and event.phase == "down" then direction = "down" isMoving = true elseif event.keyName == "right" and event.phase == "down" then direction = "right" isMoving = true elseif event.keyName == "left" and event.phase == "down" then direction = "left" isMoving = true end end end function hasCollided( obj1, obj2 ) if ( obj1 == nil or obj2 == nil ) then return false end 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 Runtime:addEventListener( "enterFrame", onUpdate ) Runtime:addEventListener( "key", onKey )

Sorry about my english, I’m not a native one.