Calculating Density

I’m working on creating a module to give the realistic density but it’s definitely gone wrong somewhere.
This is the part where I calculate it.

	local partSize = part.Size
	local studsInAMeter = 3.57 --1 stud = 0.28 meters
	local volume = (partSize.X/studsInAMeter) * (partSize.Y/studsInAMeter) * (partSize.Z/studsInAMeter)
	local materialMass = self.MaterialMassesPerCubicMeterInKG[tostring(part.Material)]
	local mass = (materialMass * (partSize.X/studsInAMeter)) + (materialMass * (partSize.Y/studsInAMeter)) + (materialMass * (partSize.Z/studsInAMeter))

I get the volume by dividing the X,Y and Z by how many studs are in a meter and then multiplying all those together. I then get it’s material weight per cubic meter.
Once I have the weight I multiply it by each X,Y and Z and then add them all together.

self.MaterialMassesPerCubicMeterInKG is shown here:

	MaterialMassesPerCubicMeterInKG = {
		["Enum.Material.SmoothPlastic"] = 960,
		["Enum.Material.Glass"] = 615,
		["Enum.Material.Wood"] = 730,
		["Enum.Material.WoodPlanks"] = 730,
		["Enum.Material.Grass"] = 400,
		["Enum.Material.Brick"] = 1992,
		["Enum.Material.Concrete"] = 2400,
	}	

Unfortunately, this is producing strange results including bigger sized blocks getting smaller densities.
Anyone got any ideas?

2 Likes

Can you test these for a few blocks and post the sizes and output values?

Why don’t you use part:GetMass() / studsInAMeter ?

he wants a realistic mass as the density

If he wants to implement his own weight calculations he can use:

local realMass = part:GetMass() / PhysicalProperties.new(part.Material).Density / studsInAMeter * MaterialMassesPerCubicMeterInKG[part.Material.Name]

2 Likes

Is there a difference between what you’re trying to do with certain calculations and what the engine already provides as far as getting the density of a material goes?

local materialDensity = PhysicalProperties.new(Enum.Material.Grass).Density

I figure that getting density with long equations may not provide you with intended results such as being realistic for real world physics but not so much for Roblox.

1 Like

There’s a missing left parenthesis in that code after .new, by the way.

Would there be a way to create a “custom” properties object like the one in your code for every part?

Fixed, there’s not supposed to be a parenthesis there at all. I also used the wrong datatype, there’s no Custom in the constructor.

As for creating physical properties, yes you can. The point of the PhysicalProperties constructor is to allow you to, as the class name states, create custom physical properties to apply to a BasePart.

@PlaceRebuilder Thanks! It seems to be getting a correct amount now, problematic though because of the cap of 100.


@colbert2677 I’m trying to apply my own realistic densities, the platform’s default material density values are far off from what I’d like.

2 Likes