# Draging a map in directx

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

## Recommended Posts

Hi everyone ,

i'm in the process of making a gis with directx and i have to drag the mouse around the map see different part of it (like google map). The main problem with this is the speed of the drag when zooming in and out. Setting the scroll speed to a static number when zoom out and draging the map is easy but when I zoom in i have to slow the speed of the dragging in order to give the gis a proper smooth movement. I wonder if anyone has any experience with this and if someone has any idea of a mathematical formula to do this smoothly. The way I do this right now is the following and static but is working well. thanks in advance.

[source lang="csharp"] public void UpdateScene(float x , float y, float z)
{
float currentX;
float currentY;
float currentZ;

currentX = x * 0.00014f;
currentY = y * 0.00314f;
currentZ = z * 0.0005f;

camPosition.X += currentX;
camPosition.Y += -currentY;

// Console.Write("CamPosition Z: " + camPosition.Z +"\r");

if (camPosition.Z + currentZ < 1.1f && camPosition.Z + currentZ > -1.5)
{
camPosition.Z += currentZ;
}
else
{
currentZ = 0;
}

camView = camView * Matrix.Translation(new Vector3(currentX, -currentY, -currentZ));
WVP = World * camView * camProj ;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData, cbPerObjectBufferDesc);
}[/source]

##### Share on other sites
If you're using an orthographic projection, the area the camera can see has a width height, and depth. Transform the mouse position data to normalized coordinates (a range from zero to one) if you're currently using pixels or something else. Then multiply the size of the camera view with delta mouse position, negate, and use the result to translate the camera.

If you're using a perspective projection this won't work, so let me know if that's the case.

##### Share on other sites
For some reason the edit button doesn't work. Read 'Then multiply the size of the camera view' in the post above as 'Then multiply the width and height of the camera view'.

##### Share on other sites
Here is the camera and world setup i'm not sure if it's othorgraphic... thanks for your help.

[source lang="csharp"] private void SetWorldAndCam()
{
// Set Constant Buffer
cbPerObjectBufferDesc = new BufferDescription();
cbPerObjectBufferDesc.SizeInBytes = Marshal.SizeOf(typeof(cbPerObject));
cbPerObjectBufferDesc.Usage = ResourceUsage.Default;
cbPerObjectBufferDesc.BindFlags = BindFlags.ConstantBuffer;
cbPerObjectBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
cbPerObjectBufferDesc.OptionFlags = ResourceOptionFlags.None;
cbPerObjectBufferDesc.StructureByteStride = Marshal.SizeOf(typeof(cbPerObject));

// Set Camera
camPosition = new Vector3(0.0f, 0.0f, -1.0f);
camTarget = new Vector3(0.0f, 0.0f, 0.0f);
camUp = new Vector3(0.0f, 1.0f, 0.0f);
camView = Matrix.LookAtLH(camPosition, camTarget, camUp);
camProj = Matrix.PerspectiveFovLH(0.4f * 3.14f, (float)ptd.Height / ptd.Width, 0.00001f, 1000.0f);
World = Matrix.Identity;
WVP = World * camView * camProj;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData,cbPerObjectBufferDesc);

}[/source]

##### Share on other sites
duh stupid me i answered my own question... it's Perspective : camProj = Matrix.PerspectiveFovLH(0.4f * 3.14f, (float)ptd.Height / ptd.Width, 0.00001f, 1000.0f);

##### Share on other sites
well without knowing it you answered another of my questions i didnt want an perpective projection but an orthgraphic one. you help is much appreciated.

##### Share on other sites
I'm still confused about this. My dragging is working except that it's rather a challenge to drag at close zoom because the movement is going to fast.

My camera is a perspective :

[source lang="csharp"] private void SetWorldAndCam()
{
// Set Constant Buffer
cbPerObjectBufferDesc = new BufferDescription();
cbPerObjectBufferDesc.SizeInBytes = Marshal.SizeOf(typeof(cbPerObject));
cbPerObjectBufferDesc.Usage = ResourceUsage.Default;
cbPerObjectBufferDesc.BindFlags = BindFlags.ConstantBuffer;
cbPerObjectBufferDesc.CpuAccessFlags = CpuAccessFlags.None;
cbPerObjectBufferDesc.OptionFlags = ResourceOptionFlags.None;
cbPerObjectBufferDesc.StructureByteStride = Marshal.SizeOf(typeof(cbPerObject));

// Set Camera
camPos = 0.42;
camPosition = new Vector3(0.0f, 0.0f, -100.0f);
camTarget = new Vector3(0.0f, 0.0f, 0.0f);
camUp = new Vector3(0.0f, 1.0f, 0.0f);
camView = Matrix.LookAtLH(camPosition, camTarget, camUp);
camProj = Matrix.PerspectiveFovLH(0.4f * 3.14f, (float)ptd.Width / ptd.Height, 0.00001f, float.MaxValue);
World = Matrix.Identity * 10000;

WVP = World * camView * camProj;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData,cbPerObjectBufferDesc);

}[/source]
and here is wher I do my dragging :

[source lang="csharp"] public void UpdateScene(float x , float y, float z)
{
float currentX;
float currentY;
float currentIH = ptd.Height;
float currentIW = ptd.Width;

currentX = x ;
currentY = y ;

Ray mr = PanelToWordSpace();
Console.WriteLine(mr.Position);

#region Zoom
if (z != 0)
{
if (z > 0 && camPos > 0.09 )
{
camPos = camPos - 0.07;

}
else
{
if (z < 0 && camPos < 0.98)
{
camPos = camPos + 0.07;
}
}
Console.WriteLine(camPos);
}
#endregion

camView = camView * Matrix.Translation(currentX,-currentY,0.00f);
camProj = Matrix.PerspectiveFovLH((float)camPos * 3.14f, (float)ptd.Width/ ptd.Height, 0.00001f, float.MaxValue);
WVP = World * camView * camProj ;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Write(cbPerObj);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData, cbPerObjectBufferDesc);
}
[/source]

The main problem I have is having the mouse cursor stick to the place I am dragging and give the drag a proper speed. Any help would be appreciated.