Sign in to follow this  
FreJa

glut cursor problem

Recommended Posts

Hi, So I'm using a first-person style camera, controlled with the mouse, in my project. My problem is that when the cursor hits the limits of the screen I can't rotate any further (as one would expect) and so I can't make 360º spins... How can this be solved? Note: I know glutWarpPointer exists, but my calculations are, obviously, based on the cursor position, and if the cursor is always in the center.. it isn't very helpful. Thanks

Share this post


Link to post
Share on other sites
Ok, so, I got it more or less working. It's rotating correctly, however, it's not smooth at all.. really jerky.
I'm using gametutorial.com code:


void Camera::RotateView(float angle, float x, float y, float z)
{
Vector3f vNewView;

// Get the view vector (The direction we are facing)
Vector3f vView = _lookAt - _position;

// Calculate the sine and cosine of the angle once
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

// Find the new x position for the new rotated point
vNewView[0] = (cosTheta + (1 - cosTheta) * x * x) * vView[0];
vNewView[0] += ((1 - cosTheta) * x * y - z * sinTheta) * vView[1];
vNewView[0] += ((1 - cosTheta) * x * z + y * sinTheta) * vView[2];

// Find the new y position for the new rotated point
vNewView[1] = ((1 - cosTheta) * x * y + z * sinTheta) * vView[0];
vNewView[1] += (cosTheta + (1 - cosTheta) * y * y) * vView[1];
vNewView[1] += ((1 - cosTheta) * y * z - x * sinTheta) * vView[2];

// Find the new z position for the new rotated point
vNewView[2] = ((1 - cosTheta) * x * z - y * sinTheta) * vView[0];
vNewView[2] += ((1 - cosTheta) * y * z + x * sinTheta) * vView[1];
vNewView[2] += (cosTheta + (1 - cosTheta) * z * z) * vView[2];

// Now we just add the newly rotated vector to our position to set
// our new rotated view of our camera.
_lookAt = _position + vNewView;
}

void Camera::onMousePassiveMotion(int x, int y)
{
int middleX = (int)_winWidth >> 1; // This is a binary shift to get half the width
int middleY = (int)_winHeight >> 1; // This is a binary shift to get half the height
float angleY = 0.0f; // This is the direction for looking up or down
float angleZ = 0.0f; // This will be the value we need to rotate around the Y axis (Left and Right)
static float currentRotX = 0.0f;

// If our cursor is still in the middle, we never moved... so don't update the screen
if( (x == middleX) && (y == middleY) ) return;

// Set the mouse position to the middle of our window
glutWarpPointer(middleX, middleY);

// Get the direction the mouse moved in, but bring the number down to a reasonable amount
angleY = (float)( (middleX - x) ) / 500.0f;
angleZ = (float)( (middleY - y) ) / 500.0f;

// Here we keep track of the current rotation (for up and down) so that
// we can restrict the camera from doing a full 360 loop.
currentRotX -= angleZ;

// If the current rotation (in radians) is greater than 1.0, we want to cap it.
if(currentRotX > 1.0f)
currentRotX = 1.0f;
// Check if the rotation is below -1.0, if so we want to make sure it doesn't continue
else if(currentRotX < -1.0f)
currentRotX = -1.0f;
// Otherwise, we can rotate the view around our position
else
{
// To find the axis we need to rotate around for up and down
// movements, we need to get a perpendicular vector from the
// camera's view vector and up vector. This will be the axis.
Vector3f vAxis = cross(_lookAt - _position, _up);
vAxis = normalize(vAxis);

// Rotate around our perpendicular axis and along the y-axis
RotateView(angleZ, vAxis[0], vAxis[1], vAxis[2]);
}

// Rotate around the y axis no matter what the currentRotX is
RotateView(angleY, 0, 1, 0);
}



Any ideas?

Thanks again!

Share this post


Link to post
Share on other sites
Well it's impossible to tell exactly, but if it's *really* jerky, and you are trying to push mouse buttons, that is because the glut passive motion function is only called when the mouse moves and no mouse buttons are pushed. So if you push one of the mouse buttons (for say, shooting) even for what seems an instant, it will create some massive jerkiness.

Other than that, with a quick glance through, I can't see anything else that would cause it to be *really* jerky.

Share this post


Link to post
Share on other sites
Quote:
Original post by MortusMaximus
Well it's impossible to tell exactly, but if it's *really* jerky, and you are trying to push mouse buttons, that is because the glut passive motion function is only called when the mouse moves and no mouse buttons are pushed. So if you push one of the mouse buttons (for say, shooting) even for what seems an instant, it will create some massive jerkiness.

Other than that, with a quick glance through, I can't see anything else that would cause it to be *really* jerky.


I'm not pushing any buttons.
If I comment out the glutWarpPointer in my onMousePassiveMotion, the rotation is smooth but wrong, of course.

Any more ideas?

Thanks again!

Share this post


Link to post
Share on other sites
ooooookk, now this is veery weird...
I recompiled and ran the code on a Windows machine (was on a Mac previously).. and its working (not jerky anymore)... any idea why it isn't working on a mac ??

Thanks!

Share this post


Link to post
Share on other sites
Graphics card? ...

I experienced jerky-ness because of the trig functions ( cos, sin, etc. )
Try to find another means of a camera. Say one that uses vectors, quaternions, or something. I'm not saying to never use trig ( how do you not? ) but don't make it a soul need.

Also, GLUT isn't top-notch nowadays. It is good for demos or prototypes, but if you are planning on doing more, I'd recommend a wonderful thing called GLFW ( OpenGL FrameWork )

Just throwing out suggestions.

Share this post


Link to post
Share on other sites
Quote:
Original post by PCN
Graphics card? ...

I experienced jerky-ness because of the trig functions ( cos, sin, etc. )
Try to find another means of a camera. Say one that uses vectors, quaternions, or something. I'm not saying to never use trig ( how do you not? ) but don't make it a soul need.

Also, GLUT isn't top-notch nowadays. It is good for demos or prototypes, but if you are planning on doing more, I'd recommend a wonderful thing called GLFW ( OpenGL FrameWork )

Just throwing out suggestions.


I don't think it has anything to do with graphics card or processor performance, as simply commenting the glutWarpPointer call makes the rotation smooth (but wrong, as I said previously)... meaning all the calculations are done just the same.
I googled for a bit and found at least one other person with the same problem as me, but no solution... :(

thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this