Tutorial - Callbacks (2)

From Newton Wiki
Revision as of 08:02, 10 June 2019 by WikiSysop (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Template:Languages

Newton's Third Law of Motion states
For every action there is an equal and opposite reaction.

Lets break this down to what it really means for you and your program:

At this point, we are assuming you have successfully created the Newton World and your Rigid Body(s).


Step 1 of 2: Making your object move

Applying Force and Torque to an object

For every action... equates to a function you write and assign via the NewtonBodySetForceAndTorqueCallback().

Somewhere in your code, you must store the current amount of Force (throttle setting, thrust, whatever) and Torque (yaw, pitch, roll, or whatever) you want to apply to your object and in what direction. Remember that you can apply force and torque in X, Y, Z and -X, -Y, -Z. During your programs loop you must be calling NewtonUpdate() to update the Newton world. One of the first functions Newton calls during your call to NewtonUpdate() is the function you created and assigned via the NewtonBodySetForceAndTorqueCallback(). It's this function that takes your force and torque values that you want to apply to your object and transfer them into Newton. In this function, you translate (convert) your data from whatever format you are storing your data in to a format that is acceptable to Newton and then transfer this information into Newton via:

For FORCE, use NewtonBodySetForce() or NewtonBodyAddForce()

For TORQUE, use NewtonBodySetTorque() or NewtonBodyAddTorque()

  • add example source code here

Once your function finishes assigning the Force and Torque values to Newton, Newton processes this information and gives you the reaction....


Step 2 of 2: Making your object move

Applying Transformation to an object

...there is an equal and opposite reaction. equates to a function you write and assign via the NewtonBodySetTransformCallback().

Near the end of your call to NewtonUpdate() and after Newton has taken all your force and torque values, applied them, calculated any collisions, etc, etc, etc Netwon calls the function you created and assigned via the NewtonBodySetTransformCallback(). It is through this function that Newton sends you the results (i.e. the reaction). During this call, you take the values passed to your function via:

const float* matrix

and Translate (convert, transform) them to a format suitable to your needs and assign them appropriately to your graphics engine (or whatever).

FYI: You may need access to your own object and its data in these callbacks. Use the NewtonBodySetUserData() when you create your collision object to store a pointer to your data and then use the NewtonBodyGetUserData() in the callbacks to get access to your object and its data.

// apply force and torque callback (called when the body is on the active body list)
static void newtonApplyForceAndTorque(const NewtonBody *_body) {
    NewtonBodyAddForce(_body, force); // force is an array (in XYZ order) containing the force to be applied to the body
}
// transformation callback (called after the body has been simulated)
void newtonApplyTransformation(const NewtonBody *_body, const dFloat *_matrix) {
    mesh = NewtonBodyGetUserData(_body); // get user data attached to the body
    mesh.transform(_matrix); // transform mesh according to the matrix
}
int main() {
    ...
    NewtonBodySetForceAndTorqueCallback(body, newtonApplyForceAndTorque); // register the apply force and torque callback
    NewtonBodySetTransformCallback(body, newtonApplyTransformation); // register the transformation callback
    ...
}

IT DIDN'T MOVE!!

It didn't huh? Well, how much mass did you give your object? How much force or torque did you give it? Let me explain, if your object has a mass of 400,000 units and you only apply 1 (one) unit of force, nothing is going to happen. Newton does not translate the units of force and mass. You are responsible for doing this. So, if your object weighs 400,000 units (grams, ounces, kilo, tons, whatever) then you must apply enough force to move it. Try making the force equal to the mass to start with, double it if necessary.

Also, if you want to move your body according to keypress for example, then the apply force and torque callback must be called when the user presses the key.In order to do this, you'll need to unfreeze the body and add it into the active body list by calling NewtonWorldUnfreezeBody():

NewtonWorldUnfreezeBody(newtonWorld, bodyToBeSimulated);

and to freeze the body again use NewtonWorldFreezeBody()