Jump to content
• Advertisement

Public Group

# Draging a map in directx

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

If you intended to correct an error in the post then please contact us.

## 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);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);
}[/source]

#### Share this post

##### Share on other sites
Advertisement
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 this post

##### 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 this post

##### 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);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);

}[/source]

#### Share this post

##### 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 this post

##### 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 this post

##### 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);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);

}[/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);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);
}
[/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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
19
2. 2
JoeJ
15
3. 3
4. 4
5. 5
• Advertisement

• 24
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001778
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!