It has come to my attention that most PTC games and users use the traditional "if player is under you y = y +1" enemy AI formula while this method might not necessarily be the worst way of going about enemy AI it is very primitive and could be improved by using a little bit of trigonometry. Don't let that big word scare you off trigonometry is generally simple enough and eventually if you go to high school you will learn it, but applying it to a video game is something else altogether and I am here to teach you about it.

So fist off before anything we need to learn PI, Radians, and a few trigonometric functions, don't worry this will be quick!

Skip the parts you already know to save time!

PI is 3.14..., it's a number that when multiplied by a circles diameter (The length of a circle from two opposite ends) will always accurately return the circles circumference or distance around it, for example

8 * pi = 25.1327412287

it will also return half the circumference of a circle if you multiply it by the radius, or half the diameter, the distance between the circles edge and the center of the circle.

4 * pi = 12.5663706144

now Radians are basically another system of measuring angles besides degrees revolving around an angles radius, because a radius is half an angles diameter it will always return half the circumference when multiplied by PI, one radian is the distance of the radius stretched across its circles circumference and how much it covers.

So now that we know what Pi, Radians, Radius, Diameter and circumference means let's get into trigonometry.

Trigonometry is basically figuring out all the angles and sides of a right triangle by obtaining a single angle of one of the corners (besides that of the 90 degree one) to figure out the angle of a corner by using the lengths of the triangle we use a function called arctan, or atan as it is named in PTC.

Atan takes two parameters, the X distance (aka horizontal or left and right) of the two points (in this case those two points are the character and the enemy) and the Y distance (aka vertical or up and down).

So in order for us to find the angle of the character relative to the enemy we have to find the X and Y distance between them.

**DO NOT TYPE THE CODE EXAMPLES INTO PTC YET!**

We do this by subtracting the enemies co-ordinates from the characters co-ordinates like this.

characterX - enemyX

characterY - enemyY

We then plug them into the Atan function to get the angle.

angle = atan(characterX - enemyX,characterY - enemyY)

now with the angle we then use in in conjunction with sin and cosine and the velocities of the enemy (unless the enemy has no velocity variable)

enemVelX = enemVelX = (sin(angle)*0.1)

enemVelY = enemVelY + (cos(angle)*0.1)

it is very important that we use the trig functions sin for x and cos for y, I could get really in depth of why it needs to be this way but for the time being we don't need to know this, but if you really want to know basically it's because sin gets the ratio of the horizontal (or X) length divided by the hypotenuse or distance between the player, same thing goes for Cosine except vertical (or Y) instead of horizontal.

After the game adds the velocities with sin and cosine you will start to see that the enemyX and Y variables get closer and closer to the player X and y variables until they match, or the enemy just moves from one side of the player to another this is because we multiplied the sin/cos by 0.1 in order to make the enemy travel slower since the trig functions sin and cos return a variable too large so it always be on the character, multiplying it by 0.1 simply "lags" the enemy and moves the enemy slower, you can play with it and change it by any variable you want.

So if you were to implement this into a game the code would look like:

characterY = 7 enemX = 100 enemY = 100 enemVelX = 0 enemVelY = 0 @gameloop 'player sprite SPSET 0,68,0,0,0,0,16,16 'witch (or enemy) sprite SPSET 1,100,0,0,0,0,16,16 IF BUTTON() == 1 THEN characterY = characterY - 1 IF BUTTON() == 2 THEN characterY = characterY + 1 IF BUTTON() == 4 THEN characterX = characterX - 1 IF BUTTON() == 8 THEN characterX = characterX + 1 angle = ATAN(characterX - enemX,characterY - enemY) enemVelX = (enemVelX + (SIN(angle)*0.1))*FRICTION enemVelY = (enemVelY + (COS(angle)*0.1))*FRICTION enemX = enemX+enemVelX enemY = enemY+enemVelY SPOFS 0,characterX,characterY,0 SPOFS 1,enemX,enemY,0 VSYNC 1 GOTO @gameloop

So basically that's all, feel free to play around with this, edit it in case I made any mistakes and use this in your programs, hopefully better games will be made now that I have made this knowledge more widespread in the community. if you have any questions or comments please reply I will respond as soon as possible, I guess that's all so see ya and have fun!

**One more thing!**

I forgot to mention why I even taught you the math up there besides trig, you see when you use atan PTC doesn't give you the angle in degrees instead, and while this might not be an issue because cos and sin work with it fine you might want to use the DEG() function to convert the radian value into degrees if you ever wish to use SPANGLE, that's all.