How do I Optimize My AI

Get help using Construct 2

Post » Fri Apr 24, 2015 12:07 pm

Hey,

I have a project with these roaming AI which don't use path finding but instead each has collision sprites pinned to their body and they use those to check collisions against other physics objects and thrust away from them, here's a snippet of my code to show generally how I'm putting it together:

Image

I'm quite happy it all works well enough but I'm finding that with 8 / 12 / 16 AI in the scene my CPU is climbing up and up, I want there to be quite a lot of AI if possible so I'm wondering if there's better ways to achieve similar results.

I'm using a lot of if this is overlapping blah blah to pick instances, where I'm betting there's a smarter way to pick these guys with UID's and the such but I can't think of how.

Here's a video of them in action https://www.youtube.com/watch?v=FQCrYhMVQKU
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

Post » Fri Apr 24, 2015 12:32 pm

I did something similar with a top down car game, but it was with fewer instances and it soon become a nightmare to make some intelligent AI driving. The video looks impressive congratulations.

I assume you are doing all of this per tick, maybe try reducing it and doing it per X seconds.

Another way would be to check first the distance of the ships to obstacles (maybe including directions/movement sort of predicting what will happen), if it is in a "safe zone" then do no more checks to save CPU.
B
7
S
2
Posts: 15
Reputation: 614

Post » Fri Apr 24, 2015 2:32 pm

just one detail, you are setting the "isavoiding" to true and in the same sub event you ask if it was set, you can actually delete that condition;

I see you created a very fluid IA, but for the effect i think a mettod where there is only needed a bullet and a rotate; at start set rotate disabled and taking angle to a position (objective). If collides with a rock, check if there is overlaping at offset to your left,if not, slow down speed while activate rotate until there is no collision in that direction, if there is an overlaping at left, rotate in the other direction. When there is no more collision, deactivate rotate, and rotate angle to objective again.
Actually they could become very smart (like a seeking missil avoiding rocks) or weird results...

ill try to recreate it and show it later :)
B
23
S
4
Posts: 110
Reputation: 1,393

Post » Fri Apr 24, 2015 9:51 pm

How are you pairing the ships with the colliders? From what it looks like you're using an overlap check to pick the ship from the colliders. You could eliminate those collision checks by putting the colliders and ship into a container.

Another thing you can do is check for collider overlaps once and store the result in a variable.
every tick:
--- controller_smallShip: set blockedL to false
--- controller_smallShip: set blockedR to false
--- controller_smallShip: set blockedF to false
--- controller_smallShip: set blockedB to false

colliderL is overlapping obstacles:
--- controller_smallShip: set blockedL to true
colliderR is overlapping obstacles:
--- controller_smallShip: set blockedR to true
colliderF is overlapping obstacles:
--- controller_smallShip: set blockedF to true
colliderB is overlapping obstacles:
--- controller_smallShip: set blockedB to true

Then for the remainder of the event sheet just check those booleans. You shouldn't need to actually check for collisions again that tick unless you change the object's postions in the event sheet. This technique is mainly useful if you you're doing the same overlap check in multiple places.
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Sat Apr 25, 2015 8:41 am

Amazing thanks for the info everyone I'm working through these suggestions now. Straight off the bat, @rf900 limiting every tick to seconds has made quite a big saving.

@kingpirux that would be interesting to see, however I want this AI to use the same control / physics as the player, the player can take control of the AI at any point and I want the interactions between the AI and player to be similar.

@R0J0hound - yes I am using a lot of overlapping to pick, this is a good idea I'll integrate it and see what happens

thanks!
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

Post » Sat Apr 25, 2015 11:25 am

As r0j0 said, put your ship & collider into a container so they're picked & created (& destroyed) together. That plus running collision checks every 0.5s or so are essential.

Your various directional bools look dodgy. Because each direction negates the other, you'll be favoring the one that's last in the event order (eg. if obstacles are in front of & behind the ship, thrustBackwards always ends up true). The results in the video look good though :)

I wonder if you even need 4 colliders per ship. Checking the rear could be eliminated since you're always travelling forwards, so maybe you could just have one big 'sensor' collider in front. On each collision check, you get the angles from each obstacle to the ship (possibly weighted by their size & distance) & work out a new heading for the ship.

Or how about a force-based system with no collision checking: you apply forces away from obstacles based on distance & obstacle radius. Slap on a speed limit (chipmunk can do this) & the physics will never spaz out.

Then you do some cosmetic things to make it look like the ai ships have the same controls as the player.

I think I'll have a go at the heading & force based pathfinding, given me an idea for something of my own...
B
27
S
12
G
1
Posts: 157
Reputation: 3,359

Post » Sat Apr 25, 2015 3:29 pm

Ok this is probably not what you're after but it's an idea for very cheap pseudo-pathfinding using chipmunk physics forces to push things along to a target:

https://dl.dropboxusercontent.com/u/523 ... ng%27.capx

A problem with it is that the drones get stuck in cul-de-sacs. A solution might be to detect when the speed drops below a limit, then enable a "reversing" boolean. When reversing you just move away from the target for a say 5 or 10 secs. After that you set it to false (I'll give this a go if I have time).
B
27
S
12
G
1
Posts: 157
Reputation: 3,359

Post » Sun Apr 26, 2015 8:52 am

@mattb thanks for this - I really like your example and suggestions. I think I'm going to persist with my collided style but also use distances as you suggest for some of the objects to avoid. The colliders are working quite nicely atm, "f obstacles are in front of & behind the ship, thrustBackwards always ends up true" - this is true, but my collided shapes look like this:

Image

And when an object is in front of the ship, the ship will also boost left or right - the 'last collided' check makes the direction of sideways thrust mostly work out except if the ship is surrounded, at which point it just shoots the asteroid in front of it :)

I'm setting up the containers now and reducing as many overlap checks as possible, will get back to you with the results. I'm currently using 20-35% CPU with 12 ships in the scene:

Image
Last edited by CrudeMik on Sun Apr 26, 2015 9:00 am, edited 1 time in total.
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

Post » Sun Apr 26, 2015 8:53 am

lol for some reason when I shot that screengrab CPU was at 18%, it's not like that most the time haha
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

Post » Sun Apr 26, 2015 11:09 am

CrudeMik wrote:if the ship is surrounded, at which point it just shoots the asteroid in front of it :)


Why didn't I think of that? Would solve my pathfinding issue at a stroke.

About your collision shapes - if they're not already, I'd suggest making them rectangular or even triangular quadrants for efficiency.
B
27
S
12
G
1
Posts: 157
Reputation: 3,359

Next

Return to How do I....?

Who is online

Users browsing this forum: max2612 and 23 guests