Yes you are are right.
Here is my work so far:
type _Point3D
x as float
y as float
z as float
endtype
type _CtrlGizmo3D
o as integer
l as integer
endtype
// set display properties
device_Init(1024, 768, 0)
pointer_Init()
// set window properties
SetWindowTitle("Gizmo 3D")
SetOrientationAllowed(1, 1, 1, 1)
global cam as _Camera
global cam_org as _Point3D
global gizmo as _CtrlGizmo3D
global point as _Point3D
global dist as float
camera_Create(cam, 0.0, 10.0, 0.0, 0.0, 20.0, 10.0, 0.0)
camera_SetWalkMode(cam, 1)
camera_SetActive(cam, 1)
ctrlgizom3D_Create(gizmo, 2.0, 2.0, 2.0)
do
pointer_Update()
camera_Update(cam)
if GetRawKeyState(_W) then camera_MoveLocalZ(cam, 0.25)
if GetRawKeyState(_S) then camera_MoveLocalZ(cam, -0.25)
if GetRawKeyState(_D) then camera_MoveLocalX(cam, 0.25)
if GetRawKeyState(_A) then camera_MoveLocalX(cam, -0.25)
ctrlgizmo3D_Draw(gizmo)
cam_org.x = GetCameraX(1)
cam_org.y = GetCameraY(1)
cam_org.z = GetCameraZ(1)
dist = ctrlgizom3D_GetDistFromPoint(gizmo, cam_org)
point = Get3DPointFromScreen(thePointer.x, thePointer.y, dist)
Print("Dist = " + str(dist))
Print("x = " + str(point.x))
Print("y = " + str(point.y))
Print("z = " + str(point.z))
sync()
loop
function ctrlgizom3D_Create(g ref as _CtrlGizmo3D, lx as float, ly as float, lz as float)
g.o = CreateVector3(0.0, 0.0, 0.0)
g.l = CreateVector3(Abs(lx), Abs(ly), Abs(lz))
endfunction
function ctrlgizom3D_Delete(g ref as _CtrlGizmo3D)
DeleteVector3(g.o)
DeleteVector3(g.l)
endfunction
function ctrlgizmo3D_Draw(g ref as _CtrlGizmo3D)
x as float
y as float
z as float
lx as float
ly as float
lz as float
x1 as float
y1 as float
x2 as float
y2 as float
x = GetVector3X(g.o)
y = GetVector3Y(g.o)
z = GetVector3Z(g.o)
lx = GetVector3X(g.l)
ly = GetVector3Y(g.l)
lz = GetVector3Z(g.l)
x1 = GetScreenXFrom3D(x, y, z)
y1 = GetScreenYFrom3D(x, y, z)
x2 = GetScreenXFrom3D(x + lx, y, z)
y2 = GetScreenYFrom3D(x + lx, y, z)
DrawLine(x1, y1, x2, y2, MakeColor(255, 0, 0), MakeColor(255, 0, 0))
x2 = GetScreenXFrom3D(x, y + ly, z)
y2 = GetScreenYFrom3D(x, y + ly, z)
DrawLine(x1, y1, x2, y2, MakeColor(0, 255, 0), MakeColor(0, 255, 0))
x2 = GetScreenXFrom3D(x, y, z + lz)
y2 = GetScreenYFrom3D(x, y, z + lz)
DrawLine(x1, y1, x2, y2, MakeColor(0, 0, 255), MakeColor(0, 0, 255))
endfunction
function ctrlgizom3D_GetDistFromPoint(g ref as _CtrlGizmo3D, point ref as _Point3D)
d as float
p as integer
p = CreateVector3(point.x, point.y, point.z)
d = GetVector3Distance(g.o, p)
endfunction d
function Get3DPointFromScreen(x as float, y as float, z as float)
point3D as _Point3D
VectorX# = Get3DVectorXFromScreen(x, y)
VectorY# = Get3DVectorYFromScreen(x, y)
VectorZ# = Get3DVectorZFromScreen(x, y)
// get a vector that points in the direction the camera is looking
VectorX2# = Get3DVectorXFromScreen(GetVirtualWidth()/2.0, GetVirtualHeight()/2.0)
VectorY2# = Get3DVectorYFromScreen(GetVirtualWidth()/2.0, GetVirtualHeight()/2.0)
VectorZ2# = Get3DVectorZFromScreen(GetVirtualWidth()/2.0, GetVirtualHeight()/2.0)
// normalise Vector in the direction of Vector2
Dot# = VectorX#*VectorX2# + VectorY#*VectorY2# + VectorZ#*VectorZ2#
if ( Dot# > 0 )
VectorX# = VectorX# / Dot#
VectorY# = VectorY# / Dot#
VectorZ# = VectorZ# / Dot#
endif
point3D.x = VectorX# * z + GetCameraX(1)
point3D.y = VectorY# * z + GetCameraY(1)
point3D.z = VectorZ# * z + GetCameraZ(1)
endfunction point3D
the output of the code is the following video:
As you can see when the camera is looking towards the z-axis the Get3DPointFromScreen(x as float, y as float, z as float) function gives correct results, but when the camera is looking the origin from a different point, then the coordinates of the mouse pointer are not translated correctly.