My Interpretation of the collision detection example!

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
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

My Interpretation of the collision detection example!

Post by renegadeandy »

Hi!

I have just followed the collision detection example - and have tried to put it into practice within my code.

Now, nothing crashes - however no red triangles are drawn over the highlighted polys and I need a bit of help to try to figure out why this is the case.

The main code is this :

Code: Select all

#include "main.h"
#include "Buster.h"

// Irrlicht Namespaces

using namespace irr;


scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0; 


int main()

{
	/////SETUP CORE DEVICE AND DRIVER SCENE MANAGER AND GUIENV AND EVENT RECEIVERS///// 
	device = createDevice(EDT_OPENGL,dimension2d<s32>(512, 512),16,false,false,0);
	  
	MainEventReceiver receiver;
	
	HandleKeyEvents keyEvents;
	
	receiver.AddEventReceiver(&keyEvents);
	
	device->setEventReceiver(&receiver);
	
	device->setWindowCaption(L"Valley Attack - Irrlicht engine");
    
    IVideoDriver* driver = device->getVideoDriver();

    ISceneManager* smgr = device->getSceneManager();

    IGUIEnvironment* guienv = device->getGUIEnvironment();

    //ENDING SETUP OF CORE THINGS
    
    Buster bust(smgr,driver,"Bob",50,1);
    bust.hurt(5000);
  
////////SETUP HEIGHTMAP TERRAIN
   IAnimatedMesh* terrain = smgr->addTerrainMesh("Main_Ground",driver->createImageFromFile("C:/irrlicht-1.4/irrlicht-1.4/media/terrain-texture.jpg"),driver->createImageFromFile("C:/Documents and Settings/Andy/workspaceC++/Valley_Attack/heightMaps/heightmap512x512.bmp"),core::dimension2d< f32 >(20.0f, 20.0f),1000.0f,core::dimension2d< s32 >(64, 64));

scene::IAnimatedMeshSceneNode* terrainNode = smgr->addAnimatedMeshSceneNode(terrain); 


terrainNode->setMaterialFlag(video::EMF_LIGHTING, false);
  terrainNode->setScale(core::vector3df(10,10,10));
  terrainNode->setMaterialFlag(video::EMF_WIREFRAME, true);
  terrainNode->setMaterialTexture(1, driver->getTexture("C:/irrlicht-1.4/irrlicht-1.4/media/detailmap3.jpg"));
 
//DONE SETTING UP HEIGHTMAP TERRAIN


///////SETUP CAMERA AND STARTING POSITIONS
RTSCamera* camera = new RTSCamera(device,smgr->getRootSceneNode(),smgr,-1,1000.0f,10.0f,10.0f); 

camera->setTarget(vector3df(2352,2862,-1317));
camera->setROTX(91);	//setup camera to show a nice angle on things!
camera->setROTY(-330);
////DONE SETTING UP CAMERA STUFF


//camera->setTarget(bust.node->getPosition());
  
guienv->addStaticText(L"Hello World! This is the Irrlicht software engine running Valley Attacks first trial!",rect<int>(10,10,200,30), true, true, 0, -1);




//////////TRIAL SETTING UP collision detection bellow

scene::ITriangleSelector* selector = 0;
	
	if (terrainNode)
	{		
		//q3node->setPosition(core::vector3df(-1370,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(
            terrain->getMesh(0), terrainNode, 128);
		terrainNode->setTriangleSelector(selector);
	}

