Collison Problem

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
chaitanyaraghav
Posts: 12
Joined: Mon Jul 07, 2008 4:26 pm
Location: India

Collison Problem

Post by chaitanyaraghav »

I first implemented the 3rd person RTS cam made by omar and then changed the code a little bit!

I removed the sphere and replace it with the faerie and also removed the plane and instead placed the quake3 map file

Everything works fine and i also tweaked the camera settings and it works fine.

But the real problem started when i implemented collision.Before collision the camera movement was correct but after collision the camera movement in wrong and also there is no collision taking place

Code: Select all

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Including Headers///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

bool up    = false;
bool down  = false;
bool right = false;
bool left  = false;

int speed=1;


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

/////////////////////////////////////////////////////////////////////////////////
/////////////////////Keyboard Input Handling////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class MyEventReceiver : public IEventReceiver
{
public:
   virtual bool OnEvent(const SEvent& event)
   {

         if (event.EventType == EET_KEY_INPUT_EVENT)
      {
         switch(event.KeyInput.Key)
         {
         case KEY_KEY_W:

            up = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_S:

            down = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_A:

                     left = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_D:

            right = event.KeyInput.PressedDown;
            break;

         }

         //return true;
      }

      return false;
   }

};

//////////////////////////////////////////////////////////////////////////////////
////////////////////////********Main Funtion********/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{

  MyEventReceiver receiver;
  
  IrrlichtDevice *device =
        createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
            false, false, false, &receiver);

  device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");


  IVideoDriver* driver = device->getVideoDriver();
  
  ISceneManager* smgr = device->getSceneManager();
  
  IGUIEnvironment* guienv = device->getGUIEnvironment();

  //////////////////////////////////////////////////////////////////////
 //////////////////////////Player//////////////////////////////////////
 /////////////////////////////////////////////////////////////////////	
 
 IAnimatedMeshSceneNode* hero = smgr->addAnimatedMeshSceneNode(
		smgr->getMesh("../media/faerie.md2"));
 

 if( hero )
   {      
    
    hero->setPosition(core::vector3df(50,-20,70));
   
    hero->setMaterialFlag(video::EMF_LIGHTING, false);
    
    hero->setMaterialTexture(0, driver->getTexture("../media/faerie5.bmp"));
    
    device->getCursorControl()->setVisible(false); 
   }
 
   
  /////////////////////////////////////////////////////////////////////
  ///////////////////////////Level Map////////////////////////////////
  ////////////////////////////////////////////////////////////////////
 
  IAnimatedMesh * map;
 
  device->getFileSystem()->addZipFileArchive("../media/map-20kdm2.pk3");   
 
  map = smgr->getMesh("20kdm2.bsp");
 
  ISceneNode* node = 0;
 
  if(map)
	node= smgr->addOctTreeSceneNode(map->getMesh(0));

  if(node)
        node->setPosition(vector3df(-1300,-144,-1249));
   
  ITriangleSelector* selector = 0;
	
	if (node)
	{		
		node->setPosition(core::vector3df(-1370,-130,-1400));
		selector = smgr->createOctTreeTriangleSelector(
        map->getMesh(0), node, 128);
		node->setTriangleSelector(selector);
	}

  /////////////////////////////////////////////////////////////////////////
  ///////////////////////////Camera and game loop/////////////////////////
 ////////////////////////////////////////////////////////////////////////
	
  ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
  
  ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
		                     selector, camera, core::vector3df(30,50,30),
		                     core::vector3df(0,-3,0),
	                         core::vector3df(0,50,0));

  selector->drop();

  camera->addAnimator(anim);
  anim->drop();
  
  while(device->run())
    {

          vector3df c = hero->getPosition();
          vector3df d= hero->getRotation();
          float diry = ((d.Y+90)*3.14)/180;
          

          if (up)
          {

            c.X += speed * cos((d.Y) * 3.14/ 180);
            c.Z -= speed * speed *sin((d.Y) * 3.14 / 180);

         }



         if (down)
         {
            c.X -= speed * cos((d.Y) * 3.14 / 180);
            c.Z += speed * sin((d.Y) * 3.14 / 180);
         }



         if (left)
         {
            d.Y -= 0.3;
         }



         if (right)
         {
            d.Y += 0.3;
         }



         hero->setRotation(d);
         int xf = (c.X-sin(diry)*100);
         int yf =(c.Z-cos(diry)*100);
         int zf =50;

         hero->setPosition(c);


         camera->setTarget(c);

         c.Y +=200.f;
         c.Z -= 150.f;

         camera->setPosition(vector3df(xf,zf,yf));
         driver->beginScene(true, true, SColor(0,200,200,200));

         smgr->drawAll();
         guienv->drawAll();
        		
         driver->endScene();
  }     //device->drop();

 return 0;
} 
please help![/code]
Nobody is perfect!!
nd i am nobody!!
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

just guessing but are you sure that you should drop the selector BEFORE adding the animator? i dont think so.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: Collison Problem

Post by rogerborg »

chaitanyaraghav wrote:after collision the camera movement in wrong and also there is no collision taking place
There is collision detection taking place, on the camera.

If you want to stop the faerie mesh from moving through the level mesh, then create and attach another collision response animator for it.

As for the camera movement being "wrong", you're trying to force the height to 50 every frame, on a camera that's got a collision response animator attached. It's bumping against the roof of the level mesh.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
chaitanyaraghav
Posts: 12
Joined: Mon Jul 07, 2008 4:26 pm
Location: India

Post by chaitanyaraghav »

thanks for the replies
i will try it out!

