Help! Collision detection with Newton and .irr! HOW?

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.
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

Help! Collision detection with Newton and .irr! HOW?

Post by jhend60 »

Hi all.
I am pretty desparate to find a fairly simple way of using newton with irrlicht and creating collision detection with a irr scene (only 1 mesh, the terrain). I need to be able to walk over the terrain, and up and down hills/mountains to a certain extent. I am using irrlicht and newton, without any wrappers (do I need a wrapper???) I was wondering if there is a fairly simple way I could get the triangles from the irr mesh and create collision detection.

Here is my current code:

Code: Select all

#include <irrlicht.h>
#include <iostream>
#include "newton.h"
using namespace irr;

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

#pragma comment(lib, "Irrlicht.lib")

int main()
{
	

	IrrlichtDevice* device =
		createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480),32,true);

	if (device == 0)
		return 1; 

	device->setWindowCaption(L"Target Practice 3D [");

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
    ICameraSceneNode* camera = 0;
	
	
	ICursorControl* cursor = device->getCursorControl();

	smgr->loadScene("3d1.irr");



	camera = smgr->addCameraSceneNodeFPS(0, 100.000000f, 5000.0f,-1,0,0,false,0.25f);
	camera->setFarValue(50000.0f);
	core::vector3df camerapos = camera->getPosition();
	// and draw everything.

	int lastFPS = -1;

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, video::SColor(0,0,0,0));
		smgr->drawAll();
		driver->endScene();
        cursor->setVisible(false);
		int fps = driver->getFPS();
		

		if (lastFPS != fps)
		{
		  core::stringw str = L"Target Practice 3D";
		  str += driver->getName();
		  str += "] FPS:";
		  str += fps;
		 

		  device->setWindowCaption(str.c_str());
		  lastFPS = fps;
		}

	}

	device->drop();
	
	return 0;
}


Fairly simple huh?

Exactly how and where would I use/put code to allow collision detection from 3d1.irr to stop the camera going through the ground? And allow it to climb up/down mountains/hills/trenches?

Any help would be GREATLY appreciated as I am only in year 9 and started c++ 2 months ago.

If I decided to use a simple 3ds mesh instead of using irr, would collision detection be any easier? If so, please share.

Thanks so much.
jontan6
Posts: 278
Joined: Fri Jun 13, 2008 5:29 pm

Post by jontan6 »

i think some people did answer you already in a previous thread that you made
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

uh...

Post by jhend60 »

You don't seem to understand me.
I posted my code as I have tried the answers people gave me in my other thread, but I was UNABLE to implement it. I was looking for someone to post direct code changes for me (which would be FANTASTIC) or some direct indication of WHERE in my code to put the code, and what code EXACTLY to use. Remember, in my previous thread, I was asking about standard Irrlicht collision detection, but now I am trying to use newton. Please Help.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

There are examples showing you how to iterate through the scene graph which will give you access to each node in the scene, including those loaded from your .irr file.

So then when you find your terrain node you just create a Newton representation of the terrain. I can't help you on how to do that though as i don't know Newton, if you ask on their forum they might be able to help you... Or maybe one of the Newton wrappers here will have support to help with this. You don't need a wrapper, they just make it easier to use the library with irrlicht and may provide some abstraction.
Image Image Image
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

okay...

Post by jhend60 »

Well... firstly... how can I get access to the scene node from the irr file? Do I use the name parameter from IrrEdit somehow?

What pointer do i need to make? Plz explain.

Is there a wiki someewhere that could help me?
SwitchCase
Posts: 170
Joined: Sun Jul 01, 2007 11:41 pm
Location: Manchester, UK

Post by SwitchCase »

There are examples showing you how to iterate through the scene graph which will give you access to each node in the scene...
As JP suggests, you don't access the scene node from the .irr file... scene nodes are created when you load your .irr file. You access them through your scene manager. The method JP describes could be as follows:

Code: Select all

s32 i = 0;
while( smgr->getSceneNodeFromId(i) )
{
   if( smgr->getSceneNodeFromId(i)->getName() == "nameOfSceneNodeYourLookingFor" )
   {
       // do something
   }

   // More if this or that scene nodes can be done here
   
   i++;
}
If you want to access any scene node you identify outside the while loop then declare a pointer to an ISceneNode and set it equal to the pointer returned by the getSceneNodeFromID() member function. For example:

Code: Select all

ISceneNode* mySceneNode;
And then where the "// do something" comment is above put something like

Code: Select all

mySceneNode = smgr->getSceneNodeFromId(i);

There's tutorials, examples, a doxygen and a wiki.
Last edited by SwitchCase on Tue Oct 28, 2008 5:17 pm, edited 2 times in total.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: uh...

Post by rogerborg »

jhend60 wrote:You don't seem to understand me.
I think that we understand you perfectly: we've had a lot of practice. If it seems like we're being less than helpful, it's simply because it gets tiresome reading exactly the same questions over and over, when the answers are already in the forum, examples and or API documentation. It's nothing personal.

Right, if you know the name of the scene node that contains your ground/level mesh, then you can retrieve it with ISceneManager::getSceneNodeFromName().

After that, setting up collision detection with that mesh is show in example 07.Collision/main.cpp in your SDK examples.

You can use createOctTreeTriangleSelector() as shown there, or if your node is a terrain node, then createTerrainTriangleSelector() is a better method.

The next thing to look for is createCollisionResponseAnimator(). It's attached to a camera node in example 07, but you can attach the animator to any scene node.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Johan
Posts: 32
Joined: Thu Jun 12, 2008 7:51 pm
Location: south africa

Post by Johan »

Hi, just go to google .search for this- newton tree collision irrlicht -And thats that. you will find what your looking for ,its pretty easy .newton uses treecollision for well basicly terrain ,stage geometry etc.. if u want a moving object that doesnt use basic shapes u will have to search for -convex hull- instead of tree collision..happy coding
compulsive compiler
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

kk

Post by jhend60 »

I have done what you have suggested above, but how will I use this with Newton? I need to get the Mesh itself, and I can't simply use ->getMesh. I am using the tutorial here: http://www.irrlicht3d.org/wiki/index.ph ... nDetection as a base for my code. I have got it to work with separate 3d meshes (3ds) but cannot get the mesh from the irr scene. How will I return the mesh to Newton so I can create the collision from the mesh?
Example:

ground.nwtn_collision = CreateCollisionFromMesh(nWorld,groundMesh->???(groundMesh is the pointer I made from SwitchCase (Thanks for that!))

So, can anyone point me in the right direction?

Thanks. I am (slowly) learning how to use irrlicht and it's functions.
Johan
Posts: 32
Joined: Thu Jun 12, 2008 7:51 pm
Location: south africa

Post by Johan »

halo again .well again you go to google search for- getting imesh from irredit files - you should find example where it shows casting to imesh . im sorry i cant provide direct links im using my cellphone for posting now so google search will have to do . enjoy
compulsive compiler
SwitchCase
Posts: 170
Joined: Sun Jul 01, 2007 11:41 pm
Location: Manchester, UK

Post by SwitchCase »

You should note that the pointer points to a scene node, not a mesh so I suggest changing the name from groundMesh to groundNode or something, just to avoid any confusion.

Once you have your scenenode (which you do) you can access the mesh by typecasting to the appropriate type of scene node (various types of scene nodes inherit ISceneNode) and then calling the getMesh() member. Example:

Code: Select all

IMesh* groundMesh;

switch( groundNode->getType() )
{
   case ESNT_MESH:
      groundMesh = ((IMeshSceneNode*)groundNode)->getMesh();
      break;

   // case other enumerated scene node types
}
For animated meshes the method of getting a pointer to it's mesh varies a little since it may contain many different frames (thus differing local vertex positions which are treated as seperate meshes, i believe).

Again, a search of the forum or a scan through the docs will give you these answers.
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

huh...

Post by jhend60 »

When I do what you suggested, I can only use ->getMeshBuffer from groundMesh. I need to be able to use ->getMesh to put the mesh into newton. What are the next steps required to do this?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

in SwitchCases's example groundMesh IS the mesh....
Image Image Image
jhend60
Posts: 44
Joined: Mon Oct 27, 2008 10:11 am
Location: behind you
Contact:

im getting sick of this

Post by jhend60 »

ive decided to just load the 3ds model itself (without irr) into the engine and do it myself, as I have some wierd errors when I got this code to finally work.
SwitchCase
Posts: 170
Joined: Sun Jul 01, 2007 11:41 pm
Location: Manchester, UK

Post by SwitchCase »

Another thing...

You may run into problems using irrlicht's IMesh as a mesh description for Newton collision objects - the getMeshBuffer() member function of IMesh can be used to get hold of the vertices and indices of a mesh so that you can then "convert" the mesh into one that works with Newton.

Your also likely to run into problems using Newton itself - since Newton is no longer developed (or so it seems), it's going to become increasingly difficult to get support - I suggest looking at alternative physics libs at some point - I personally recommend Bullet.

[EDIT]
Your "weird errors" are unlikely to be anything to do with loading a scene from .irr files - it shouldn't make a difference.
Post Reply