Hello,
I am working on an app that involves objects (enemies) following another object (the player) around a level. I have walls setup around each level also that work off of a collision detection code not using physics.
I have the object's working so they chase the player and so they do not run through the walls. The problem I'm having right now is that once the enemy runs into a wall that the player is on the other side of, it just keeps running into the wall and bouncing off of it instead of finding a way around the wall.
I have tried to code this on my own but have not been able to get it to successfully work so I have decided to once again come to the Corona Community for assistance.
Here is an example of my current code for creating two enemies and having them move and detect collisions with the walls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | local Npc = {} Npc[1] = display.newImageRect( "Sprite.png", 25, 35 ) Npc[1].x = 568 Npc[1].y = -18 Npc[1].dir = 1 localGroup:insert(Npc[1]) Npc[2] = display.newImageRect( "Sprite.png", 25, 35 ) Npc[2].x = 480 Npc[2].y = 219 Npc[2].dir = 1 localGroup:insert(Npc[2]) local function hasCollided(obj1, obj2) if obj1 == nil then return false end if 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 local function NpcMovement ( event ) if (gameState == "Play") then local randomDir = math.random(1,2) for i = 1, #Npc do if ( ( myPlayer.x > Npc[i].x ) and ( myPlayer.y < Npc[i].y ) ) then -- To the right and up if ( randomDir == 1 ) then if ( Npc[i].blocked == 3 ) then Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 else Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 end else if ( Npc[i].blocked == 1 ) then Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 else Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 end end elseif ( ( myPlayer.x < Npc[i].x ) and ( myPlayer.y < Npc[i].y ) ) then -- To the left and up if ( randomDir == 1 ) then if ( Npc[i].blocked == 4 ) then Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 else Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 end else if ( Npc[i].blocked == 1 ) then Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 else Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 end end elseif ( ( myPlayer.x > Npc[i].x ) and ( myPlayer.y > Npc[i].y ) ) then -- To the right and down if ( randomDir == 1 ) then if ( Npc[i].blocked == 3 ) then Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 else Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 end else if ( Npc[i].blocked == 2 ) then Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 else Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 end end elseif ( ( myPlayer.x < Npc[i].x ) and ( myPlayer.y > Npc[i].y ) ) then -- To the left and down if ( randomDir == 1 ) then if ( Npc[i].blocked == 4 ) then Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 else Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 end else if ( Npc[i].blocked == 2 ) then Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 else Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 end end elseif ( ( myPlayer.x == Npc[i].x ) and ( myPlayer.y < Npc[i].y ) ) then -- Same X and up if ( Npc[i].blocked == 2 ) then Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 else Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 end elseif ( ( myPlayer.x == Npc[i].x ) and ( myPlayer.y > Npc[i].y ) ) then -- Same X and down if ( Npc[i].blocked == 1 ) then Npc[i].y = Npc[i].y - 5 Npc[i].dir = 1 else Npc[i].y = Npc[i].y + 5 Npc[i].dir = 2 end elseif ( ( myPlayer.x > Npc[i].x ) and ( myPlayer.y == Npc[i].y ) ) then -- Same Y and right if ( Npc[i].blocked == 4 ) then Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 else Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 end elseif ( ( myPlayer.x < Npc[i].x ) and ( myPlayer.y == Npc[i].y ) ) then -- Same Y and left if ( Npc[i].blocked == 3 ) then Npc[i].x = Npc[i].x + 5 Npc[i].dir = 3 else Npc[i].x = Npc[i].x - 5 Npc[i].dir = 4 end end for n = 1, #wall do if hasCollided(Npc[i], wall[n]) == false then else if ( Npc[i].dir == 1 ) then Npc[i].y = Npc[i].y + 5 Npc[i].blocked = 2 elseif ( Npc[i].dir == 2 ) then Npc[i].y = Npc[i].y - 5 Npc[i].blocked = 1 elseif ( Npc[i].dir == 3 ) then Npc[i].x = Npc[i].x - 5 Npc[i].blocked = 4 elseif ( Npc[i].dir == 4 ) then Npc[i].x = Npc[i].x + 5 Npc[i].blocked = 3 end end end if ( Npc[i].x < 15 ) then Npc[i].x = 15 end if ( Npc[i].x > 780 ) then Npc[i].x = 780 end if ( Npc[i].y < 15 ) then Npc[i].y = 15 end if ( Npc[i].y > 820 ) then Npc[i].y = 820 end end end end for i = 1, #Npc do timer.performWithDelay(600, NpcMovement, 0 ) end |