Page 1 of 1

Dynamically changing the mass

Posted: Sat Feb 05, 2011 7:04 am
by Shevy
So in my simulation I want to dynamically change the mass depending on a the situation.
However using Entity.Mass does not set the mass. The most it does is make it kinematic if the value entered is between the boundaries specified.

The only way I know how to is by using the functions BecomeKinematic() and then BecomeDynamic(mass) right afterwards... which resets it back to 0,0,0 position.
This is highly annoying and would rather be able to avoid even if i reset the world Transform to the previous transform in the same.

For more info I have boxes that I want to move when the player pushes them upon pressing a certain key. Otherwise they do not move no matter what. Also I would like them to fall if they are pushed off a cliff for example.

So essentially the idea I have right now is to make them dynamic with a really high mass, and then onInteraction with the player they reduce their mass, move in a 1:1 ratio with the players velocity, and then go back to their old mass when released.
This also makes it so hitting other boxes does not force them to move because their masses are equal.

EDIT: I am sorry for this post I just realized that I am able to do it with primitive Entity types, but not a compoundBody.
I was decompositing a mesh into separate convexHulls and putting them into a compoundBody, even if the object just had one mesh/convexMesh.
but I can probably find a work around this.

EDIT 2: sorry for all the edits but here is my real problem now....

Code: Select all

// for debugging purposes I know this is Compound and I only want the first body because only one exists
 BEPUphysics.Entities.Entity temp = ((BEPUphysics.Entities.CompoundBody)parent.PhysicsBody).SubBodies[0];
 if (temp != null)
 {
      // the code reaches here without breaking but then breaks on this command
      // giving me a system.ArgumentOutOfRange error
      // temp is of type ConvexHull
      temp.Mass = 25;
 }
So the question is why a system argument out of range error?

additional info: it looks to be a typical argument exception, happens in mscorlib.dll
says index can not = -1 or greater than size and of the collection.

also temp.Mass = -1 gives me the error that it can not make an entity in a compound body kinematic.
but instead use the functions at hand on the body. Which makes sense lol :P

Re: Dynamically changing the mass

Posted: Sat Feb 05, 2011 7:44 am
by Norbo
So the question is why a system argument out of range error?
My guess is that it's related to calling BecomeDynamic on a child entity, which should really be disallowed (as you noticed with setting the mass to -1, which protected it from having a kinematic child). The physical properties of the child entity are not used by the parent entity outside of the initialization steps. The inertia tensor of the parent would have to be forced to do a recalculation after updating the child's mass properties.

There's a lot of interweaving complexities going on there that would better be left alone by assuming that calling either BecomeDynamic or BecomeKinematic on a child entity was illegal. Performing that action should throw an exception (the fact that it doesn't is basically a bug), however v0.15.0 completely designs away the ability for such a problem to occur. Entities themselves will no longer be nested in compound bodies, but rather shapes will be nested within compound shapes which are in turn used by entities.

An alternative to using mass to 'lock' shapes is using SingleEntity(Linear/Angular)Motors. Whenever you don't want an object to be affected by interactions, activate a linear and angular motor in servo mode which target the entity's 'frozen' state (whatever position and orientation it had at the time of freezing). When the player chooses to interact, the motors would let go. This approach has the advantage of having consistent entity properties throughout the simulation and a proper, robust physical solution to locking.

Re: Dynamically changing the mass

Posted: Sat Feb 05, 2011 8:05 am
by Shevy
Thank you for the quick response, and again I am very impressed with the amount of features and flexibility this engine has. (More than I can handle it seems :P)
In the mean time I am going to try to implement the Motor constraint approach. Rather than debug and mess with masses.
however v0.15.0 completely designs away the ability for such a problem to occur
I keep hearing good things about 0.15.0. Can't wait for it to come out :)

Thank you again,
Shevy