collision help

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.
Devil Master
Posts: 81
Joined: Wed Apr 23, 2008 8:47 pm

Post by Devil Master »

I was reading this topic and decided to test the code this topic is about, so I added the following lines to a program I created (immediately after placing an FPS camera):

Code: Select all

scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
 scene::ITriangleSelector * selector1;
 camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
 camera->setFarValue(120000.0f);
 scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
 meta->drop();
 camera->addAnimator(anim);
 anim->drop();
 // scene::ISceneNodeAnimator* an = smgr->createCollisionResponseAnimator(selector1, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));
 // selector1->drop();
 // camera->addAnimator(an);
 // an->drop();
Note that I commented the last 4 lines. If I compile and run the program like that, the camera falls down for an infinite distance, as I suppose it should do. But if those lines are active and I compile the program like that, the executable crashes immediately after I run it. Why?

I'm using Dev-C++ 4.9.9.2 and Irrlicht 1.4.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

You're not setting selector1 to anything; it's initialised to point to garbage.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Devil Master
Posts: 81
Joined: Wed Apr 23, 2008 8:47 pm

Post by Devil Master »

rogerborg wrote:You're not setting selector1 to anything; it's initialised to point to garbage.
I checked the collision tutorial, where the ITriangleSelector is set to 0, so I did the same with selector1, in the same line where I declare it. I recompiled the program and ran it again. It still crashes.
Anything else that I might have missed?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Maybe giving us more code and make sure it's updated == what you actually use and gives you problems would help us help you.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Devil Master
Posts: 81
Joined: Wed Apr 23, 2008 8:47 pm

Post by Devil Master »

Okay, here's my whole code. I use two #defines to activate or deactivate parts of the code without having to comment or decomment lots of stuff every time, so:
#ifdef TRIANGLESELECTOR encloses the collision code I copied from this topic
#ifdef THIS_CAUSES_A_CRASH encloses the actual line that causes the program to crash if it is compiled

Code: Select all

#include "irrlicht.h"

using namespace irr;

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

#pragma comment(lib, "Irrlicht.lib")
#define TRIANGLESELECTOR
#define THIS_CAUSES_A_CRASH

int main()
{
 IrrlichtDevice *device = createDevice(EDT_OPENGL,dimension2d<s32>(800,600),32,true,false,false,0);
			  
 IVideoDriver* driver = device->getVideoDriver();
 ISceneManager* smgr = device->getSceneManager();
 IGUIEnvironment* guienv = device->getGUIEnvironment();
 
 smgr->addLightSceneNode(0, core::vector3df(0,0,0),video::SColorf(1.0f,1.0f,1.0f,1.0f), 50000.0f);
 
 // Desert
 IAnimatedMesh* mesh9 = smgr->getMesh("modelli/deserto1.3ds");
 IAnimatedMeshSceneNode* node9 = smgr->addAnimatedMeshSceneNode( mesh9 );
 if (node9)
 {
  node9->setMaterialFlag(EMF_LIGHTING, false);
  node9->setFrameLoop(0, 310);	
  node9->setMaterialTexture( 0, driver->getTexture("modelli/deserto1.bmp") );
  node9->setPosition(core::vector3df(0,-123,272));
 }
 
 // Pyramid
 IAnimatedMesh* mesh10 = smgr->getMesh("modelli/piramide1.x");
 IAnimatedMeshSceneNode* node10 = smgr->addAnimatedMeshSceneNode( mesh10 );
 if (node10)
 {
  node10->setMaterialFlag(EMF_LIGHTING, true);
  node10->setFrameLoop(0, 310);	
  node10->setMaterialTexture( 0, driver->getTexture("modelli/piramide.bmp") );
  node10->setPosition(core::vector3df(0,-2,1000));
  // node10->setScale(core::vector3df(10,10,10));
 }
    
 scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
 camera->setFarValue(120000.0f);
 
 device->getCursorControl()->setVisible(false);

#ifdef TRIANGLESELECTOR
 scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
 scene::ITriangleSelector * selector1 = 0;
 camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
 scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(30,30,30),core::vector3df(0,-3,0),core::vector3df(0,50,0));
 meta->drop();
 camera->addAnimator(anim);
 anim->drop();
 scene::ISceneNodeAnimator* an = smgr->createCollisionResponseAnimator(selector1, camera, core::vector3df(30,30,30),core::vector3df(0,0,0),core::vector3df(0,50,0));

 #ifdef THIS_CAUSES_A_CRASH
 selector1->drop();
 #endif

 camera->addAnimator(an);
 an->drop();
