I think the likeliest problem is that there is a non-constant amount of time between updates -- when collision detection catches up with real time, your rectangles have already collided and are overlapping. It's not enough just to stop movement, you need to set the position of one of the two rectangles (probably the 'player') so that the edges are touching but not overlapping.
Also, I don't think the order of the objects passed to the collider collision callback is deterministic; I'm not sure that your player will always be objA, sometimes it might be objB. I know this caused me some grief when I was using that module.
Also, if you press up or down in addition to a direction (or alternate pressing keys) you can move all the way through the blocking object. This happens because your code only allows the player to be blocked in one direction at a time. You might want to change this so that the player can be blocked in more than one direction. To do this, you could change your code in player.lua to look like this (though I'm not sure this is the best option, it's relatively simple):
Code: Select all
if(this.xSpeed ~= 0) then
if(this.xSpeed > 0 and not this.blockedRight) then
this.x = this.x + this.xSpeed * dt
end
if(this.xSpeed < 0 and not this.blockedLeft) then
this.x = this.x + this.xSpeed * dt
end
end
if(this.ySpeed ~= 0 and not this.blockedUp) then
if(this.ySpeed < 0) then
this.y = this.y + this.ySpeed * dt
end
if(this.ySpeed > 0 and not this.blockedDown) then
this.y = this.y + this.ySpeed * dt
end
end
and in level.lua to look like this:
Code: Select all
if(objA == player.colRect and objB == gmap.allRect)
or (objB == gmap.allRect and objA == player.colRect) then
if(player.xSpeed > 0) then
player.blockedRight = true
end
if(player.xSpeed < 0) then
player.blockedLeft = true
end
if(player.ySpeed > 0) then
player.blockedDown = true
end
if(player.ySpeed < 0) then
player.blockedUp = true
end
end
The problem is now the player can get 'wedged' into the object which is bad. I didn't spend enough time inspecting your code to figure out how to reset the player's position to outside the object -- you should probably do that in the gmap.colTrue function. You should be able to use the objects to determine the exact positions of the fixed objects to determine where to move the player.
Hope this helps.