Monday, September 12, 2011

He's... learning!

Can it be?

First - I promised my cousin I'd put this up.  Here you are Andee!
Her first doodle on my tablet, turned out great!
Too much whitespace, because I accidentally exported the image at the wrong size.  Sorry!

Anyway, can it be?

Potentially.

So, if you all remember (or scroll down) the other day I was having compounding headaches over my pathfinding algorithms.  They worked, but they did a rather poor job of avoid collisions with other units.  Avoiding collisions isn't a necessary factor, however in my game when two ships touch they... well they go boom.


Close Enough.

So have I come up with a miraculous last-minute save?
...no.  No, I have not.  Turns out, there's been a lot of guys working on making pathfinding AI not crash and bump into each other, and, well, there's not really any one grand solution to the entire problem.  It exists, and workarounds are never solid and infallible.  Some of my choices worked well enough, for a while, but then every once and while the system would freeze up. Rather than try and devote myself wholly to fixing pathfinding once and for all as, I researched alternative options.

As happy as I am with pathfinding as a concept, it didn't actually look incredibly natural or appealing which was a slight issue for me, but I was so dedicated to pathfinding that I didn't want to give up on it.  Like I said, it was, for the most part, completely functional.  One day I'll go back to my pathfinding when it is nessecary and spruce up the code, and augment the system to do something for collision avoidance.

So.. how are my spaceships operating now?  It's only halfway complete, the original goal was for my spaceships not to crash into the planets, that I completed with ease, but I ran into issues getting my ships not to crash into each other.

Now, my ships don't crash into each other, they just fly through planets.
MAKE UP YOUR MIND!
I found a few articles on different methods of AI behavior, including one on an Emergent Steering technique called flocking, that was originally developed, if the articles are to be trusted, for animation.
For those of you interested, here's my main article of reference and then this is the site where I first saw the flocking behavior exhibited, which includes some code that I heavily based mine on, almost exact line-for-line I'm almost ashamed to admit, but I will end up making significant tweaks to factor in collision avoidance.  I'm somewhat afraid this is some debauched code-version of plagiarism, but I will and do give credit where credit is due.  I'm just not sure on the exact logistics of it.  That's not the only flocking code every written, and nobody owns the right to flocking code per se.

Regardless, its actually results in greatly improved-looking trajectories for my spaceships, it looks more natural and space-like, if that is even a concept.  I'm quite pleased with the direction flocking is moving in.

The quick rundown of flocking is each unit, referred to as boids in the above link, has a list of it's peers (which I already had implemented in the "friends/enemies" system) and adjusts it's velocity based on three factors: Separation, Alignment, and Cohesion.  In the game world they are all represented as Vectors, or rather as having both magnitude (or speed, depending) and a direction. Separation is the force that drives each unit apart from one another, and keeps them from crashing (my favorite).  Alignment is the "force" that finds the average direction among the group and adjusts each unit to try and meet that average.  And last but not least, Cohesion, which somewhat contradictory to Seperation, finds the average center of the group, and then brings all the units in closer to that location.  It simply brings in an outlier in the group.  If Separation is made stronger by a arbitrary amount (but greater than 1x) it tends to keep Cohesion from bringing anyone in too closely.

There are a few other elements to flocking that I am implementing out of blind faith, almost.  Unlike pathfinding I did not find a tutorial or ample resource for flocking/Emergent Steering, I copied what code I could find and adopted it.  Once I understand the logistics of flocking more, I may elaborate upon them later.

Long story short, I practically scrapped all of my work on pathfinding in favor of flocking, and once I get the units to avoid planets, paddles and, you know, the asteroid in my game, I will be wholly satisfied with the result (fingers crossed though)

Flocking, keep in mind, is not meant to find a path from A to B, but if all my units need to watch out for are two planets and a fast-moving asteroid, finding the shortest path isn't necessarily... well, necessary.  Regardless, I am thinking of a combination of the two techniques for later games, it's an interesting thought.

Annnnnnnnd doodles.
I save the best for last, so at least you all scroll down and kinda-sorta-maybe-not-really read this.

Peace,

-Kev

No comments:

Post a Comment