program crashes while adding terain triangle selector

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
bochek
Posts: 12
Joined: Mon Dec 19, 2005 7:20 pm

program crashes while adding terain triangle selector

Post by bochek »

THIS IS WEIRD!!!

dont know why its doing this but when i try to make a triangle selector the program generates a microsoft error report and closes.

i have debugged the program down to the line of code that is generating the error.

mapSelector = smgr->createTerrainTriangleSelector(terrain, 0);

mapSelector is a pointer in the private: area of a class called game, the code is part of the same class. so there should be no access problems, and it seems to work with all of the other pointers i have.

my code is as followes

Code: Select all

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

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

class game
{
      public:
             void setup()
             {
                  device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600),32,false);
                  printf("DONE!");
                  smgr = device->getSceneManager();
                  driver = device->getVideoDriver();
                  env = device->getGUIEnvironment();
             }
             void addCamera()
             {
                //initate the key map	
                SKeyMap keyMap[8];
                
                //fill the key map
                keyMap[0].Action = EKA_MOVE_FORWARD;
                keyMap[0].KeyCode = KEY_UP;
                keyMap[1].Action = EKA_MOVE_FORWARD;
                keyMap[1].KeyCode = KEY_KEY_W;
                            
                keyMap[2].Action = EKA_MOVE_BACKWARD;
                keyMap[2].KeyCode = KEY_DOWN;
                keyMap[3].Action = EKA_MOVE_BACKWARD;
                keyMap[3].KeyCode = KEY_KEY_S;
                            
                keyMap[4].Action = EKA_STRAFE_LEFT;
                keyMap[4].KeyCode = KEY_LEFT;
                keyMap[5].Action = EKA_STRAFE_LEFT;
                keyMap[5].KeyCode = KEY_KEY_A;
                            
                keyMap[6].Action = EKA_STRAFE_RIGHT;
                keyMap[6].KeyCode = KEY_RIGHT;
                keyMap[7].Action = EKA_STRAFE_RIGHT;
                keyMap[7].KeyCode = KEY_KEY_D;
                
                camera = smgr->addCameraSceneNodeFPS(0, 100, 500, -1, keyMap, 8);
               	camera->setPosition(core::vector3df(1900*2,300*2,3700*2));
            	camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
            	camera->setFarValue(12000.0f);
             }
             
             void hideCursor()
             {
                  device->getCursorControl()->setVisible(false);
             }
             
             void addTerain()
             {
                scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode( "media/moonmap2.bmp");
                terrain->setScale(core::vector3df(60, 5, 60));
                terrain->setMaterialFlag(video::EMF_LIGHTING, true);
                terrain->setMaterialTexture(0, driver->getTexture("media/moontexture.jpg"));
                terrain->setMaterialTexture(1, driver->getTexture("media/detailmap3.jpg"));

              }
              
              void addTerrinTriangleSelector()
              {
                mapSelector = smgr->createTerrainTriangleSelector(terrain, 0);
                terrain->setTriangleSelector(mapSelector);
                mapSelector->drop();
                
                scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
                mapSelector, camera, core::vector3df(60,100,60),
                core::vector3df(0,-1,0), 
                core::vector3df(0,50,0));
                camera->addAnimator(anim2);
              }
              
              void addCameraCollision()
              {
 
              }
              
              void run()
              {
                int lastFPS =-1;
            	while(device->run())
            	if (device->isWindowActive())
            	{
            		driver->beginScene(true, true, 0 );
            
            		smgr->drawAll();
            		env->drawAll();
            
            		driver->endScene();
            
            
            		int fps = driver->getFPS();
            		if (lastFPS != fps)
            		{
            			core::stringw str = L"Terrain Renderer - Irrlicht Engine [";
            			str += driver->getName();
            			str += "] FPS:";
            			str += fps;
            
            			device->setWindowCaption(str.c_str());
            			lastFPS = fps;
            		}
            	}
            
            	device->drop();
             }
      private:
              
                IrrlichtDevice* device;
                scene::ISceneManager* smgr;
            	video::IVideoDriver* driver;
            	gui::IGUIEnvironment* env;
            	scene::ICameraSceneNode* camera;
            	scene::ITerrainSceneNode* terrain;
	protected:
            	scene::ITriangleSelector* mapSelector;
};

