View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Moving away from or towards the player

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1Haegr  Members

Posted 04 April 2011 - 10:42 PM

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.

### #2haegarr  Members

Posted 05 April 2011 - 02:08 AM

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.

### #3/ BB1995   Members

Posted 05 April 2011 - 02:27 AM

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.

### #4haegarr  Members

Posted 05 April 2011 - 04:00 AM

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.

### #5szecs  Members

Posted 05 April 2011 - 04:14 AM

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.

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

### #6Haegr  Members

Posted 05 April 2011 - 07:12 AM

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.

### #7SimonH  Members

Posted 05 April 2011 - 07:29 AM

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

### #8Haegr  Members

Posted 05 April 2011 - 07:39 AM

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.

### #9SimonH  Members

Posted 05 April 2011 - 08:13 AM

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

### #10haegarr  Members

Posted 05 April 2011 - 08:13 AM

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.

### #11Haegr  Members

Posted 05 April 2011 - 09:27 AM

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?

### #12SimonH  Members

Posted 05 April 2011 - 09:36 AM

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?

Pretty much, but it's 'calculate the vector between the two, normalize this (using the distance) and then reverse it to get the opposite direction'.
Stickmen Wars 2 is in development.
Meanwhile try Bloodridge

### #13Haegr  Members

Posted 05 April 2011 - 09:56 AM

Thank you both very much for the help, I'll get this implemented now.

### #14Haegr  Members

Posted 05 April 2011 - 04:46 PM

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)

### #15SimonH  Members

Posted 05 April 2011 - 05:48 PM

No easy answers there! Try looking at the red3D steering examples for some ideas.
Stickmen Wars 2 is in development.
Meanwhile try Bloodridge

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.