Tutorial - Irrlicht 1.7.2 & Newton 1.5.3 Tutorial

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

This is my first tutorial on how to integrate Newton 1.5.3 with Irrlicht 1.7.2. This is a modification of Hacim's tutorial.

// Newton 1.5.3 integration into Irrlicht 1.7.2
// by Reza (newleon@gmail.com)
// May the 9th, 2011
// Modified version of a tutorial by Hacim (http://gpwiki.org/index.php/Irrlicht:Physics)

#include <Irrlicht.h>
#include "newton.h"
#include <iostream>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

static NewtonWorld* nWorld;
static NewtonBody* body;

IVideoDriver* driver = 0;
IrrlichtDevice *device = 0;
ISceneManager* smgr = 0;
 
ISceneNode *boxNode = 0;
ISceneNode *cam = 0;
unsigned int lasttick;

void InitScene()
{
	device = createDevice(EDT_OPENGL, dimension2d<u32>(800, 600), false);

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	nWorld = NewtonCreate (NULL, NULL);

	IMesh* boxMesh = smgr->getMesh("data/smallcube.3ds");
	boxNode = smgr->addMeshSceneNode(boxMesh);
	boxNode->setMaterialTexture(0, driver->getTexture("data/crate.jpg"));
	boxNode->setMaterialFlag(EMF_LIGHTING, false);

	NewtonCollision *collision;
	collision = NewtonCreateBox(nWorld, 0, 0, 0, NULL);
	body = NewtonCreateBody (nWorld, collision);
	NewtonReleaseCollision (nWorld, collision);	

	NewtonBodySetUserData(body, boxNode);
	NewtonBodySetMassMatrix (body, 100.0f, 1.0f, 1.0f, 1.0f);
	
	matrix4 mat;
	mat.setTranslation(vector3df(0.0, 0.0, 0.0));
	NewtonBodySetMatrix(body, mat.pointer());

	// set angular velocity with respect to X, Y, Z
// old fashion
	//float omega[3] = {0.0f, 0.0f, 5.0f};
	//NewtonBodySetOmega (body, &omega[0]);
// irrlicht fashion
	vector3df omega = vector3df(1.0, 5.0, 0.0);
	NewtonBodySetOmega (body, &omega.X); //&omega.X = &omega[0]; address of first element

	// set translational velocity with respect to X, Y, Z
// old fashion
	//float vel[3] = {10.0, 10.0, 10.0};
	//NewtonBodySetVelocity(body, &vel[0]);
// irrlicht fashion
	vector3df vel = vector3df(0.0, 0.0, 25.0);
	NewtonBodySetVelocity(body, &vel.X);

	ICameraSceneNode* cam = smgr->addCameraSceneNode();
	cam->setPosition(vector3df(0, 100, -100));
	cam->setTarget(vector3df(0.0, 0.0, 0.0));
}

void DrawScene()
{
	if (device->getTimer()->getTime() > lasttick + 10)
	{	
		lasttick = device->getTimer()->getTime();
		NewtonUpdate(nWorld, 0.01f);
	}
	
// we should update graphic scene based on physic node
// old fashion
	//float matrix[4][4];
        //NewtonBodyGetMatrix(body, &matrix[0][0]);
	//matrix4 mat;
	//memcpy(mat.pointer(), matrix, sizeof(float)*16);

// irrlicht fashion
	matrix4 mat;
	NewtonBodyGetMatrix(body, mat.pointer());
	boxNode->setPosition(mat.getTranslation());
        boxNode->setRotation(mat.getRotationDegrees());
}

int main()
{
   InitScene();
   while(device->run())
   {
      DrawScene();
      driver->beginScene(true, true, SColor(0, 0, 127, 127));
      smgr->drawAll();
      driver->endScene();
   }
   NewtonDestroy(nWorld);
   device->drop();
   return 0;
}

I will explain this code in detail very soon. I tried this code using VS2008.