Ogre3d Engine, First Person Collision Detection

Started by
5 comments, last by hybridhaylee 10 years, 3 months ago
Hey there!
I am currently working on a jumping puzzle game concept based on a first person view. Being new at this, its been a steep learning curve but am getting there in very small strides.
I am having a few problems, I was hoping you would help me. Orge forum isn't really active enough and am getting no responses.
So here it goes.
I have a scenenode which has a camera and sphere attached to it, I was going to use the sphere around the camera to flag any collisions with objects within the game. I couldn't figure out how to put collision detection on the camera itself so i thought this would be simpler.
Below is how i create and attach my nodes.

	tdynamicnode = this->_obj_mgr_scene->getRootSceneNode()->createChildSceneNode("DynamicNode");
	tdynamicnode->attachObject(_obj_camera_one);
	
	Entity* tempdynentity = nullptr;
	tempdynentity = this->_obj_mgr_scene->createEntity("Sphere",tspheremesh);
	tdynamicnode->attachObject(tempdynentity);
Originally I had controls controlling the camera (using the mouse for rotation and WASD for X,Z ) but now since I have the both the sphere and the camera, I transfered the controls to move the scenenode so the camera and the sphere woudl move together.
Below is the code for moving the scenenode.

if (tnotexiting) 
	{
		this->_obj_mgr_camera_one->frameRenderingQueued(pframe_event);	

		this->_node_player_disp = Vector3::ZERO;
		if(this->_actn_move_fwd)
		{
			this->_node_player_disp += Vector3::NEGATIVE_UNIT_Z * 100 * pframe_event.timeSinceLastFrame;
		}
		if(this->_actn_move_bkwrd)
		{
			this->_node_player_disp += Vector3::UNIT_Z * 100 * pframe_event.timeSinceLastFrame;
		}
		if(this->_actn_strafe_left)
		{
			this->_node_player_disp += Vector3::NEGATIVE_UNIT_X * 100 * pframe_event.timeSinceLastFrame;
		}
		if(this->_actn_strafe_right)
		{
			this->_node_player_disp += Vector3::UNIT_X * 100 * pframe_event.timeSinceLastFrame;
		}
		this->tdynamicnode->translate(this->_node_player_disp);
	}
	

bool tresult = true;

	if(pkeyevt.key == OIS::KC_W)
	{
		this->_actn_move_fwd = true;
	}
	else if(pkeyevt.key == OIS::KC_S)
	{
		this->_actn_move_bkwrd = true;
	}
	else if(pkeyevt.key == OIS::KC_A)
	{
		this->_actn_strafe_left = true;
	}
	else if(pkeyevt.key == OIS::KC_D)
	{
		this->_actn_strafe_right = true;
	}
My next problem is I dont know how to change the rotation of the scenenode when moving the camera rotation. Ideally i would like to rotate the X and Z axis on yaw rotation. Essentially I would like it behaviouring simpler to minecraft.
But this can be fixed another time, I need collision detection working before i can fix fiddly bits.
Now for the collision detection. I need the sphere big enough to encase camera (its currently in front of the camera), then am guessing I use something similar to this to detect collision.

Node::ChildNodeIterator itr = this->_node_obstacles->getChildIterator();


 bool tcollide = false;
 while (itr.hasMoreElements())
 {
  SceneNode* n = (Ogre::SceneNode *)itr.getNext();


  if(n->_getWorldAABB().intersects(_node_player->_getWorldAABB()))
  {
   tcollide = true;
  }
 }


 if(tcollide)
 {
   this->_node_player->translate(-5 * this->_node_player_disp);
 }

I need help with getting the collision to work properly. its so frustrating when its almost there but you cant figure out what it is. Ogre's website doesn't have much.

Advertisement
I would start by not using the scene node bounding box method you are currently using. This method is not meant to be used externally hence the underscore at the start of its name. I have read somewhere not all scene managers maintain it so don't rely on it. I think you need to iterate over the attached objects on each scene node and get their bounding boxes to test your collision against.

As for adding pitch/roll/yaw to your camera try having a hierarchy of three scene nodes one for rotating in each direction.

Hope this helps...good luck!

I am guessing you are talking about the _getWorldAABB() method?

I think there is a getBoundingBox() method would i be using that to intersect?

Thanks for the reply!

The Ogre AABB's necessarily don't have anything to do with the object's actual dimensions, it's just what's stored in the mesh file. For example animated meshes often have their AABB's expanded to account for all possible poses they can be in, preventing unwanted culling. Furthermore, AABB is exactly that, axis-aligned bounding box; in games objects are usually not just axis aligned. In general Ogre implements intersection tests only for the purposes of rendering (to see what's visible and what should be culled), not for game collision detection.

I would recommend integrating a physics engine such as Bullet instead, to handle both collision detection and character & object movement. Then you can author collisions for objects separate from rendering, based on what fits best for each case (box, sphere, capsule, convex hull etc.)

Have no doubt though, using a physics engine will have a steep learning curve as well. But you can eg. take a look at OgreBullet http://www.ogre3d.org/tikiwiki/OgreBullet which is a wrapper library that does the integration for you.

Thanks great help!

I was actually considering using the bullet engine, I have a whiz through some of the documents and they weren't very clear on how to implement it. Is there a step by step guide on how to get set up, the stuff i have read on it says something about Cmake i think.. I have no idea what that is

If you are happy using a library to do collision detection then there is MOC - Minimal Ogre Collision.

http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Minimal+Ogre+Collision

This might be enough for your needs.

If you are happy using a library to do collision detection then there is MOC - Minimal Ogre Collision.

http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Minimal+Ogre+Collision

This might be enough for your needs.

This could be what i need but... I need the character to have the ability to jump too.. I dont know if i need Bullet for that or if I can simulate it with Ogre.

Thanks for your reply.

This topic is closed to new replies.

Advertisement