#include <iostream>
#include <irr/irrlicht.h>
#include <newton/newton.h>
using namespace std;
using namespace irr;
//Les fonctions
void addForce(const NewtonBody *nBody);
void setTransform(const NewtonBody* body, const float* matrix);
//Les variables generales
NewtonWorld *world = NewtonCreate(NULL,NULL);
scene::ISceneManager *scenegraph = NULL;
int fer = NewtonMaterialCreateGroupID(world);
class Boule
{
public:
void init(core::vector3df pos, core::vector3df posJoint)
{
//On crée un coprs qui gere la physique
body = NewtonCreateBody(world,NewtonCreateSphere(world,2,2,2,NULL));
//On ajoute une boule
node = scenegraph->addSphereSceneNode(2);
//On ajoute un fil a cette boule 
scene::ISceneNode *tube = scenegraph->addAnimatedMeshSceneNode(
scenegraph->getMesh("data/tube.3ds"), node);
//On regle les parametre des fils
tube->setPosition(core::vector3df(0,8,0));
tube->setScale(core::vector3df(5,10,5));
//On definis les proprietes du corps
NewtonBodySetMassMatrix(body, 100,0,0,0);
NewtonBodySetUserData(body, node);
//On diminue les frotements de l'air
NewtonBodySetLinearDamping(body, 0.001);
//On lui attibue un material qui est simlaire a celui du fer
NewtonBodySetMaterialGroupID(body, fer);
//On desactive le blocage automatique
NewtonBodySetAutoFreeze(body, 0);
//On cree une matrice qui recupere la position envoiyé au body.
//puis on la met dans les proprietes du body
core::matrix4 mat;
mat.setTranslation(pos);
NewtonBodySetMatrix(body, &mat[0]);
//Ici on s'occupe du Joint
pJoint[0] = posJoint.X;
pJoint[1] = posJoint.Y;
pJoint[2] = posJoint.Z;
//On cree une jointure ici un fil
joint = NewtonConstraintCreateBall(world, pJoint, body, NULL);
//On attribu les callback au body
NewtonBodySetForceAndTorqueCallback(body, addForce);
NewtonBodySetTransformCallback(body, setTransform);
}
void setMove()
{
float v[3] = {8,0,0};
//On ajoute une vitesse
NewtonBodySetVelocity(body, v);
}
private:
NewtonBody *body;
NewtonJoint *joint;
scene::ISceneNode *node;
float pJoint[3];
};
int main()
{
//Materail fer sur fer
NewtonMaterialSetDefaultElasticity (world, fer, fer, .97);
NewtonMaterialSetDefaultFriction (world, fer, fer, 0, 0.1);
IrrlichtDevice *device = createDevice(video::EDT_DIRECT3D9,
core::dimension2d<s32>(800,600), 32, false, true, false);
scenegraph = device->getSceneManager();
video::IVideoDriver *driver = device->getVideoDriver();
device->setWindowCaption(L"Boulier irr/newt");
//La camera
scene::ICameraSceneNode *camera = scenegraph->addCameraSceneNode();
camera->setPosition(core::vector3df(0,0,-40));
//La lumiere
scene::ILightSceneNode *lumiere = scenegraph->addLightSceneNode();
//Un petit skybox pour le style
scenegraph->addSkyBoxSceneNode(
driver->getTexture("data/irrlicht2_up.jpg"),
driver->getTexture("data/irrlicht2_dn.jpg"),
driver->getTexture("data/irrlicht2_lf.jpg"),
driver->getTexture("data/irrlicht2_rt.jpg"),
driver->getTexture("data/irrlicht2_ft.jpg"),
driver->getTexture("data/irrlicht2_bk.jpg"));
Boule boule;
boule.init(core::vector3df(0,-8,0), core::vector3df(0,10,0));
Boule boule1;
boule1.init(core::vector3df(-4,-8,0), core::vector3df(-4,10,0));
Boule boule2;
boule2.init(core::vector3df(-8,-8,0), core::vector3df(-8,10,0));
Boule boule3;
boule3.init(core::vector3df(4,-8,0), core::vector3df(4,10,0));
Boule boule4;
boule4.init(core::vector3df(8,-8,0), core::vector3df(8,10,0));
boule4.setMove();
//un Boulier (eu enfin le cadre)
scene::ISceneNode *node1 = scenegraph->addAnimatedMeshSceneNode(
scenegraph->getMesh("data/boulier2.3ds"));
//On l'ajute (a l'oeul
) node1->setScale(core::vector3df(8,8,8));
node1->setRotation(core::vector3df(0,90,0));
node1->setPosition(core::vector3df(0,0,0));
while(device->run())
{
driver->beginScene(true, true, video::SColor(128,128,128,128));
scenegraph->drawAll();
NewtonUpdate(world, 0.01);
driver->endScene();
}
device->drop();
return 0;
}
void addForce(const NewtonBody *nBody)
{
float mass, ix, iy, iz;
//On recupere la masse et l'inertie du corp
NewtonBodyGetMassMatrix(nBody, &mass, &ix, &iy, &iz);
//on cree une force tridimentionelle
float force[3] = {0, mass * -9.81,0};
//On les envoi au corps
NewtonBodyAddForce(nBody, force);
}
void setTransform(const NewtonBody* body, const float* matrix)
{
core::matrix4 mat;
mat.setM(matrix);
scene::ISceneNode *tmp = (scene::ISceneNode*)NewtonBodyGetUserData(body);
tmp->setPosition(mat.getTranslation()); // set position
tmp->setRotation(mat.getRotationDegrees()); // and rotation
}