Changing object mass?

So, I am trying to create a ball throwing game however I can’t work out how to change the mass.

I want to be able to specify a mass, and input the required density to do so. (lets say it’s a 10kg tennis ball, therefore density is…). To do this, I need the area!

[lua]

local ball = display.newCircle(playerhero.x, playerhero.y, 5)

physics.addBody(ball, “dynamic”, {density = 1, bounce=0.1, friction=1})

ball:setFillColor(0,1,0)

ball.alpha = 1

local ballarea = (math.pi*(5/30)^2) --note pi r ^2    the r is /30 to relate this back to metres due to the scale.

print(“ball area =”…ballarea)

print(“ball mass =”…ball.mass)

[/lua]

I thought the calc for mass was density*area (according to corona docs), but when I calc the area myself I get the following:

ball area =0.087266462599716

ball mass =0.11111111938953

As density is 1, I would expect these to be identical. I can’t manually change mass, so I was planning to calculate required density.  

You would need to remove and re-add the physics body with a new density.

Rob

Thanks Rob, that was my second option.

How is mass calculated then out of curiosity? (How is the body area calculated?)

Secondly, what are the units of mass in Corona :slight_smile: ?I assume as the scale is metres and gravity is m.s^-2 that mass will be kg?

According to our docs:  https://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html

“density is multiplied by the area of the body’s shape to determine its mass.”

The area of a rectangle would be width * height. Circle are math.PI * radius ^ 2. (If I’m remembering my geometry correctly. For irregular shapes, I’m sure there are other formulas to compute them.

Rob

you don’t specify a radius for your body, so your circle (in the display) is really a rectangle (in physics)

turn on hybrid draw mode and this should be evident if true

so your area is (10/30)^2 = 0.111111  (and thus so is your mass, since density = 1.0)

Thanks Dave that clears it up. I had assumed that as the radius was specified against the display object which the body is derived from it would have used that.

Even though I had got it working, this logic was really bugging me :slight_smile: !

You would need to remove and re-add the physics body with a new density.

Rob

Thanks Rob, that was my second option.

How is mass calculated then out of curiosity? (How is the body area calculated?)

Secondly, what are the units of mass in Corona :slight_smile: ?I assume as the scale is metres and gravity is m.s^-2 that mass will be kg?

According to our docs:  https://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html

“density is multiplied by the area of the body’s shape to determine its mass.”

The area of a rectangle would be width * height. Circle are math.PI * radius ^ 2. (If I’m remembering my geometry correctly. For irregular shapes, I’m sure there are other formulas to compute them.

Rob

you don’t specify a radius for your body, so your circle (in the display) is really a rectangle (in physics)

turn on hybrid draw mode and this should be evident if true

so your area is (10/30)^2 = 0.111111  (and thus so is your mass, since density = 1.0)

Thanks Dave that clears it up. I had assumed that as the radius was specified against the display object which the body is derived from it would have used that.

Even though I had got it working, this logic was really bugging me :slight_smile: !