CameraSceneNode rotation trouble ?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
zapoutix
Posts: 2
Joined: Sat Jun 25, 2005 1:57 am

CameraSceneNode rotation trouble ?

Post by zapoutix »

hi,
i have been beginning irrlicht for 3 hours and i have a trouble with camera rotation.
I am using ode and i would like to put physic on a camera.
For the physic i am using ODE.

Code: Select all



#include <irrlicht.h>

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

#include "ode/ode.h"
#include <math.h>


#define MASS (1.0)
static dWorldID world;
static dSpaceID space;
static dJointGroupID contactgroup;
static dBodyID body;
static dGeomID cube;
static dBodyID body2;
static dGeomID cube2;
static dGeomID sol;
scene::ISceneNode* node = 0;
irr::scene::ICameraSceneNode *camera = 0;


class MyEventReceiver : public IEventReceiver
{
public:
  virtual bool OnEvent(SEvent event)
  {
    switch(event.KeyInput.Key)
      {
      case KEY_ESCAPE:
	{
	  exit(1);
	}
      }
    return(false);
  }

};

static void nearCallback (void *data, dGeomID o1, dGeomID o2)
{
  /* exit without doing anything if the two bodies are connected by a joint */
  dBodyID b1,b2;
  dContact contact;


  b1 = dGeomGetBody(o1);
  b2 = dGeomGetBody(o2);
  if (b1 && b2 && dAreConnected (b1,b2)) return;

  contact.surface.mode = 0;
  contact.surface.mu = 0.1;
  contact.surface.mu2 = 0;
  if (dCollide (o1,o2,0,&contact.geom,sizeof(dContactGeom))) {
    dJointID c = dJointCreateContact (world,contactgroup,&contact);
    dJointAttach (c,b1,b2);
  }
}

void QuaternionToEuler(const dQuaternion quaternion, irr::core::vector3df &euler)
{
  dReal w,x,y,z;
  w=quaternion[0];
  x=quaternion[1];
  y=quaternion[2];
  z=quaternion[3];
  double sqw = w*w;
  double sqx = x*x;
  double sqy = y*y;
  double sqz = z*z;

  euler.Z = (irr::f32) (atan2(2.0 * (x*y + z*w),(sqx - sqy - sqz + sqw))
                        *irr::core::GRAD_PI);

  euler.X = (irr::f32) (atan2(2.0 * (y*z + x*w),(-sqx - sqy + sqz + sqw))
                        *irr::core::GRAD_PI);

  euler.Y = (irr::f32) (asin(-2.0 * (x*z - y*w))
                        *irr::core::GRAD_PI);
}

static void simLoop ()
{
  int i;
  dReal* ode_pos;
  dQuaternion result;
  irr::core::vector3df pos;
  irr::core::vector3df rot;

  dSpaceCollide (space, 0, &nearCallback);
  dWorldStep (world,0.05);
  dJointGroupEmpty (contactgroup);


  // cube physic
  //    ode_pos = (dReal*)dGeomGetPosition(cube);
  //    pos.set((irr::f32)ode_pos[0],(irr::f32)ode_pos[1],(irr::f32)ode_pos[2]);
  //    node->setPosition(pos);
  //    dGeomGetQuaternion(cube, result);
  //    QuaternionToEuler(result,rot);
  //    node->setRotation(rot);


  // camera physic
   ode_pos = (dReal*)dGeomGetPosition(cube2);
   pos.set((irr::f32)ode_pos[0],(irr::f32)ode_pos[1],(irr::f32)ode_pos[2]);
   camera->setPosition(pos);
   dGeomGetQuaternion(cube2, result);
   QuaternionToEuler(result, rot);
   camera->setRotation(rot);


}

int main()
{
  dMass m;
  MyEventReceiver receiver;
  IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
					core::dimension2d<s32>(640,480),
					16, false, false, false, &receiver);

  video::IVideoDriver* driver = device->getVideoDriver();
  scene::ISceneManager* scenemgr = device->getSceneManager();

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

  node = scenemgr->addTestSceneNode(1.0f);
  node->setPosition(core::vector3df(0, 0, 10));
  camera = scenemgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
  //   camera = scenemgr->addCameraSceneNode(
  // 					0,
  // 					vector3df(0, 1, 0)
  // 					);

  device->getCursorControl()->setVisible(false);

  /////////////////////////////// ODE PART /////////////////////////////
  world = dWorldCreate();
  space = dHashSpaceCreate (0);
  contactgroup = dJointGroupCreate (00);
  dWorldSetGravity (world, 0, -0.5, 0);
  dCreatePlane (space , 0, 1, 0, 0);

  body = dBodyCreate(world);
  body2 = dBodyCreate(world);
  dBodySetPosition (body, 0, 0, 10);
  dBodySetPosition (body2, 0, 1, 0);
  dMassSetBox (&m, 1, 1, 1, 1);
  dMassAdjust (&m, MASS);
  cube = dCreateBox (space, 1, 1, 1);
  cube2 = dCreateSphere (space, 1.0);
  dGeomSetBody (cube, body);
  dGeomSetBody (cube2, body2);
  /////////////////////////////// - ODE //////////////////////////////


  // draw everything
  while(device->run() && driver)
    {
      driver->beginScene(true, true, video::SColor(255,0,0,255));
      simLoop();
      dBodyAddForce(body2, 0, 0, 0.01);
      scenemgr->drawAll();
      driver->endScene();

    }

  // delete device
  device->drop();
  return 0;
}

When i am using CameraSceneNodeFPS all works fine but with CameraSceneNode camera doesn't rotate ...

The camera is in ODE sphere physic and i add a force to it then the camera is rolling. :roll:

did i something wrong ?

thanks,
Math

IRRlicht looks good :)


PS :I am french sorry for my english:(
muckelzwerg
Posts: 16
Joined: Wed May 04, 2005 11:38 am

Post by muckelzwerg »

ICameraSceneNode does NOT automatically react on mousemovement.
(ICameraSceneNodeFPS does)
You will have to implement that yourself.


-- -- muckelzwerg
Post Reply