why basic_collision_detection newton with irr doesn't work?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

why basic_collision_detection newton with irr doesn't work?

Postby fjpeces » Thu Dec 16, 2010 12:00 pm

hello i have downladed Newton SDK 1.5.3, and i have IRRlicht 1.7.2

I have created a project and i've copied the example:
http://www.irrlicht3d.org/wiki/index.ph ... nDetection

Code: Select all
Code:

#include <irrlicht.h>
#include <newton.h>


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

   #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;

//Struct for collision object
struct SObject
{
//Irrlicht scene node
IMeshSceneNode *irr_node;

//Newton collision object
NewtonCollision *nwtn_collision;

};

//Function to create a NewtonCollision from irrlicht mesh
NewtonCollision *CreateCollisionFromMesh(NewtonWorld *nWorld, IMesh *irr_mesh )
{
u32 nMeshBuffer = 0; //Mesh Buffer count
IMeshBuffer *mesh_buffer = NULL;
float *vertices; //Array to store vertices

u32 nVertices = 0;

//Get number of vertices
for( nMeshBuffer=0 ; nMeshBuffer < irr_mesh->getMeshBufferCount() ; nMeshBuffer++ )
{
nVertices += irr_mesh->getMeshBuffer(nMeshBuffer)->getVertexCount();
}


//create buffer for vertices
vertices = new float[nVertices * 3];
u32 tmpCounter = 0;

//Get (irr_)mesh buffers and copy face vertices
for( nMeshBuffer=0 ; nMeshBuffer < irr_mesh->getMeshBufferCount() ; nMeshBuffer++ )
{

    mesh_buffer = irr_mesh->getMeshBuffer(nMeshBuffer);

    //Get pointer to vertices and indices
    S3DVertex *S3vertices = (S3DVertex*)mesh_buffer->getVertices();


//copy vertices from mesh to buffer
for(u32 i=0; i<mesh_buffer->getVertexCount(); i++)
{

        vertices[tmpCounter++] = S3vertices[i].Pos.X;
        vertices[tmpCounter++] = S3vertices[i].Pos.Y;
        vertices[tmpCounter++] = S3vertices[i].Pos.Z;

    }

}

//Create Newton collision object
NewtonCollision *collision_obj = NewtonCreateConvexHull(nWorld,nVertices,vertices,sizeof(float)*3,NULL);

//delete vertices
delete [] vertices;

return collision_obj;

}

//Function to create a NewtonCollision from irrlicht mesh with tree optimization
NewtonCollision *CreateTreeCollisionFromMesh(NewtonWorld *nWorld, IMesh *irr_mesh )
{
//Create new (tree optimized) collision mesh
NewtonCollision *collision_obj = NewtonCreateTreeCollision(nWorld ,NULL);

//Begin collision mesh construction
NewtonTreeCollisionBeginBuild(collision_obj);

u32 nMeshBuffer = 0; //Mesh Buffer count
int v_index[3] = {0,0,0}; //vertex indices
IMeshBuffer *mesh_buffer = NULL;
float array[9]; //Array to store 3 vertices

//Get (irr_)mesh buffers and copy face by face to collision mesh
for( nMeshBuffer=0 ; nMeshBuffer < irr_mesh->getMeshBufferCount() ; nMeshBuffer++ )
{
mesh_buffer = irr_mesh->getMeshBuffer(nMeshBuffer);

//Get pointer to vertices and indices
S3DVertex *vertices = (S3DVertex*)mesh_buffer->getVertices();
u16 *indices = mesh_buffer->getIndices();

//Fill collision mesh
for(u32 i=0; i<mesh_buffer->getIndexCount(); i+=3)
{
v_index[0] = indices[ i ];
v_index[1] = indices[i+1];
v_index[2] = indices[i+2];

// 1st position vertex
array[0] = vertices[ v_index[0] ].Pos.X;
array[1] = vertices[ v_index[0] ].Pos.Y;
array[2] = vertices[ v_index[0] ].Pos.Z;

// 2nd position vertex
array[3] = vertices[ v_index[1] ].Pos.X;
array[4] = vertices[ v_index[1] ].Pos.Y;
array[5] = vertices[ v_index[1] ].Pos.Z;

// 3rd position vertex
array[6] = vertices[ v_index[2] ].Pos.X;
array[7] = vertices[ v_index[2] ].Pos.Y;
array[8] = vertices[ v_index[2] ].Pos.Z;

//Add new face to collision mesh
NewtonTreeCollisionAddFace(collision_obj, //collision mesh to add face to

                    3, //number of vertices in array
                    (float*)array, //pointer to vertex array
                    3*sizeof(float),//size of each vertex
                    1); //ID of the face

}
}
//End collision contruction , set 1 as 2dn param for optimization
NewtonTreeCollisionEndBuild(collision_obj,0);

return collision_obj;

}

//Check for collision between two SObjects
bool CheckForCollision(NewtonWorld *nWorld, SObject *obj_A, SObject *obj_B)
{
//Matrix to store irr_node position
matrix4 mat_A,mat_B;

//Copy position
mat_A.makeIdentity();
mat_B.makeIdentity();

mat_A.setTranslation(obj_A->irr_node->getPosition());
mat_B.setTranslation(obj_B->irr_node->getPosition());

const int nContacts = 2;
float contacts[3 * nContacts];
float normals[3 * nContacts];
float penetration[ nContacts ];

//Check for collision between collision meshes,
// returns number of contact points
int nHits = NewtonCollisionCollide( nWorld,nContacts,

                    obj_A->nwtn_collision, (float*)&mat_A[0],
                    obj_B->nwtn_collision, (float*)&mat_B[0],
                    contacts,
                    normals,
                    penetration);

//Collision detected if nHits > 0
if( nHits > 0)
return true;
return false;

}

