Tutorial - Irrlicht and Newton start

From Newton Wiki
Revision as of 08:02, 10 June 2019 by WikiSysop (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The barebone code of integrating Newton with Irrlicht , roughly resembles tutorials one using Irrlicht for rendering.

#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;


#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "Newton.lib")

int main()
{
   //create Newton world
   static NewtonWorld* nWorld = NewtonCreate (NULL, NULL);
   // set Irrlicht
   IrrlichtDevice *device = createDevice(video::EDT_SOFTWARE,
	                           core::dimension2d<s32>(800,600), false);

   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

   	// add mesh   
   scene::IAnimatedMesh* mesh = smgr->getMesh("D:\\irrlicht-1.1\\media\\earth.x");
   scene::IAnimatedMeshSceneNode* beat = smgr->addAnimatedMeshSceneNode( mesh );
    if (beat)
	{	
		beat->setMaterialFlag(video::EMF_LIGHTING, false);	
		beat->setMaterialTexture( 0, driver->getTexture("D:\\irrlicht-1.1\\media\\earth.bmp") );
	}

	core::aabbox3d<f32> box = beat->getBoundingBox();
	core::vector3df size = box.getExtent() * IrrToNewton; 
	NewtonCollision* collision = NewtonCreateBox(nWorld, size.X, size.Y, size.Z, 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;
}