Jump to content

detect cliffs edges


InsaneHero
 Share

Recommended Posts

I did the usual google but was surprised not to find this one easily...

 

I have some AI controlled characters that plummet to their death by falling off every edge in the world.  I'd like them to notice that there's no platform in front of them and stop instead.

 

I'm using Arcade physics for the platforms, and couldn't see a tidy way of detecting collisions somewhere else than where the sprite presently is located.  (An untidy way would be to have an invisible 'test' sprite and move it around in front of the visible one... or worse do the move, wait a frame for the update, test collisions, and undo the move if it is off the edge - which would flicker)

 

Link to comment
Share on other sites

Yep, I could do that but it's a bit fiddly as the 'platforms' are created in a slightly weird way (they're buildings with variable widths) so I'd need to write additional code to generate the detection zones.

 

I'm just skimming through getObjectsUnderPointer to see if that could be repurposed to getObjectsAtLocation (seems highly likely).

Link to comment
Share on other sites

Untested but should work, I'll test it and try to get it into the dev branch later today (I'm heading out right now).

    /**    * Given a Group and a Pointer this will check to see which Group children overlap with the Pointer coordinates.    * Each child will be sent to the given callback for further processing.    * Note that the children are not checked for depth order, but simply if they overlap the Pointer or not.    *    * @method Phaser.Physics.Arcade#getObjectsUnderPointer    * @param {Phaser.Pointer} pointer - The Pointer to check.    * @param {Phaser.Group} group - The Group to check.    * @param {function} [callback] - A callback function that is called if the object overlaps with the Pointer. The callback will be sent two parameters: the Pointer and the Object that overlapped with it.    * @param {object} [callbackContext] - The context in which to run the callback.    * @return {array} An array of the Sprites from the Group that overlapped the Pointer coordinates.    */    getObjectsUnderPointer: function (pointer, group, callback, callbackContext) {        if (group.length === 0 || !pointer.exists)        {            return;        }        return getObjectsAtLocation(pointer.x, pointer.y, group, callback.callbackContext, pointer);    },    /**    * Given a Group and a location this will check to see which Group children overlap with the coordinate.    * Each child will be sent to the given callback for further processing.    * Note that the children are not checked for depth order, but simply if they overlap the coordinate or not.    *    * @method Phaser.Physics.Arcade#getObjectsAtLocation    * @param {Phaser.Pointer} pointer - The Pointer to check.    * @param {Phaser.Group} group - The Group to check.    * @param {function} [callback] - A callback function that is called if the object overlaps with the Pointer. The callback will be sent two parameters: the callbackArg and the Object that overlapped the location.    * @param {object} [callbackContext] - The context in which to run the callback.    * @return {array} An array of the Sprites from the Group that overlapped the coordinate.    */    getObjectsAtLocation: function (x, y, group, callback, callbackContext, callbackArg) {        if (group.length === 0 || !pointer.exists)        {            return;        }        this.quadTree.clear();        this.quadTree.reset(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);        this.quadTree.populate(group);        var rect = new Phaser.Rectangle(x, y, 1, 1);        var output = [];        this._potentials = this.quadTree.retrieve(rect);        for (var i = 0, len = this._potentials.length; i < len; i++)        {            if (this._potentials[i].hitTest(x, y))            {                if (callback)                {                    callback.call(callbackContext, callbackArg, this._potentials[i].sprite);                }                output.push(this._potentials[i].sprite);            }        }        return output;    },
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...