int main()
{
//Create irrlicht device
IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(800, 600), 16,

                    false, false, true, 0);


//Create newton world
NewtonWorld *nWorld = NewtonCreate(NULL,NULL);

// set 0 for exact collision detection,
// 1 or n for use in applications where speed is more important
NewtonSetSolverModel(nWorld,1);

/*
Set the caption of the window to some nice text. Note that there is
a 'L' in front of the string. The Irrlicht Engine uses wide character
strings when displaying text.
*/
device->setWindowCaption(L"Basic Collision Detection with Newton GD");

/*
Get a pointer to the video driver, the SceneManager and the
graphical user interface environment, so that
we do not always have to write device->getVideoDriver(),
device->getSceneManager() and device->getGUIEnvironment().
*/
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
//IGUIEnvironment* guienv = device->getGUIEnvironment();


//Create two SObjects
SObject sphere;
SObject cube;

//Load mesh and create new irrlicht node
sphere.irr_node = smgr->addMeshSceneNode(smgr->getMesh("cube.3ds")->getMesh(0));
cube.irr_node = smgr->addMeshSceneNode(smgr->getMesh("cube.3ds")->getMesh(0));
sphere.irr_node->getMaterial(0).EmissiveColor.set(255,0,255,5);
cube.irr_node->getMaterial(0).EmissiveColor.set(255,0,255,255);

//Now create collision mesh from irrlicht mesh
sphere.nwtn_collision = CreateTreeCollisionFromMesh(nWorld,sphere.irr_node->getMesh());
cube.nwtn_collision = CreateTreeCollisionFromMesh(nWorld,cube.irr_node->getMesh());

//Set position
sphere.irr_node->setPosition(vector3df(-5,0,0));
cube.irr_node->setPosition(vector3df(0,0,0));

//Add camera
smgr->addCameraSceneNode(0, vector3df(0,1,-7), vector3df(0,0,0));

/*
Ok, now we have set up the scene, lets draw everything:
We run the device in a while() loop, until the device does not
want to run any more. This would be when the user closed the window
or pressed ALT+F4 in windows.
*/

//Defines where the node should be moved
float vel=0.1f;

while(device->run())
{
driver->beginScene(true, true, SColor(0,200,200,200));

smgr->drawAll();

driver->endScene();

//let's move the sphere
sphere.irr_node->setPosition(sphere.irr_node->getPosition()+vector3df(1,0,0)*vel);
if(sphere.irr_node->getPosition().X > 5.5f)
vel = -0.05f;
if(sphere.irr_node->getPosition().X < -5.5f)
vel = 0.05f;
//check for collision and set red color if collision detected
if(CheckForCollision(nWorld,&sphere,&cube))
cube.irr_node->getMaterial(0).EmissiveColor.set(255,255,0,0);
else //use blue color
cube.irr_node->getMaterial(0).EmissiveColor.set(255,0,0,255);
}

// We need to release newton collision mesh if we don't need it anymore
NewtonReleaseCollision(nWorld, sphere.nwtn_collision);
NewtonReleaseCollision(nWorld, cube.nwtn_collision);

//Destroy newton world
NewtonDestroy(nWorld);

device->drop();

return 0;

}



It doesn't work the cubes all the time present the same color. But i don't know where is my mistake. If someone wanted i could send him my project and run it.

Than you!!!
fjpeces
 
Posts: 7
Joined: Tue Dec 14, 2010 10:44 am

Re: why basic_collision_detection newton with irr doesn't work?

Postby Julio Jerez » Thu Dec 16, 2010 1:54 pm

you need to upgrade to sdk 2.26, 1.53 is upsoleted
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: why basic_collision_detection newton with irr doesn't work?

Postby fjpeces » Fri Dec 17, 2010 10:41 am

thank you Julio!

I have just upgraded my Newton version.

I have some questions about Newton, and I need your advice.

1. What i need is detect the collision betwen two meshes, and the scenario is the next: I need detect collisions betwen faces, so that Hull convex detection is useless in my case because of its collision detection behavior. I need accuracy.

One of the mesh will me moving inside another one, as a mouse running in a pipe and i want to detect internal collisions. I'm dummy in Newton and i need someone oriented me about what collision type i'd need in this case. I have tested Hull convex collision and it doesn't work in this case because always one mesh is inside another, it will detect the collision and i only want detect when the internal mesh touch some of faces of the external mesh. I don't know if my case it could be simulated using Newton or I have to use another physics engine. If someone wants to help me i could send a draw of my scenario.

Than you very much in advance!
fjpeces
 
Posts: 7
Joined: Tue Dec 14, 2010 10:44 am

Re: why basic_collision_detection newton with irr doesn't work?

Postby Julio Jerez » Fri Dec 17, 2010 1:18 pm

Oh but Netwon does not do mesh to mesh collision.
The closest is compound collision to compound collision or compound collisiont to Mesh.

Mesh/Mesh collison is too difficult for me, may some the other engines can help you with that.
I beleiev soem say they sopport it, Noewton does not and I have no plans to do it.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron