Game Twister
Would you like to react to this message? Create an account in a few clicks or log in to continue.


These forums are closed for viewing purposes only. To join the Game Twister community, please register at the new forums here.
 
HomeHome  Latest imagesLatest images  SearchSearch  Log inLog in  RegisterRegister  

 

 Programming Tip: moving an enemy toward the player

Go down 
3 posters
AuthorMessage
SC
Admin
Admin
SC


Number of posts : 317
Age : 34
Location : n/a
Registration date : 2009-01-07

Programming Tip: moving an enemy toward the player Empty
PostSubject: Programming Tip: moving an enemy toward the player   Programming Tip: moving an enemy toward the player EmptyTue Feb 10, 2009 11:26 pm

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 114837_diagram
Back to top Go down
http://gametwister.net
Thrilla
Level 20
Level 20
Thrilla


Number of posts : 365
Location : <_<
Registration date : 2009-01-09

Programming Tip: moving an enemy toward the player Empty
PostSubject: Re: Programming Tip: moving an enemy toward the player   Programming Tip: moving an enemy toward the player EmptyWed Feb 11, 2009 1:34 am

what a crappy diagram
Back to top Go down
Xarnor
Level 11
Level 11
Xarnor


Number of posts : 67
Age : 29
Location : Right here.
Registration date : 2009-01-25

Programming Tip: moving an enemy toward the player Empty
PostSubject: Re: Programming Tip: moving an enemy toward the player   Programming Tip: moving an enemy toward the player EmptySat Mar 07, 2009 8:33 am

Thrilla wrote:
what a crappy diagram

What a crappy post.

That's a cool trick SC, I might try it sometime. That's actionscript right?
Back to top Go down
SC
Admin
Admin
SC


Number of posts : 317
Age : 34
Location : n/a
Registration date : 2009-01-07

Programming Tip: moving an enemy toward the player Empty
PostSubject: Re: Programming Tip: moving an enemy toward the player   Programming Tip: moving an enemy toward the player EmptyTue Mar 10, 2009 3:16 am

Xarnor wrote:
Thrilla wrote:
what a crappy diagram

What a crappy post.

That's a cool trick SC, I might try it sometime. That's actionscript right?

indeed.
Back to top Go down
http://gametwister.net
Sponsored content





Programming Tip: moving an enemy toward the player Empty
PostSubject: Re: Programming Tip: moving an enemy toward the player   Programming Tip: moving an enemy toward the player Empty

Back to top Go down
 
Programming Tip: moving an enemy toward the player
Back to top 
Page 1 of 1
 Similar topics
-
» looking for some programming help
» New To Programming? Read This
» Programming Regs Lounge

Permissions in this forum:You cannot reply to topics in this forum
Game Twister :: MultiMedia :: Flash-
Jump to:  
Powered by phpBB © 2001, 2002 phpBB Group
Create a forum on Forumotion | ©phpBB | Free forum support | Report an abuse | Forumotion.com