Newton and Irrlicht

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
jlulian38
Posts: 11
Joined: Fri Apr 21, 2006 3:57 am

Newton and Irrlicht

Post by jlulian38 »

Right now I'm able to get Newton to excute, spawn an Entity bound to a node, and some other things. However right now I can't get my BSP mesh into Newton. So i can't let my objects move with an gravitational force or they'll fall right through the ground. Just to let you know I'm using Newton 1.5 and Irrlicht 1.0


So basically I'm asking if anyone knows how i can accomplish this. As i don't know how to do this.

I've tried Mercio's Code and attempting modifying it, but whenever i try to run the code it Crashes my computer.

Thanks in advance if anyone can help.
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

try to set up his project and if it doesnt work, debug! theres also other newton examples floating around the forums.
jlulian38
Posts: 11
Joined: Fri Apr 21, 2006 3:57 am

Post by jlulian38 »

I'm working on that now, but i ran into a bit of a problem, as in an annoying sonov***** kind of problem.

Code: Select all

IMeshBuffer *mb;
I have no idea what *mb is, and IMeshBuffer doesn't exist.
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

mb is a variable, silly. of type IMeshBuffer.

Anyways, I'd need to see the surrounding code to see why he uses IMeshBuffer.
jlulian38
Posts: 11
Joined: Fri Apr 21, 2006 3:57 am

Post by jlulian38 »

I knew *mb was a variable. Also I have to update ALL the code, because the header doesn't work in Dev C++ (Which is all i can use)

Code: Select all

NewtonCollision* g_newtonmap = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(g_newtonmap);
	int cMeshBuffer, j;
	int v1i, v2i, v3i;
	IMeshBuffer *mb;

	float vArray[9]; // vertex array (3*3 floats)

	int tmpCount = 0;

	for (cMeshBuffer=0; cMeshBuffer<g_map->getMesh(0)->getMeshBufferCount(); cMeshBuffer++)
	{	
		mb = g_map->getMesh(0)->getMeshBuffer(cMeshBuffer);

		video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*)mb->getVertices();

		u16* mb_indices  = mb->getIndices();

		// add each triangle from the mesh
		for (j=0; j<mb->getIndexCount(); j+=3)
		{
			v1i = mb_indices[j];
			v2i = mb_indices[j+1];
			v3i = mb_indices[j+2];
	
			vArray[0] = mb_vertices[v1i].Pos.X;
			vArray[1] = mb_vertices[v1i].Pos.Y;
			vArray[2] = mb_vertices[v1i].Pos.Z;
			vArray[3] = mb_vertices[v2i].Pos.X;
			vArray[4] = mb_vertices[v2i].Pos.Y;
			vArray[5] = mb_vertices[v2i].Pos.Z;
			vArray[6] = mb_vertices[v3i].Pos.X;
			vArray[7] = mb_vertices[v3i].Pos.Y;
			vArray[8] = mb_vertices[v3i].Pos.Z;

			NewtonTreeCollisionAddFace(g_newtonmap, 3, (float*)vArray, 12, 1);
		}

	}
	NewtonTreeCollisionEndBuild(g_newtonmap, 0);
	g_newtonmapbody = NewtonCreateBody(nWorld, g_newtonmap);

	
	// set the newton world size based on the bsp size
	float boxP0[3]; 
	float boxP1[3]; 
	float matrix[4][4]; 
	NewtonBodyGetMatrix (g_newtonmapbody, &matrix[0][0]); 
	NewtonCollisionCalculateAABB (g_newtonmap, &matrix[0][0],  &boxP0[0], &boxP1[0]); 
	// you can pad the box here if you wish
	//boxP0.y -= somevalue; 
	//boxP1.y += somevaluef; 
	NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);
^ Ze Code
jlulian38
Posts: 11
Joined: Fri Apr 21, 2006 3:57 am

Post by jlulian38 »

