Jump to content

On Initialisation, Efficiently Ensure Sprites Don't Overlap


rolandino
 Share

Recommended Posts

Hi,
Wonder if I could ask a question ....

I wish to generate a set of sprites at random positions on and off screen. I have this working, the creation is not a problem. However what I would like to do is to ensure that on initialisation the sprites don't overlap with each other. And to do this efficiently. Once the game starts, handling collision etc is not the issue here, it's just ensuring that on initialisation there is no overlap.

I'm using Arcade Physics, so could use the http://docs.phaser.io/Phaser.Physics.Arcade.html#collide functionality, but can only see repetitive use of this being inefficient.

Any thoughts or suggestions anyone?

Many thanks,
R
 

Link to comment
Share on other sites

Although it doesn't mention it explicitly, this example has a routine for ensuring all objects have a unique position based on a grid; this could probably be adapted pretty easily to your situation: http://examples.phaser.io/_site/view_full.html?d=groups&f=depth+sort.js&t=depth%20sort

 

I think if you didn't want to use a grid, you'd probably have to run some iterative separation routine using overlap rather than collide (which won't work until the sprites are given a velocity and have stopped intersecting in the first place) to 'nudge' the sprites away from one another by a certain amount, and keep going until there are no more overlaps. I think in this case it would seem inefficient but it'd probably be pretty quick nonetheless if you use Arcade's own intersects/separation stuff.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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