Jump to content
  • Advertisement
Sign in to follow this  

Help with some camera code

This topic is 2177 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 currently using slimdx to make a GIS and i'm having some kind of problem with the camera which i'm not quite sure how to fix. I`ve been able to make the cursor follow the place i'm dragging but when I drag the camera the second time it is reset to the original position. Any help would be appreciated. i'm using an orthographic projection. The PanelToWorldSpace convert the view to a 1 to -1 coordinates system.
[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;
mouseX = x;
mouseY = y;

Ray mr = PanelToWordSpace(x,y);

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

if (z < 0 && camPos < 1.4)
camPos = camPos + 0.07;
camProj = Matrix.OrthoLH(ptd.Width*3, ptd.Height*3, 0.00001f, float.MaxValue);

mouseNormalX = mr.Position.X;
mouseNormalY = mr.Position.Y;

float mouseNormalDeltaX = oldMouseNormalX - mouseNormalX;
float mouseNormalDeltaY = oldMouseNormalY - mouseNormalY;
float mouseDeltaX = oldMouseX - mouseX;
float mouseDeltaY = oldMouseY - mouseY;

camProj = camProj * Matrix.Translation(new Vector3(-mouseNormalDeltaX, -mouseNormalDeltaY, 0.0f));

WVP = World * camView * camProj ;
cbPerObj.WVP = Matrix.Transpose(WVP);
DataStream cbData = new DataStream(Marshal.SizeOf(typeof(cbPerObject)), true, true);
cbData.Position = 0;
cbPerObjectBuffer = new Buffer(device, cbData, cbPerObjectBufferDesc);
context.VertexShader.SetConstantBuffer(cbPerObjectBuffer, 0);
oldMouseNormalX = mouseNormalX;
oldMouseNormalY = mouseNormalY;
oldMouseX = mouseX;
oldMouseY = mouseY;

[source lang="csharp"] private Ray PanelToWordSpace(float x , float y)
Vector3 near = new Vector3(x,y,0.00001f);
Vector3 far = new Vector3(x,y,float.MaxValue);
Vector3 vNearW = Vector3.Unproject(near,0,0,ptd.Width,ptd.Height,0,1,World );
Vector3 vFarW = Vector3.Unproject(far, 0, 0, ptd.Width, ptd.Height, 0, 1, World);
Vector3 vDirect = vFarW - vNearW;
vDirect = Vector3.Normalize(vDirect);
return new Ray(vNearW, vDirect);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

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!