int main()
{
    game Game;
    Game.setup();
    Game.addCamera();
    Game.hideCursor();
    Game.addTerain();
    Game.addTerrinTriangleSelector();
    Game.addCameraCollision();
    Game.run();
    system("PAUSE");
    return 0;
}

dont know why this is happening, any help would be appreciated.

Bochek
Guest

Post by Guest »

Code: Select all

 void addTerrinTriangleSelector() 
              { 
                mapSelector = smgr->createTerrainTriangleSelector(terrain, 0); 
                terrain->setTriangleSelector(mapSelector); 
                mapSelector->drop(); 
                
                scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator( 
                terrain->getTriangleSelector(), camera, core::vector3df(60,100,60), 
                core::vector3df(0,-1,0), 
                core::vector3df(0,50,0)); 
                camera->addAnimator(anim2); 
              } 
the reason is when you created your terrain and you want to use the terrain triangle selector you have to use terrain->getTriangleSelector after you created the triangle selector.

i dont know the hell why it is like it is, but this solution works for me :) [/quote]
bochek
Posts: 12
Joined: Mon Dec 19, 2005 7:20 pm

Post by bochek »

i wish i knew what you where trying to say there, it just doesn't make sence to me, are you telling me to change

Code: Select all

terrain->setTriangleSelector(mapSelector);
to

Code: Select all

 terrain->getTriangleSelector(mapSelector);
???

Bochek[/code]
Guest

Post by Guest »

no no thats wrong :)

you have to change the way you create the camera collision animator in addTerrinTriangleSelector:

from:
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
mapSelector, camera, core::vector3df(60,100,60),
core::vector3df(0,-1,0),
core::vector3df(0,50,0));
camera->addAnimator(anim2);

to:
scene::ISceneNodeAnimator* anim2 = smgr->createCollisionResponseAnimator(
terrain->getTriangleSelector(), camera, core::vector3df(60,100,60),
core::vector3df(0,-1,0),
core::vector3df(0,50,0));
camera->addAnimator(anim2);



that could be the solution (but it could be something else too).
try it out and report if it worked or not :)

good luck, see you!
Guest

Post by Guest »

interesting enough, it did solve the crashing problem, but for some strange reason, there is no collision now :S

Bochek
bochek
Posts: 12
Joined: Mon Dec 19, 2005 7:20 pm

Post by bochek »

oops, my bad, it still does crash, i accadently had that one part commented out. so still the same problem

and its crashing before that

at the part

Code: Select all

 mapSelector = smgr->createTerrainTriangleSelector(terrain, 0);
bochek[/code]
bochek
Posts: 12
Joined: Mon Dec 19, 2005 7:20 pm

Post by bochek »

anyone?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

This :

Code: Select all

void addTerain() 
             { 
                scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode( "media/moonmap2.bmp"); 
                terrain->setScale(core::vector3df(60, 5, 60)); 
                terrain->setMaterialFlag(video::EMF_LIGHTING, true); 
                terrain->setMaterialTexture(0, driver->getTexture("media/moontexture.jpg")); 
                terrain->setMaterialTexture(1, driver->getTexture("media/detailmap3.jpg")); 

              } 
should be

Code: Select all

void addTerain() 
             { 
                terrain = smgr->addTerrainSceneNode( "media/moonmap2.bmp"); 
                terrain->setScale(core::vector3df(60, 5, 60)); 
                terrain->setMaterialFlag(video::EMF_LIGHTING, true); 
                terrain->setMaterialTexture(0, driver->getTexture("media/moontexture.jpg")); 
                terrain->setMaterialTexture(1, driver->getTexture("media/detailmap3.jpg")); 

              } 
terrain is declared as class member function, but since you redeclare the variable in the function, then it's not saved to the class data member, it's saved to the local variable in that function. So the function addTerrinTriangleSelector( which is mis-spelled BTW :P ), is accessing the terrain class variable which is both un-initialized( very dangerous, need a contructor for game class ) and is never set to an actual terrain node, which is why it crashes.
Image
Post Reply