gui.ScreenToVector() and ply:GetCursorAimVector() does not work for what I’m doing because they assume the screen position is eyepos and eyeangles of the player. I want to trace from my screen to a world position from a different position.

Here’s a failed attempt my friend Mathias (Dr Magnusson) did.

[lua]

function MouseToScreen()

local eP = LocalPlayer():EyePos()

local eA = LocalPlayer():EyeAngles()

```
local FoV = LocalPlayer():GetFOV()
local s = { W = ScrW(), H = ScrH() }
local x, y = gui.MousePos()
local m = { X = x, Y = y }
local Yaw = ( ( ( m.X / s.W ) * FoV ) - (FoV/2 ) )
local Pitch = ( ( ( m.Y / s.H ) * FoV ) - (FoV/2 ) ) * -1
local Dir = Angle( eA.p + Pitch, eA.y + Yaw, 0 ):Forward()
return util.TraceLine( { start = eP, endpos = eP + (Dir*2048) } ).HitPos
```

end

[/lua]

and here’s an attempt I did on converting a function from some XNA shit

[lua]

local function MouseToVector(x_position, y_position)

```
local x = -(x_position - (ScrW()-ScrH()/2)) / (smooth_fov/100*ScrH()/2)
local y = (y_position - (ScrH()-ScrH()/2)) / (smooth_fov/100*ScrH()/2)
local z = 0
local mag = x*x + y*y
if mag > 1 then
local scale = 1 / math.sqrt(mag)
x = x*scale
y = y*scale
else
z = math.sqrt(1-mag)
end
return Vector(x,-z,-y)
```

end

[/lua]

C++

[cpp]

D3DXVECTOR3 CD3DArcBall::ScreenToVector( float fScreenPtX, float fScreenPtY )

{

// Scale to screen

FLOAT x = -(fScreenPtX - m_Offset.x - m_nWidth/2) / (m_fRadius* m_nWidth/2);

FLOAT y = (fScreenPtY - m_Offset.y - m_nHeight/2) / (m_fRadius* m_nHeight/2);

```
FLOAT z = 0.0f;
FLOAT mag = x*x + y*y;
if( mag > 1.0f )
{
FLOAT scale = 1.0f/sqrtf(mag);
x *= scale;
y *= scale;
}
else
z = sqrtf( 1.0f - mag );
// Return vector
return D3DXVECTOR3( x, y, z );
```

}

[/cpp]