For starters, I have used newton before with many graphics sdks. So I know how it works, and how to implement it. I was implementing it with the gdk for a bit, but moved on to the Ogre SDK for various reasons. Now, I assume you have gotten the Newton world set up? If not, here is a basic code to get that setup :
NewtonWorld* nWorld = NewtonCreate(NULL, NULL);
Now, when you have newton set up, yes, I know, that is one line of code, unless you want to set the default material parameters, you should define two callbacks. Newton uses a torque and force callback and a transform callback. The transform callback is used to position and rotate an object you made. Now, on track with the development. You will want to create an object I assume. Now, one main problem I found with newton, is the lack of a Vector3 class or a Matrix4 class to use. So you will have to define your own! How lucky... Anyhow, I may just post the code for my own dMatrix and dVector classes so you can jumpstart easier. But back to creation of rigid bodies. It doesn't honestly take that much to create a rigid body. Here is a simple way of creating one :
dbMakeObjectBox(id, sizeX, sizeY, sizeZ);
NewtonCollision* nCol = NewtonCreateBox(nWorld, sizeX, sizeY, sizeZ, NULL);
NewtonBody* nBod = NewtonCreateBody(nWorld, nCol);
NewtonBodySetMassMatrix(nBod, mass, 150.0f, 150.0f, 150.0f);
NewtonBodySetUserData(nBod, (void*)id);
That will create a box to a size you define. Simply replace the sizeX, sizeY, and sizeZ with float data types( as in real numbers ). You must set the userdata to the objects ID for use in the callbacks. Now, with all that done, you have to define callbacks for you to manipulate your object. The first callback I will tell you about is the applyForceAndTorque callback. You can really call it anything, but here is an example of what you would do:
void applyForceAndTorqueCallback(NewtonBody* body)
{
float force[3];
float torque[3];
force[0] = 0.0f; // force on the X axis.
force[1] = nGravity; // define nGravity. Its the gravity variable...
force[2] = 0.0f; // force on the Z axis.
torque[0] = 0.0f;
torque[1] = 0.0f;
torque[2] = 0.0f;
NewtonBodyAddForce(body, force);
NewtonBodyAddTorque(body, torque);
}
What that does is define a callback which sets no torque, and sets the force to only act upon the Y axis, to simulate gravity. Then it applies the force and torque to the rigid body passed to it by the simulation. Register this function to the rigid body with :
NewtonBodySetForceAndTorqueCallback(nBody, applyForceAndTorqueCallback);
That simply tells newton to call that every loop to check for a force or torque to apply to the rigid body. Pretty basic concept.
Now, to the slightly more advanced callback, the setTransformCallback. Here is an example of it:
void setTransformCallback(NewtonBody* body, const float* matrix)
{
dMatrix mat; // custom defined class. I'll post it for you.
memcpy(mat.M, matrix, sizeof(float)*16);
int objID = (int)NewtonBodyGetUserData(body);
dbPositionObjectX(objID, mat.getTranslation().m_x);
dbPositionObjectY(objID, mat.getTranslation().m_y);
dbPositionObjectZ(objID, mat.getTranslation().m_z);
dbRotateObjectX(objID, mat.getRotationDegrees().m_x);
dbRotateObjectY(objID, mat.getRotationDegrees().m_y);
dbRotateObjectZ(objID, mat.getRotationDegrees().m_z);
}
That basically makes an instance of my custom matrix class, copies the matrix handed over by the function to my matrix instance, then gets the objects ID from the rigid body(so you know what object you are talking about), then positions and rotates based on the matrix(which the .m_x and so on is calling my vector class, which I will also upload for you). Now, you register that callback in the same way.
NewtonBodySetTransformCallback(nBody, setTransformCallback);
Now, that should be all you need to do with objects. Now on to updating newton. Very simple. In your main loop, simply call this:
NewtonUpdate(nWorld, 0.01f);
Of course, you need to set a pointer to your NewtonWorld instance. And there you have it, that implementation
should work. If there are problems, just ask, I am fairly skilled with newton. Notice I said fairly. I've attached a *.rar file with the dMatrix and dVector classes used in the code above. Oh, and a math file needed for the dMatrix class.
WC Physics - PhysX wrapper for Dark GDK.