Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Camera Issues

Started by Ainokea Nov 2, 2004 at 12:24 PM 21 replies 1.4k views
Original Post
Ainokea
Ainokea
When using my camera I only am able to move once. For example: Im looking at a ball and I move the mouse down. I only move down a notch and I cant move anymore. Here is my code for the mouse look function:

void Camera::MouseLook(int screenWidth, int screenHeight, float MouseSensitivity)
{

  float CurrentRotationX;
  // the coordinates of our mouse coordinates
  int MouseX, MouseY;

  // the middle of the screen in the x direction
  int MiddleX = screenWidth/2;

  // the middle of the screen in the y direction
  int MiddleY = screenHeight/2;

  // vector that describes mouseposition - center
  Math::Vector3 MouseDirection(0, 0, 0);

  // The maximum angle we can look up or down, in radians
  double maxAngle = 1;

  // This function gets the position of the mouse
  SDL_GetMouseState(&MouseX, &MouseY);

  // if the mouse hasn't moved, return without doing
  // anything to our view
  if((MouseX == MiddleX) && (MouseY == MiddleY))
    return;

  // otherwise move the mouse back to the middle of the screen
  SDL_WarpMouse(MiddleX, MiddleY);

  // get the distance and direction the mouse moved in x (in
  // pixels). can't use the actual number of pixels in radians,
  // as only six pixels  would cause a full 360 degree rotation.
  // So I use a mousesensitivity variable that can be changed to
  // vary how many radians we want to turn in the x-direction for
  // a given mouse movement distance

  // The positive rotation is counter-clockwise. 
  // Moving the mouse down is a negative rotation about the x axis
  // Moving the mouse right is a negative rotation about the y axis
  MouseDirection.x = (MiddleX - MouseX)/MouseSensitivity; 
  MouseDirection.y = (MiddleY - MouseY)/MouseSensitivity;

  CurrentRotationX += MouseDirection.y;
  
  // don't want to rotate up more than one radian, so I cap it.
  if(CurrentRotationX > 1)
  {
    CurrentRotationX = 1;
    return;
  }
  // don't want to rotate down more than one radian, so I cap it.
  if(CurrentRotationX < -1)
  {
    CurrentRotationX = -1;
    return;
  }
  else
  {
    // get the axis to rotate around the x-axis. 
    Math::Vector3 Axis(view.x-pos.x,view.y-pos.y,view.z-pos.z);
    Axis.Cross(upVector);
    // To be able to use the quaternion conjugate, the axis to
    // rotate around must be normalized.
    Axis.Normalize();

    // Rotate around the y axis
    Rotate(MouseDirection.y, Axis.x, Axis.y, Axis.z);
    // Rotate around the x axis
    Rotate(MouseDirection.x, 0, 1, 0);
  }
}

And here is my main function:

int main(int argc, char *argv[])
{
    initialize();
    GLInit();

  int done=0;

  while(done == 0)
  {
    SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }

      if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
      }
      if(event.type == SDL_MOUSEMOTION)
      {
            SDL_WM_SetCaption("Mouse is moved.",0);
            camera.MouseLook(640,480,1.0);
      }
    }
    	
    
    draw();
  }     
        

    return 0;
}

______________With the flesh of a cow.
ajas95
ajas95
SDL_WarpMouse generates a mouse motion event. So every time you move the mouse, it updates the camera and posts another event, which is then read and moves the camera back to where it was.

[edit] I take that back, it seems you do return if the new mouse position is in the middle [/edit]

[edit2] CurrentRotationX is a local variable (and uninitialized, at that), but you're using it like it's persistent. Is it also a member variable of the camera or something? If so you need to delete the float CurrentRotationX; line at the top.
Ainokea
Ainokea
it is initilized:
CurrentRotationX += MouseDirection.y;
______________With the flesh of a cow.
Ainokea
Ainokea
Quote:
Original post by Anonymous Poster
"CurrentRotationX += MouseDirection.y;" is not an initialisation. It is an addition to itself, and if its original value is uninitialised then the result of this line of code is, technically, undefined also.

I'm not saying this will fix your problem, I haven't read the code in enough detail to comment, I'm just replying to Ainokea's last post.

-- AP

If you dont init your variable it auotmatically inits to 0..
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
from looking at your code
i would guess that....

