Refining on-the-fly pit spawning in Impact

Posted on June 28, 2012 | 2 minute read

I’ve been continuing to play around around with my procedural level experiment in Impact. In the last update I talked about spawning obstacles (just pits for now) on the fly. The pits were spawning, but I had another problem: pits would sometimes spawn too closely together and overlap each other, creating “superpits” that the player wasn’t able to jump over.

I needed to have a minimum distance between pits. I’m not sure if my way of doing this is the best solution because it kills excess pits after they’re created instead of preventing them from being spawned in the first place (this is the same thing I’m doing with the collision tiles, as outlined in my last post). So each pit and collision tile is going through a “I’m alive. Wait, should I be here? Die if I shouldn’t.” type of thing. But it works and so far there hasn’t been a noticeable performance hit that I can see.

So here’s how I’m currently doing it. Again, over-commented for the purpose of this post.

        var allPits = ig.game.getEntitiesByType( 'EntityPit1' );
        // Get distance from this pit to every other pit
        for (var i = 0, otherPit; otherPit = allPits[i]; i++) {
            var distanceToPit = this.distanceTo(otherPit);
            // If this pit is less than 1 pit's width apart from another pit,
            if (distanceToPit <= this.size.x * 2) {
                // Kill this pit
                console.log('kill');
                this.kill();
            }
        }

Now regardless of how high the chances are of a pit being spawned, one will never exist less than a pit’s width apart from another:

On the fly obstacles in JavaScript




Categories:game dev dev games
comments powered by Disqus