I'm working on a gamemode and trying to make a top down kinda view with WASD movement but the player always aiming at the cursor.
I'm using CalcView to view from thirdperson, and trying to use ply:GetCursorAimVector() to trace from the player to the cursor position, but I have a strong feeling I am either doing it wrong, or GetCursorAimVector gets skewed when you mess with CalcView
(inside a player class for the gamemode (Fretta))
[code]
function CLASS:CalcView( ply, pos, angles, fov )
gui.EnableScreenClicker(true)
local mVec = ply:GetCursorAimVector()
local trace = {}
trace.startpos = ply:EyePos()
trace.endpos = ply:EyePos() + mVec * 9000
trace.filter = ply
local tRes = util.TraceLine(trace)
if tRes.Hit or tRes.HitWorld then
local cAim = tRes.HitPos --+ Vector(0,0,ply:EyePos().z)
local tAng = (cAim - ply:EyePos()):Angle()
debugoverlay.Cross(tRes.HitPos, 32, .05, Color(255,255,255,255))
debugoverlay.Line(ply:EyePos(), tRes.HitPos, .05)
ply:SetEyeAngles(tAng)
end
local view = {}
view.origin = pos + Vector(250,0,250)
view.angles = Angle(45,180,0)
view.fov = fov
return view
end
[/code]
The trace is obviously wrong (as i can see from the debugoverlay)
What am I messing up?
[editline]2nd November 2010[/editline]
I've tried also using gui.ScreenToVector(gui.MousePos()) but that doesnt work either.
Does mVec return a Z value that is anything but 0?
[code]
function AngFromScreen( )
local x, y, ang
x = ScrW( ) / 2 - gui.MouseX( )
y = ScrH( ) / 2 = gui.MouseY( )
ang = math.atan2( y, x )
return math.deg( ang )
end
pl:SetEyeAngles( Angle( pitch, AngFromScreen( ), 0 ) )
[/code]
Figuring pitch is probably going to be a mess and will probably involve tracing. Perhaps a simple 2d check for entities to aim at?
[code]
local k, v, pos, min, max
for k, v in pairs( ents.GetAll( ) ) do
if v == pl then
continue
end
if not table.HasValue( proper_entities, v:GetClass( ) ) then
continue
end
min, max = v:GetWorldSpaceAABB( )
min = min:ToScreen( )
max = max:ToScreen( )
if not ( min.x <= x and max.x <= x ) then
continue
end
if not ( min.y <= y and max.y <= y ) then
continue
end
pitch = ( v:GetPos( ) - pl:GetShootPos( ) ):Angle( ).p
break
end[/code]
CursorAimVector is from the player's eye position rather than where CalcView moves it to, afaik.
Of course, none of this is tested, so you would need to mess around with it.
[lua]
local screenRatio = ScrH()/ScrW()
local FOV = LocalPlayer():GetFOV()
local yaw = FOV*gui.MouseX()/ScrW()-FOV/2
local pitch = (FOV*gui.MouseY()/ScrH()-FOV/2)*screenRatio
[/lua]
Something like that.
I shoulda mentioned that my camera is at a 45 deg angle downwar, not DIRECTLY top down - so I dont think converting from screen positions will work correctly O.o
[editline]2nd November 2010[/editline]
Got it working.
Sorry, you need to Log In to post a reply to this thread.