Page 1 of 1

Crushing the hero (collision detection)

Posted: Tue May 10, 2011 5:03 pm
by CondeNostaw
Short version: How can I know my hero is being crushed using only native Irrlicht animators/functions?

Suppose you're making a third person game. Let's say this is the hero (sorry for the use of crappy art, but english is not my native language and I need these images to express myself):
Image

You want your hero to walk over the terrain, climb stairs and so on. So, you can add a ISceneNodeAnimatorCollisionResponse to his node. This will be no big deal because the hero collision surface can be simplified to a ellipsoid and the terrain has a triangle selector, so you have the hero going up and down:
Image

Now let's say the hero has fallen in a deadly trap. A huge block of solid concrete is moving in his direction and there is nowhere to run to:

Image

The hero stays still, waiting for his certain death. What is supposed to happen? The block is supposed to push the hero to the wall and kill him by crushing him. My questions are:

1. How can the block push the player? The player has a collision response animator, but it won't change his position unless the hero is moving. Try it, and you will see.

2. How can the game system know that the player was crushed (by the block or anything else)?

I know I could solve these problems by using a physics engine or writing my own collision algorithms, but I want to know if it is possible to solve them using only native Irrlicht animators/functions/whatever.

Posted: Tue May 10, 2011 5:16 pm
by serengeor
Just by using what irrlicht provides by default won't do what you need here I think.

1) you could try bbox collisions and move the player manually and also check if it doesn't collide with other block. If it does, the player is "crushed".

2) if you check for bbox collisions yourself you can send some sort of events, if you have event system.

Posted: Tue May 10, 2011 5:26 pm
by CondeNostaw
1) you could try bbox collisions and move the player manually and also check if it doesn't collide with other block. If it does, the player is "crushed".
Let me clarify this: the player can walk over the block if he has the chance. In fact, it might not be a block. It might be a huge cog or a indiana-like killer sphere or even a large hand. That is not supposed to matter because it has a triangle selector and the collision response animator should handle this.

Posted: Tue May 10, 2011 5:40 pm
by serengeor
CondeNostaw wrote:
1) you could try bbox collisions and move the player manually and also check if it doesn't collide with other block. If it does, the player is "crushed".
Let me clarify this: the player can walk over the block if he has the chance. In fact, it might not be a block. It might be a huge cog or a indiana-like killer sphere or even a large hand. That is not supposed to matter because it has a triangle selector and the collision response animator should handle this.
Well, I'm not that much familiar with collision response animator.
*looks at API*
Seems that its possible to do with it.
The animator has:

Code: Select all

virtual void 	setCollisionCallback (ICollisionCallback *callback)=0
 	Sets a callback interface which will be called if a collision occurs.
So if you derive from ICollisionCallback and set it you can receive collisions from specific nodes.

More info:
http://irrlicht.sourceforge.net/docu/cl ... ponse.html

But even though its possible to do it with irrlicht, if you intend to use it in a game, you should use some other physics library for the sake of your CPU.

Posted: Tue May 10, 2011 6:11 pm
by Radikalizm
I really wouldn't use the built-in irrlicht collision functions for this, Irrlicht is a rendering engine, and you clearly need a physics engine, so it'd make your life much easier to just take that option

Posted: Tue May 10, 2011 6:24 pm
by CondeNostaw
So if you derive from ICollisionCallback and set it you can receive collisions from specific nodes.
I'll try this. But even if it works when the hero is still, I will have guess where he is supposed to move and change his position by myself.

I will have enemies walking around and I want them to be crushed as well so it is like an inherited behavior. Does that give any ideas?

Posted: Tue May 10, 2011 6:32 pm
by serengeor
CondeNostaw wrote:
So if you derive from ICollisionCallback and set it you can receive collisions from specific nodes.
I'll try this. But even if it works when the hero is still, I will have guess where he is supposed to move and change his position by myself.

I will have enemies walking around and I want them to be crushed as well so it is like an inherited behavior. Does that give any ideas?
Well, yeah. You will need to move them yourself. Irrlicht doesn't really do much on the "physics" side. You should just grab bullet physics library, or any other library that looks comfortable to use for you and does what you need.
Irrlicht's "physics" is only meant for small scale stuff like node picking, checking intersections between few nodes.

Posted: Tue May 10, 2011 6:51 pm
by nespa
if 2 meshes collided returns true:

Code: Select all

bool nGetMeshesCollide(IAnimatedMeshSceneNode* mesh1,IAnimatedMeshSceneNode* mesh2)
	{ 
		return mesh1->getTransformedBoundingBox().intersectsWithBox(mesh2->getTransformedBoundingBox());
	}


here, an animator between 2 animated meshes, (the cube is the mesh, the hero is the entity):

Code: Select all

void nSetCollisions(IAnimatedMeshSceneNode* mesh,IAnimatedMeshSceneNode* entity,int mode,
											float SX,float SY,float SZ,
											float GX,float GY,float GZ,
											float OFFX,float OFFY,float OFFZ)
	
	{  
		IMeshManipulator* meshManipulator = SceneManager->getMeshManipulator();

		ITriangleSelector* selector;

		switch(mode)
		{
		case(0):
			selector = SceneManager->createTriangleSelectorFromBoundingBox(mesh); 
			break;
		case(1):
			selector = SceneManager->createTriangleSelector(mesh->getMesh(),mesh); 
			break;
		case(2):
			selector = SceneManager->createOctTreeTriangleSelector(mesh->getMesh(),mesh,meshManipulator->getPolyCount(mesh->getMesh()));
			break;
		case(3):
			selector = SceneManager->createTerrainTriangleSelector((ITerrainSceneNode*)mesh); 
			break;
		
		}

		

		mesh->setTriangleSelector(selector);

		
		// create collision response using all objects for collision detection

  		scene::ISceneNodeAnimator* anim =
   		SceneManager->createCollisionResponseAnimator(selector, entity, core::vector3df(SX,SY,SZ), core::vector3df(GX,GY,GZ), core::vector3df(OFFX,OFFY,OFFZ));
    	entity->addAnimator(anim);

	}

Posted: Tue May 10, 2011 6:54 pm
by serengeor
nespa wrote:see here:

Code: Select all

nGetMeshesCollide(IAnimatedMeshSceneNode* mesh1,IAnimatedMeshSceneNode* mesh2)
	{ 
		return mesh1->getTransformedBoundingBox().intersectsWithBox(mesh2->getTransformedBoundingBox());
	}
[/code]
He said he wants per triangle collisions, I think.

Posted: Tue May 10, 2011 9:36 pm
by Mel
Radikalizm wrote:I really wouldn't use the built-in irrlicht collision functions for this, Irrlicht is a rendering engine, and you clearly need a physics engine, so it'd make your life much easier to just take that option
I agree. Irrlicht's collision system is for more basic stuff, like picking up nodes or to navigate a static map without passing through walls, which is something useful for visualization purposes, perhaps, but for a game, it is too little, and too unoptimized.

Posted: Thu May 12, 2011 2:17 am
by CondeNostaw
Ok, I gave up using only Irrlcht functions for this.

For the record, the CollisionCallback does not work in this situation. It just isn't called.