#include <Irrlicht.h>
#include <newton.h>
// use scale factor between Newton and IRR
const float NewtonToIrr = 32.0f;
const float IrrToNewton = (1.0f / NewtonToIrr);
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
int main()
{
//create Newton world
NewtonWorld* nWorld = NewtonCreate ();
// set Irrlicht
IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(1024, 768), 32,false, false, false);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
// add mesh
scene::IMeshSceneNode* beat = smgr->addSphereSceneNode(10,64);
if (beat)
{
beat->setMaterialFlag(video::EMF_LIGHTING, false);
}
core::aabbox3d<f32> box = beat->getBoundingBox();
core::vector3df size = box.getExtent() * IrrToNewton;
NewtonCollision* collision = NewtonCreateBox(nWorld, size.X, size.Y, size.Z, NULL, NULL);
static NewtonBody* beat_Body = NewtonCreateBody(nWorld, collision);
NewtonReleaseCollision (nWorld, collision);
// set the body mass and inertia
NewtonBodySetMassMatrix (beat_Body, 1.0f, 5.0f, 1.0f, 5.0f);
//NewtonBodySetUserData(beat_Body, beat);
//NewtonBodySetDestructorCallback (beat_Body, DestroyPistonEvent);
core::matrix4 matrix = beat->getRelativeTransformation();
core::vector3df origin = matrix.getTranslation() * IrrToNewton;
matrix.setTranslation (origin);
NewtonBodySetMatrix(beat_Body, &matrix.M[0]);
// animate the body by setting the angular velocity
float omega[] = {0.0f, 10.0f, 0.0f};
NewtonBodySetOmega (beat_Body, &omega[0]);
// add camera & hide cursor
smgr->addCameraSceneNode(0, core::vector3df(3,0,3), core::vector3df(0,0,0));
device->getCursorControl()->setVisible(false);
// count FPS
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,100,101,140));
//update the physics at the same rate as the render
float fps = (float) driver->getFPS();
if (fps > 0.0f)
NewtonUpdate(nWorld, 1.0f / fps);
// rotate node
core::matrix4 mat;
NewtonBodyGetMatrix(beat_Body, &mat.M[0]);
//Set node position
beat->setPosition(mat.getTranslation() * NewtonToIrr);
// set node rotation
core::vector3df euler;
NewtonGetEulerAngle ( &mat.M[0], &euler.X);
beat->setRotation(euler * (180.0f / 3.1416f));
smgr->drawAll();
driver->endScene();
int fps_int = driver->getFPS();
if (lastFPS != fps_int)
{
core::stringw str = L"Irrlicht Engine - Quake 3 Map example [";
str += driver->getName();
str += "] FPS:"; str += fps_int;
device->setWindowCaption(str.c_str());
lastFPS = fps_int;
}
}
// instead call
NewtonDestroyAllBodies (nWorld);
// finish newton & irrlicht
NewtonDestroy(nWorld);
device->drop();
return 0;
}