Moving away from or towards the player

Started by
13 comments, last by SimonH 13 years ago
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.
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 Steering Behaviors and its open source counterpart OpenSteer for such kinds of things.

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 Steering Behaviors and its open source counterpart OpenSteer for such kinds of things.


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.

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.

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 use OpenSteer but just directed him to a place where some research can be done.

[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.

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 use 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
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.
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.
Stickmen Wars 2 is in development.
Meanwhile try Bloodridge
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.

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.

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.
Stickmen Wars 2 is in development.
Meanwhile try Bloodridge

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.

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.

This topic is closed to new replies.

Advertisement