Donut Shaped Patrol Destination Area

Get help using Construct 2

Post » Sun Sep 01, 2013 8:43 pm

Hi guys. I'm trying to make a natural looking patrol point for my AI but I really don't know the math I should be using. Here's what the area will look like.



I know it will involve a maximum radius and a minimum radius but I really don't know how I could designate a point within that.
B
10
S
3
G
3
Posts: 234
Reputation: 3,467

Post » Sun Sep 01, 2013 11:17 pm

Just use a circular sprite object?
B
47
S
18
G
84
Posts: 2,266
Reputation: 49,786

Post » Mon Sep 02, 2013 12:12 am

...No. It wouldn't work that way. Anyways, I found the math. Here is the solution for anyone with a similar problem.



If you set "Patrol Area" to a random positive number, it will go to a random circular point that as far away from the object as that random number is.Sushin2013-09-02 00:13:04
B
10
S
3
G
3
Posts: 234
Reputation: 3,467

Post » Mon Sep 02, 2013 1:46 am

Good to see you're solved your problem by yourself. But, as I made such an effort to draw such a nice graphic, I will post a more general info that also takes into account the diameter of the ring.



c = center
a = angle
r = radius to outer boundary
r' = radius to inner boundary
p = new point within destination zone
d = distance from r' to p

a = random(360)
d = random(r - r')
p.x = c.x + cos(a) * (r' + d)
p.y = c.y + sin(a) * (r' + d)
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Mon Sep 02, 2013 2:16 am

[QUOTE=tulamide] Good to see you're solved your problem by yourself. But, as I made such an effort to draw such a nice graphic, I will post a more general info that also takes into account the diameter of the ring.



c = center
a = angle
r = radius to outer boundary
r' = radius to inner boundary
p = new point within destination zone
d = distance from r' to p

a = random(360)
d = random(r - r')
p.x = c.x + cos(a) * (r' + d)
p.y = c.y + sin(a) * (r' + d)[/QUOTE]

That's a nice graph. It helps me understand what's going on a little more, but I'm actually having a problem with something else now.

I'm trying to make the enemy go to an area that is around the player based on their current position, so if the enemy is too close to the player, he will back off. If he's too far, he gets closer. Basically, I want the enemy to move into the area around the circle by moving to and from the angle direction between the player and the enemy.

This is what I have but it's not quite working. The angle is off-set at a weird spot. I'd appreciate your help a second time.Sushin2013-09-02 02:16:46
B
10
S
3
G
3
Posts: 234
Reputation: 3,467

Post » Mon Sep 02, 2013 7:27 am

I'm not sure if I understand you right. But if so, then you have to calculate the angle FROM the enemy TO the player, not the other way round.

Scenario:
Player is at (100, 100) with a zone of radius 50. Enemy is at (250, 50)
Now the angle from the player's perspective to the enemy would be ~341.57. But the angle you need is the one from the enemy's perspective to the player, which is ~161.57
The rule is that the first pair of coordinates in the angle expression has to be the one of whose perspective you're trying to get the angle.

angle(playerXY, enemyXY) => the angle that points towards the enemy
angle(enemyXY, playerXY) => the angle that points towards the player

But that's not all in this case. You also want to move the enemy from its current position to the proximity zone of the player, which in this case is a 50 pixel radius around the player. So you have to SUBTRACT that radius from the total distance between enemy and player.

The enemy calculation would then look something like that:
self.x + cos(angle(self.x, self.y, player.X, player.y)) * (distance(self.x, self,y, player.x, player.y) - 50)
self.y + sin(angle(self.x, self.y, player.X, player.y)) * (distance(self.x, self,y, player.x, player.y) - 50)

You would need to replace 50 by the correct radius.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Mon Sep 02, 2013 5:38 pm

[QUOTE=tulamide] I'm not sure if I understand you right. But if so, then you have to calculate the angle FROM the enemy TO the player, not the other way round.

Scenario:
Player is at (100, 100) with a zone of radius 50. Enemy is at (250, 50)
Now the angle from the player's perspective to the enemy would be ~341.57. But the angle you need is the one from the enemy's perspective to the player, which is ~161.57
The rule is that the first pair of coordinates in the angle expression has to be the one of whose perspective you're trying to get the angle.

angle(playerXY, enemyXY) => the angle that points towards the enemy
angle(enemyXY, playerXY) => the angle that points towards the player

But that's not all in this case. You also want to move the enemy from its current position to the proximity zone of the player, which in this case is a 50 pixel radius around the player. So you have to SUBTRACT that radius from the total distance between enemy and player.

The enemy calculation would then look something like that:
self.x + cos(angle(self.x, self.y, player.X, player.y)) * (distance(self.x, self,y, player.x, player.y) - 50)
self.y + sin(angle(self.x, self.y, player.X, player.y)) * (distance(self.x, self,y, player.x, player.y) - 50)

You would need to replace 50 by the correct radius.[/QUOTE]

Works like a charm! Thanks a ton.
B
10
S
3
G
3
Posts: 234
Reputation: 3,467


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests