Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL mouse handling


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Astado89   Members   -  Reputation: 135

Like
0Likes
Like

Posted 22 March 2013 - 10:21 AM

Hi together,

 

sorry if I made the topic wrong, but I'm new to this forum.

 

I'm currently writing on a little game which is supposed to be a third person game.

So I created a camera to rotate around the character.

So good so far. My issue is that the mouse reaches the corners of my display and the camera stops rotating.

 

I tried working with glutwarppointer to warp my pointer back to the center of the screen.

But it triggers my camera to rotate back, because the glutpassivemousefunc is called.

 

I tried using a BOOL to keed the camera moving just when its false and tried

 

mouselock = true;

glutwarppointer(windowwidth/2, window height/2);

mouselock = false;

 

but it still triggers the camera.

 does anyone got any ideas?

 

Just so you know I'm working with Mac OS X Mountainlion and Xcode.

 

Any help is very much appreceated.


Edited by Astado89, 24 March 2013 - 12:53 AM.


Sponsor:

#2 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 22 March 2013 - 11:01 AM

Have you tried using the MacOS' function to set the mouse position?

 

Then you can check if you reached the screen.width, if it did, keep increasing the rotation, but moving the mouse back to the edge (thus locking it on screen). Or like 3ds max, you could set it to the other side of the window as if it did a 360...

 

Not sure if this is updated, but here's the idea: http://hints.macworld.com/article.php?story=2008051406323031


Edited by Danicco, 22 March 2013 - 11:01 AM.


#3 Astado89   Members   -  Reputation: 135

Like
0Likes
Like

Posted 24 March 2013 - 12:52 AM

Hi Danicco,

 

thanks a lot for your reply.

 

Trying to keep the mouse in the center of the screen seemed to be the best solution to me. But the camera wont move fluently anymore because the function glutwarppointer is called every time the mouse is moved.

 

But I got another idea after 3 months of thinking smile.png

 

Now the mouse jumps back to the center of the screen every time it reaches the edges.

 

My solution is as follows:

 

I got a class that controls the OpenGL device functions.

The class is called inside the glut function and given the parameters.

Heres the function prototype.

-(void)mousepassivenotion:(int)_x y:(int)_y;

 

 

Inside the class I save the old point of the mouse within 2 int variables.

When I move the mouse the old and new values are given to my camera class.

-(void)movetomousemotionx:(int)_x y:(int)_y xold:(int)_xold yold:(int)_yold {
    if (_xold-_x < 500) {
        NSLog(@"%d %d : %d %d",_x, _x, _xold, _yold);
        if (_xold > _x) {
            [self performSelectorOnMainThread:@selector(surroundviewpoint:) withObject:[NSNumber numberWithInt:(_xold-_x)
            /mousesensivity] waitUntilDone:YES];
        }
        else if (_x > _xold) {
            [self performSelectorOnMainThread:@selector(surroundviewpoint:) withObject:[NSNumber numberWithInt:(_xold-_x)
            /mousesensivity] waitUntilDone:YES];
        }
        if (_yold > _y) {
            [self performSelectorOnMainThread:@selector(overviewviewpoint:) withObject:[NSNumber numberWithInt:(_y-_yold)
            /mousesensivity] waitUntilDone:YES];
        }
        else if (_yold < _y) {
            [self performSelectorOnMainThread:@selector(overviewviewpoint:) withObject:[NSNumber numberWithInt:(_y-_yold)
            /mousesensivity] waitUntilDone:YES];
        }
    }
} 

 

 

In the first line i check if the difference between old and new is bigger than 500 in the horizontal(x) axis.

I may add the y-axis too. This is because after glutwarppointer the function glutpassivemotion is called and the difference is bigger than 500 and is ignored. Then I call my surroungviewpoint function with the rotation radius.

Heres my surroundviewpoint function.

-(void)surroundviewpoint:(NSNumber *)_rotation {
    glPushMatrix();
    glLoadMatrixd(Systemmatrix);
    glTranslated(0, 0, -distance);
    glRotated(-topdown, 1, 0, 0);
    glRotated(_rotation.floatValue, 0, 1, 0);
    glRotated(topdown, 1, 0, 0);
    glTranslated(0, 0, distance);
    glGetDoublev(GL_MODELVIEW_MATRIX, Systemmatrix);
    glPopMatrix();
}

 

 

So now the mouse is pointing at the center of the screen.

There is only one more point. When you turn the camera around and reach the edge you can notice for a second that the mouse is not moving.

So I tried calling my camera function manually after calling glutwarppointer and parameterize as the new point the center and as the old point the center minus the original difference.

int dif[2];
dif[0] = _x-x; //x is the old value
dif[1] = _y-y; //y is the old value
x = windowwidth/2-dif[0];
y = windowheight/2-dif[1];

glutWarpPointer(windowwidth, windowheight);

[camera movetomousemotionx:x+dif[0] y:y+dif[1] xold:x yold:y];

But still you can notice it.

 

Sorry for that much code but I wanted others to be able to solve the problem reading the post. smile.png


Edited by Astado89, 25 March 2013 - 08:38 AM.


#4 Astado89   Members   -  Reputation: 135

Like
0Likes
Like

Posted 25 March 2013 - 08:29 AM

Ok now I got it fixed!

 

after glutwarppointer is called the mouse is suppressed for a short amount of time.

This also occurred using the CGWarpMouseCursorPosition function.

 

So these are not to be used in a game.

 

Instead use

CGEventRef event = CGEventCreateMouseEvent(Nil, kCGEventMouseMoved, CGPointMake(windowwidth/2, windowheight/2), 
                   kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, event);

 

This moves the mouse to the middle without creating any interference.


Edited by Astado89, 25 March 2013 - 08:32 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS