Camera Collision

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
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Camera Collision

Post by Elmo115 »

I am working on a third person game. I currently have collision detection working for my character, but the camera needs some refining. When the camera comes in contact with and object, it stops and you cant see through it. This is good. Though what happens most of the time, is that the camera starts to shake. The only way to get it to stop is to move back into the open. I'll post the section of the code below that deals with the collision. Any ideas on the problem would be appreciated.

Code: Select all

// Add triangle selector
    scene::ITriangleSelector* selector = 0; 
    selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);    

    // add a user controlled camera
	scene::ICameraSceneNode* camera = 
	camera = smgr->addCameraSceneNode(0);
	camera->setPosition(core::vector3df(-20,20,-20));

    // add animated hero.
	video::SMaterial material;
	material.Textures[0] = driver->getTexture("characters/sydney/texture.bmp");
	material.Lighting = true;
	//scene::IAnimatedMeshSceneNode* node = 0;
	scene::IAnimatedMesh* heromesh = smgr->getMesh("characters/sydney/amesh.md2");
	if (heromesh)
	{
		hero = smgr->addAnimatedMeshSceneNode(heromesh);
		hero->setPosition(core::vector3df(20,-15,20));
		hero->setMD2Animation(scene::EMAT_STAND);
		hero->setScale(core::vector3df(1.5,1.5,1.5));
		hero->getMaterial(0) = material;
	}
	// Hero collision detection.
   if (selector) 
   { 
      scene::ISceneNodeAnimator* anim =
      smgr->createCollisionResponseAnimator( 
          selector, hero,
          core::vector3df(17,25,20), 
          core::vector3df(0,-3,0),
          core::vector3df(0,8,0));        
      hero->addAnimator(anim); 
      anim->drop(); 
      selector->drop(); 
   } 
   // Camera collision detection.
   if (selector) 
   { 
      scene::ISceneNodeAnimator* cameraanim =
      smgr->createCollisionResponseAnimator( 
          selector, camera,
          core::vector3df(15,25,5), 
          core::vector3df(0,3,0),
          core::vector3df(0,5,0));        
      camera->addAnimator(cameraanim); 
      cameraanim->drop();
      selector->drop();  
       
   } 
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I would imagine you might be better off doing the camera collision yourself. Fair enough to use the hero collision as it is though..

What you'd do is, each frame, cast a line from the hero to the camera (should be this direction, not reversed) and then place the camera at the collision point (if there is any, if not then just leave it as it is). That should do the trick i think... If you don't know what i mean by casting a line for collision then look in the scene collision manager at getCollisionPoint.

The line should be cast in the direction i stated, hero to camera, because then it will handle the scenario of having more than one object in between the hero and the camera. If you cast the line from camera to hero then you'd just place the camera at the intersection with the first object and would still be blocked by the others. In the case that i stated you find the intersection point closest to the hero.

You should probably then also make sure that the camera isn't too close to the hero (or maybe even inside) after all that, if it is then you could rotate it around a bit, away from the collision to try and get a better angle.
Image Image Image
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Post by Elmo115 »

Ok sounds good. I'll give it a try.
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Post by Elmo115 »

I just thought of something. The way our camera works is that it rotates freely around the hero. If you use a ray, does this still work for our camera or is it just for a camera fixed on the hero?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Should work fine really, as the camera's always looking at the hero isn't it?
Image Image Image
Elmo115
Posts: 18
Joined: Wed Feb 06, 2008 11:35 pm

Post by Elmo115 »

Yes the camera is always looking at the hero. Thanks.
Post Reply