Hi,
have anybody a sample code for collisions with Newton and Irrlicht Terrain?!
or an idea have i can release this?
---
Hat jemand ein Beispiel code für collisions mit Newton und dem Irrlicht Terrain??
Oder eine Idee wie man das machen könnte?
Newton & Irrlicht Terrain ?
Hi, i write a function for Newten & Irrlicht Terrain based on an Newten Tutorial and an post in Newton Game Dynamics Forum
http://newtondynamics.com/forum/viewtop ... hlight=nwt.
But i have a problem nothing collision with the terrain.
Can anybody help me?
Or see anybody a bug?
PS: I waiting since 2 days for an email with my password for the irrlicht forum.
Sorry for my bad english.
http://newtondynamics.com/forum/viewtop ... hlight=nwt.
But i have a problem nothing collision with the terrain.
Can anybody help me?
Or see anybody a bug?
PS: I waiting since 2 days for an email with my password for the irrlicht forum.
Sorry for my bad english.
Code: Select all
#include "StdAfx.h"
core::stringw wStr1;
const float IrrToNewton = 1.0f / 1024.0f;
const float NewtonToIrr = 1024.0f;
void CGame::CT2()
{
//scene::ITerrainSceneNode* terrain;
char meshName[255] ="XYZ";
core::dimension2d<f32> stretchSize;
//scene::IAnimatedMesh* terrainMesh;
//scene::ISceneNode* terrainNode;
video::IImage* heightMapImage;
video::IImage* texture;
stretchSize = core::dimension2d<f32> ( 32.125f, 32.125f );
texture = driver->createImageFromFile ( "../../media/terrain-texture.jpg" );
heightMapImage = driver->createImageFromFile ( "../../media/terrain-heightmap.bmp" );
driver->beginScene ( true, true, video::SColor ( 0, 0, 0, 0 ) );
wStr1 = L"Loading Terrain...";
guienv->getBuiltInFont ( )->draw ( wStr1.c_str ( ), core::rect<s32> ( 0, 0, 200, 150 ), video::SColor ( 128, 255, 0, 0 ), true, true );
driver->endScene ( );
g_map = smgr->addTerrainMesh ( meshName, texture, heightMapImage, stretchSize, 256.0f, core::dimension2d<s32> ( 64, 64 ) );
g_mapnode = smgr->addOctTreeSceneNode ( g_map, smgr->getRootSceneNode ( ), -1, 128 );
//g_mapnode->setMaterialTexture(1, driver->getTexture("../../media/detailmap3.jpg"));
g_mapnode->setMaterialType(video::EMT_DETAIL_MAP);
driver->beginScene ( true, true, video::SColor ( 0, 0, 0, 0 ) );
wStr1 = L"Loading Newton...";
guienv->getBuiltInFont ( )->draw ( wStr1.c_str ( ), core::rect<s32> ( 0, 0, 200, 150 ), video::SColor ( 128, 255, 0, 0 ), true, true );
driver->endScene ( );
/*light = smgr->addLightSceneNode(0, core::vector3df(0,30,0),
video::SColorf(1.0f, 1.0f, 1.0f, 1.0f), 6000.0f); */
//////////////////////////////////////////////////////////////////////////
//
// 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 = NULL;
core::vector3df vArray[3], e0, e1, area; // vertex array (3*3 floats)
float mag = 0.0f;
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 )
{
//printf("Ding Dong\n");
v1i = mb_indices[j];
v2i = mb_indices[j + 1];
v3i = mb_indices[j + 2];
vArray[0] = mb_vertices[v1i].Pos * IrrToNewton;
vArray[1] = mb_vertices[v2i].Pos * IrrToNewton;
vArray[2] = mb_vertices[v3i].Pos * IrrToNewton;
e0 = vArray[1] - vArray[0];
e1 = vArray[2] - vArray[0];
area = e0.crossProduct ( e1 );
mag = area.dotProduct ( area );
if ( mag > -0.00001 )
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 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);
}
-
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
Why do you do this ? :
e0 = vArray[1] - vArray[0];
e1 = vArray[2] - vArray[0];
area = e0.crossProduct ( e1 );
mag = area.dotProduct ( area );
if ( mag > -0.00001 )
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 12, 1 );
Maybe take this code out? I have terrain collisions working with Newton, I did not take a step like this. Try replacing with just:
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 12, 1 );
Also, I believe this code is incorrect:
vArray[0] = mb_vertices[v1i].Pos * IrrToNewton;
vArray[1] = mb_vertices[v2i].Pos * IrrToNewton;
vArray[2] = mb_vertices[v3i].Pos * IrrToNewton;
Do it like this:
vArray[0] = mb_vertices[v1i].Pos;
vArray[1] = mb_vertices[v2i].Pos;
vArray[2] = mb_vertices[v3i].Pos;
I don't know why you used the scaling factor. I had a hard time setting this up, it helps to get a feel for where things are being placed, I figured this out by writing all of my vertices in the terrain to a file so I could verify they were being added correctly. It's not pretty nor fun, but it did help. I do not have my code in front of me (I'm at work) but try the fixes I suggested and if they don't work, I'll post my code to setup Newton Terrain using a mesh buffer.
e0 = vArray[1] - vArray[0];
e1 = vArray[2] - vArray[0];
area = e0.crossProduct ( e1 );
mag = area.dotProduct ( area );
if ( mag > -0.00001 )
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 12, 1 );
Maybe take this code out? I have terrain collisions working with Newton, I did not take a step like this. Try replacing with just:
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 12, 1 );
Also, I believe this code is incorrect:
vArray[0] = mb_vertices[v1i].Pos * IrrToNewton;
vArray[1] = mb_vertices[v2i].Pos * IrrToNewton;
vArray[2] = mb_vertices[v3i].Pos * IrrToNewton;
Do it like this:
vArray[0] = mb_vertices[v1i].Pos;
vArray[1] = mb_vertices[v2i].Pos;
vArray[2] = mb_vertices[v3i].Pos;
I don't know why you used the scaling factor. I had a hard time setting this up, it helps to get a feel for where things are being placed, I figured this out by writing all of my vertices in the terrain to a file so I could verify they were being added correctly. It's not pretty nor fun, but it did help. I do not have my code in front of me (I'm at work) but try the fixes I suggested and if they don't work, I'll post my code to setup Newton Terrain using a mesh buffer.
Hi,
thank you for the answer.
I try your tip but my application stop/crash atI'm looking forward to your next answer and hope that you post your code.
thank you for the answer.
I try your tip but my application stop/crash at
Code: Select all
NewtonTreeCollisionAddFace ( g_newtonmap, 3, &vArray[0].X, 12, 1 );
-
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
Again, no code in front of me (sorry)
But I think I know what the problem is now:
Try passing vArray as an array of 9 floats, instead of using the core::vector3df
Newton assumes an array of 9 consecutive floats describing a face, it won't know how to handle a vector3df, and is probably accessing what it thinks is valid memory in a float array but instead causes a segmentation fault.
That SHOULD fix it, if not harass me to post the code
But I think I know what the problem is now:
Try passing vArray as an array of 9 floats, instead of using the core::vector3df
Newton assumes an array of 9 consecutive floats describing a face, it won't know how to handle a vector3df, and is probably accessing what it thinks is valid memory in a float array but instead causes a segmentation fault.
That SHOULD fix it, if not harass me to post the code
-
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
I got the code in front of me, might as well post it becuase I would have found this useful had it existed before I wrote it:
// get terrain mesh buffer for lod 0 (full detail) for collision physics
scene::SMeshBufferLightMap mb;
((scene::ITerrainSceneNode*)m_pNode)->getMeshBufferForLOD(mb, 0);
// these are very important to avoid crashes
mb.Indices.set_free_when_destroyed(false);
mb.Vertices.set_free_when_destroyed(false);
// create newton collision tree with terrain vertices
m_pCollision = NewtonCreateTreeCollision(pWorld, NULL);
NewtonTreeCollisionBeginBuild(m_pCollision);
// get vertex data
video::S3DVertex2TCoords *vertices = (video::S3DVertex2TCoords*)mb.getVertices();
int vi1, vi2, vi3;
int index_count = mb.getIndexCount();
u16 *indices = mb.getIndices();
float vArray[9];
// create collision tree faces
for (int i = 0; i < index_count; i += 3)
{
vi1 = indices;
vi2 = indices[i + 1];
vi3 = indices[i + 2];
// build face data
vArray[0] = vertices[vi1].Pos.X; vArray[1] = vertices[vi1].Pos.Y; vArray[2] = vertices[vi1].Pos.Z;
vArray[3] = vertices[vi2].Pos.X; vArray[4] = vertices[vi2].Pos.Y; vArray[5] = vertices[vi2].Pos.Z;
vArray[6] = vertices[vi3].Pos.X; vArray[7] = vertices[vi3].Pos.Y; vArray[8] = vertices[vi3].Pos.Z;
// add face to tree
NewtonTreeCollisionAddFace(m_pCollision, 3, vArray, 12, 1);
}
// finalize tree, create static body and set material id
NewtonTreeCollisionEndBuild(m_pCollision, 0);
m_pBody = NewtonCreateBody(pWorld, m_pCollision);
// optional for setting material ID (these are really fun)
// get physics material from environment and set
m_matID = m_pEnvironment->GetPhysicsID(m_sPhysics);
NewtonBodySetMaterialGroupID(m_pBody, m_matID);
// get terrain mesh buffer for lod 0 (full detail) for collision physics
scene::SMeshBufferLightMap mb;
((scene::ITerrainSceneNode*)m_pNode)->getMeshBufferForLOD(mb, 0);
// these are very important to avoid crashes
mb.Indices.set_free_when_destroyed(false);
mb.Vertices.set_free_when_destroyed(false);
// create newton collision tree with terrain vertices
m_pCollision = NewtonCreateTreeCollision(pWorld, NULL);
NewtonTreeCollisionBeginBuild(m_pCollision);
// get vertex data
video::S3DVertex2TCoords *vertices = (video::S3DVertex2TCoords*)mb.getVertices();
int vi1, vi2, vi3;
int index_count = mb.getIndexCount();
u16 *indices = mb.getIndices();
float vArray[9];
// create collision tree faces
for (int i = 0; i < index_count; i += 3)
{
vi1 = indices;
vi2 = indices[i + 1];
vi3 = indices[i + 2];
// build face data
vArray[0] = vertices[vi1].Pos.X; vArray[1] = vertices[vi1].Pos.Y; vArray[2] = vertices[vi1].Pos.Z;
vArray[3] = vertices[vi2].Pos.X; vArray[4] = vertices[vi2].Pos.Y; vArray[5] = vertices[vi2].Pos.Z;
vArray[6] = vertices[vi3].Pos.X; vArray[7] = vertices[vi3].Pos.Y; vArray[8] = vertices[vi3].Pos.Z;
// add face to tree
NewtonTreeCollisionAddFace(m_pCollision, 3, vArray, 12, 1);
}
// finalize tree, create static body and set material id
NewtonTreeCollisionEndBuild(m_pCollision, 0);
m_pBody = NewtonCreateBody(pWorld, m_pCollision);
// optional for setting material ID (these are really fun)
// get physics material from environment and set
m_matID = m_pEnvironment->GetPhysicsID(m_sPhysics);
NewtonBodySetMaterialGroupID(m_pBody, m_matID);