4
\$\begingroup\$

I'm making a 2D platformer, shooter type game in LibGDX, and I'm wondering about Box2D's mass system. I'ven't used mass for anything in the game so far, and everything has worked ok. We move the player, enemies, and bullets around by body.applyLinierImpulse(new Vector2(impulseX, impulseY), body.getWorldCenter) and even have a variable jump height system, and it all works fine. To change the speed or impact of objects, we just change the impulse. I've tried using aplyForce() but it won't move the body. Everything is usually just a simple polygon, but when I'm creating fixtures there's an option to set the density, and bodies have the option to set the massData, but I don't know what either of these properties does. The gravity of the world is around 11. Can anyone tell me how I should be handling mass with my objects in the game? Thanks.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

As Bálint explained, mass in Box2D basically works according to Newton's laws of motion.

Sounds like what you're really after though is how mass works in Box2D in practical programmatic terms. As you're suspecting, that's related to fixtures' densities and bodies' mass data.

Insights on density and mass can be found throughout the Box2D version 2.3.0 User Manual.

For example, in Section 2.2 of the manual, where it's talking about the second parameter to the create-fixture method:

The second parameter is the shape density in kilograms per meter squared. A static body has zero mass by definition, so the density is not used in this case.

Meanwhile, in Section 6.1, it states:

b2_staticBody A static body does not move under simulation and behaves as if it has infinite mass. Internally, Box2D stores zero for the mass and the inverse mass.

And a little later:

b2_dynamicBody A dynamic body is fully simulated. They can be moved manually by the user, but normally they move according to forces... A dynamic body always has finite, non-zero mass. If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram and it won’t rotate.

Essentially, to summarize, you'll need to use dynamic bodies with greater than zero masses to see mass related physical effects. And you'll probably prefer to use their fixtures' density settings to establish those masses where these densities are the areal mass densities for the shape associated with the fixture. Body masses are then calculated as the sum of the masses of all the shapes associated with the dynamic body where the mass of each shape is this areal mass density times the area of the shape.

\$\endgroup\$
2
\$\begingroup\$

Mass defines how easily something can be moved.

Imagine this: you have 2 boulder in front of you, they are the same size, but one of them is out of lead, the other is pure aluminium. Which one's easier to get to a certain velocity? The aluminium one of course.

The formula for force is F = m * a, where F is the force, m is the mass, a is acceleration. You can reorder the formula to get a = F / m. This means, that the heavier an object is, the smaller it's acceleration is if the force stays the same, and smaller acceleration means slower speed.

The mass also affects many many other aspects, like momentum (aka: how hard is it to move or stop something), but the main factor is acceleration.

Using force instead of pure acceleration or velocity makes the player actually "feel" how heavy an object is and it doesn't allow the player to push around everything with an ease.

It also makes the interaction between objects more realistic. Going back to the original boulder example, if I put a boulder as big as you in front of you and you start hitting it with pellets, then it won't move. However if you start throwing equally big boulders at it, it will fly away very easily (and I'll be scared of you).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .