Is it possible to manipulate the Group and Mask properties?

I currently am generating (using a factory) a certain amount of objects. I want all these objects to be collidable with each other. However if I set the group and mask to the same thing in the collisionobject then it collides with itself and goes off in a random direction.

What would be better for me is that if each object could have its own group and then the mask could be a list of all the groups except that one. I’m not worried about performance here as it is not a large amount of objects (ie within the 16). As I am using a factory this would have to be done in code.

Is this possible? If not, is there any other way of dealing with lots of objects of the same base Game Object type being able to collide with each other?

As there is only going to be eight to ten of these it feels like the simplest solution will be just to have eight to ten Game Objects and not use a factory, but that doesn’t feel like a clean solution. Everything about these objects will be the same, I just want them to collide with each other and not themselves.

1 Like

To give an example of the idea, imagine a wall made up of bricks. If that wall collapses you’d want all the bricks to handle proper collisions with each other, so they’d end up in a proper pile.

Using dynamic physics (so we can use Box2D) and base brick Game Object, how would you set up the group and mask so they all properly collided with each other? Everything I have tried ends up with the brick colliding with itself.

(This isn’t what is happening in my game, but it is easier to explain like that)

I guess I’m not used to having to use a group and mask. The likes of Unity allow everything to collide with everything else and the engine simply lets you know the id of what you have collided with. I guess that’s what I am looking for, I don’t need to specify groups, I just need everything to collide with everything else.

1 Like

I see this hasn’t been answered and I’m wondering the same thing, so I thought I would bump this thread.

In my game players and enemies can use the same type of units. I have a base unit game object which is then put in to 2 different collections for player and enemy variations. The thing is the colliders should be the same shape, size and position for both player and enemy, but I would like them to have different groups and masks so that they can behave differently when they encounter an enemy or a player unit.

I have some initialisation code which behaves differently depending on whether the unit is a player or enemy, so if there’s a way of manipulating masks and groups via code I could set it up there.

1 Like

The short answer is no, unfortunately you can’t manipulate mask or group during runtime. You would need to handle the collision filtering yourself or try find another solution to the problem.

That’s the conclusion I came to and unfortunately it is what drove me away from Defold. I have game designs based around this and the limitations of Defold regarding collisions meant I would have to do so much extra work that I get for free with other engines (I ended up going back to Unity).

Another option I tried was having lots of different types of the the same Game Objects (blue, red etc) and using their tags so that each object had a mask of one type and collides with every possible mask apart from the one it is. It would have been a pain to configure it, but done once would be OK. But then I found that I couldn’t place objects with a collider (and hence using physics) wherever I wanted. I found on the forum that to move an object that uses physics you first have to disable the collider, so I tried that. that was great for moving it, but re-enabling the collider meant the Game Object snapped back to its old position. I then started plans of applying forces to objects to get them into the right position until I realised what I was trying to do was crazy.

I guess I’m just not doing a game that Defold is designed for (which is weird as it is a very, very simple game). I can’t help but feel that if you want people to use use Defold you are going to have to look into the physics and collision elements as currently they are extremely limiting.

Good luck.

Can’t you have two collision objects in your base unit game objects, one for the player to enemy collisions and one for enemy to player collisions and in your player and enemy script disable the collision object that you don’t need? Or perhaps have a collision object factory that spawns one or the other depending on unit type?

The main thing is that in the long run I wanted to use something like polymorphism avoid the risk of one box being edited and not the other seeing as they are supposed to be the same shape and size. I suppose it’s not that big of a deal in a small game like this.

I just thought of one potential work around. During collision you get the id of the other object you collide with, I could use that id in some way, though any solution I can think of is probably over-engineering a small game like this. So I’ll just go the old fashioned way of making each collision box twice.

First of all I just want to clarify that I am not part of the Defold team, I am just a user just as you are (or maybe were). You can do what you want and use physics, but not with automatic, built in collision handling and gravity. You would need to handle collision events your self, but on the other hand you would then have very good control over what collides with what and when.
If I look at your initial question though, I would say that objects colliding with themselves are a bug - if by objects you mean collisionobjects and not game objects containing several collision objects (that intercollide).

@Razzlero: I think you misunderstood the suggestion @britzl gave you. The suggestion is having one shared game object, but allow that game object to have two collision objects. One with mask and group for enemy and one for player. In the init-function of a script attached to the game object you would determine if the game object running the script is an enemy or player and disable one of the colliders accordingly.

@Razzlero: @Johan’s clarification was spot on. One base unit game object with to collision components where you disable the one you’re not using (depending on if the unit is a player or an enemy)

@amanset: I don’t know how you have things set up to get objects colliding with itself. Here’s a quick test I put together. One large “static” collision object and a bunch of “dynamic” ones. All have “default” group and “default” mask. I turned on physics debug to see everything without sprites and other graphics:

The simulation runs perfectly:

Can you please describe how your setup looks?

2 Likes

Sorry for the lack of a reply there. I just went to the forum as a friend has started to use Defold and I wanted to send her a link that answers some of her questions. I’ll have a look and see if I can find something that shows the problem.

All I can say right now (I am at work and probably shouldn’t be typing this) is that I had an empty scene with just a single object in it. As soon as I started it went flying off the screen. The “collides with itself” explanation was all I could come up with. It may also have been a bug very specific to that version and has now been fixed.

1 Like

Aha! If you have one game object with many inter-colliding collision object components you might get weird results since each component wants to reposition the game object.

2 Likes

ops, wanted to create the same topic, but found this. sad to hear, that there are no ways to set up the Group and Mask properties dynamically

for example how I done that before in flash and phaserJS games:

CATEGORY_BALL = 1;
CATEGORY_PLAYER = 2;
CATEGORY_GROUND1 = 4;
CATEGORY_GROUND2 = 8;
CATEGORY_GROUND3 = 16;

MASK_BALL = CATEGORY_PLAYER | CATEGORY_GROUND1 | CATEGORY_GROUND2 | CATEGORY_GROUND3;
MASK_PLAYER1 = CATEGORY_BALL | CATEGORY_GROUND1 | CATEGORY_PLAYER;
MASK_PLAYER2 = CATEGORY_BALL | CATEGORY_GROUND2 | CATEGORY_PLAYER;
MASK_PLAYER3 = CATEGORY_BALL | CATEGORY_GROUND3 | CATEGORY_PLAYER;
MASK_GROUND = CATEGORY_GROUND1 & CATEGORY_GROUND2 & CATEGORY_GROUND3;

then I set up the group and mask for each one when create new objects…

any alternative ideas how to release this? create 3 collision objects in PlayerGameObject and disable/enable it?

I’m not 100% sure I understand what you want to achieve with the setup you describe. Is it to differentiate the collisions of player 1, 2 and 3 and to know which player it is that collided with the ground?

@britzl imagine that each characters(players) in the game can collide only with own “ship”, and can’t collide with others.

This approach limit to create flexible physics games… sounds like the feature request for allowing to set up the collision object physics properties in runtime, at least before the adding by Factory to collection. in priority: mass, mask, group.

Ok, but can’t you solve that by also checking message.other_id? I assume the id of your own ship is known?

@britzl look please:
image

when the ball will hit the character 1, he will bounce and then collide the character 2 but NO ground 2, and will continue to fall with velocity.

what difference would it make if I know the id of grounds(ships)? nothing… the collision with objects will continue to work.

and - all objects are added by CollectionFactory.

udpate:
I found the alternative solution: create 3 CollisionObjects inside Character Game Object. and enable one of needed, others - disable. not really like this solution, but right now - the other way - only create GameObjects in Editor and set up mask manually.