Enemy Movement Done Right ● SmileBASIC Source

Register

# Enemy Movement Done Right

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.
Updated
Rating