Calculate total force vector acting through body centre

Hi,

I was hoping for a GetForce method in Box2D to give the total force acting through the centre of a body at any point in time. I need to remove a portion of the force each time step to try and remove some sideways movement.

How do you calculate this overall force vector? There is no GetForce method, just GetEverythingApartFromTheOneItemYouWant type of methods 😉

I thought if I could GetAcceleration and GetMass I could perhaps work it out, but there is only GetxxVelocity?


Regards

i
Getting the acceleration is easy enough if you keep a short graph of an object's movement. I guess box2D doesn't keep methods like that because it's not relevant to the physics simulation. b2Body has a GetMass() method already. Atleast in Box2D v2.1.

You could also use the contact listener and register all contact impulses to a vector. Other forces, except gravity, are added by you, so you should be able to make something that registers them too. If you haven't already, make a class that you attach to all b2Body::UserData*. This class could contain it's own ApplyForce, that registers the force and then sends it along to the body it owns. After that you should only use the top level class you made to apply forces.

Then there's the third alternative of changing Box2D source and add your GetForces() to b2Body itself.

Good luck!
Hi,

Thanks. The force acting on a body at any point in time must be part of the physics simulation mustn't it? If 2 objects collide I assume the 2 force vectors are needed to calculate the reactant forces.

I am doing a top down car simulation (no gravity) where I am applying a force to the front wheels and need to remove the lateral force to keep the motion in the direction of the front wheels (rather than setting linear velocity manually which I believe is a bad thing?).

In terms of contact impulses, not sure what you mean here? I am at the moment just trying to model the cars movement and so contacts are not being made with other bodies? Or have I misunderstood what a contact impulse is?

Regards


i
The engine doesn't automatically sum forces and store them on the body. For example, contact forces are stored as impulses in the b2Contact class. So you will have to add forces manually.

Are you applying any forces? If so, capture those.
Are there any contact impulses? If so, capture those and divide by the time step.
Is gravity active? If so, multiply by the mass to get the force.

Now add up the forces.
Thanks,

Although never used contact impulses yet, do you mean retrieve all the force vectors from the contact impulses collection, and add each x, y component to get a resultant force for all contacts? Also, at which step do you do this calculation, after each step, or PreSolve etc?

I was hoping to just extract _mforce somehow:

Code:

inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point)
{
   if (m_type != b2_dynamicBody)
   {
      return;
   }

   if (IsAwake() == false)
   {
      SetAwake(true);
   }

   m_force += force;
   m_torque += b2Cross(point - m_sweep.c, force);
}

Original thread: https://box2d.org/forum/viewtopic.php?t=5350