# Math and Distance Comparison issues for Enemy AI

This forum is currently in read-only mode.
0 Favourites
• 4 posts
• Hey, I'm new to the forums, and have been experimenting with Construct (and similar programs, such as Multimedia Fusion 2) for a good while now.

I've been toying with the idea of a top-view action/RPG-type game in Construct, and have been having difficulty with implementing enemy AI (which, as you would know, is pretty damn important for a game like this.)

As I haven?t been able to find much in the way of action/RPG enemy AI tutorials here on the forums and the wiki, I thought I?d try to adapt a tutorial written for MMF2.

I?ve implemented the ?Improved Chase/Escape Algorithm? from this http://www.clickteam.co.uk/resources/GTM/GTM10.htm tutorial, which basically determines the distance between the player and the enemy (apparently by using Pythagoras? theorem for triangles), and checks if it is lower than the appropriate amount.

Player approach from the West or the North seems to work as it aught to, but approach from the East and South is pretty messed up. If the ProtoEnemy object is approached from the East or the South, it won't be alerted to the player's presence until the player actually passes through the object. I?m guessing this has something to do with x, y coordinates, but I just can?t get my head around it.

http://img840.imageshack.us/i/sqrtproblem.jpg/

This is what the algorithm looks like in the Events editor

http://img833.imageshack.us/i/sqrt.jpg/

And here is a hand-written version of the same; blue is enemy, red is player.

Please note that I am already aware of the LineOfSight behaviour ? my idea is to use that in conjunction with this: for example, having the enemy spot the player using LineOfSight, approach the player and then ?engage? with the player ? stopping a small distance away from him and then making its decisions to wait, jump around, take a swipe at him, etc.

I'll try to post a .cap file if anyone requests it.

Thanks in advance,

Razza

• Welcome to the forms.

I recommend you use the distance expression instead of that long expression, which seems to be missing a few parenthesis.

so your condition should now look like this:

``````distance(player.X, player.Y, enemy.X, enemy.Y) Lower or Equal 2500[/code:28cyosyy]
You probably want to change the 2500 value to something lower, because I can almost guarantee that when the cap is previewed the enemy will be within 2500 pixels of the player, and will flash instantly.

-Cheers``````
• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Thanks for your reply.

Yep, that way works. It's a lot simpler, too. It would have been good if I knew about that before.

By the way, I always thought that the 2500 didn't seem right, but, for some reason, the expression I was using needed a really high number to work? (Not that it worked properly anyway >_<)

Thanks again

• Thanks for your reply.

Yep, that way works. It's a lot simpler, too. It would have been good if I knew about that before.

By the way, I always thought that the 2500 didn't seem right, but, for some reason, the expression I was using needed a really high number to work? (Not that it worked properly anyway >_<)

Thanks again

its was using that cause your expressions was flawed, there should only be one square root and your missing brackets.

but anyhow use the distance(x1,y1,x2,y2) formula its much faster for you and the computer

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)