Acctually, i think i fixed most of it. Now however it crashes when i try to run it. The current code (This is from something I'm working on) is this

This code acctually worked :D
It turned out i wasn't setting nWorld to the NewtonCreateWorld(NULL,NULL);
thing.
But now it works :D

Code: Select all

#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 NewtonBody* nBody;
IrrlichtDevice *device = 0;
float lasttick = 0;
NewtonBody* body = 0;
scene::ISceneNode* boxNode = 0;
video::IVideoDriver* driver = 0;
scene::ISceneManager* scenemgr = 0;
gui::IGUIEnvironment* guienv = 0;
NewtonCollision *collision = 0;
NewtonCollision *g_newtonmap = 0;
NewtonBody* g_newtonmapbody = 0;
NewtonWorld* nWorld = NewtonCreate (NULL, NULL);
float torque[3];

void DrawScene()
{
	if (device->getTimer()->getTime() > lasttick + 10) {	
		NewtonUpdate(nWorld, 0.01f);
		lasttick = device->getTimer()->getTime();
		
	 NewtonBodyAddTorque(body,torque);	
  }	
	 float matrix[4][4];
        NewtonBodyGetMatrix(body, &matrix[0][0]);
    
	matrix4 mat;
	memcpy(mat.M, matrix, sizeof(float)*16);
    boxNode->setPosition(mat.getTranslation());
    boxNode->setRotation(mat.getRotationDegrees());
}
void InitScene()
{
// start up the engine
        device = createDevice(video::EDT_OPENGL,
              core::dimension2d<s32>(800,600));
        driver = device->getVideoDriver();
        scenemgr = device->getSceneManager();
        guienv = device->getGUIEnvironment();
        device->getCursorControl()->setVisible(false);
        device->setWindowCaption(L"Newton Physics");
        device->getFileSystem()->addZipFileArchive("baseq3.pk3");
             scene::IAnimatedMesh* bspmesh = 0;
             bspmesh = scenemgr->getMesh("nemesis.bsp");
             
             scene::ISceneNode* bspnode = 0;
             bspnode = scenemgr->addOctTreeSceneNode(bspmesh->getMesh(0));
             bspnode->setPosition(core::vector3df(0,0,0));
             bspnode->setScale( irr::core::vector3df(2,2,2) );
    //////////////////////////////////////////////////////////////////////////
	//
	// Create the newton collision tree from the map mesh
	//
	// Remember to use (video::S3DVertex) if you are loading a mesh without lightmaps
	// for your level. (Like a .x or .3ds level)
	//
	//////////////////////////////////////////////////////////////////////////
	g_newtonmap = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(g_newtonmap);
	int cMeshBuffer, j;
	int v1i, v2i, v3i;
	scene::IMeshBuffer *mb;

	float vArray[9]; // vertex array (3*3 floats)

	int tmpCount = 0;
	for (cMeshBuffer=0; cMeshBuffer<bspmesh->getMesh(0)->getMeshBufferCount(); cMeshBuffer++)
	{	
		mb = bspmesh->getMesh(0)->getMeshBuffer(cMeshBuffer);

		video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*)mb->getVertices();

		u16* mb_indices  = mb->getIndices();

		// add each triangle from the mesh
		for (j=0; j<mb->getIndexCount(); j+=3)
		{
			v1i = mb_indices[j];
			v2i = mb_indices[j+1];
			v3i = mb_indices[j+2];
	
			vArray[0] = mb_vertices[v1i].Pos.X;
			vArray[1] = mb_vertices[v1i].Pos.Y;
			vArray[2] = mb_vertices[v1i].Pos.Z;
			vArray[3] = mb_vertices[v2i].Pos.X;
			vArray[4] = mb_vertices[v2i].Pos.Y;
			vArray[5] = mb_vertices[v2i].Pos.Z;
			vArray[6] = mb_vertices[v3i].Pos.X;
			vArray[7] = mb_vertices[v3i].Pos.Y;
			vArray[8] = mb_vertices[v3i].Pos.Z;

			NewtonTreeCollisionAddFace(g_newtonmap, 3, (float*)vArray, 12, 1);
		}

	}
	NewtonTreeCollisionEndBuild(g_newtonmap, 0);
    g_newtonmapbody = NewtonCreateBody(nWorld, g_newtonmap);

	
	// set the newton world size based on the bsp size
	float boxP0[3]; 
	float boxP1[3]; 
	float matrix[4][4]; 
	NewtonBodyGetMatrix (g_newtonmapbody, &matrix[0][0]); 
	NewtonCollisionCalculateAABB (g_newtonmap, &matrix[0][0],  &boxP0[0], &boxP1[0]); 
	// you can pad the box here if you wish
	//boxP0.y -= somevalue; 
	//boxP1.y += somevaluef; 
	NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);
	//End Newton Stuff
	// hide cursor
	device->getCursorControl()->setVisible(false);       
     boxNode = scenemgr->addTestSceneNode(60);
    boxNode->setMaterialTexture(0, driver->getTexture("media/stones.jpg"));
     collision = NewtonCreateBox(nWorld, 38, 38, 38, NULL);
    body = NewtonCreateBody (nWorld, collision);	
	NewtonBodySetUserData(body, boxNode);
	NewtonBodySetMassMatrix (body, 100.0f, 1.0f, 1.0f, 1.0f);
	matrix4 mat;
	mat.setTranslation(vector3df(0,0,0));
	NewtonBodySetMatrix(body, &mat.M[0]);
    float omega[3] = {1.0f, 2.0f, 1.0f};
    NewtonBodySetOmega (body, &omega[0]);
    torque[0] = 6.1; //x 
    torque[1] = 5.1; //y    
    torque[2] = 1.0; //z
   NewtonBodyAddTorque(body,torque);
        //Sets The Camera's KeyMap
         SKeyMap keyMap[8];
                 keyMap[0].Action = EKA_MOVE_FORWARD;
                 keyMap[0].KeyCode = KEY_UP;
                 keyMap[1].Action = EKA_MOVE_FORWARD;
                 keyMap[1].KeyCode = KEY_KEY_W;
                
                 keyMap[2].Action = EKA_MOVE_BACKWARD;
                 keyMap[2].KeyCode = KEY_DOWN;
                 keyMap[3].Action = EKA_MOVE_BACKWARD;
                 keyMap[3].KeyCode = KEY_KEY_S;
                
                 keyMap[4].Action = EKA_STRAFE_LEFT;
                 keyMap[4].KeyCode = KEY_LEFT;
                 keyMap[5].Action = EKA_STRAFE_LEFT;
                 keyMap[5].KeyCode = KEY_KEY_A;
                
                 keyMap[6].KeyCode = KEY_KEY_D;                
                 keyMap[6].Action = EKA_STRAFE_RIGHT;
                 keyMap[7].KeyCode = KEY_RIGHT;
                 keyMap[7].Action = EKA_STRAFE_RIGHT;
        // add a first person shooter style user controlled camera
        scene::ISceneNode* camnode = scenemgr->addCameraSceneNodeFPS(0,100,250,-1,keyMap,8,true);
        camnode->setPosition(core::vector3df(0,0,120));
        camnode->setRotation(core::vector3df(0,180,0));
        //Collisions
          scene::ITriangleSelector* selector = 0;
          selector = scenemgr->createOctTreeTriangleSelector(bspmesh->getMesh(0), bspnode, 128);
	      bspnode->setTriangleSelector(selector);
	      selector->drop();
	      scene::ISceneNodeAnimator* anim = 0;
	      anim = scenemgr->createCollisionResponseAnimator(selector, camnode, core::vector3df(30,50,30),
	      core::vector3df(0,-100,0),
       	  core::vector3df(0,50,0));
          camnode->addAnimator(anim);
          anim->drop();    
        //Add Coder Credits
        IGUIStaticText* text = guienv->addStaticText(L"This was coded by Julian R. Moore",rect<int>(10,10,200,30), true);
        text->enableOverrideColor(true);
        text->setOverrideColor(video::SColor(255,255,0,0));
} 
int main()
{
   
   InitScene();

   
   while(device->run())
   {
      DrawScene();

      driver->beginScene(true, true, video::SColor(0,0,0,0));
      NewtonBodyAddTorque(body,torque);
      NewtonUpdate(nWorld,0.01f);
      scenemgr->drawAll();
      guienv->drawAll();
      driver->endScene();
   }
   NewtonDestroy(nWorld);
   NewtonReleaseCollision (nWorld, collision);
   
   device->drop();

   return 0;
}    
        
    
        
Post Reply