NewtonApplyForceAndTorque

From Newton Wiki
Jump to: navigation, search

NewtonApplyForceAndTorque

typedef void (*NewtonApplyForceAndTorque) (const NewtonBody* body, dFloat timestep, int threadIndex);

Usage

This is probably the most often used Newton callback. This callback is called once per frame, during NewtonUpdate call. It allow you to apply force and torque to a simulated body, to make physical shape move and interact dynamically, common use for this can be like simple gravity, to complex character controller or raycast car physics, this callback lets you tell newton what a object does physically in a simulated newton world.

You can assign it to body using NewtonBodySetForceAndTorqueCallback function.

Parameters

Remarks

  • This is not a library function, but a callback event.
  • The callback is not called for bodies that are static (Zero mass or Collision trees and heightmap collision shapes).
  • Changing from Newton 1.53, this callback is now also always called for sleeping / frozen bodies in newton 2.0, applying sufficient force in the callback will wake up the newton body back to normal state (see NewtonBodyGetSleepState NewtonBodyGetAutoSleep and NewtonBodySetAutoSleep).

Example

   float force[3], torque[3];
   
   void _cdecl callbackForce(const NewtonBody* body)
   {
       NewtonBodyAddForce( body, force ); 
       NewtonBodyAddTorque( body, torque );
   }

Simple Earth Gravity example:

void _cdecl callbackSimpleGravity(const NewtonBody* body)
{
	float force[3];
	float mass;
	float inertia[3];

	NewtonBodyGetMassMatrix( body, &mass, &inertia[0] );

	float force[0] = 0.0f;
	float force[1] = -9.8f * mass;
	float force[2] = 0.0f;

	NewtonBodyAddForce( body, &force[0] );
}

A more 'object-oriented' example:

   void _cdecl callbackForce(const NewtonBody* body)
   {
       CSceneMoveableObject *obj = ((CSceneMoveableObject*)NewtonBodyGetUserData(body));
   
       NewtonBodyAddForce( body, obj->get_toApply_Force() );
       NewtonBodyAddTorque( body, obj->get_toApply_Torque() );
   
       obj->ClearForceTorque();
   }


See also

NewtonBodySetForceAndTorqueCallback NewtonUpdate NewtonBodyAddForce NewtonBodyAddTorque NewtonBodySetForce NewtonBodySetTorque