Update mass when creating game object

Howdy, I have multiple different ball objects that I want to spawn all from the same location. Each one has a different diameter, sprite, mass and restitution (think basketball vs bowling ball). To start I created a factory that spawns a ball game object and then posts a message to update its sprite and uses go.set_scale to update its size.

This has worked fine for now, but I realize mass and restitution cannot be updated once the game object is created. I was thinking of having separate game objects, one for each ball, and then spawning these from a single factory. However, it seems a factory can only spawn a single kind of game object.

So now I am thinking of having n factories, where n is number of balls, and having the correct factory spawn the ball when its time to do so. Does this sound like the simplest solution to do what I desire or am I missing something more obvious?

1 Like

That should work!

I would also recommend that you open a feature request on GitHub for a b2d.body.set_mass() function to be added to the rest of the box2d functionality

It looks like we started on something for this already:

As you see, setting mass is more involved, and we need more scaffolding around the internal structures of Box2D. Ofc the goal is to support most (if not all) functions, but it also requires some effort.

1 Like

Thanks for quick response! Ill make a feature request for it and implement the idea that I shared to solve this for now.

1 Like