Page 1 of 1

Help with the collision

Posted: Fri Feb 01, 2008 4:53 pm
by chaotischerapostel
This is the code.

Code: Select all

#include <irrlicht.h>
#include <iostream>

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

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

int main()
{
	//Create Irrlicht, the driver, the scene manager, and triangle selectors
	IrrlichtDevice *device = 
		createDevice(EDT_DIRECT3D9, dimension2d<s32>(512, 384), 32,
			false, false, false, 0);
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IMetaTriangleSelector* metaSelector; 
	ITriangleSelector* selector = 0; 
		// Create the meta selector 
		metaSelector = smgr->createMetaTriangleSelector();

	//Hide the mouse
	device->getCursorControl()->setVisible(false);

	//The walls, ground
	IAnimatedMesh* box = smgr->getMesh("../Amphitheatrum Flavium/media/Coliseum.b3d");
	ISceneNode* node1 = 0;
	if (box) 
		node1 = smgr->addOctTreeSceneNode(box->getMesh(0),0,-1,256,true);
	if (node1) 
	{   // Create a Octree triangle selector 
		selector = smgr->createOctTreeTriangleSelector(box->getMesh(0), node1, 256); 
		node1->setTriangleSelector(selector); 
		// Then add it to the metaselector 
		metaSelector->addTriangleSelector(selector); 
		selector->drop(); 
	}

	//Skybox
	driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
	smgr->addSkyBoxSceneNode(
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_up.jpg"),
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_dn.jpg"),
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_lf.jpg"),
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_rt.jpg"),
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_ft.jpg"),
		driver->getTexture("../Amphitheatrum Flavium/media/irrlicht2_bk.jpg"));

	//The camera
	ICameraSceneNode* camera =
		smgr->addCameraSceneNodeFPS(0,100.0f,2000.0f);
	camera->setPosition(vector3df(0,50,0));
	camera->setTarget(vector3df(2397*2,343*2,2700*2));
	camera->setFarValue(20000.0f);
	//Create response animator for the camera
	ISceneNodeAnimatorCollisionResponse* animator;
	animator = smgr->createCollisionResponseAnimator( 
		metaSelector, camera, 
		vector3df(10,45,10), 
		vector3df(0,0,0), 
		vector3df(0,24,0)); 
	camera->addAnimator(animator); 
	animator->drop(); 
		metaSelector->drop();

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

		smgr->drawAll();

		driver->endScene();
	}
	device->drop();

	return 0;
}
I am kind of confused on whats going wrong. The mesh is just a flat floor with 4 walls surrounding it without a top. But I am only getting collision detection for the wall, the floor is not being detected. I am using a metaselector so that when there are more objects added I can just add them to the meta and be done with it, but I thought mabye that could cause a problem, beacuse it is only adding another step for the engine at this point. Could someone please make an atempt at helping me.

Posted: Fri Feb 01, 2008 5:36 pm
by rogerborg
Good news: the code looks fine!

Bad news: so I don't know what's going wrong!

If I simply replace the getMesh for your Coliseum.b3d mesh with this:

Code: Select all

device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
IAnimatedMesh* box = smgr->getMesh("20kdm2.bsp");
Then collision on that quake level works just fine, without any other code changes. All the object refCounts are what I'd expect, the code looks right, it behaves right (with a different model), so unless there's something screwy about the way getMesh(0) is working for that particular .b3d mesh, I'm out of ideas.

I'd suggest stepping into selector = smgr->createOctTreeTriangleSelector(box->getMesh(0), node1, 256); and seeing if the mesh returned from getMesh(0) seems to have sensible meshbuffers and vertices. Are you comfortable with doing that?

Posted: Fri Feb 01, 2008 6:09 pm
by chaotischerapostel
Opps, sorry looking back at the code I noticed that there is no gravity. Without out gravity I have no problem but as soon as I add gravity the bottom of the mesh gets no gravity. I also tryed turning of the Y-axis gravity and adding it to the X-axis, which send the camera right into the walls, the walls still gets detected but I can move the camera right through the floor. BTW it has 12 vertices, but I'm not sure what a mesh bufer is.