I am trying to work on something that involves using collision detections but is not using Physics. Physics will not work for what I need and I don’t want the answer I’m told just to be “use physics only for the detection part then”
I am using Peach’s tutorial from awhile ago for Arrow Movement and if I try to use physics for the detections it will not detect the collision if you hold down the arrow and go through the object.
I have all of my walls (the things I’m trying to detect collisions with) in an array and need to loop through them in my warp function to make sure the player is not moving through walls.
Right now my warp function looks like this
local function warp ( event )
if (gameState == "Play") then
if ( myPlayer.x \< 15 ) then
myPlayer.x = 15
end
if ( myPlayer.x \> 780 ) then
myPlayer.x = 780
end
if ( myPlayer.y \< 15 ) then
myPlayer.y = 15
end
if ( myPlayer.y \> 820 ) then
myPlayer.y = 820
end
for i = 1, #wall do
if ( myPlayer.x \> ( wall[i].x + wall[i].width ) ) then return false
elseif ( ( myPlayer.x + myPlayer.width) \< wall[i].x) then return false
elseif ( myPlayer.y \> ( wall[i].y + ( wall[i].height/2 ) ) ) then return false
elseif ( ( myPlayer.y + myPlayer.height ) \< wall[i].y ) then return false
else
if ( playerDir == 1 ) then
myPlayer.y = myPlayer.y +3
elseif ( playerDir == 2 ) then
myPlayer.y = myPlayer.y -3
elseif ( playerDir == 3 ) then
myPlayer.x = myPlayer.x -3
elseif ( playerDir == 4 ) then
myPlayer.x = myPlayer.x +3
end
end
end
end
end
Runtime:addEventListener( "enterFrame", warp )
The loop with the walls was taken from here and then edited a little - http://developer.anscamobile.com/forum/2011/09/05/best-method-collision-detection#comment-54441
This section of the code is where I’m having problems
for i = 1, #wall do
if ( myPlayer.x \> ( wall[i].x + wall[i].width ) ) then return false
elseif ( ( myPlayer.x + myPlayer.width) \< wall[i].x) then return false
elseif ( myPlayer.y \> ( wall[i].y + ( wall[i].height/2 ) ) ) then return false
elseif ( ( myPlayer.y + myPlayer.height ) \< wall[i].y ) then return false
else
I have two main problems right now.
My first problem is that this only loops though wall number one a bunch of times and the collision detection works for that wall but since it is not looping through any of the other walls their have no detection.
My second problem is that the walls Y is half way down the image when it is vertical so the detection only works with the bottom half of the wall and not the top. I then have the same problem with horizontal lines only blocking the right half and not the left.
I was wondering if anyone can help me with either (or both) of these problems. I have been trying different ways of this over the last few hours but haven’t been able to figure it out so I came to the forums now for some help.
Thanks,
- Ertzel [import]uid: 69700 topic_id: 15809 reply_id: 315809[/import]