You need to wrap the angle correctly, to make the guard turn to the right direction.
In guard.lua in line 45 you calculate the angle difference by
What you need is this instead:
Code: Select all
((self.r-aim+math.pi)%(2*math.pi)-math.pi)
This gives you an angle between -pi and pi. The whole line then looks like this:
Code: Select all
local r = self.r - (((self.r-aim+math.pi)%(2*math.pi)-math.pi) * dt * self.turnSpeed)
By the way,in line 44, why not use this instead (swap target and self and omit the math.pi):
Code: Select all
local aim = math.atan2(target.y - self.y, target.x - self.x)
(This is because the vector pointing from A to B is (B-A), not (A-B). An here you need the vector from the guard to the target, so this is the correct order)
Edit:
And for the other problem, you can use the same formula. In Guard:canSee you have the angle between the guard and (x,y) and you have the guards angle. To calculate the difference and the check, use this:
Code: Select all
local difference = (a-self.r+math.pi)%(2*math.pi)-math.pi
if math.abs(difference) > self.visionWidth/2 then return false else return true end