From Newton Wiki
Jump to: navigation, search

In Newton, materials allow the user to define how bodies will interact with each other. Specifically, materials affect Newton's builtin collision response code.

You pass Newton three functions in NewtonMaterialSetCollisionCallback. Newton calls your functions to handle collisions between objects of the specified two materials. For example, here's how I'd tell Newton about my functions StartContact, MidContact, and EndContact, which I want to use to handle collisions between objects of the default material type:

 int matID = NewtonMaterialGetDefaultGroupID(world);
 NewtonMaterialSetCollisionCallback(world, /* here's how to handle collisions... */
     matID,matID, /* between objects of these two materials (both default here) */
     &myContactInfo, /* a class I use inside StartContact, MidContact, EndContact */

Julio wrote [edited]:

You create materials in newton by asking them from the engine. When you start the default material is already created for you. It's common to create only a few additional materials, because you have to write code to handle collisions between any pair of materials.

When you call the funtion NewtonCreateMaterial, Newton adds a node into the Materials data (an undirected graph), and connects it to all the other materials. Then it returns the node ID to the caller. This ID in what you asign to the rigid body using the function NewtonBodySetMaterialGroupID and that does it.

When you create a complex set of materials you need to set the material pair properties by assigning them individally using the MaterialPair interface functions. One of those properties is the set of functions call back for a particular material pair.

The trick is to configure your materials and material pair relations right after you initialize the engine, and keep the Material ID's handy from when you created the rigid bodies.

See the tutorial using materials to see how to do it.

<Insert more stuff on Materials here...>

Here is an example of a material pair handling strategy from the forums: [1]

If you want to do collision the way games handle it traditionally, that is possible too, do the following:

  • Store the rigid body with the graphics companion class
  • Add a funtion to the call like
  • OnCollision (MyObject* otherObf, other param);
  • Do not create any materials, just used the default
  • Using the material pair interface set the default material callbacks
  • From inside the CallbackBegin do this:
MyBody *bodyA = NewtonGetUserData(body0)
MyBody *bodyB = NewtonGetUserData(body1)

Then in Process contacts do the following:

bodyA->OnCollision(bodyB, oher data)
bodyB->OnCollision(bodyA, oher data)

That should do it, but you will find that you will have to add lot of logic to filter what kind of object you are colliding with, making the callback very slow. That is what the Materials graph system does for you.