Jump to content

Moving a group of sprites with arcade physics collide on


ForgeableSum
 Share

Recommended Posts

Suppose I want to move a group of sprites with collide to a coordinate using moveToXY. The method works great for single sprites but with multiple sprites on collide, sprites in the back won't make it to their destination because they are blocked by the first sprite that made it to the destination.   

 

Is there a simple an efficient way to move multiple sprites with collide to a single destination? Essentially I want them to move as one object to one destination, instead of as multiple objects to one destination. 

 

I get that I could put all sprites in a  physics-enabled sprite, but that doesn't solve me problem because the sprites are starting from different positions (i.e. they could not literally be contained within the parent sprite, so moving them as one body would be impractical). 

Link to comment
Share on other sites

If you want them to move in formation, you could call moveToXY and add their current position to the position you want to move them to, that will mean they all move to a relative position based on the target position, rather than all trying to move to the same position. This still may not be what you want but it's a step in the right direction - in the end you probably need a flocking/boids/pathfinding system of some variety to do this properly.

game.physics.arcade.moveToXY(sprite, target.x + sprite.x, target.y + sprite.y, false, 5000);
Link to comment
Share on other sites

lewster, this is a great idea and it's something I hadn't thought of. I will do this in combination with a function that causes the sprite to stop when overlapped. I'm sure there is an event I could use to call that function. then, of course, a pathfinding system which takes this into account. 

 

using collide on all sprites wasn't an option anyway because it sucks up too much memory. I literally go down to 10 fps with 150 sprites set to collide with each other.  

Link to comment
Share on other sites

 

If you want them to move in formation, you could call moveToXY and add their current position to the position you want to move them to, that will mean they all move to a relative position based on the target position, rather than all trying to move to the same position. This still may not be what you want but it's a step in the right direction - in the end you probably need a flocking/boids/pathfinding system of some variety to do this properly.

game.physics.arcade.moveToXY(sprite, target.x + sprite.x, target.y + sprite.y, false, 5000);

Turns out this solution doesn't work for me. Because sometimes the sprites start out really far from each other and need to move to a location between each other (so uniform movement doesn't make sense). 

 

What I really need is the following:

* Sprites can move to a single x/y from anywhere on the map (even if they are scattered)

* When they get to the destination x/y, they gather around the coordinates in an organized fashion (not overlapping each other, not colliding, etc)

 

I was thinking in the direction of a grid system, but I read on here that it's not a good idea with physics enabled because the system doesn't work with multiples. I thought of creating a makeshift grid: essentially plotting each x/y destination for each sprite so they circle (or create a rectangle) around the destination in an organized way. But that is going to be complicated. I wonder if there are any other options?

 

Supposing the width and height of the sprites are 10, what's the math look like on plotting destinations around a central location? I'm not very good at math ... I'm sure it involves pi (lol). 

Link to comment
Share on other sites

@lewser, the method of plotting individual coordinates works quite well. I'm now adding an offset to each x/y destination, then checking to see if the sprite is within an arbitrary number of pixels from the destination. Because the offset simply adds +15 to x/y  destinations on each loop iteration, the result is that the units form a diagnal line.  This is perfect for my needs because I ultimately wanted to create different sprite formations in different shapes (circles, rectangles, lines, etc) based on player input. 

 

One thing I am curious about though. Is perpetually looping through sprites to check if they've reached their destination the best way to stop a sprite via moveToXY? Surprisingly, it has no impact on performance even if there are 50+ sprites moving. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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