# bot chasing the player problem

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My map is an indoor map. It's an office building so it's hallways and rooms. I have my path nodes at the intersections of the hallway as well as the doorways leading into the rooms. As the player moves around the map, the player class records the closest path node and the bot uses that to find the player. The problem happens when the bot reaches the node the player is assumed to be at. Because of the distance between nodes, the bot doesn't know where to go because the player is close enough to the recorded node but too far from the bot for it to attack. So I need to figure out a way for the code to determine if the player position is in-between the current node and the next node or the current node and the previous node. That way the bot will move to the node in the players direction. any thoughts?

##### Share on other sites
Firstly, make sure that all possible points in your world are within attack range of a node. Once you've done that, your problem goes away. That is, if the bot is moving A to B and the player is between A and B, but closer to B, then the bot should enter an attack mode once the player is in range before reaching node B. If the player is between B and C, then the change to attack state won't trigger until after the bot has reached B.

Skizz

##### Share on other sites
I agree with your suggestion but I was hoping for a code solution instead of adding a ton more path nodes. There may be situations in which more path nodes cause different problems. If I can tell if a posistion is in-between two nodes, that would work.

##### Share on other sites
Create a line AB. Create a perpendicular line that passes through B, call it ABperp. Determine which side of this line the target is on.

Skizz

##### Share on other sites
thanks! I'll try that.

##### Share on other sites
I was thinking about this on my bicycle on the way into work this morning (it's where I have all my best ideas!). It turns out that to determine which side of the perpendicular line the player is on, do:

AB dot BP

where AB is the vector from node A to node B and BP is the vector from node B to the player. If the result of that dot product is < 0 then the player is between A and B, if it's > 0 the player is on the other side of B.

Skizz

##### Share on other sites
I'll give that a try. I couldn't seem to get my head around the first suggestion but I understand this one.

##### Share on other sites
It works! You da man! Thank you so much! :)

##### Share on other sites
The solution that keeps on giving.

Getting the dot product between two vectors solved yet another problem I had.

The problem was the player could have run past the bot in the opposite direction the bot was traveling. As the bot travels from node a to node b, it encounter the player. If the player runs around the bot towards a, the nodes need to be switch because the bot needs to turnaround and move in the other direction.

cool. Weeeeee.

##### Share on other sites

If your bots have the freedom to point in many directions, you can use the following to determine which way the bot should turn:

Br dot BP

where Br is the bot's right vector (perpendicular to look at vector) and BP is the vector from the bot to the player. Negative means turn left, positive to turn right.

Skizz

##### Share on other sites
It might be helpful to treat th player as a node, so that when the player is visible, the monster doesn't use the node system at all. This solves the problem you had with the player running past the monster, as well as a bunch of other things.

##### Share on other sites
A simple way to do this is with forces.

Basically, as your pathfinding, you have a strong force that is pulling the bot towards the next node. As you get close enough, that force dies down and is replaced with the force for the next node, ect.

This can give you a bit more in terms of freedom. For example, you can have a "scared" variable in your bot that determines the strength of a force pulling your bot towards the nearest cover point. If your bot is scared (ie. the player outmatches the bot significantly), it hugs the corners as its running away from the player.

The running towards/away from the player can be made up of two forces. One pulling the bot towards the player (to attack), the second (simply represented as having a negitive magnitude), is pulling the bot away from the player (because of its fear vs. its "anger").

This should solve your problems, and make your bots quite a bit more humanlike. (you can run your fsm on top of your "emotions" system, by using a set of weights per state, and then picking the "best" state as your current one. an example:

Melee attack -> Angry * 0.1 , Scared * -5, -0.5 * playerdistance
Ranged attack -> angry * 0.1, Scared * -5, 1.5 * playerdistance
Flee -> angry * -1, Scared * 5, 7 * playerdistance

etc.

:-)

##### Share on other sites

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628711
• Total Posts
2984342

• 23
• 11
• 10
• 13
• 14