LoginLogin

Enemy Movement Done Right

Root / Submissions / [.]

GuzzlerCreated:
A simple way of moving sprites that was commonly used in NES games is IF PLAYER IS LEFT THEN MOVE ENEMY LEFT. That is disgusting. It makes for very choppy movement. What you need is trigonometry! I'll explain. There is a distance between the player sprite and the enemy sprite; X distance and Y distance. The two make an angle. To make the enemy move on this angle, you need to calculate the sine and cosine. You also need the Pythagorean theorem. Remember soh cah toa. Sine = Opposite/Hypotenuse, Cosine = Adjacent/Hypotenuse, Tangent = Opposite/Adjacent. So the adjacent is your X, and the opposite is Y. With this, we can make the enemy move. But we still need the hypotenuse. We can find the hypotenuse with the Pythagorean theorem. Here's my code:
EX=EX+(EX-PX)/SQR(POW(EX-PX,2)+POW(EY-PY,2))
EY=EY+(EY-PY)/SQR(POW(EX-PX,2)+POW(EY-PY,2))
This is written in SmileBASIC code just to be clear. So since Leg12+Leg22=Hypotenuse2, we found Hypotenuse2, then found the square root of it to find the length of the hypotenuse. That's pretty much it. I hope this wasn't too complex. If you have any questions tell me in the comments.

You have thoughtful ways of programming, can I say, I have tried to this doing one COS(ATAN( )) function. and it worked well. Do you know if there are commons or differences that can be achieved through successfully implimenting a round versus a fraction versus a decimal sum? I.E how fast will the enemy process its current momevent equations? Because I get this fr and feel stuck.

Well I thought this was at least a little bit obvious, but nice tutorial nonetheless!

Replying to:JustGreat
Well I thought this was at least a little bit obvious, but nice tutorial nonetheless!
Thank you!

Replying to:SpaceKadeta
You have thoughtful ways of programming, can I say, I have tried to this doing one COS(ATAN( )) function. and it worked well. Do you know if there are commons or differences that can be achieved through successfully implimenting a round versus a fraction versus a decimal sum? I.E how fast will the enemy process its current momevent equations? Because I get this fr and feel stuck.
I'm sorry, I don't know. I don't understand the question very well either. Maybe someone else could help here.