Problem with my mouse event receiver

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
FrSl
Posts: 19
Joined: Thu Jan 19, 2006 5:03 pm

Problem with my mouse event receiver

Post by FrSl »

Hello,

I've a problem with my mouse receiver.
I want to create a camera who can turn around 1 point and zoom in and out at that point. Fist I used this code for my event receiver:

Code: Select all

        if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
                case EMIE_MOUSE_MOVED:
                {
                    MouseSpeedX = (OldX-event.MouseInput.X);
                    MouseSpeedY = (OldY-event.MouseInput.Y);                      
                    OldX = event.MouseInput.X;
                    OldY = event.MouseInput.Y;
                    MouseMoved = true;
                }
            }
        }
This code is working well but when the mousepointer reaches the end of the screen then it doesn't work anymore, because MouseSpeedX = (1024 - 1024) = 0 so no movement anymore... After that I came up with the following idea:

A part of my event receiver:

Code: Select all

        if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
        {
            switch(event.MouseInput.Event)
            {
                case EMIE_MOUSE_MOVED:
                {
                    MouseSpeedX = (512-event.MouseInput.X);
                    MouseSpeedY = (384-event.MouseInput.Y);                      
                    device->getCursorControl()->setPosition(512,384);                  
                    MouseMoved = true;
                }
            }
        }
But here I do have a problem with the CursorControl, because when I change the position of the mouse then the same event receiver will start again and after that again... an endless loop... Because of that endless loop is the program always busy with the mouse receiver and doesn't execute the rest of the program anymore. Everything stays grey(normal camera background collor) and no meshes are visible.

This is the MouseMoving part of my main loop:

Code: Select all

        if(MouseMoved)
        {    
            cameraAngle+=MouseSpeedX * 0.1;                   
            v = camera->getPosition();           
            camera->setPosition(core::vector3df(cos(cameraAngle)*CameraZoom,v.Y - MouseSpeedY,sin(cameraAngle)*CameraZoom));        
            MouseMoved = false;
        }
Then my last idea is to drop the receiver everytime and change the mouseposition then and after that create a new receiver but that does sound a bit slow to me...

So I don't know what to change... I would be very happy if someone could help me out with this problem :)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You should probably be creating your own camera scene node that does all of this. Your high level event receiver shouldn't have to deal with the camera movement stuff...

The easy solution would be to keep the mouse in one place and keep track of the distance that the mouse has moved. Your camera should do all of the camera related work.

Code: Select all

      // adapted from CCameraFPSSceneNode.cpp 
      core::position2d<f32> cursorpos = CursorControl->getRelativePosition(); 

      if (!core::equals(cursorpos.X, 0.5f) || 
         !core::equals(cursorpos.Y, 0.5f)) 
      { 
         MouseMove.Y += (0.5f - cursorpos.X) * RotateSpeed; 
         MouseMove.X += (0.5f - cursorpos.Y) * RotateSpeed; 
         CursorControl->setPosition(0.5f, 0.5f); 
      } 
FrSl
Posts: 19
Joined: Thu Jan 19, 2006 5:03 pm

Post by FrSl »

Ok,

Then I try to create the camera movement without the event receiver...

Thank you for the code :)
FrSl
Posts: 19
Joined: Thu Jan 19, 2006 5:03 pm

Post by FrSl »

I found the problem with the event receiver!

When you set the EventReceiver before the first frame then IrrLicht will create more then 1 event...
The first frame event where the mouse is Moved from 0,0 to it's current position and all the other mousemovements of the user.

That causes for strange behavior of the engine without proper results. For everyone else who has problems with this, you can solve it by writing something like this in you're main loop:

Code: Select all

        if (Started && !device->getEventReceiver())
        {
           device->setEventReceiver(&receiver);
        }
        else
           Started = true;
This way you can start the EventReceiver after the first frame, so that it don't find the first events. (The eventreceiver must not be set in the createDevice function.) A bit strange solution but now I've no problems anymore :D
Post Reply