Page 1 of 1

Newton and Irrlicht, slightly less complecated tutorials...

Posted: Thu Dec 15, 2005 8:51 am
by Guest
I was wondering, would it be possible for someone expierence with c++ programming to write a better understandable tutorial on integrating Newton with Irrlicht. Because I'm sorry but the ones they have now require so many models to be loaded and so many scene nodes to be set, and I can't understand it and it blows my mind. I have been coding c++ and messing with Irrlicht for 1-2 years, and I can't understand Newton with Irrlicht.

Thanks.

"im going crazy" :shock:

heh

Posted: Thu Dec 15, 2005 9:15 am
by Guest
i agree - a tut for newton (and ode) starting with the basics would be good for those less clued up.

Posted: Thu Dec 15, 2005 12:50 pm
by Guest
hmm, when you are coding for more than 1year with irrlicht this should be no problem - otherwise you should read the newton manual or general docs on physics in games. I checked that newton tutorial on the day I downloaded irrlicht and I understood everything.... (also remember that these are tutorials on how to combine both APIs.. it doesnt teach you how to use these APIs with all their features, so you should first learn both APIs and then read the tutorial on how to use both)

Posted: Thu Dec 15, 2005 1:16 pm
by Xaron
I will write such a basic irrlicht + newton tutorial if I find a little bit time for that. :)

Regards - Xaron

Posted: Thu Dec 15, 2005 7:09 pm
by pfo
Honestly, I thought about writing some better Newton tutorials from what I learned from the SDK examples, but I would just be re-inventing the wheel. If you sit down with them, you will learn, I promise you. It might take longer depending on your knowledge of programming, math and physics, but you will learn. If you look in the FAQ section of this forum, I posted how to do Newton drag and drop, it's almost identical to the source from the Newton example. So why make a tutorial out of that?

Re: Newton and Irrlicht, slightly less complecated tutorials

Posted: Thu Dec 15, 2005 9:48 pm
by MarcoTMP
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

Posted: Thu Dec 15, 2005 10:02 pm
by MikeR
I've seen this a lot in these examples

Code: Select all

NewtonToIrr
, but have no clue where it is comming from. What file has this in it?

Posted: Fri Dec 16, 2005 12:42 am
by dhenton9000
MikeR wrote:I've seen this a lot in these examples

Code: Select all

NewtonToIrr
, but have no clue where it is comming from. What file has this in it?

You have to supply it the value is 32. Kind of like the answer to the utlimate question of the universe, but - 10.

Posted: Fri Dec 16, 2005 5:10 am
by dhenton9000
Here is the code associated with my tutorial. See if this helps and let me know what questions you have, so I can put answers into the pdf file that I will write for it

http://s-fonline.com/webhosting/dhenton ... csteer.zip

Re: Newton and Irrlicht, slightly less complecated tutorials

Posted: Fri Dec 16, 2005 7:40 am
by Xaron
MarcoTMP wrote:in step 2.c. you add this line
NewtonBodySetForceAndTorqueCallback(myBody, setForceAndTorque);
Now newton will call your function setForceAndTorque each time you call NewtonUpdate().
Well this is a point which drived me crazy a while ago.

setForceAndTorque will not be called each time you call NewtonUpdate()! It gets only called if the related body is affected by some force, which saves a lot of time.

If you want to use this for things like player movement (ships etc.), ensure you add the following lines when you create your player newton body:

Code: Select all

NewtonBodySetAutoFreeze( body, 0 );
NewtonWorldUnfreezeBody( nWorld, body );
Now setForceAndTorque will be called each time you call NewtonUpdate(). ;)

Regards - Xaron