Hi,
I'm currently writing a program which requires the coordinates of the mouse to place things within DirectX. The code I'm using is as follows:
Function GetWorldMouseClickPoint() As D3DVECTOR
'return position in the world where the mouse was clicked
g_D3DDevice.GetViewport viewport
vIn.x = CursorX: vIn.y = CursorY: vIn.Z = 0
D3DXVec3Unproject vNear, vIn, viewport, matProj, matView, matWorld
'compute point on far clip plane at cursor
vIn.Z = 1
D3DXVec3Unproject vFar, vIn, viewport, matProj, matView, matWorld
D3DXVec3Subtract vDir, vFar, vNear
t = (-(vFar.Z) / vDir.Z)
GetWorldMouseClickPoint.x = vFar.x + t * vDir.x
GetWorldMouseClickPoint.y = vFar.y + t * vDir.y
end function
and i get the X,Y coordinates of the mouse from ...
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
CursorX = x / Screen.TwipsPerPixelX
CursorY = y / Screen.TwipsPerPixelY
End Sub
The problem I'm having is that the coordinates returned from GetWorldMouseClickPoint don't quite match the movement of the mouse. For example if I move the mouse upwards only, the image which I'm placing at the coordinates of GetWorldMouseClickPoint will appear to move diagonally up and to the left, but its x coordinate will remain the same.
I'm guessing this is something to do with my view or world matrices, but I don't fully understand how they work and so am having trouble finding a solution to my problem. The matrices are set up as follows
sub SetUpView()
D3DXMatrixRotationY matWorld, 0
g_D3DDevice.SetTransform D3DTS_WORLD, matWorld
D3DXMatrixLookAtLH matView, vec3(0#, 0#, camZ), _
vec3(camX, camY, 0), _
vec3(0, 1#, 0#)
g_D3DDevice.SetTransform D3DTS_VIEW, matView
D3DXMatrixPerspectiveFovLH matProj, 3.14159265358979 / 3, 1, 1, 2000
g_D3DDevice.SetTransform D3DTS_PROJECTION, matProj
end sub
the variables CamX and CamY are both = 50 and CamZ = -900.
I should also mention that I'm trying to show everything in 2 Dimensions, however when i move anything, I'm giving it a Z coordinate to match its Y coordinate, so that as something moves up the screen, it also appears to move away. I initially thought that was a possible cause of my problem, but couldn't see a reason why.
I would greatly appreciate any ideas?
Thanks in advance,
Gav.