Page 1 of 1

Help with collisions via Rays [SOLVED]

Posted: Sat Mar 26, 2011 12:24 am
by sephoroph
Alright guys i need help getting rays to work.

First and foremost i did use the 'search button' and did not find anything to help me.. many examples but nothing that helped

I'm trying to get collision detection with my terrain which is a obj mesh thrown into a coppercube scene and then exported to .irr

Here is a cube and the terrain i'm trying to cast a ray to:
Image


I am trying to cast a ray from a cube on the map to 9,000 units below the the cube and praying that it hits the terrain

Here is the code of me initializing the cube

Code: Select all

IAnimatedMesh* cube = smgr->getMesh("../media/cube.obj");
if (!cube)
{
	device->drop();
}
p = smgr->addAnimatedMeshSceneNode(cube);

Here is the code initializing the terrian

Code: Select all

smgr->loadScene("../media/scene.irr");

	core::array<scene::ISceneNode *> nodes;
	smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes); // Find all nodes
	scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
	for (u32 i=0; i < nodes.size(); ++i)
	{
		scene::ISceneNode * node = nodes[i];
		scene::ITriangleSelector * selector = 0;

		switch(node->getType())
		{
		case scene::ESNT_CUBE:
		case scene::ESNT_ANIMATED_MESH:
			// Because the selector won't animate with the mesh,
			// and is only being used for camera collision, we'll just use an approximate
			// bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0)
			selector = smgr->createTriangleSelectorFromBoundingBox(node);
		break;

		case scene::ESNT_MESH:
		case scene::ESNT_SPHERE: // Derived from IMeshSceneNode
			selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
			break;

		case scene::ESNT_TERRAIN:
			selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
			break;

		case scene::ESNT_OCTREE:
			selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
			break;

		default:
			// Don't create a selector for this node type
			break;
		}

		if(selector)
		{
			// Add it to the meta selector, which will take a reference to it
			meta->addTriangleSelector(selector);
			// And drop my reference to it, so that the meta selector owns it.
			selector->drop();
		}
	}
And here is the collision detection i was trying to set up:

Code: Select all

core::line3d<f32> ray;
vector3df pos;
vector3df pos2;
pos  = p->getPosition();
pos2 = p->getPosition();
pos2.Y-=9000;
	
ray.start =pos;
ray.end = pos2;
vector3df a;
ray.getClosestPoint(a);
	
core::vector3df intersection;
core::triangle3df hitTriangle;

scene::ISceneNode * selectedSceneNode =
collMan->getSceneNodeAndCollisionPointFromRay(ray,intersection,hitTriangle);
after the call to the collision manager collMan intersection = (0,0,0)
and ray.getClosestPoint the a = 0,0,0


I am confused on why my ray is not picking up anything on the terrain..

If you need more information to help me solve this just let me know.

Thanks in advance

Posted: Sat Mar 26, 2011 8:05 am
by nespa
where is SetTriangleSelector?

Code: Select all

Node->setTriangleSelector(selector);
to bind the selector to your node, then drop the selector

see Collision example 07

[/code]

Posted: Sat Mar 26, 2011 5:13 pm
by sephoroph
Sick poop Nespa, I think it worked. I owe you big time haha :D