• Advertisement
Sign in to follow this  

Moving away from or towards the player

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'd like to implement some simple AI, which would have the enemies moving away from my player, or towards him if they are a larger enemy.

I was looking for suggestions on how people might go about this, I currently plan on sampling the distance and dot product between the enemy and the player and updating the enemies velocity accordingly, my problem lies in how to adjust the angle - I had planned on reversing the dot product to some extent, entering an angle and the players position which would give me the desired enemy position.

I am just wondering if these would be a half decent way to implement such a thing, or if any one had any better ideas. I should mention that this is in 3D but ignoring the Y axis, so it's really just a 2D test.

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
The dot-product is a tool that uses 2 difference vectors (not position vectors). Which difference vectors do you want to use?

Do you need an explicit angle at all? You can compute the difference vector from the current enemy position to the current (or predicted) player position. With it you have both the total distance and (after normalization) the direction of movement (assuming that pathfinding didn't play a role here).

If not yet known: Usually one is directed to look at [url="http://www.red3d.com/cwr/steer/"]Steering Behaviors[/url] and its open source counterpart [url="http://opensteer.sourceforge.net/"]OpenSteer[/url] for such kinds of things.

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1301990921' post='4794537']
The dot-product is a tool that uses 2 difference vectors (not position vectors). Which difference vectors do you want to use?

Do you need an explicit angle at all? You can compute the difference vector from the current enemy position to the current (or predicted) player position. With it you have both the total distance and (after normalization) the direction of movement (assuming that pathfinding didn't play a role here).

If not yet known: Usually one is directed to look at [url="http://www.red3d.com/cwr/steer/"]Steering Behaviors[/url] and its open source counterpart [url="http://opensteer.sourceforge.net/"]OpenSteer[/url] for such kinds of things.
[/quote]

Why would one go about using OpenSteer? That's just steering one away from actually learning how to do the real work.

Also, that is hard to do....Very hard.

Share this post


Link to post
Share on other sites
[quote name='BB1995' timestamp='1301992056' post='4794547']
Why would one go about using OpenSteer? That's just steering one away from actually learning how to do the real work.

Also, that is hard to do....Very hard.
[/quote]
The OP asked for "on how people might go about this", and OpenSteer demonstrates such a thing. The OP is free to pick the theory only, look at the implementation, find references to other solutions, or else decide to directly use OpenSteer; I haven't made any suggestion to actually [i]use[/i] OpenSteer but just directed him to a place where some research can be done.

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1301997625' post='4794567']
[quote name='BB1995' timestamp='1301992056' post='4794547']
Why would one go about using OpenSteer? That's just steering one away from actually learning how to do the real work.

Also, that is hard to do....Very hard.
[/quote]
The OP asked for "on how people might go about this", and OpenSteer demonstrates such a thing. The OP is free to pick the theory only, look at the implementation, find references to other solutions, or else decide to directly use OpenSteer; I haven't made any suggestion to actually [i]use[/i] OpenSteer but just directed him to a place where some research can be done.
[/quote]

Hey, don't argue with her, she's too hot

Share this post


Link to post
Share on other sites
I am looking more to learn from this so I would prefer to do this myself and understand how it is done.

How would I go about using the two positions and normalization to get the direction? While I understand how to find the distance it is the direction I am having trouble with.

Share this post


Link to post
Share on other sites
Ok, so you have a player at P (px,pz) and an enemy at E (ex,ez). Make a vector V (vx,vz) from the enemy to the player: vx=px-ex, vz=pz-ez.
Normalize V: length=sqrt(vx*vx+vz*vz), vx=vx/length, vz=vz/length. V now has a length of 1 and points from the enemy to the player.
To make the enemy move towards the player, multiply V by the enemy's speed and add V to the enemy position E: ex=ex+speed*vx, ez=ez+speed*vz.
To make the enemy move away from the player, multiply V by the enemy's speed and subtract V from the enemy position E: ex=ex-speed*vx, ez=ez-speed*vz.
You can find the direction from V but you don't really need to.

Share this post


Link to post
Share on other sites
Thank you very much Simon, I can see how that works.

I would still like to understand the process of using normalization, I am not quite sure how it would return the direction because I was under the impressing there were 2 normals, not 1.

Share this post


Link to post
Share on other sites
[quote name='Haegr' timestamp='1302010754' post='4794619']
Thank you very much Simon, I can see how that works.

I would still like to understand the process of using normalization, I am not quite sure how it would return the direction because I was under the impressing there were 2 normals, not 1.
[/quote]
Don't confuse 'normals' (vectors at right angles to things) with 'normalizing' (making a vector have a length of 1).
Vectors implicitly have a direction: the vector (1,0) has a length of 1 and points along the x axis, (1,1) has length sqrt(2) and points 45' up from the x axis, (-1,0) has length 1 and points down the x axis, &c.

Share this post


Link to post
Share on other sites
[quote name='Haegr' timestamp='1302010754' post='4794619']
I would still like to understand the process of using normalization, I am not quite sure how it would return the direction because I was under the impressing there were 2 normals, not 1.
[/quote]
The term "normalization" in the context of vectors means that the vector is divided by its length, yielding in a vector of the same direction but with unit length (i.e. its length is 1). Although any vector (with length > 0) can be used for a specific direction, the unit vector is usually favored. The difference vector between 2 points encodes both the distance of the 2 points and the direction from the one to the other point.

The therm "normal" in the context of vectors means usually that the vector is perpendicular to something, e.g. to another vector or to a surface or whatever.

Share this post


Link to post
Share on other sites
Thank you both very much for explaining that!

So to move away from the player I would calculate the distance between the two, normalize this and then reverse it to give me the opposite direction?

Share this post


Link to post
Share on other sites
[quote name='Haegr' timestamp='1302017239' post='4794659']
Thank you both very much for explaining that!

So to move away from the player I would calculate the distance between the two, normalize this and then reverse it to give me the opposite direction?
[/quote]
Pretty much, but it's 'calculate the [b]vector[/b] between the two, normalize this (using the distance) and then reverse it to get the opposite direction'.

Share this post


Link to post
Share on other sites
Thank you both very much for the help, I'll get this implemented now.

Share this post


Link to post
Share on other sites
Just looking for a bit more added advice here.

I'm adding behavior for the AI to detect if it is too close to another ai, and move accordingly. My problem is that if I happen to get two opposite movement vectors, after I add them the result will be 0. How should one respond in this situation? (for my simulation the ai will simply explode, but I mean this as a broader question, if I were doing simulation for example)

Share this post


Link to post
Share on other sites
No easy answers there! Try looking at the [url="http://www.red3d.com/cwr/steer/"]red3D[/url] steering examples for some ideas.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement