Make objects inside a group collide with themselves in Phaser.io?
Asked Answered
C

2

5

This question is regarding phaser.io. Can someone show me how to make objects inside a group collide with each other. Right now when i do,

//board is the group and it has many elements
//Floor is where all the elements fall to
game.physics.arcade.collide(this.board, this.floor);

All the elements inside the group stack on top of each other when they hit the floor at the same position. In other words the stack builds at one position, the latest one hiding the previous element and not upwards in the sense one on top of another at a higher position. How can i make the stack build upwards? If you could just show me how to make elements inside a group collide would be great. Thanks.

Cheloid answered 8/2, 2015 at 7:13 Comment(0)
C
11

You can collide a Group with itself:

game.physics.arcade.collide(this.board, this.board);

Although on that note I would point you at this tutorial on adding a simple 1d sort array, which will help considerably if the Group is densely populated: http://ra3s.com/wordpress/dysfunctional-programming/pruning-collision-detection-with-a-1d-sort/

Cark answered 8/2, 2015 at 11:29 Comment(4)
Ah yes ofcourse. Thanks!! Btw, an awesome article on 1d sort.Cheloid
Can this be used with p2 physics as well? Or should we use onBeginContact?Rood
P2 uses contact events for all collision, so you'd have to use those yes.Cark
Any reason why the self collision example does not have the accelerating array support? Has the performance issue been fixed in the current Phaser version?Ormond
S
1

Just to update accepted answer..

In phaserjs version 3.55.2 following code should be used:

game.physics.world.addCollider(this.board, this.board);
Setser answered 10/8, 2021 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.