[solved] ITerrainSceneNode + Newton Game Dynamics

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
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

[solved] ITerrainSceneNode + Newton Game Dynamics

Post by siny »

Hi,

I'am trying to generate Newton Map Cillision for Newton World. I tried to use code from one of examples from Irrilicht tutorial site:

Code: Select all

terrainMesh = terrain->getMesh();
	nMapCollision = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(nMapCollision);
     
	u32 cMeshBuffer, j;
	int v1i, v2i, v3i;
	IMeshBuffer *mb;
	float vArray[9]; // vertex array (3*3 floats)
	int tmpCount = 0;
	
	scene::IMesh *mesh = terrainMesh;
	
	for (cMeshBuffer=0; cMeshBuffer<mesh->getMeshBufferCount(); cMeshBuffer++)
	{
		mb = mesh->getMeshBuffer(cMeshBuffer);
		S3DVertex2TCoords*/*video::S3DVertex*/ mb_vertices = (S3DVertex2TCoords*/*video::S3DVertex**/)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;
			box.addInternalPoint(mb_vertices[v1i].Pos.X, mb_vertices[v1i].Pos.Y, mb_vertices[v1i].Pos.Z);
			box.addInternalPoint(mb_vertices[v2i].Pos.X, mb_vertices[v2i].Pos.Y, mb_vertices[v2i].Pos.Z);
			box.addInternalPoint(mb_vertices[v3i].Pos.X, mb_vertices[v3i].Pos.Y, mb_vertices[v3i].Pos.Z);
			NewtonTreeCollisionAddFace(nMapCollision, 3, &vArray[0], 12, 1);
		}
	}

	NewtonTreeCollisionEndBuild(nMapCollision, 0);
	NewtonBody* nmapbody = NewtonCreateBody(nWorld, nMapCollision);
But it doesn't works.
When i debug program I get, that there is ony one MeshBuffer and

Code: Select all

mb->getIndices();
returns 0 so no vertices are being added to NewtonCollisionTree.

Is there another way to do this, or maybe I have to list Vertices??
Please help.

P.s. sorry for my english but i'am beginner...
Last edited by siny on Sat May 05, 2007 11:45 am, edited 1 time in total.
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

use "terrainMesh = terrain->getMesh(0);" instead of "terrainMesh = terrain->getMesh();"
Jacky_J
Posts: 55
Joined: Fri Apr 27, 2007 5:01 am

Post by Jacky_J »

I too am getting this problem, however getMesh doesn't take any parameters, so i get a compile error from what you suggested roxaz. I'm using irrlicht 1.3
white tiger
Posts: 269
Joined: Tue Oct 31, 2006 3:24 pm
Contact:

Post by white tiger »

in IrrNewt i use getMeshBufferForLOD [pass 0 as second parameter to return a very detailed tree]. it ereturn a SMeshBufferLightMap wich you can cast to IMeshBuffer (and erase all the initial IMesh part)
Zeuss
Posts: 114
Joined: Mon Nov 08, 2004 9:02 pm
Location: Canberra - Australia
Contact:

Post by Zeuss »

white tiger is right, I did a bit of experimenting a while back, passing any other value apart from pass 0, you seem to get a mesh with holes in it.
Help make Irrlicht even Better! Create and submit your own Irrlicht Extension
Want a Games Education? Try The Academy of Interactive Entertainment
siny
Posts: 10
Joined: Fri Apr 20, 2007 11:59 am

Post by siny »

I searched i found this working:

Code: Select all

    NewtonCollision* nMapCollision = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(nMapCollision); 

	int cMeshBuffer, j;
	int v1i, v2i, v3i;
	IMeshBuffer *mb;
	
	float vArray[9]; // vertex array (3*3 floats)
	
	int tmpCount = 0;
	scene::SMeshBufferLightMap mb2;
    ((scene::ITerrainSceneNode*)terrain)->getMeshBufferForLOD(mb2, 0); 
    
   	mb2.Indices.set_free_when_destroyed(false);
    mb2.Vertices.set_free_when_destroyed(false);
    
	video::S3DVertex2TCoords  *vertices = (video::S3DVertex2TCoords*)mb2.getVertices(); 
	u16 *indices = mb2.getIndices();
	
	// indeksujemy wszystkie trojkaty planszy
	for (int i = 0; i < mb2.getIndexCount() ; i += 3)
	{
       // pobieramy kolejne trojkaty
	   v1i = indices[i];
	   v2i = indices[i + 1];
	   v3i = indices[i + 2];

		// budujemy powierzchnie sceny newtona z kolejnych wierzcholkow 
		// kolejnych trojaktow
		vArray[0] = vertices[v1i].Pos.X*terrainSize.X;
		vArray[1] = vertices[v1i].Pos.Y*terrainSize.Y;
		vArray[2] = vertices[v1i].Pos.Z*terrainSize.Z;
		vArray[3] = vertices[v2i].Pos.X*terrainSize.X;
		vArray[4] = vertices[v2i].Pos.Y*terrainSize.Y;
		vArray[5] = vertices[v2i].Pos.Z*terrainSize.Z;
		vArray[6] = vertices[v3i].Pos.X*terrainSize.X;
		vArray[7] = vertices[v3i].Pos.Y*terrainSize.Y;
		vArray[8] = vertices[v3i].Pos.Z*terrainSize.Z;
		   
		// add face to tree
		NewtonTreeCollisionAddFace(nMapCollision , 3, (float*)vArray, 12, 11);
	}
	
   	NewtonTreeCollisionEndBuild(nMapCollision, 0);
	mapBody = NewtonCreateBody(nWorld, nMapCollision); 
	NewtonBodySetUserData(mapBody,terrain);
	
	// ustawiamy wielkosc swiata newton na wielkosc wygenerowanego terenu
	float boxP0[3];
	float boxP1[3];
	float matrix[4][4];
	
	NewtonBodyGetMatrix (mapBody,&matrix[0][0]);
	NewtonCollisionCalculateAABB (nMapCollision, &matrix[0][0],  &boxP0[0], &boxP1[0]); 
    
   	boxP1[1] += 1000;
	NewtonSetWorldSize (nWorld, (float*)boxP0, (float*)boxP1);

	NewtonReleaseCollision(nWorld,nMapCollision);
    
    // ustawiamy typ materialu dla ladu
    NewtonBodySetMaterialGroupID(mapBody,m_concreteMaterial);	
Post Reply