How do I Optimize My AI

0 favourites
  • 14 posts
From the Asset Store
Units do not overlap each other and use different ways if there are several free ways.
  • 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:

    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

  • 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.

  • 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

  • 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.

  • 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!

  • 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...

  • 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).

  • 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:

    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:

  • lol for some reason when I shot that screengrab CPU was at 18%, it's not like that most the time haha

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • pretty cool fix you get here yes i relized that you where using physics, at the begining i thought you where using only 8 direction behaviour

  • Okay thanks for all the help everyone, I've manage to optimize the game down so that I can have about 20 of these AI doing their thing in a scene and it runs at 60 on my crappy laptop.

    Here's a simple build of an area: https://dl.dropboxusercontent.com/u/331 ... index.html

    You can't do much, but on load press ENTER to take control of one of the AI and use WASD / QE to thrust. I don't know what sort of frame rates you will get but hopefully it'll be stable.

    Thanks again, there's still much more optimization that can be done, but at least now I can get back to designing gameplay.

  • This is really good, feels like you're playing an MMO version of asteroids. It's fun to watch the ai go about their business & controls are a challenge. Definitely a lot of possibilities for mission design, & it'd be nice if they were sandbox style with multiple ways to complete them, with stealth, hacking & action all being options.

    Also anyone trying this should press the right arrow key

  • mattb - haha! Forgot about that.

    Cheers for the feedback, will be sure to update the devlog once there's some actual gameplay to try

    One of the last thing I put in was a stabilizer, if you hold left shift, it increases drag but consumes fuel, it allows you to stop drifting. The AI fly with the stabilizer always on at the moment and they freak out when it's switched off.

    Oh also, press F to turn your light off - for stealth. Currently working on 'alert' states so that the AI attack you when you do something they don't like.

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