Angles (Visibilty Cones & Turning)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Angles (Visibilty Cones & Turning)

Post by IMP1 »

So I was interested by Jimanzium's idea about Sound Stealth. So I tried to implelemt something similar. Basically, the guards have a hearing range (shown by the blue circle when debugging) and a visibility cone (shown by the yellow cone when debugging). Thay have patrols which they're meant to follow, facing the next destination in their patrol. When they see you (that is, you're in their visibility cone), the level resets.

The Problems
  • The guard turns the wrong way sometimes. At the top right position in his patrol, he turns anti-clockwise (which woud make no sense) whereas all the other times he turns, he turns clockwise.
  • Also, the guards only 'see' you when you are at a certain angle to them. The level only resets if you are both in their vision cone and sufficiently to the right of them.
I'm, like, 90% sure the problem will be in class/guard.lua, specifically in the update (for the turning problem) and canSee (for the vision problem).
I'm just not sure how LÖVE deals with angles.
Attachments
Sneaky Sneaky.love
(4.21 KiB) Downloaded 106 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Angles (Visibilty Cones & Turning)

Post by micha »

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

Code: Select all

(self.r - aim) 
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
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Re: Angles (Visibilty Cones & Turning)

Post by IMP1 »

So angles go from -pi/2 to pi/2, rather than 0 to 2*pi... Okay.

Thanks very much. It all seems to be working now.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Angles (Visibilty Cones & Turning)

Post by micha »

No, angles range can be anything. But: Angles returned by atan2 are always between 0 and 2*pi. And if you want to calculate the difference between the angle 0 and the angle 2*pi-0.000001 then using the plain difference gives you 2*pi-0.000001, even though the actual difference in terms of angles is 0.0000001. You have to take this into account, when you operate with angle-differences. From the guards point of view, an angle larger than pi does not make sense. Something directly behind the guard has an angle of pi, and everything else has to have an angle with a smaller magnitude. Objects on the "left" have a positive angle, objects to the "right" a negative one. In other words: angles can have any value, but if you work with relative angles (or angle differences) you have to wrap them, such that they are between -pi and pi.
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Re: Angles (Visibilty Cones & Turning)

Post by IMP1 »

That makes far too much sense.
Thanks for explaining it to me.
Post Reply

Who is online

Users browsing this forum: No registered users and 217 guests