scene::ISceneNodeAnimator* anim =smgr->createCollisionResponseAnimator(
			selector, camera, core::vector3df(30,50,30),
			core::vector3df(0,-3,0),
			core::vector3df(0,50,0));

			selector->drop();

		camera->addAnimator(anim);
		anim->drop();
	

		scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
					bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
					bill->setMaterialTexture(0, driver->getTexture(
				         "C:/irrlicht-1.4/irrlicht-1.4/media/particle.bmp"));
					bill->setMaterialFlag(video::EMF_LIGHTING, false);
					bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));
		
	//collision detection end
	
		
		
		
		
		scene::ISceneNode* selectedSceneNode = 0;
		scene::ISceneNode* lastSelectedSceneNode = 0;
  
		
		
		
		
		
		core::line3d<f32> line;
		core::vector3df intersection;
					core::triangle3df tri;

    while(device->run())

    { 
    	
    
int fps = driver->getFPS();
 core::stringw str =fps;
 
device->setWindowCaption(str.c_str());




driver->beginScene(true, true, SColor(255,100,101,140));

	smgr->drawAll();

	//HIGHLIGHT THE SELECTED NODE
	
	
	//test

			line.start = camera->getPosition();
			line.end = line.start +
	         (camera->getTarget() - line.start).normalize() * 1000.0f;

			
			if (smgr->getSceneCollisionManager()->getCollisionPoint(
				line, selector, intersection, tri))
			{
				bill->setPosition(intersection);
					
				driver->setTransform(video::ETS_WORLD, core::matrix4());
				//driver->setMaterial(material);
				driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
			}
	//end test
	
	
		selectedSceneNode = smgr->getSceneCollisionManager()->
	          getSceneNodeFromCameraBB(camera);

			if (lastSelectedSceneNode)
				lastSelectedSceneNode->setMaterialFlag(
	                video::EMF_LIGHTING, true);

			if (selectedSceneNode == terrainNode)
	        
				selectedSceneNode = 0;

			if (selectedSceneNode)
				selectedSceneNode->setMaterialFlag(
	               video::EMF_LIGHTING, false);

			lastSelectedSceneNode = selectedSceneNode;
		//END OF HIGHLIGHTED
	
	
	
	
	guienv->drawAll();
	
	
	
	
	driver->endScene();
 
	printf("Camera x:%i, y:%i, z:%i",(int)camera->getAbsolutePosition().X,(int)camera->getAbsolutePosition().Y,(int)camera->getAbsolutePosition().Z);

	//printf("Field of view : %i",(int)camera->getROTX())  ;

	}

 

    device->drop();



    return(0);

}

I am unsure what this billboard is for - and have commented out the line using the material - material - was unsure what it was for.

Please offer some advice!

And
radiant
Posts: 112
Joined: Fri Feb 22, 2008 8:04 pm
Location: Mexico

Post by radiant »

you should really take a look at the API documentacion in the irrlicht/doc folder, most questions should be solved in there, and u should debug your app to see wich lines are being procesed and wich arent
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

All lines are being processed - there is no reason for them not to be.

If there is no errors, and nothing stopping line execution - how will the manual help!?
radiant
Posts: 112
Joined: Fri Feb 22, 2008 8:04 pm
Location: Mexico

Post by radiant »

well... there are a few if statements... wich might be returning false and thus result in the lines inside not being procesed...

and the api would hel in telling u for instance what a billboard is... or what

//driver->setMaterial(material);

does... that sorth of stuff

and one more thing, people usually wont debug your app for you, most times all errors in an app is because u are not doing something as u should be doing it... the api also tells u how to use most classes functions..

best regards...
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

You need driver->setMaterial(material) in there, why did you comment it out? It tells the driver what material to use to draw the lines, use the same material as in the tutorial/example, otherwise the driver just uses the last material it was told to use (probably by another scene node) and that's probably a bad material to use for drawing lines/triangles, hence why you can't see them.
Image Image Image
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

after you use the material to dress the faires actually its set back to 0, meaning it has next tio no use.....also it doesnt explain why collision detection also aint working with the camera and the terrain
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

So you're agreeing that you definetly need driver->setMaterial in there? Seeing as a null material would give the driver nothing to draw the lines with...
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

renegadeandy wrote:after you use the material to dress the faires actually its set back to 0, meaning it has next tio no use.....also it doesnt explain why collision detection also aint working with the camera and the terrain
That's not what you said though. What you said was that the triangle wasn't visible. JP has diagnosed the problem that you reported.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

Yeah that now works,

HOweveer collision with the terrain still doesnt work - works with the fps cam however, so I am thinking there is something wrong with the camera now, I posted that in the code snippet section and pmed the creator but I have a bad feeling that he isnt active anymore.
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

Could somebody please offer some advice as to exactly why my camera doesnt work with collision detection with the terrain. Are there any other decent RTS Style camers which I could try instead. I have tried contacting the original developer several times but I think he is now inactive within this community.

Thanks

Andy
Post Reply