you get the mouse info
then you adjust currentrotationX
then you do a check to make sure currentrotationX doesnt get beyond +-1
then you return - without rotating

then the next frame, when you call it again
your mouse is still going the same direction
you adjust currentrotationX - it goes back out of bounds
you fix it and return - without rotating again

so as long as you try to move the mouse in the same direction, it never moves except for the very first time - it returns before getting to the rotate step


Get rid of those return statements after you fix currentrotationX to be =1 or =-1
and get rid of this else statement, you want to rotate regardless of hitting the bound
CurrentRotationX = -1;
return;
}
else
{
see if that helps...
Ainokea
Ainokea
nope it doesnt work but thanks anyways.
______________With the flesh of a cow.
ajas95
ajas95
Quote:
If you dont init your variable it auotmatically inits to 0..


That's true for global variables and statics but not locals. And anyway, what's the point of +=ing it, just to have it destroyed when the function exits (especially because it's not used for anything else).

The first, simplest thing to try is putting the word 'static' before 'float CurrentRotationX;'. Then it'll both be initialized and persistent. I would also try turning up the MouseSensitivity (maybe 100.0?), and see if that works.
Ainokea
Ainokea
Quote:
Original post by ajas95
Quote:
If you dont init your variable it auotmatically inits to 0..


That's true for global variables and statics but not locals. And anyway, what's the point of +=ing it, just to have it destroyed when the function exits (especially because it's not used for anything else).

The first, simplest thing to try is putting the word 'static' before 'float CurrentRotationX;'. Then it'll both be initialized and persistent. I would also try turning up the MouseSensitivity (maybe 100.0?), and see if that works.

I just did everything you menetioned about 3 seconds ago.
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
have the function print out the values of currentrotationx and the mousedirection stuff

chances are theyre putting out some wierd values

also put a print statement in the last block where you actually do the rotation, see if the function is even getting to that line or returning first
in fact, put a different print message before every return statement
figure out whats happenning each time
Ainokea
Ainokea
hapha: If I take out the return statements the camera doesnt move at all.
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
heres a silly question....

where do you store the camera orientation information?

each time you call this function you do Rotate at the end
but Rotate doesnt seem to return anything, is it altering your camera data behind the scenes? or is it just doing a single rotate then never accumulating them anyplace
Ainokea
Ainokea
Quote:
Original post by haphazardlynamed
heres a silly question....

where do you store the camera orientation information?

each time you call this function you do Rotate at the end
but Rotate doesnt seem to return anything, is it altering your camera data behind the scenes? or is it just doing a single rotate then never accumulating them anyplace

I have Quaternion called view that stores my viewing information.
Here is the inside of my Rotation function:
void Camera::Rotate(float Angle, float x, float y, float z){  Math::Quat temp, Qview, result;  temp.x = x * sin(Angle/2);  temp.y = y * sin(Angle/2);  temp.z = z * sin(Angle/2);  temp.w = cos(Angle/2);  Qview.x = view.x;  Qview.y = view.y;  Qview.z = view.z;  Qview.w = 0;  result = temp * quat_view * temp.Conjugate();   view=result;}
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
ok
so it sounds like Rotate alters some of your camera class private data (not listed)
im going to assume that works, since you said the first rotate works

and the the problem is more likely in your program flow someplace

put printf statements in each different if/then/else branch and see which way the function is switching and why
itl probably be really obvious then

ex:
if(CurrentRotationX > 1)
{
printf("currentrotationx was bigger than 1, im fixing it\n");
CurrentRotationX = 1;
return;
}
Ainokea
Ainokea
I have declared my self stupid. I cant find out the problem even when I log down my variables after the if statements as such:
  // if the mouse hasn't moved, return without doing  // anything to our view  if((MouseX == MiddleX) && (MouseY == MiddleY))  {      std::cout<<"mouse hasnt moved \n";    return;  }  // don't want to rotate up more than one radian, so I cap it.  if(CurrentRotationX > 1)  {      std::cout<<"Current rotation==bigger then 1\n";    CurrentRotationX = 1;    return;  }  // don't want to rotate down more than one radian, so I cap it.  if(CurrentRotationX < -1)  {      std::cout<<"Current rotation==smaller then 1\n";    CurrentRotationX = -1;    return;  }

Here is my log file:
Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1Current rotation==smaller then 1mouse hasnt moved mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved Current rotation==bigger then 1Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved Current rotation==bigger then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1Current rotation==smaller then 1mouse hasnt moved mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved Current rotation==smaller then 1mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved 

Hope someone can help me now.
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
needs more info

you forgot the put a print statement in one of the most important branches
you need "applying rotation of X degress now"
in the very last section where it really does do the rotation

id also reccomend a function start message, "mouselook has been called on framenumber X"


and I strongly reccomend that you also print the values of currentrotationx and mousedirection.x and mousedirection.y,
who knows, they might be getting changed to something totally unexpected
Ainokea
Ainokea
Updated log file:

mouse hasnt moved CurrentRotationX0.99MouseDirection.x-0.01MouseDirection.y-0.01CurrentRotationX0.92MouseDirection.x-0.1MouseDirection.y-0.07CurrentRotationX0.87MouseDirection.x-0.11MouseDirection.y-0.05mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX0.78MouseDirection.x-0.14MouseDirection.y-0.09CurrentRotationX0.42MouseDirection.x-0.57MouseDirection.y-0.36mouse hasnt moved mouse hasnt moved CurrentRotationX0.16MouseDirection.x-0.44MouseDirection.y-0.26CurrentRotationX-0.44MouseDirection.x-0.88MouseDirection.y-0.6mouse hasnt moved mouse hasnt moved CurrentRotationX-0.74MouseDirection.x-0.41MouseDirection.y-0.3mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX-1MouseDirection.x0.2MouseDirection.y0CurrentRotationX-0.94MouseDirection.x0.26MouseDirection.y0.06mouse hasnt moved mouse hasnt moved CurrentRotationX-0.87MouseDirection.x0.28MouseDirection.y0.07mouse hasnt moved CurrentRotationX-0.66MouseDirection.x0.62MouseDirection.y0.21mouse hasnt moved CurrentRotationX-0.3MouseDirection.x0.66MouseDirection.y0.36mouse hasnt moved CurrentRotationX-0.07MouseDirection.x0.34MouseDirection.y0.23mouse hasnt moved CurrentRotationX0.42MouseDirection.x0.68MouseDirection.y0.49mouse hasnt moved CurrentRotationX0.94MouseDirection.x0.48MouseDirection.y0.52mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX1MouseDirection.x-0.44MouseDirection.y0mouse hasnt moved CurrentRotationX0.89MouseDirection.x-1.01MouseDirection.y-0.11mouse hasnt moved CurrentRotationX0.52MouseDirection.x-1.03MouseDirection.y-0.37mouse hasnt moved CurrentRotationX0.31MouseDirection.x-0.49MouseDirection.y-0.21CurrentRotationX-0.11MouseDirection.x-0.79MouseDirection.y-0.42mouse hasnt moved mouse hasnt moved CurrentRotationX-0.3MouseDirection.x-0.25MouseDirection.y-0.19CurrentRotationX-0.44MouseDirection.x-0.1MouseDirection.y-0.14mouse hasnt moved mouse hasnt moved CurrentRotationX-0.58MouseDirection.x-0.06MouseDirection.y-0.14mouse hasnt moved CurrentRotationX-0.86MouseDirection.x-0.03MouseDirection.y-0.28mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX-1MouseDirection.x0.47MouseDirection.y0mouse hasnt moved CurrentRotationX-0.8MouseDirection.x1.32MouseDirection.y0.2mouse hasnt moved CurrentRotationX-0.26MouseDirection.x1.19MouseDirection.y0.54mouse hasnt moved CurrentRotationX0.01MouseDirection.x0.52MouseDirection.y0.27mouse hasnt moved CurrentRotationX0.59MouseDirection.x0.8MouseDirection.y0.58mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX1MouseDirection.x-0.21MouseDirection.y0CurrentRotationX0.94MouseDirection.x-0.24MouseDirection.y-0.06mouse hasnt moved mouse hasnt moved CurrentRotationX0.82MouseDirection.x-0.2MouseDirection.y-0.12mouse hasnt moved CurrentRotationX0.48MouseDirection.x-0.42MouseDirection.y-0.34mouse hasnt moved CurrentRotationX-0.01MouseDirection.x-0.36MouseDirection.y-0.49mouse hasnt moved CurrentRotationX-0.28MouseDirection.x-0.09MouseDirection.y-0.27mouse hasnt moved CurrentRotationX-0.94MouseDirection.x-0.12MouseDirection.y-0.66mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX-0.87MouseDirection.x0.75MouseDirection.y0.13mouse hasnt moved CurrentRotationX-0.39MouseDirection.x0.74MouseDirection.y0.48CurrentRotationX-0.05MouseDirection.x0.24MouseDirection.y0.34CurrentRotationX0.35MouseDirection.x0.24MouseDirection.y0.4mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX0.78MouseDirection.x0.21MouseDirection.y0.43mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX0.74MouseDirection.x-0.97MouseDirection.y-0.26mouse hasnt moved CurrentRotationX0.1MouseDirection.x-1.13MouseDirection.y-0.64mouse hasnt moved CurrentRotationX-0.36MouseDirection.x-0.58MouseDirection.y-0.46mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX-1MouseDirection.x1.39MouseDirection.y0mouse hasnt moved CurrentRotationX-0.73MouseDirection.x0.78MouseDirection.y0.27mouse hasnt moved CurrentRotationX-0.04MouseDirection.x1.68MouseDirection.y0.69mouse hasnt moved CurrentRotationX0.91MouseDirection.x1.29MouseDirection.y0.95mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved CurrentRotationX1MouseDirection.x-1.11MouseDirection.y0CurrentRotationX0.94MouseDirection.x-0.46MouseDirection.y-0.06mouse hasnt moved mouse hasnt moved CurrentRotationX0.67MouseDirection.x-0.73MouseDirection.y-0.27mouse hasnt moved CurrentRotationX0.53MouseDirection.x-0.24MouseDirection.y-0.14CurrentRotationX0.31MouseDirection.x-0.18MouseDirection.y-0.22mouse hasnt moved mouse hasnt moved CurrentRotationX0.15MouseDirection.x-0.09MouseDirection.y-0.16mouse hasnt moved CurrentRotationX-0.29MouseDirection.x-0.11MouseDirection.y-0.44mouse hasnt moved CurrentRotationX-0.82MouseDirection.x-0.02MouseDirection.y-0.53mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved mouse hasnt moved 

I dont see how your rating can be so low. You are really helpful...
______________With the flesh of a cow.
haphazardlynamed
haphazardlynamed
but leave in the previoius messages too, so not only do you know the current numbers, but can also see when its deciding to fix them or not

and i still suspect that the Rotate stuff at the bottom might be getting missed




this part of the log looks wierd:
CurrentRotationX-0.74 // -.74
MouseDirection.x-0.41
MouseDirection.y-0.3
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
mouse hasnt moved
CurrentRotationX-1 // -1 (i would assume it got capped)
MouseDirection.x0.2
MouseDirection.y0 /// Very strange, whys is this zero? it should be -.26 and lower, if currentrotaitonx really did get sent below -1
haphazardlynamed
haphazardlynamed
i just thought of a point of confusion

is currentrotationX the absolute camera angle? or the amount to adjust the camera rotatoin this frame (rotation velocity)
Ainokea
Ainokea
Now I fixed that problem (I started a new project but used the same camera code!!!) but mouselook only works if my position moved. I have a feeling thats because of when I call it but just in case here is my movement code:
void Camera::Move(float speed){    Math::Vector3 v(view.x - pos.x,view.y - pos.y,view.z - pos.z);    Math::Vector3 vVector = v;    vVector.Normalize();    pos.x += vVector.x * speed;     // Add our acceleration to our position's X    pos.z += vVector.z * speed;     // Add our acceleration to our position's Z    view.x += vVector.x * speed;    // Add our acceleration to our view's X    view.z += vVector.z * speed;    // Add our acceleration to our view's Z}
______________With the flesh of a cow.
James Trotter
James Trotter
I suppose the speed variable is increased when the camera is moved around. So, it's understandable that you do this:

Quote:
pos.x += vVector.x * speed;     // Add our acceleration to our position's Xpos.z += vVector.z * speed;     // Add our acceleration to our position's Z


This will obviously change the position based on the current speed of the camera.

But the other two statements there:

Quote:
view.x += vVector.x * speed;    // Add our acceleration to our view's Xview.z += vVector.z * speed;    // Add our acceleration to our view's Z


What are they for?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.