Problem with getCollisionPoint with rotating 3ds mesh

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
Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Problem with getCollisionPoint with rotating 3ds mesh

Post by Ondninja »

Hi i have checked lots of collision threads, and im not sure if some of the solutions also covered my problem.

Here it goes.
I am doing collision detection in a top down 2d/3d space game.

First i make a rough test with bounding box colision between my bullet / missile mesh and the enemies to save CPU for the ray - triangle test.

Then i make a test with:
if(getCollisionPoint(activeProjectiles.getforwardRayFromMe(), triangleSelector, collisionPoint, triangle))

Where getforwardRayFromMe() is a short ray going forward from the projectile.

This usally works fine, but now that a have made a rotating end of level Boss, the problem is that at around half the boss the collision dosen't work.


I tried to make a simple Cube model in 3ds to test.
It is like the collision only works on 50% of every side.

Code: Select all

----------
|          |
|          | 
|          |
----------
   \   /
   A  B
For example if the projectile is A it is hitting the cube. But B would go right through.

I have thought about the following causees:
- Something about culling, because often the side cannot be seen graphically. But it should not have anything to do the the actual vertices which still exists even if the renderer culls them.

- The normals of the cube.


The code: excluding the outer bounding box check

Code: Select all

triangleSelector = device->getSceneManager()->createTriangleSelector(boss->node->getMesh()->getMesh(0),boss->node);	boss->node->setTriangleSelector(triangleSelector);
core::triangle3df triangle;
vector3df collisionPoint;
if(device->getSceneManager()->getSceneCollisionManager()->getCollisionPoint(activeProjectiles[i].getforwardRayFromMe(), triangleSelector, collisionPoint, triangle))
{

}

Can you direct me towards the source of the problem?
Is it the model format, or the rotating mesh that is the cause?

							


(I use Irrlicht 1.3.1)
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Kind of hard to tell without having access to the model and a runnable sample, but the obvious thing is that

Code: Select all

createTriangleSelector(boss->node->getMesh()->getMesh(0), ...
Will create a triangle selector for the 0th frame of mesh, and the selector will not animate with the mesh.

That's pre-1.4, of course. With the new skinned meshes, getMesh(X) doesn't even get you the right mesh, so collision detection is totally boned now. Ho ho.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Post by Ondninja »

Hi. I only use static meshes. But I made all meshes IAnimatedMesh so that i later could animate if neccesary.

So i thought it should be the same if a used the 0 frame, when it's all there are?

I have just changed my code to use IMeshSceneNode all over. It gave the same result.

It's like there are some blind angles where it goes right through.

Should i update something after each rotation?
From what does the triangle selecter builds it's list? From the untransformed mesh?
Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Post by Ondninja »

I have tried to modify the collision tutorial, and it seems that i can create a similar effect there.

one of the faeries is slowly rotating. If you keep pointing at her wing forexample, it is successfully colliding with most of the time. But at some angles after the wing have passed you there is again a "blind spot".

The blindspot is not when you are looking along the wing, which would be the most obvious. It is when you can see most of it.

Here is the modified example, (with UGLY code modifications and iinstantiations every update etc.)

I have placed the camera in the right position. If you don't touch the mouse it should point right at the faeries wing. When one of the wings comes towards you notice that there are some times when the red triangle is now drawn.

Code: Select all


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

using namespace irr;

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


int main()
{
	// let user select driver type

	video::E_DRIVER_TYPE driverType;

	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");

	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;
		case 'b': driverType = video::EDT_DIRECT3D8;break;
		case 'c': driverType = video::EDT_OPENGL;   break;
		case 'd': driverType = video::EDT_SOFTWARE; break;
		case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
		case 'f': driverType = video::EDT_NULL;     break;
		default: return 0;
	}

	// create device

	IrrlichtDevice *device =
		createDevice(driverType, core::dimension2d<s32>(640, 480), 16, false);
		
	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	
	device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");

	
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* q3node = 0;
	
	if (q3levelmesh)
		q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

	/*
	So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here
	comes something different: We create a triangle selector. A triangle selector
	is a class which can fetch the triangles from scene nodes for doing different
	things with them, for example collision detection. There are different triangle
	selectors, and all can be created with the ISceneManager. In this example,
	we create an OctTreeTriangleSelector, which optimizes the triangle output a l
	little bit by reducing it like an octree. This is very useful for huge meshes
	like quake 3 levels.
	Afte we created the triangle selector, we attach it to the q3node. This is not
	necessary, but in this way, we do not need to care for the selector, for example
	dropping it after we do not need it anymore.
	*/

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

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
		selector->drop();
	}


	/*
	We add a first person shooter camera to the scene for being able to move in the quake 3
	level like in tutorial 2. But this, time, we add a special animator to the 
	camera: A Collision Response animator. This thing modifies the scene node to which
	it is attached to in that way, that it may no more move through walls and is affected
	by gravity. The only thing we have to tell the animator is how the world looks like,
	how big the scene node is, how gravity and so on. After the collision response animator
	is attached to the camera, we do not have to do anything more for collision detection,
	anything is done automaticly, all other collision detection code below is for picking.
	And please note another cool feature: The collsion response animator can be attached
	also to all other scene nodes, not only to cameras. And it can be mixed with other
	scene node animators. In this way, collision detection and response in the Irrlicht
	engine is really, really easy.
	Now we'll take a closer look on the parameters of createCollisionResponseAnimator().
	The first parameter is the TriangleSelector, which specifies how the world, against
	collision detection is done looks like. The second parameter is the scene node, which
	is the object, which is affected by collision detection, in our case it is the camera.
	The third defines how big the object is, it is the radius of an ellipsoid. Try it out 
	and change the radius to smaller values, the camera will be able to move closer to walls
	after this. The next parameter is the direction and speed of gravity. You could
	set it to (0,0,0) to disable gravity. And the last value is just a translation: Without
	this, the ellipsoid with which collision detection is done would be around the camera,
	and the camera would be in the middle of the ellipsoid. But as human beings, we are 
	used to have our eyes on top of the body, with which we collide with our world, not
	in the middle of it. So we place the scene node 50 units over the center of the 
	ellipsoid with this parameter. And that's it, collision detection works now. 
	*/

	scene::ICameraSceneNode* camera = 
		smgr->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, 0, 0, true);
	camera->setPosition(core::vector3df(-40,34,-79));
	

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

	/*
	Because collision detection is no big deal in irrlicht, I'll describe how to
	do two different types of picking in the next section. But before this,
	I'll prepare the scene a little. I need three animated characters which we 
	could pick later, a dynamic light for lighting them,
	a billboard for drawing where we found an intersection,	and, yes, I need to
	get rid of this mouse cursor. :)
	*/

	// disable mouse cursor

	device->getCursorControl()->setVisible(false);

	// add billboard

	scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
	bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
	bill->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
	bill->setMaterialFlag(video::EMF_LIGHTING, false);
	bill->setMaterialFlag(video::EMF_ZBUFFER, false);
	bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));

	// add 3 animated faeries.

	video::SMaterial material;
	material.Textures[0] = driver->getTexture("../../media/faerie2.bmp");
	material.Lighting = true;

	scene::IAnimatedMeshSceneNode* node1 = 0;
	scene::IAnimatedMeshSceneNode* node2 = 0;
	scene::IAnimatedMeshSceneNode* node3 = 0;
	scene::IAnimatedMesh* faerie = smgr->getMesh("../../media/faerie.md2");

	if (faerie)
	{
		node1 = smgr->addAnimatedMeshSceneNode(faerie);
		node1->setPosition(core::vector3df(-70,0,-90));
		//node->setMD2Animation(scene::EMAT_RUN);
		node1->getMaterial(0) = material;

		node2 = smgr->addAnimatedMeshSceneNode(faerie);
		node2->setPosition(core::vector3df(-70,0,-30));
		//node->setMD2Animation(scene::EMAT_SALUTE);
		node2->getMaterial(0) = material;

		node3 = smgr->addAnimatedMeshSceneNode(faerie);
		node3->setPosition(core::vector3df(-70,0,-60));
		//node->setMD2Animation(scene::EMAT_JUMP);
		node3->getMaterial(0) = material;
	}

	material.Textures[0] = 0;
	material.Lighting = false;


	selector = smgr->createOctTreeTriangleSelector(faerie->getMesh(0), node1, 128);
	node1->setTriangleSelector(selector);
	selector->drop();	// Add a light

	smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
		video::SColorf(1.0f,1.0f,1.0f,1.0f),
		600.0f);


	/*
	For not making it to complicated, I'm doing picking inside the drawing loop.
	We take two pointers for storing the current and the last selected scene node and 
	start the loop.
	*/


	scene::ISceneNode* selectedSceneNode = 0;
	scene::ISceneNode* lastSelectedSceneNode = 0;

	camera->setRotation(core::vector3df(26,271,0));
	
	int lastFPS = -1;

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, 0);

		smgr->drawAll();

		/*
		After we've drawn the whole scene whit smgr->drawAll(), we'll do the first
		picking: We want to know which triangle of the world we are looking at. In addition,
		we want the exact point of the quake 3 level we are looking at.
		For this, we create a 3d line starting at the position of the camera and going 
		through the lookAt-target of it. Then we ask the collision manager if this line
		collides with a triangle of the world stored in the triangle selector. If yes,
		we draw the 3d triangle and set the position of the billboard to the intersection 
		point.
		*/
		node1->updateAbsolutePosition();
		node1->setAnimationSpeed(0);
		
		core::vector3df currentRoation = node1->getRotation();
		core::vector3df rot = core::vector3df(0,0.2f,0);

		currentRoation += rot;
		node1->setRotation(currentRoation);

		selector = smgr->createOctTreeTriangleSelector(faerie->getMesh(0), node1, 128);
	node1->setTriangleSelector(selector);
	selector->drop();	// Add a light

		core::line3d<f32> line;
		line.start = camera->getPosition();
		line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

		core::vector3df intersection;
		core::triangle3df tri;

		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));
		}


		/*
		Another type of picking supported by the Irrlicht Engine is scene node picking
		based on bouding boxes. Every scene node has got a bounding box, and because of
		that, it's very fast for example to get the scene node which the camera looks
		at. Again, we ask the collision manager for this, and if we've got a scene node,
		we highlight it by disabling Lighting in its material, if it is not the 
		billboard or the quake 3 level.
		*/

		selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromCameraBB(camera);

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

		if (selectedSceneNode == q3node || selectedSceneNode == bill)
			selectedSceneNode = 0;

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

		lastSelectedSceneNode = selectedSceneNode;


		/*
		That's it, we just have to finish drawing.
		*/

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
		  core::stringw str = L"Collision detection example - Irrlicht Engine [";
		  str += driver->getName();
		  str += "] FPS:";
		  str += camera->getRotation().X;
		  str += "#";
		  str += camera->getRotation().Y;
		  str += "#";
		  str += camera->getRotation().Z;

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

	device->drop();
	
	return 0;
}



Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Post by Ondninja »

