? Enemy Movement Done Right ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

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.
Author
Guzzler
Updated
Rating
2 votes
Categories
Keywords
  • Trig
  • Trigonometry
  • Angle
  • Movement
  • Enemy
4 Comment(s) JustGreat JustGreat First Month Joined in the very first month of SmileBASIC Source Website Avatar Taboo I didn't change my avatar for 180 days Website Night Person I like the quiet night and sleep late. Express Yourself Well I thought this was at least a little bit obvious, but nice tutorial nonetheless! Guzzler Guzzler First Week Joined in the very first week of SmileBASIC Source Website Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Thank you! SpaceKadeta 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. Guzzler Guzzler First Week Joined in the very first week of SmileBASIC Source Website Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength I'm sorry, I don't know. I don't understand the question very well either. Maybe someone else could help here.