Math and Distance Comparison issues for Enemy AI

For questions about using Classic.

Post » Mon Jul 19, 2010 6:01 am

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 havent been able to find much in the way of action/RPG enemy AI tutorials here on the forums and the wiki, I thought Id try to adapt a tutorial written for MMF2.

Ive implemented the Improved Chase/Escape Algorithm from this [url:3od2livt]http://www.clickteam.co.uk/resources/GTM/GTM10.htm[/url:3od2livt] 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. Im guessing this has something to do with x, y coordinates, but I just cant get my head around it.

[url:3od2livt]http://img840.imageshack.us/i/sqrtproblem.jpg/[/url:3od2livt]
This is what the algorithm looks like in the Events editor

[url:3od2livt]http://img833.imageshack.us/i/sqrt.jpg/[/url:3od2livt]
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
B
1
G
2
Posts: 3
Reputation: 631

Post » Mon Jul 19, 2010 6:53 am

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:
[code:28cyosyy]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
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Mon Jul 19, 2010 7:20 am

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
B
1
G
2
Posts: 3
Reputation: 631

Post » Tue Jul 20, 2010 2:39 pm

[quote="Razza":3gkhdf01]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[/quote:3gkhdf01]

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
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests