Interaction between elements of different groups

Hello guys

I would like to know if it’s possible do communicate 2 items that are on 2 different group . Let me explain: I have a group of objects that moves on the screen random ( group 1) ; and 2 other immovable objects that are included in group 2.

Can I detect a collision, through the “collision” EventListener, between an element of a group and an element of the other group ?

Thanks in advance guys

Hi @briesanji,

If you plan to use physics-based collision detection, then yes, you can detect collisions between objects in different groups (basically, it doesn’t matter which group objects are in).

HOWEVER (and this is extremely important), you can’t move entire groups around independently of each other when using physics-based collision detection. Meaning, you can move the objects around as their own objects, but you can not shift the x/y/scale/rotation of the entire display group which contains them, while keeping the other group in its original position. All groups that contain collision-relevant objects must retain the same position/scale/rotation, or the collision detection will be inaccurate and effectively useless.

Hope this helps,

Brent

Thanks for your help Brent
Yes i figured out what is my problem. My game is composed by a character that is anchored at the center of un screen and a group of element (bg that di bigger than screen size , obstacles , walls, etc…) that is moved by a virtual joypad.
When I use the transitions on The entire group to move it, the x,y coordinates of the single elements don’t change from their initial position. Then The collision can’t be detected even if it seems that my elements are touching.

This is a big problem because I have a lot of elements in that group and I can’t move each one singly.
What solution do you suggest to me?? If exist a solution :frowning: :frowning: :frowning:

Hi @briesanji,

From your description, it sounds like you’re building a game where a player moves around a larger world, and the “camera” tracks around the world, while the player remains approximately (or exactly) in the center of the screen while the rest of the world scrolls around him. True?

If so, you can actually achieve this nicely and still use physics. The trick is to simply offset the player’s position in the exact inverse of how you move the “world” around. For example, if the world moves x+4 and y+7, simply update the player’s position to x-4 and y-7 from where it was.

Now, the other important thing is that you must move the entire “stage” around to move the world… that includes all groups. So effectively, you’re moving both the world group AND the group containing the player, but in the same time step, you’re moving the player back to center, so it appears as if the player hasn’t moved in relation to the screen center. In this way, the physics world maintains the proper collision detection, because everything is moving in unison from a group position/scale/rotation standpoint.

Hopefully that makes sense…

Brent

@Brent. You saved my project you are a god. Really I will never cease to thank you.

Hi @briesanji,

If you plan to use physics-based collision detection, then yes, you can detect collisions between objects in different groups (basically, it doesn’t matter which group objects are in).

HOWEVER (and this is extremely important), you can’t move entire groups around independently of each other when using physics-based collision detection. Meaning, you can move the objects around as their own objects, but you can not shift the x/y/scale/rotation of the entire display group which contains them, while keeping the other group in its original position. All groups that contain collision-relevant objects must retain the same position/scale/rotation, or the collision detection will be inaccurate and effectively useless.

Hope this helps,

Brent

Thanks for your help Brent
Yes i figured out what is my problem. My game is composed by a character that is anchored at the center of un screen and a group of element (bg that di bigger than screen size , obstacles , walls, etc…) that is moved by a virtual joypad.
When I use the transitions on The entire group to move it, the x,y coordinates of the single elements don’t change from their initial position. Then The collision can’t be detected even if it seems that my elements are touching.

This is a big problem because I have a lot of elements in that group and I can’t move each one singly.
What solution do you suggest to me?? If exist a solution :frowning: :frowning: :frowning:

Hi @briesanji,

From your description, it sounds like you’re building a game where a player moves around a larger world, and the “camera” tracks around the world, while the player remains approximately (or exactly) in the center of the screen while the rest of the world scrolls around him. True?

If so, you can actually achieve this nicely and still use physics. The trick is to simply offset the player’s position in the exact inverse of how you move the “world” around. For example, if the world moves x+4 and y+7, simply update the player’s position to x-4 and y-7 from where it was.

Now, the other important thing is that you must move the entire “stage” around to move the world… that includes all groups. So effectively, you’re moving both the world group AND the group containing the player, but in the same time step, you’re moving the player back to center, so it appears as if the player hasn’t moved in relation to the screen center. In this way, the physics world maintains the proper collision detection, because everything is moving in unison from a group position/scale/rotation standpoint.

Hopefully that makes sense…

Brent

@Brent. You saved my project you are a god. Really I will never cease to thank you.