I will just add that if you don't see the error from where you start, try moving a bit to the the left and look toward at the "row" of feries.

Sometimes that is where the error is most obvious.

A am beginning to think it is something with rotation.

I tested with the animation. It works fine. when the ferie jumps the red triangle follow up with her. (if i call createOctTreeTriangleSelector every frame it is)

But the rotation seems to do something.

I have tried to write debug information in isOnSameSide and isPointInside which is called by getIntersectionWithLine which again is called by getCollisionPoint method. But i have not untill now been able to find something yet.
Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Post by Ondninja »

I have testet the ray - triangle intersection with an implementation of the Barycentric coordinate method just to check, if it has something to do with that calculation. But fortunately it gave the same result, as the existing "getIntersectionWithLine".

I would like if someone would just quickly try to copy paste the modified example code above, to see the result.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

AFAICT, the problem is the use of an oct tree triangle selector. It seems to be occluding itself in unexpected ways. Given the time that it takes to create one, it's not something that you'd want to do every frame anyway.

The code below (elided to its basics) should be closer to what you want. It creates a new triangle selector from the current animated mesh each frame. As getMesh(x) is currently borked for boned meshes, the code below will only work for unboned meshes.

Note that it allocates and destroys a new triangle selector each frame, which is fragmentastic. I have a patch languishing in the tracker to make triangle selectors self-animating, but it also relies on getMesh(x) working. :(

Code: Select all

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

using namespace irr;

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


int main()
{
   // create device
   IrrlichtDevice *device =
      createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480), 32, false);
      
   if (device == 0)
      return 1; // could not create selected driver.

   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

   scene::ICameraSceneNode* camera =
      smgr->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, 0, 0, true);
   
   device->getCursorControl()->setVisible(false);

   // add an animated faerie
   video::SMaterial material;
   material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
   material.Lighting = false;

   scene::IAnimatedMeshSceneNode* node1 = 0;
   scene::IAnimatedMesh* faerie = smgr->getMesh("../../media/faerie.md2");

   if (faerie)
   {
      node1 = smgr->addAnimatedMeshSceneNode(faerie);
      node1->setPosition(core::vector3df(0,0,40));
      node1->setMD2Animation(scene::EMAT_RUN);
      node1->getMaterial(0) = material;
   }

   material.setTexture(0, 0);

   while(device->run())
   if (device->isWindowActive())
   {
      driver->beginScene(true, true, 0);

  	  node1->setRotation(node1->getRotation() + core::vector3df(0,0.2f,0));

	  smgr->drawAll();

      core::line3d<f32> line;
      line.start = camera->getPosition();
      line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

      core::vector3df intersection;
      core::triangle3df tri;

	  scene::ITriangleSelector * selector 
		  = smgr->createTriangleSelector(faerie->getMesh((s32)node1->getFrameNr()), node1);

      if (smgr->getSceneCollisionManager()->getCollisionPoint(
         line, selector, intersection, tri))
      {
         driver->setTransform(video::ETS_WORLD, core::matrix4());
         driver->setMaterial(material);
         driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
      }

      selector->drop();

      driver->endScene();
   }

   device->drop();
   
   return 0;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Ondninja
Posts: 20
Joined: Tue Aug 14, 2007 10:58 am

Post by Ondninja »

Ok thanks.

I am not using any animation. And will not be using bone animation.
And i need not check every frame, because it is only when a bounding box intersection is true i check.

So how about if a manually run through all the faces of the mesh, when i need to? Then the triangles must be positiones where they really are according to the nodes orientation right? Or is the problem deeper down, so that the triangle information available is not always up to date?

It does not matter if I use an occtreeselector or another selector.
All meshes have only reasonable few faces.

(Maybe i misunderstood the point of your last answer? Did you tell me how i got it to work or did you say it sometimes fails in the current implementation?)
Post Reply