Mike Welsh @mike

Age 38, Male


Penn State

San Diego

Joined on 2/24/00

Exp Points:
5,596 / 5,880
Exp Rank:
Vote Power:
6.47 votes
Police Captain
Global Rank:
B/P Bonus:
9y 6m 26d

Programming Tip: moving an enemy toward the player

Posted by mike - February 10th, 2009

The player is at position (playerX, playerY), and an enemy is at (enemyX, enemyY). You want the enemy to move 5 units directly toward the player. A lot of times, you'll see people do:

dx = player.x - enemy.x;
dy = player.y - enemy.y;
angle = Math.atan2( dy, dx );
speedX = 5 * Math.cos( angle );
speedY = 5 * Math.sin( angle );
enemy.x += speedX;
enemy.y += speedY;

It took us three hefty trigonometry operations: atan2, sin, and cos. First, we're going from the x and y components (dx and dy) to the angle using atan2. Then we're going BACK to the components using sin and cos! Surely we can avoid running in a circle like this! (haha, in a circle, get it?)

It's a little easier if you think with vectors. We already have a vector pointing in the direction we want: the <dx, dy> vector points straight from the enemy to the player! All we have to do is resize it to our desired speed.

dx = player.x - enemy.x;
dy = player.y - enemy.y;
length = Math.sqrt( dx*dx + dy*dy );
dx /= length; dy /= length; // normalize (make it 1 unit length)
dx *= 5; dy *= 5; // scale to our desired speed
enemy.x += dx;
enemy.y += dy;

We normalize the vector by dividing it by its length. Now our vector has a length of one unit, so we can scale it by our desired speed. Instead of three trig functions, we now have just a sqrt!

It's common to rotate your enemy to face the player. In this case, you DO want to use atan2 to get the angle. You can still avoid the sin and cos, though, by doing what we did above.

Just thought I'd share this little tip, hope it's helpful to someone. Here's a bad diagram:

Programming Tip: moving an enemy toward the player



That was easy enough for me to understand, and that means it's awesome. Thanks.

Nice! That'll come in really handy. That's the sort of thing I just never think of.

I really wish my brain could absorb math easily. It hurts when I calculate. D:

nice =) so you figured it all out yourself? should make a tutorial and submit it =) sure there will be a huge flock of people trying their hand and submitting it =) good work

Vector maths is so insanely useful in graphical programming. My own Vector2D class is the most used class in my code library by far, so much so in fact that I've had to employ object pooling to speed things up!

I love the diagram


willy. :3


Oh shit.

Remember when the hockey puck hit you in the nipple at London?
Good times... Good times.....

That's smart, boy do I feel like an idiot for doing it the other way!

Everything mathematical is so much easier with vectors. Seriously. Whenever anyone I know (including myself) has learnt vector maths, they've just gone "holy crap, why the hell did I waste so much time studying regular trigonometry!"

It also helped me understand the theory behind complex numbers. There's a BIG secret about the square root of minus one, and I don't mean non-existence.

I totally got that. Reminded me of my math classes, god i hate analitic geometry so much.

I didnt really get it. My sarcasm sucks D:

Now, why is normalizing called normalizing? Surely they could have thought up a better name for it.. like the one I can't think of right now.

your so cool

or wrap it in a class. Which is so much nicer in c++ when you have operator overloading.

WoW, that was easy to read! Thanks!

you never seace to amaze me mike

I was like wtf until i saw the diagram, that should be in every programming book from now on.

Also, adding sniper rifles saves time with this coding :D


More Results