Get 3D coordinates from 2D mouse position on Random Terrain!
How work the Ray Cacting, is very simple and can use from any language.
Just make some Ray with Cylinders and check witch one is closer on Terrain Height, the rest is easy.
This is sample code, put your desired properties on the cylinders and you can write the code with your way.
Dim oPicSphere As DarkGDK.Basic3D.Sphere
oPicSphere = New DarkGDK.Basic3D.Sphere(8)
oPicSphere.Color = Color.LightYellow
oPicSphere.Position(0, 0, 0)
oPicSphere.Visible = True 'True only for tests
Dim oRayCastSphere As DarkGDK.Basic3D.Sphere
oRayCastSphere = New DarkGDK.Basic3D.Sphere(3)
oRayCastSphere.Color = Color.Yellow
oRayCastSphere.Position(0, 0, 0)
oRayCastSphere.Visible = True 'True only for tests
Dim oRayCastCylinder(101) As DarkGDK.Basic3D.Cylinder
For i = 0 To oRayCastCylinder.Count - 1
oRayCastCylinder(i) = New DarkGDK.Basic3D.Cylinder(10)
oRayCastCylinder(i).Color = Color.LightYellow
oRayCastCylinder(i).Position(0, 0, 0)
oRayCastCylinder(i).Rotate(90, 0, 0)
oRayCastCylinder(i).Scale(3, 100, 3)
oRayCastSphere.Limbs.Item(0).GlueObject(oRayCastCylinder(i))
oRayCastCylinder(i).Visible = True 'True only for tests
Next i
oRayCastCylinder(0).Position(0, 0, 5)
For i = 1 To oRayCastCylinder.Count - 1
oRayCastCylinder(i).Position(0, 0, oRayCastCylinder(i - 1).Z + 11)
Next i
Public w3DMouseX, w3DMouseZ As Single
Function myGet3DMouse() As Boolean
Dim vectorx, vectory, vectorz, mult
Dim camerax, cameray, cameraz, c
'Pic X, Z World Coordinates from Mouse Screen X, Y
DarkGDK.Basic3D.Basic3D.PickScreen(DarkGDK.IO.Mouse.X, DarkGDK.IO.Mouse.Y, 1)
vectorx = DarkGDK.Basic3D.Basic3D.PickX
vectory = DarkGDK.Basic3D.Basic3D.PickY
vectorz = DarkGDK.Basic3D.Basic3D.PickZ
camerax = DarkGDK.Camera.DefaultCamera.CurrentPositionX
cameray = DarkGDK.Camera.DefaultCamera.CurrentPositionY
cameraz = DarkGDK.Camera.DefaultCamera.CurrentPositionZ
mult = DarkGDK.Core.Abs(cameray / vectory)
vectorx = vectorx * mult + camerax
vectory = vectory * mult + cameray
vectorz = vectorz * mult + cameraz
oRayCastSphere.Position(camerax, cameray, cameraz)
oRayCastSphere.Point(vectorx, vectory, vectorz)
oPicSphere.Position(camerax, cameray, cameraz)
oPicSphere.Point(vectorx, vectory, vectorz)
'Find Y World Coordinate
For c = 0 To oRayCastCylinder.Count - 1
If oRayCastCylinder(c).Y <= oTerrain.HeightAt(oRayCastCylinder(c).X, oRayCastCylinder(c).Z) Then Exit For
Next c
'I Set the range between 1-100 Cylinder
If c < 1 Then Return False : Exit Function 'To Close ''Set Max Close Distance
If c > oRayCastCylinder.Count - 1 Then Return False : Exit Function 'To Far ''Set Max Far Distance
oPicSphere.Position(oRayCastCylinder(c - 1).X, oRayCastCylinder(c - 1).Y, oRayCastCylinder(c - 1).Z)
'Micro Positioning
Do Until oPicSphere.Y <= oTerrain.HeightAt(oPicSphere.X, oPicSphere.Z)
oPicSphere.Move(1)
Loop
oPicSphere.Visible = True
w3DMouseX = oPicSphere.X
w3DMouseZ = oPicSphere.Z
Return True
End Function