Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've just started writing a 3D First Person Shooter in Java, and I'm having a bit of trouble with tracking the mouse movements and moving the camera accordingly. I'm implementing typical FPS movement, in that the mouse stays at the center of the screen.

Moving the mouse left and right (horizontal movement) is fine - each time the mouse is moved, I get it's position on the screen. I then use atan2 to get the angle I need to rotate around the y axis to get to this position, and apply the rotation accordingly.

My problem is when I try to change the pitch of the view (i.e. rotating around the x axis - essentially looking up and down). I can get the y coordinate of the mouse, and the y coordinate of the center of the screen, but how do I calculate how much I need to rotate around the x axis to get to that point?

Thanks in advance!

share|improve this question
    
uh.... what's the difference between the x and y axis? Can't you just do the same thing? –  Liosan Mar 19 '13 at 12:29
    
@Liosan I've tried that, and it's not working out for me. So to get the y rotation, I'm using double yRad = Math.atan2(mouseX, centerX);, so theoretically to get the x rotation, I should use double xRad = Math.atan2(mouseX, centerX), correct? This is giving me some very weird/erratic behaviour (and thus I thought I must have been doing something wrong). –  Kumalh Mar 19 '13 at 12:48
    
I'm sure it's just a typo in your comment, but you're not using mouseY and centerY. –  Byte56 Mar 19 '13 at 13:11
    
Yep, sorry about that - it's a typo.. Math.atan2(mouseY, centerY);* –  Kumalh Mar 19 '13 at 13:13
add comment

3 Answers

Maybe your problem is not related to angle calculation, but the way you take your measurements.

Most FPS games use the following scheme:

  • Read mouse position on screen
  • Move mouse back to the center of the screen

This way, every frame you measure movement of the mouse, and not the position on screen. This allows for much more fluent and precise camera movement.

share|improve this answer
add comment

I don't know why you think you should use Atan2, but you shouldn't. And I'm surprised that you get anything that looks useful that way.

double yRad = (mouseY-centerY)*sensitivityY; Where yRad is the amount to turn, and the mouse is centred after the reading is really all there is to it.

share|improve this answer
    
I've added this, and it works. However, the camera will only ever move in one direction, regardless of where I move the mouse. So if I move the mouse up or down, the camera always rotates in one direction. That was why I was using atan2. Sorry if these seem like stupid questions - I'm shocking at maths.. –  Kumalh Mar 20 '13 at 2:05
    
@Kumalh What does the rest of your rotation code look like? –  eBusiness Mar 20 '13 at 14:57
    
So after I determine the amount to rotate, I call this method for both the x and y: private void rotateY(double rad) { targetTG.getTransform(t3d); toRot.rotY(-rad); t3d.mul(toRot); targetTG.setTransform(t3d); } –  Kumalh Mar 23 '13 at 3:45
    
@Kumalh That is your exact code? Assuming toRot is a transformation matrix, it is not reset, so it will accumulate whatever transformations has been applied to it. How are you allowed to call getTransform with a parameter, it should return the transformation. In any case, I'd strongly advise that you keep track of the pitch and yaw yourself, like yRadTotal += yRad, and then for each frame set the specific rotation using these two values. –  eBusiness Mar 23 '13 at 8:54
add comment

I'm not sure if I understand what you are trying to achieve. From my understanding, and from how I am doing it in my game, the usual thing is to calculate:

horizontalAngle += ( mousePositionOnScreenX - screenCenterX ) * sensitivityX;
verticalAngle   += ( mousePositionOnScreenY - screenCenterY ) * sensitivityY;

And then of course mouse is moved back to the centre.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.