Per-Entity Gravity

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Per-Entity Gravity

Post by mcmonkey »

Is it possible to have one entity's gravity differ from that of every other entity's gravity? Or should I disable gravity for this entity and apply an impulse every tick (seems questionably accurate to me)?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Per-Entity Gravity

Post by Norbo »

There is no per entity gravity 'built in', but you can indeed just do linearVelocity += acceleration * dt on each time step.

Be careful if you're using internal timestepping with Space.Update(dt); it can do more than one time step per call to Space.Update(dt). In that case, attach a callback on Space.ForceUpdater.Starting or something similar. Also, since setting entity.LinearVelocity automatically wakes up the entity, you may want to instead use ApplyLinearImpulse which does not force activation (but which would require scaling the acceleration by the object's mass) or modifying the source to give you direct access. If the amount of gravity changes, you'll want to wake the entity up so it can react.
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Per-Entity Gravity

Post by mcmonkey »

ApplyLinearImpulse which does not force activation (but which would require scaling the acceleration by the object's mass)
Uhm... it does?

I'm used ApplyImpulse for a few things (Rather than ApplyLinearImpulse for my own stupid reasons) and it definitely does not expect scaling by mass. I actually converted my code from Bullet to BEPU recently, and had "* GetMass()" in every impulse call... I had to delete them during the transition, or the forces would be way too powerful (my object's have very high masses because I have no idea what units they work best in. I assumed a character is 100 and worked from there.)

Is there a difference between the two methods, were you thinking of something else, or did I do something horribly wrong?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Per-Entity Gravity

Post by Norbo »

Uhm... it does?

I'm used ApplyImpulse for a few things (Rather than ApplyLinearImpulse for my own stupid reasons) and it definitely does not expect scaling by mass.
The meat of ApplyLinearImpulse is just:

Code: Select all

            linearVelocity.X += impulse.X * inverseMass;
            linearVelocity.Y += impulse.Y * inverseMass;
            linearVelocity.Z += impulse.Z * inverseMass;
ApplyImpulse and its variants use ApplyLinearImpulse and ApplyAngularImpulse internally.

So, if you want to instantly change the velocity by X, the impulse needed is X * Mass since the function turns around and immediately multiplies by inverse mass.

Note that ApplyImpulse and friends do not apply forces, they apply instantaneous changes in momentum. Forces tend to be implemented as impulses applied every time step, scaled by dt. If the dt value is missing, the 'force' will be too strong by a factor of 1/dt.
Post Reply