Mouse movement

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Glaucos
Posts: 4
Joined: Fri Apr 06, 2012 7:15 pm

Mouse movement

Post by Glaucos »

Hi all,

i am attempting to create a small air fight game with irrlicht, and I want to use the mouse to mouve the plane, but I am facing the following issue: when the mouse come on the screen borders, no mouseup or mousedown movement is recognised... I tried to automatically recenter it, but it is taken as a mouse movement event by IEventReceiver.

Here is my code

Code: Select all

 
class PlayerEventReceiver : public IEventReceiver
{
    public:
 
        PlayerEventReceiver(Universe *u)
        {
                for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                        KeyIsDown[i] = false;
                univers = u;
                lmb = false;
                rmb = false;
                prendreencompte=0;
        }
 
 
        virtual bool OnEvent(const SEvent& event)
        {
                if (event.EventType == irr::EET_KEY_INPUT_EVENT)
                        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
                mup=mdown=false;
 
                if (event.EventType == EET_MOUSE_INPUT_EVENT)
                {
                    switch(event.MouseInput.Event)
                    {
                        case EMIE_MOUSE_MOVED:
 
                        if(event.MouseInput.Y - mouseY > 0)
                        {
                            mup=true;
                        }
 
                        if(event.MouseInput.Y - mouseY < 0)
                        {
                            mdown=true;
                        }
                        mouseX = event.MouseInput.X;
                        mouseY = event.MouseInput.Y;
                        std::cout << "MOuseEve,t" << std::endl;
 
                        break;
 
                        case EMIE_LMOUSE_PRESSED_DOWN:
                        lmb = true;
                        break;
 
                        case EMIE_RMOUSE_PRESSED_DOWN:
                        rmb = true;
                        break;
 
                        case EMIE_LMOUSE_LEFT_UP:
                        lmb = false;
                        break;
 
                        case EMIE_RMOUSE_LEFT_UP:
                        rmb = false;
                        break;
                    }
                }
 
                return false;
        }
 
        virtual bool IsKeyDown(EKEY_CODE keyCode) const
        {
                    return KeyIsDown[keyCode];
        }
 
        bool rmbPressed()
        {
            return rmb;
        }
 
        bool lmbPressed()
        {
            return lmb;
        }
 
        bool mMouvedDown()
        {
            return mdown;
        }
 
        bool mMouvedUp()
        {
            return mup;
        }
 
    private:
 
        bool KeyIsDown[KEY_KEY_CODES_COUNT]; // touches clavier enfoncées
        bool rmb, lmb;//bouton de la souris enfoncé
        Universe *univers;
        int mouseX, mouseY;//utilistaires pour mup et mdown
        bool mup, mdown;
        int prendreencompte;
};
 
 
How can I fix this ?

Thanks !
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Mouse movement

Post by REDDemon »

the FPS camera scene node solved the problem of the event. You can also use the difference from mouse position to center of the screen and keep the mouse in a circle on the center of the screen (I.e 400 pixel diameter) so that player don't have to move mouse too long for change position and turn to another direction. (also maybe a small circle of radiu 80 can be used as neutral zone so that the player don't need much efforto to go straight in one direction).
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Glaucos
Posts: 4
Joined: Fri Apr 06, 2012 7:15 pm

Re: Mouse movement

Post by Glaucos »

Thanks for reply.
I see... but how to to "keep the mouse in a circle on the center of the screen" ? I tried with device->getCursorCOntrol()->setPosition(), but it is handled as a mouse move event, so pitch of the plane isn't continous: it goes down when the cursor is moved from +400px to 0 by the device where it was supposed to continue to pitch up...
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Mouse movement

Post by REDDemon »

because you are using the "mouse delta movement" if you use instead as suggest by me the distance of the mouse from the center, what matters is not the mouse relative movement but the mouse position. You should of course move mouse back to corner of the circle. not to the center. If the distance is bigger than radius you just use the radius value and then you move mouse back. The math for doing that:

Code: Select all

 
a=mousepos-centerpos; // center pos will be 320,240 for a 640,480 screen. You should use vector2df. so just need to cast screen coordinates to floats.
f32 L = a.getLenght(); //get distance of the mouse from the center. 
f32 R = 400/2; // your radius. halved because 400 is diameter not radius
f32 delta = 4; //small delta to prevent continuos correction of mouse pos at every frame.
f32 r2 = 80/2; //radius of the neutral circle
 
if( L > R)
{
    L+=delta;
    a = (R/L)*a; // calculate how to move mouse back. 
    cursorcontrol->setPosition( mousepos + centerpos); //move mouse back inside the circle
}
 
if(L<=r2)
{
   a = vector2df(0,0); //make sure small mouse movements and "smalls hand shaking" will not move the ship.
}
 
// now vector "a" keep the direction and the intensity of the movement in screen space.
 
 
you can tune lot of parameters until you are satisfied.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Glaucos
Posts: 4
Joined: Fri Apr 06, 2012 7:15 pm

Re: Mouse movement

Post by Glaucos »

I got it, thanks for help !
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: Mouse movement

Post by REDDemon »

no problem :) hope it works correctly
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply