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,047
Vote Power:
6.47 votes
Rank:
Police Captain
Global Rank:
3,931
Blams:
1,268
Saves:
1,003
B/P Bonus:
16%
Whistle:
Normal
Trophies:
10
Medals:
916
Supporter:
9y 10m 19d
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

Man
That makes sence.
I've been thinking about how you do that and you just answered my question.
Thanks
-DragProductions

no offense, this is nothing more than an opinion, but why not just use a motion tween? unless of course you want less frames which doesn't always bother people..

Why even bother with a tween? Why not FBF that bad boy over to your hero character?

Why even bother with FBF when you can just webcam yourself holding puppets?

Puppets? What are you, queer? Just yell at the screen until the enemy characters move towards your player. Then instruct the players to do the same.

No cause you have to type it in. Try this:

setMovietitle (Tanks on Rampage);
TotalFrames (540);

setscript "Green Tank rolling over a school bus, a bus driver named Otto jumps out of it and begins climbing the tank, he jumps inside and knocks the guy out and begins driving the tank into the sunset";

startAnimation;

gotoAndPlay(1);

I just took a hammer and cracked open the screen so I could reach in and move the players around. You crazy earth creatures and your maths.

mike u got the best games and relly doom u dont even have to download the shareware awsome! ill be checking up on new games :)

wtf is all this for?

BORING

*SNORREEEEEE*

This helped me a lot
Thanks!

Good ol' Pythagoras. He makes a tasty theorem.

Great tutorial, thanks.

sweet! this is a lot of help for in my game! thanks!

Wow, thanks!

This will really help with my new game.

Nice tip. NEver really thought about using vectors to figure this out.

that just caused my brain to hemoredge

How do I get Artifact 19? (PM me, I probably won't check this later)

Dude, your so fuckin smart, MY brain explodes at this stuff! While you can do this no sweat!

Judging on how many medals you have, you'll probably want to know that Fear Unlimited Issue 2 has medals.

More Results