Jump to content
  • Advertisement
Sign in to follow this  
dawberj3

Mouse look function not working correctly

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this OpenGL/linux program at the moment to "look" around a scene but it isnt very smooth or accurate. This is part of the passiveMotionFunc:
void mouse(int x,int y)
{

  if ( y != windowCenterY )
  {
    float yaw = (windowCenterY) - y;
    camera.lookUp(yaw);
  }
  
}


In my render scene function I update the camera:
  camera.applyTransformation();
  glutWarpPointer(windowCenterX,windowCenterY);


Is there a better way to do this? Can people post how they achieve smooth "look" around function. Thx [Edited by - dawberj3 on August 15, 2005 6:22:09 AM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if this is relevant, but your function will fail when the mouse(x,y) function is called multiple times per frame. You should call the glutWarpPointer(windowCenterX,windowCenterY) from the mouse function to be sure your calculation works when mouse(x,y) is called more than once per frame by glut.
Also, to get a consistent response to mouse movement, you should add the x component.

Tom

EDIT: guess you found out by yourself :)

Share this post


Link to post
Share on other sites
Actually ive tweaked it a bit and it seems to be working OK now, but I have a problem that something is altering the "roll" when im flying/looking around. Here is the code that manipulates the view in the Camera class:



void Camera::spinLeft(float angle)
{

Matrix mRotation;
vector3f vNewDirection;

rotationMatrix(DEG_TO_RAD(angle), vUp[0], vUp[1], vUp[2], mRotation);

vNewDirection[0] = mRotation[0] * vDirection[0] + mRotation[4] * vDirection[1] + mRotation[8] * vDirection[2];
vNewDirection[1] = mRotation[1] * vDirection[0] + mRotation[5] * vDirection[1] + mRotation[9] * vDirection[2];
vNewDirection[2] = mRotation[2] * vDirection[0] + mRotation[6] * vDirection[1] + mRotation[10] * vDirection[2];

memcpy(vDirection, vNewDirection, sizeof(vector3f));

}

void Camera::lookUp(float angle)
{

Matrix mRotation;
vector3f vCross;

// Cross Product
vCross[0] = vUp[1]*vDirection[2] - vDirection[1]*vUp[2];
vCross[1] = -vUp[0]*vDirection[2] + vDirection[0]*vUp[2];
vCross[2] = vUp[0]*vDirection[1] - vDirection[0]*vUp[1];

rotationMatrix(DEG_TO_RAD(-angle), vCross[0], vCross[1], vCross[2], mRotation);

vector3f vNewVect;
// Inline 3x3 matrix multiply for rotation only
vNewVect[0] = mRotation[0] * vDirection[0] + mRotation[4] * vDirection[1] + mRotation[8] * vDirection[2];
vNewVect[1] = mRotation[1] * vDirection[0] + mRotation[5] * vDirection[1] + mRotation[9] * vDirection[2];
vNewVect[2] = mRotation[2] * vDirection[0] + mRotation[6] * vDirection[1] + mRotation[10] * vDirection[2];
memcpy(vDirection, vNewVect, sizeof(GLfloat)*3);

// Update pointing up vector
vNewVect[0] = mRotation[0] * vUp[0] + mRotation[4] * vUp[1] + mRotation[8] * vUp[2];
vNewVect[1] = mRotation[1] * vUp[0] + mRotation[5] * vUp[1] + mRotation[9] * vUp[2];
vNewVect[2] = mRotation[2] * vUp[0] + mRotation[6] * vUp[1] + mRotation[10] * vUp[2];
memcpy(vUp, vNewVect, sizeof(GLfloat) * 3);

}

void Camera::rotationMatrix(float angle, float x, float y, float z, Matrix mMatrix)
{

float vecLength, sinResult, cosResult, oneMinusCos;
float xx, yy, zz, xy, yz, zx, xs, ys, zs;

// Scale vector
vecLength = (float)sqrt( x*x + y*y + z*z );

// Rotation matrix is normalized
x /= vecLength;
y /= vecLength;
z /= vecLength;

sinResult = (float)sin(angle);
cosResult = (float)cos(angle);
oneMinusCos = 1.0f - cosResult;

xx = x * x;
yy = y * y;
zz = z * z;
xy = x * y;
yz = y * z;
zx = z * x;
xs = x * sinResult;
ys = y * sinResult;
zs = z * sinResult;

mMatrix[0] = (oneMinusCos * xx) + cosResult;
mMatrix[4] = (oneMinusCos * xy) - zs;
mMatrix[8] = (oneMinusCos * zx) + ys;
mMatrix[12] = 0.0f;

mMatrix[1] = (oneMinusCos * xy) + zs;
mMatrix[5] = (oneMinusCos * yy) + cosResult;
mMatrix[9] = (oneMinusCos * yz) - xs;
mMatrix[13] = 0.0f;

mMatrix[2] = (oneMinusCos * zx) - ys;
mMatrix[6] = (oneMinusCos * yz) + xs;
mMatrix[10] = (oneMinusCos * zz) + cosResult;
mMatrix[14] = 0.0f;

mMatrix[3] = 0.0f;
mMatrix[7] = 0.0f;
mMatrix[11] = 0.0f;
mMatrix[15] = 1.0f;

}





passiveMotionFunc:



void mouse(int x,int y)
{

if ( x != windowCenterX )
{
float difference = (windowCenterX) - x;
camera.spinLeft(difference/2.0);
}

if ( y != windowCenterY )
{
float difference = (windowCenterY) - y;
camera.lookUp(difference/2.0);
}

if ( x == windowCenterX && y == windowCenterY )
return;

glutWarpPointer(windowCenterX,windowCenterY);

}



When the application starts and I look around, it "tilts" the camera (z-axis rotation).

I cant seem to find the problem.

Thx

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!