#endif
 
 while(device->run())
 {
  driver->beginScene(true, true, SColor(255,50,101,190));

  smgr->drawAll();
  guienv->drawAll();
  driver->endScene();
 }
 device->drop();
 return 0;
}
For all that's worth, I also uploaded the models I used, and you can get them from here. Just unzip the file to the same directory of the executable. The models and their textures will be placed in a subdirectory called "modelli".
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

:?

Great, now selector1 is 0. How about you actually create a triangle selector before trying to use it?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

Ion Dune wrote:Looks like you want to use a collision response animator and make a meta selector for the level, stairs, etc. Have a look at example 7 in your irrlicht download, or view the tutorial 7 on this website.
uhm... did you read what i wrote??
the tutorial just do the collision, but i have to get the coordinates!
it isn't in the tutorial....

devil master:
actually, you don't put anything to the meta, nor the selector1...
of course it's crashing :D
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

B@z wrote:

Code: Select all


4
| 3
v | 2
_ v | 1
 |_ v |
   |_ v
     |_
____|
can i get the height of the pos 1,2,3,4?
Yes, you can.

The best way to do so will be dependent on your application requirements.

For starters, how are you going to calculate the (XZ) positions for which you need the height? How are you going to store and access those heights?

I ask because if you create a triangle selector for the mesh, then use the standard collision methods to draw a ray down from any given XZ position and test it against the scene nodes and then (if you don't get a hit) against the terrain, then you should be able to find a height fairly easily.

Finding the height is purely a collision issue; 'gravity' doesn't seem to have anything to do with it.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

hi!
how will i store it? just in s32 :D
and i will get the x z from the model pos (node->getPosition()).

so, i want to make a function

Code: Select all

s32 getHeight (s32 x, s32 z)
{
 // get height of the pos x z
return y;
}
yeah, it doesn't have to do anything with the gravity... :D
didn't think about it :D
but i need it for the collision :D i want to make on my own, because the collision response animator isn't too good (when it's colliding, then the charater stops.)

could you give me some code, for getting the height of a model?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sorry, that breaks my "10 minute samples" rule. Well, it's more of a guideline.

The process will be:

Code: Select all

For each (animated) mesh scene node, create a triangle selector and assign it to the node.

Each frame:
  Create a ray from your XZ position pointing straight down.
  For each mesh scene node, perform a ray/bounding box check to see if the ray intersects the node's BB.
    If so:
      Retrieve the node's triangle selector
      Test the ray against it.  If it intersects:
        If it's higher than the current highest point of intersection, it becomes your new height.
        If you're sure that your meshes don't intersect, you can stop checking now.
    
  The highest point of intersection is your height.  If no scene node was intersected, then use the terrain getHeight method to get the height.
Tutorial 7 contains most of that functionality. ISceneCollisionManager contains the rest.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

rogerborg, i love you!!! xD
it's working :P

Code: Select all

f32 CGame::getHeight(IAnimatedMeshSceneNode *node)
{
	f32 height;
	line3d<f32> line;
	vector3df nodepos = player->lower->getPosition();
        // just a little correcting, because it doesn't calculate the  collision, when the line is equal to the collisionpoint
	nodepos.Y += 10;
	line.start = nodepos;
	line.end = vector3df(nodepos.X, map.getHeight(nodepos),nodepos.Z);
	vector3df interselection;
	triangle3df triangle;
	if (smgr->getSceneCollisionManager()->getCollisionPoint(line,node->getTriangleSelector(),interselection,triangle))
	{
		height = interselection.Y;
	}
	else
		height = Game.map.getHeight(nodepos);

	return height;
}
and it climbs up the ladder xD
thank you..
now i have to do it all the models, but it won't be problem, i think

thank you again!
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Wow... great! Happy to help, although I'm sure you'd have figured it out yourself soon. You were almost there, we just needed to refine the problem a bit in order to see the solution.

By the way, there's a handy method, ISceneManager::getSceneNodesFromType() that will create an array of all scene nodes of a given type, i.e. ESNT_ANIMATED_MESH.

You can call this to get an array of all the animated mesh scene nodes that you'll have to check. If your nodes aren't changing during runtime, then you only have to call this once after you've added all your meshes.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

cool :D
but i have an array to the objects in the map, so i don't need it at present..
but maybe i have to use it later!
thx
Post Reply