# Calculating density of an object

I'm using Box2D for physics in my game, and one of the things it needs to know when creating a shape for a physics body is the shape's density. I had no idea what value I should specify, so I went to the header file where it's defined, and it says:

The density, usually in kg/m^2.

I took that to mean that you could find the density for an object by dividing its weight by its size in metres squared. If I do that for a square crate, which I want to weigh 20 kilograms and have a width of 0.72 metres:

float32 weightInKg = 20;
float32 metresSquared = 0.72 * 0.72;
float32 density = weightInKg / metresSquared;


The density is 38.5802. Is this correct? It also results in a computed mass of 20, which is correct, as the Box2D documentation says the mass is in kg. I've got gravity set to zero if that matters, as the game is top-down.

If I do a similar calculation for the character, a human whose shape is a circle with a radius of 0.5 metres:

float32 weightInKg = 80;
float32 metresSquared = pi * (0.5 * 0.5);
float32 density = weightInKg / metresSquared;


The density is 101.859. Again, this doesn't seem correct. I've read that a density of 1.0 makes sense for a human in the context of Box2D, so if that were true, I'm just slightly over. I've also read that the average density of the human body is 985 kg/m3, but I'm not even sure if my density calculation is correct, so I'm not sure how that figure translates to kg/m2.

Your density calculation is correct. Either the weight of the human is too much, or you have got to make the human bigger. In real life your human would have more volume or less mass. Which one to select is entirely up to you. And since this is top-down, you can expect to get the wrong density of the human with that formula. If it was a side view game, it would still be alright.

• More volume I can understand, because I'm only using two dimensions, with the third one (height) being the largest in this case. I don't understand why they'd need less mass though; the average weight of a male is ~80 kg. Oh wait... I guess having less mass would be a result of having more volume, right? What would you do? Less mass? – Mitch May 3 '15 at 9:36
• I think less mass makes sense, seeing as I want the sizes to still be realistic. Is there some specific formula I should use for that, or just do something like float32 weightInKg = weight * 0.5? – Mitch May 3 '15 at 9:37
• I don't know about that, but you can try doing this. You know the density of humans will be around 985 kg/m3, your mass would be density * volume (or in this case area). So density of humans 98.5 (since 1 dimension missing), multiply that with your area, and you get a figure of around 77.3 kg. Which is pretty close to what you had before. – EvilTak May 3 '15 at 16:45
• Thanks, this seems to work. :) Can you explain why that forum post that I linked to said a density of 1.0 makes sense for a human? I'm still a bit lost with that. – Mitch May 9 '15 at 8:54
• That person I think was saying that the density of water is 1.0 (in CGS units), and humans are mostly made of water, which is why a value close to 1 will be ideal for humans. It is true, although humans are a little lighter than water (like you stated). I don't use Box2D so I don't really have an idea of what the density of water would be, but I guess it would be around 100 units (hence the previous calculation for mass). – EvilTak May 9 '15 at 15:43