# How do I Pathfind away from the player?

Get help using Construct 2

### » Thu Oct 01, 2015 1:13 am

I'm currently in the works of a dream game of mine, but the AI will be the hardest aspect and I need some of your help for it.

Q: Is there a way to make a certain object use the pathfinding behavior to create a path AWAY from said specified item? For example: A deer running away from a wolf, avoiding solid trees.

I really need this, and if it isn't possible to do so with pathfinding, is there any other method? I'll take any other method I can get considering this outcome.

A Contruct 2 cap.x would be very useful, but text is fine too!

B
29
S
12
G
4
Posts: 77
Reputation: 4,446

### » Thu Oct 01, 2015 4:15 am

Pathfinding only needs a start location and a destination to work... so yes this should be possible.

What you need to do is pick a destination which is directly away from the danger and a distance proportional to how much danger the deer is feeling.

So if your deer is at dx,dy and your wolf is at wx,wy then you want to pick a destination zx,zy as:

closeness = distance(dx, dy, wx, wy)
if closeness <= maximumVisionOfDeer
howFarToRun = maximumFarToRun / closeness
dangerAngle = angle(dx, dy, wx, wy)
safeAngle = dangerAngle + 180
zx = dx + cos(safeAngle) * howFarToRun
zy = dy + sin(safeAngle) * howFarToRun
endif

Then you can pathfind from dx,dy to zx,zy.
Note: maximumFarToRun is the maximum distance the deer will run if the wolf is right next to it (closeness = 1), if your wolf can get even closer than 1 (e.g. 0.1) then this won't work the same and you might want to limit the values into the equation.
Note: if zx,zy is inside a tree, you either need a pathfinding algorithm that will go as close as it can (and not just fail), or you should jiggle the destination a bit with a random offset until it is not in a tree.

There are tons of other things you could add, but this will work to get you started I think.
B
5
Posts: 17
Reputation: 259

### » Fri Oct 02, 2015 1:24 am

InsaneHero wrote:Pathfinding only needs a start location and a destination to work... so yes this should be possible.

What you need to do is pick a destination which is directly away from the danger and a distance proportional to how much danger the deer is feeling.

So if your deer is at dx,dy and your wolf is at wx,wy then you want to pick a destination zx,zy as:

closeness = distance(dx, dy, wx, wy)
if closeness <= maximumVisionOfDeer
howFarToRun = maximumFarToRun / closeness
dangerAngle = angle(dx, dy, wx, wy)
safeAngle = dangerAngle + 180
zx = dx + cos(safeAngle) * howFarToRun
zy = dy + sin(safeAngle) * howFarToRun
endif

Then you can pathfind from dx,dy to zx,zy.
Note: maximumFarToRun is the maximum distance the deer will run if the wolf is right next to it (closeness = 1), if your wolf can get even closer than 1 (e.g. 0.1) then this won't work the same and you might want to limit the values into the equation.
Note: if zx,zy is inside a tree, you either need a pathfinding algorithm that will go as close as it can (and not just fail), or you should jiggle the destination a bit with a random offset until it is not in a tree.

There are tons of other things you could add, but this will work to get you started I think.

This is very hard for me to understand, do you think a cap.x would be possible?
B
29
S
12
G
4
Posts: 77
Reputation: 4,446

### » Sun Oct 04, 2015 1:01 am

B
29
S
12
G
4
Posts: 77
Reputation: 4,446

### » Sun Oct 04, 2015 2:25 am

Give the deer the bullet behavior and a text instance variable that we'll call brain. Then do events like this. 64 is the look ahead.

Every tick
--- deer: set angle to (player.x, player.y)
--- deer: rotate clockwise 180 degrees
--- deer: set brain to "run!"
--- deer: move forward 64 pixels

Deer is overlapping tree
--- deer: set brain to "tree!"

Every tick
--- deer: move forward -64 pixels

Deer: brain = "tree!"
Deer: is clockwise of angle(deer.x,deer.y,tree.x,tree.y)
--- deer: rotate clockwise 45 degrees
--- deer: set brain to "run!"

Deer: brain = "tree!"
Deer: is counter-clockwise of angle(deer.x,deer.y,tree.x,tree.y)
--- deer: rotate counter-clockwise 45 degrees
--- deer: set brain to "run!"

EDIT:
Just realized this will only work with one deer and one tree as is.
To make it work with more than one deer put everything as subevents of a "for each deer" event.
More importantly to make it work with more than one tree we need to pick the same tree the deer overlapped in the last two events. You can do this by setting a global variable to the tree's uid in the overlap condition. Then use pick by uid in the last two events to pick it again.
B
97
S
36
G
131
Posts: 5,519
Reputation: 83,476

### » Sun Oct 04, 2015 12:31 pm

i was trying this lots last night.

simply, giving the deer bullet and rebound behavoir will automaticly bump off trees.
then give everything physics, and when distance(deer.x,deer.y,player.x,player.y) <200 apply a force at the deer from player angle -180
(i would have posted that yesterday, but if u have walls, it can get trapped quite easily and predictably)

remember world gravity must be made 0.

think there is a sin calculation to make the angle the force is applied at more realistic.

just have bullet is overlapping flowers, play deer eating animation, set speed to 0 if you want it to chill occasionally. wait x seconds pick random angle, set speed 100
B
9
S
3
Posts: 113
Reputation: 1,009

### » Sun Oct 04, 2015 2:09 pm

Pathfind is not a good way to do something such as dynamic movement that needs to be constantly updated. The reason is each time you set a new destination, there's a delay, and it also is very costly to constantly update pathfind, especially if you have lots of deers.

So the bullet suggestions from the above or other ways is better.
B
71
S
25
G
20
Posts: 1,761
Reputation: 18,270

### » Sun Oct 04, 2015 2:21 pm

A simpler way would be to have different sprites that the deer could run to if it is lower , higher or further along from a certain position. This way it could dodge trees rather than bouncing off them.
keep updated on my new game: Still Human
B
13
S
5
G
1
Posts: 158
Reputation: 1,614

### » Sun Oct 04, 2015 10:46 pm

Thanks to everyone I've got it working! But I have one problem. When a wall is in the way (For example: A mountain or border) It turns and runs along side the wall, and because it is running beside the wall it is constantly turning, and whenever it is next to a wall it spazzes out while running away because it is changing it's angle constantly to face away from the player, but turn from the wall. I've tried multiple solutions and It's come down to making another instance variable. Every tick I have an invisible line spawn infront of the animals that detect you (through line of sight, a timer, and another instance variable). If it collides with a solid (I use invisible collision boxes overlapping objects in the background to simulate this, it's top down) it turns 45 degrees clockwise (still have to add randomization to clockwise or counterclockwise). Then I have another collision box that is spawned ontop of the running away animal that is a box 3x it's size. When this box comes in contact with a solid It turns off the instance variable that allows it to turn away from the player. The big problem I'm having is that collision has no invert or way to attach else onto so when it sets the instance variable to 0 to stop the turning away from the player to stop the spazzing, it stays at 0 and I have no way to turn it back to 1 again when it is away from a solid object. I've tried making it wait a second or less until activating it again, but it just keeps spazzing. I've also tried making it spawn another colision box ontop of the running away animal that has it's collisions rap around the other and having it so if this second one comes in contact with a solid it turns the instance variable to 1, but if it is close to the wall then the both are colliding and it defeats the whole purpose.

Sorry for the long paragraph, it's hard to explain. Can someone please help me with the spazzing?

Also, I used R0J0hound's idea of turning away, as it was easiest to understand and perfom. The problem with that is that occassionally you will see the animal turn back around at the player for less then a second due to it turning towards and away from the player at the same time. Any fixes for this too?

Thank you so much!
B
29
S
12
G
4
Posts: 77
Reputation: 4,446

### » Mon Oct 05, 2015 9:16 pm

Bump!
B
29
S
12
G
4
Posts: 77
Reputation: 4,446

Next