00:00
00:00
mike
hello!

Mike Welsh @mike

Age 38, Male

stupids

Penn State

San Diego

Joined on 2/24/00

Level:
23
Exp Points:
5,596 / 5,880
Exp Rank:
8,102
Vote Power:
6.47 votes
Rank:
Police Captain
Global Rank:
3,932
Blams:
1,268
Saves:
1,003
B/P Bonus:
16%
Whistle:
Normal
Trophies:
10
Medals:
916
Supporter:
9y 11m 11d
Gear:
1

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


1

Comments

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.

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

Tom-

willy. :3

Ummm...K.

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

cool

It's cool to see how you did something that so many people think it's difficult but in a simple way. I had to remember some things I learnt in my English class years ago to fully understand it, and it's good.

Thanks, I don't think I'll use it but someone probably will.

More Results