EDIT: As advised in the previous posts i dropped the selector after dropping the anim and i also reduced the height so that the camera doesnt collide with the roof.

Now the camera movement is corrected! :D I also tried implementing the player collsion but in vain. The collision is not taking place and when rotating near the walls the camera is getting stuck in the walls.

I have tried hard.....maybe my code is messy!!

pls help!


EDIT AGAIN:
Finally got the collision to work.I modified the code by browsing through the forum

But Another problem :cry: The camera is also free now and not getting stuck inside the walls but another problem arose soon.
After the player model climbs the stairs the camera should be positioned above the head of the model instead it stays in the starting position itself.
i.e if the player is in 1st floor then cam is placed on the ground itself

Here is my code:

Code: Select all

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Including Headers///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

bool up    = false;
bool down  = false;
bool right = false;
bool left  = false;

int speed=1;


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

/////////////////////////////////////////////////////////////////////////////////
/////////////////////Keyboard Input Handling////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

class MyEventReceiver : public IEventReceiver
{
public:
   virtual bool OnEvent(const SEvent& event)
   {

         if (event.EventType == EET_KEY_INPUT_EVENT)
      {
         switch(event.KeyInput.Key)
         {
         case KEY_KEY_W:

            up = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_S:

            down = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_A:

                     left = event.KeyInput.PressedDown;
            break;

         case KEY_KEY_D:

            right = event.KeyInput.PressedDown;
            break;

         }

         //return true;
      }

      return false;
   }

};

//////////////////////////////////////////////////////////////////////////////////
////////////////////////********Main Funtion********/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{

  MyEventReceiver receiver;
 
  IrrlichtDevice *device =
        createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
            false, false, false, &receiver);

  device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");


  IVideoDriver* driver = device->getVideoDriver();
 
  ISceneManager* smgr = device->getSceneManager();
 
  IGUIEnvironment* guienv = device->getGUIEnvironment();

  //////////////////////////////////////////////////////////////////////
 //////////////////////////Player//////////////////////////////////////
 /////////////////////////////////////////////////////////////////////   
 
 IAnimatedMeshSceneNode* hero = smgr->addAnimatedMeshSceneNode(
      smgr->getMesh("../media/faerie.md2"));
 

 if( hero )
   {     
   
    hero->setPosition(core::vector3df(50,-20,70));
   
    hero->setMaterialFlag(video::EMF_LIGHTING, false);
   
    hero->setMaterialTexture(0, driver->getTexture("../media/faerie5.bmp"));
   
    device->getCursorControl()->setVisible(false);
   }
 
   
  /////////////////////////////////////////////////////////////////////
  ///////////////////////////Level Map////////////////////////////////
  ////////////////////////////////////////////////////////////////////
 
  device->getFileSystem()->addZipFileArchive("../media/map-20kdm2.pk3");   
 
  IAnimatedMesh * map = smgr->getMesh("20kdm2.bsp");
 
  ISceneNode* node = 0;
 
  if(map)
   node= smgr->addMeshSceneNode(map->getMesh(0));

  if(node)
        node->setPosition(vector3df(-1300,-144,-1249));
   
  ITriangleSelector* selector =0;
     
  if (node)
   {      
      node->setPosition(core::vector3df(-1370,-130,-1400));
      selector = smgr->createOctTreeTriangleSelector(
      map->getMesh(0), node, 128);
      node->setTriangleSelector(selector);
	  selector->drop();
   }

  
  
    const aabbox3df& box=        hero->getBoundingBox();
    const f32 height=            (box.MaxEdge.Y - box.MinEdge.Y)/ 2.f;
    const f32 verticalOffset=    -box.MinEdge.Y - box.MaxEdge.Y;
    const f32 waist=             max_(box.MaxEdge.X - box.MinEdge.X, box.MaxEdge.Z - box.MinEdge.Z)/ 2.f;

    ISceneNodeAnimator* anim= smgr->createCollisionResponseAnimator
                         (selector, hero, vector3df(30,25,30), vector3df(0,-3,0), vector3df(0,0,0), 0.0005f);
    hero->addAnimator(anim);
    anim->drop();
  
  /////////////////////////////////////////////////////////////////////////
  ///////////////////////////Camera and game loop/////////////////////////
 ////////////////////////////////////////////////////////////////////////
   
  ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
 
  while(device->run())
    {

          vector3df c = hero->getPosition();
          vector3df d= hero->getRotation();
          float diry = ((d.Y+90)*3.14)/180;
         

          if (up)
          {

            c.X += speed * cos((d.Y) * 3.14/ 180);
            c.Z -= speed * speed *sin((d.Y) * 3.14 / 180);

         }



         if (down)
         {
            c.X -= speed * cos((d.Y) * 3.14 / 180);
            c.Z += speed * sin((d.Y) * 3.14 / 180);
         }



         if (left)
         {
            d.Y -= 0.3;
         }



         if (right)
         {
            d.Y += 0.3;
         }



         hero->setRotation(d);
         int xf = (c.X-sin(diry)*100);
         int yf =(c.Z-cos(diry)*100);
         int zf =50;

         hero->setPosition(c);


         camera->setTarget(c);

         c.Y +=200.f;
         c.Z -= 150.f;

         camera->setPosition(vector3df(xf,zf,yf));
         driver->beginScene(true, true, SColor(0,200,200,200));

         smgr->drawAll();
         guienv->drawAll();
              
         driver->endScene();
  }     //device->drop();

 return 0;
}
Hope anyone solves this prob!
Nobody is perfect!!
nd i am nobody!!
Post Reply