This is the easiest way i think, I hope it help.
The first thing you need is to understand the way physic engine work.
In Newton yow can create:
NewtonWorld: Is the place where you are goin to place physic bodies and where everything will happend.
NewtonBody: Are objects with mass, that can interact with other NewtonBody-es. They have position, rotation and a shape.
NewtonCollision: Is the shape of a NewtonBody. In Newton you create a NewtonCollision, and then assign it to a NewtonBody.
NewtonJoint: They are not physic objects, they just let you specify the way objects are joined.
Vehicles, Ragdolls, etc. are complex types of Joints.
Undestanding Newton Algorithm
Algorithm #1 -Very easy-
Code: Select all
1. Create NewtonWorld
2. Create objects
a. Define NewtonCollision
b. Create NewtonBody
c. Set Body Properties
- You have to repeat step 2 for each body you are creating
3. Update Simulation
4. Destroy NewtonWorld
So, let start it.
Code: Select all
// 1. Create NewtonWorld
NewtonWorld nWorld = NewtonCreate(NULL, NULL);
// 2. Create Objects
//a. Define NewtonCollision
NewtonCollision* myShape = NewtonCreateBox(nWorld, 10,10,10, NULL);
//b. Create NewtonBody
NewtonBody* myBody = NewtonCreateBody(nWorld, myShape);
//c. Set Body Properties
NewtonBodySetMassMatrix(myBody, 1.0f, 1.0f, 1.0f, 1.0f);
// 3. Update Simulation
// inside your while{} loop
while (Looping==true)
{
NewtonUpdate(nWorld, 0.01);
}
// 4. Destroy World
NewtonDestroy(nWorld);
At this point, you have Newton working well. Objects exist, but they will not get transformed (Moved, rotated, etc).
PART II
A.
To get objects moving you have to tell newton you want to apply some forces to bodies.
Code: Select all
static void setForceAndTorque(const NewtonBody* body)
{
float mass;
float Ixx;
float Iyy;
float Izz;
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
core::vector3df force(0, -mass * 9.8, 0.0);
core::vector3df torque(0, 0, 0);
NewtonBodyAddForce(body, &force.X);
NewtonBodyAddTorque(body, &torque.X);
}
in step 2.c. you add this line
NewtonBodySetForceAndTorqueCallback(myBody, setForceAndTorque);
Now newton will call your function setForceAndTorque each time body is affected by forces.
B. NEWTON - IRRLICHT Integration.
Let think you have created a graphical object like this
IAnimatedMeshSceneNode* mySceneNode = smgr->addAnimatedMeshSceneNode(Mesh, ...);
To link the physic object with your graphical object you need two things.
First: Add this line in step 2.c.
NewtonBodySetUserData(myBody, (void*) mySceneNode); //with this you are sending your graphic pointer to Newton.
Second:
To see this objects moving you need a Transform Callback Function.
Soon you will tell Newton "I need you call this function each simulation step"
Code: Select all
static void setTransform(const NewtonBody* body, const float* matrix)
{
//get graphic object
ISceneNode* node = (ISceneNode*) NewtonBodyGetUserData(body);
//copy matrix to an irrlicht matrix
core::matrix4 mat;
memcpy(mat.M, matrix, sizeof(float)*16);
//set transformation
node->setPosition(mat.getTranslation() * NewtonToIrr); //Adjust scales, NewtonToIrr = 32. You can find why in the Newton forum.
}
Now is time to tell newton "I need you call this function each simulation step"
In step 2.c. add the line:
NewtonBodySetTransformCallback(myBody, setTransform);
have fun!
Then i could try to talk about materials, joints and others.
I edited this line: Now newton will call your function setForceAndTorque each time body is affected by forces. Thanks Xaron