views:

639

answers:

3

I'm using Box2D (the AS3/Flash version, if it matters) and I'm trying to calculate how much weight each body is carrying.

I know how to iterate through all the bodies, and I know how to poll the Collion Detection routines, but it seems the collision forces die off to zero once weight is applied. Is there some sort of "total static force" property I'm overlooking?

A: 

I think all you need to do is loop through your b2Body instances and use the GetMass() method.

I'm not sure what you mean by "total static force".

As for weight/mass I think you apply it before any collisions, when you create the body, either set it yourself using setMass(); or have box2d estimate a mass based on the shape, using the SetMassFromShapes() method.

hth

George Profenza
I'm not looking for the weight/mass of a particular body - but the total weight the body is carrying. ie: 10 boxes stacked on top of each other, how much "load" is on the bottom box?
Andy Moore
shouldn't that be the sum of the masses of all the boxes above that bottom 1 ? var totalMass:Number = 0;for(var i:int = 0 ; i < stackedBodiesNum ; i++){totalMass += stackedBodies[i].GetMass();}trace(totalMass);I think it's something simple, but I'm not getting it, am I ?
George Profenza
Ah well the rub is that there's no way of knowing which box is stacked where - indeed, the screen is just going to be a random jumble of boxes. There's no convenient stackedBodies array :)
Andy Moore
A: 

It has been awhile since I have played with Box2D but have you tried polling GetReactionForce() on the things of interest?

I know that when I made a bridge simulation and I wanted the bridge to break based off the weight on it, I polled GetReactionForce() and if it was above a certain threshold then I deleted the joint connecting the two bodies.

Simucal
GetReactionForce would probably do the trick if I was using joints; unfortunately I am just using individual shapes. :(
Andy Moore
+1  A: 

Using the contactResult type in the optional contactListener object, you can poll the normalForce of each item to find out what forces (which intrinsically includes weight) are being applied to objects.

Note that sleeping bodies (a resting stack for example) will not trigger contactResults, but it should work fine if you turn off sleeping or if you want to measure forces only while motion is happening.

For info on how to implement the contactListener class check up on the documentation.

Andy Moore