Dragging the camera problem (Fixed)
Hi
I'm currently developing a 4x Game, and i have a problem with my camera.
I have a star map, that consists of alot of textured quads positioned on X and Y where Z is always 0.
Now when i drag the mouse i would like the camera to move, that i have no problem with. The problem is that it moves to fast. Currently i apply a scaledown factor, but this seems a weird solution, and doesn't really give the best results.
To illustrate the problem, imagine you start dragging with the cursor over one of the stars, i would like to have that star stay under the cursor while i drag, currently it drifts when i move the mouse. so that it's final position is not under the cursor. There must be a way to do this, i will provide a working sample if anyone wants to take a look, be aware that it is around 7-10 mb to download.
I also have various zoom levels, but i take it, that solving it for one zoom level will allow me to solve for all of them.
[Edited by - Firestar_Jes on February 25, 2010 10:57:27 AM]
First, for consistency, make sure the mouse position is in window coordinates, not screen coordinates.
Use the difference in the mouse position in pixels from one mouse-move to the previous to change the screen location where you draw each tile.
Use the difference in the mouse position in pixels from one mouse-move to the previous to change the screen location where you draw each tile.
On mouse-click: basePos = mousePos;...On mouse-move: deltaPosX = mousePosX - basePos.x; deltaPosY = mousePosY - basePos.y; // ---- this is probably what you're missing basePos = mousePos; //!!!!!!!!!!!!
Actually that is exactly what i'm doing at the moment, it just don't work very well.
I'm updating the camera's vector with the amount the mouse moved, it still creates drift.
This is not a 2D problem, everything is in 3D.
I'm updating the camera's vector with the amount the mouse moved, it still creates drift.
This is not a 2D problem, everything is in 3D.
Quote:.. positioned on X and Y where Z is always 0. .. This is not a 2D problem, everything is in 3D.
That's a bit confusing, I'm afraid.
So the map is displayed in 2D, you're dragging something in 3D (presumably with Z != 0) and want the 2D display to match (or something..)? If so, take a look at the D3DXProject and D3DXUnproject functions.
Or, perhaps, you could post your code where you get the mouse position, calculate the delta and display the map using the delta.
Does it occur when zoom = 1?
p.s. Not interested in a 7 meg download, thanks. [wink]
Note: you can use code and source tags to post code. Click on Edit for my post (you can look at it but won't be able to save changes) to see:
Here's how code can be displayed
//Here's some source codevoid function(..){ //stuff}
Well it is a 2D map, in the sense that the Z coord is always zero for all of the quads that make up the star map.
this is the code used for moving the camera, the cameras Z position is set according to zoom level, and the cameras target is set as the same position, but with Z 0.
Code:
this is the code used for moving the camera, the cameras Z position is set according to zoom level, and the cameras target is set as the same position, but with Z 0.
Code:
float deltaX = mouseState.X - _lastMouseState.X; float deltaY = mouseState.Y - _lastMouseState.Y;camPosition += new Vector3(-(deltaX) * _mouseMoveScale, (deltaY) * _mouseMoveScale, 0);if (camPosition.X > 1000) camPosition.X = 1000;if (camPosition.Y > 1000) camPosition.Y = 1000;if (camPosition.X < 0) camPosition.X = 0;if (camPosition.Y < 0) camPosition.Y = 0;camTarget = new Vector3(camPosition.X, camPosition.Y, 0);_cameraUpdated = true;
Here is the draw code (it's using XNA)
g.draw is the what draws the map, it calls DrawIndexedPrimitives with the projection matrix and the viewmatrix.
if (_cameraUpdated) { viewMatrix = Matrix.CreateLookAt(camPosition, camTarget, new Vector3(0, 1, 0)); } GraphicsDevice.Clear(Color.Black); g.Draw(gameTime, viewMatrix, projectionMatrix);
g.draw is the what draws the map, it calls DrawIndexedPrimitives with the projection matrix and the viewmatrix.
From my previous post.
Do you set _lastMouseState = mouseState after you update the camera*?
If not, you're moving the camera based on a change from the initial position of the mouse, not how much the mouse moved since the last update.
Also, are you using an ortho projection?
EDIT:
*Or does XNA update it automatically?
EDIT2: Your camera always has a non-zero Z position component, correct?
Quote:On mouse-move:
deltaPosX = mousePosX - basePos.x;
deltaPosY = mousePosY - basePos.y;
// ---- this is probably what you're missing
basePos = mousePos; //!!!!!!!!!!!!
Do you set _lastMouseState = mouseState after you update the camera*?
If not, you're moving the camera based on a change from the initial position of the mouse, not how much the mouse moved since the last update.
Also, are you using an ortho projection?
EDIT:
*Or does XNA update it automatically?
EDIT2: Your camera always has a non-zero Z position component, correct?
Yeah _lastMouseState is set last in the update function, and no I'm using a perspective projection.
Cross-post. Your camera always has a non-zero Z position component, correct?
EDIT: By the way, zooming is normally done by changing the fov. What does mouseMoveScale do (or is it just 1)?
EDIT: By the way, zooming is normally done by changing the fov. What does mouseMoveScale do (or is it just 1)?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement