Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've tried to figure out how to do picking with the mouse, and things work great when zoomed in all the way with the camera (Third Person Camera). And it works perfectly when using First Person Camera.

However, when using Third Person camera, if I zoom out the camera view, the picking stops working, it gives me incorrect locations and I start picking on a limited set of items in the center of the screen.

It's like I have to figure out how to scale the picking based on the zoom that I'm performing. Here's my code for picking right now. The point is to have the "targetPoint" be the position you are picking in the world coordinates:

if (_gameManager.Camera is FirstPersonCamera)
    // Get the building location from the look vector of the camera
    targetPoint = _player.Position + (_player.LookVector * x);
else if (_gameManager.Camera is ThirdPersonCamera)
    // Get the building location based on the mouse cursor
    Vector3 nearsource = new Vector3((float)_player.PreviousMouseState.X, (float)_player.PreviousMouseState.Y, 0f);
    Vector3 farsource = new Vector3((float)_player.PreviousMouseState.X, (float)_player.PreviousMouseState.Y, 1.0f);

    Vector3 nearPoint = _gameManager.GraphicsDevice.Viewport.Unproject(nearsource, _gameManager.Camera.Projection, _gameManager.Camera.View, Matrix.Identity);
    Vector3 farPoint = _gameManager.GraphicsDevice.Viewport.Unproject(farsource, _gameManager.Camera.Projection, _gameManager.Camera.View, Matrix.Identity);

    Vector3 direction = farPoint - nearPoint;

    targetPoint = _player.Position + (direction * x);

The x I'm adding is because I'm searching a set of targetPoints for a matching 'block' in the game world. That part is of no consequence I believe. The rest is for picking a targetPoint that will match a tile in the terrain made up of square blocks.

When I'm zooming in and out, the camera View is recalculated this way:

Matrix rotationMatrix = Matrix.CreateRotationX(UpDownRotation) * Matrix.CreateRotationY(LeftRightRotation);

// Create a vector pointing the direction the camera is facing.
Vector3 transformedReference = 
            Vector3.Transform(new Vector3(0, _zoomFactor, _zoomFactor), rotationMatrix);

// Calculate the position the camera is looking from.
_positionVector = transformedReference + _avatarPosition;

// Set up the view matrix and projection matrix.
View = Matrix.CreateLookAt(_positionVector, _avatarPosition, Vector3.Up);

If anyone could help me out, I'm a bit stuck with this one, I've tried a few things and only made things worse. I have yet to find out why none of the picking examples I've found in XNA creators forums or any of the ones found here mention anything about the camera zoom level compared to the game objects that are being picked.

share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.