• ScreenToVector and CalcView?
    13 replies, posted
So basically, what I'm trying to do is pretty much like Sims. I've put my calc view up a bit high, [lua] function GM:CalcView(ply, pos, angles, fov) local view = {} view.origin = pos+(angles:Up()*800) view.angles = Angle(90,0,0) view.fov = fov return view end [/lua] And I basically want to make the gui.ScreenToVector to match where I press on the screen (World), but currently I think it reacts as if the screen still is at its regular position (Without an custom calc view). My current trace (Which I know is wrong, but I never used it before.) [lua] local tr = util.QuickTrace( LocalPlayer():GetShootPos(), gui.ScreenToVector( gui.MouseX(), gui.MouseY(), LocalPlayer() ) ) print( tr.HitPos ) [/lua] My god, I have no clue how to word it.
Assuming you have your cursor visible, you could try this: [lua] local orig = LocalPlayer():GetShootPos() + CameraOffset; local dir = LocalPlayer():GetCursorAimVector(); local tr = util.QuickTrace(orig, dir*10000, LocalPlayer());[/lua]
Wasn't LocalPlayer():GetCursorAimVector() removed?
[url]http://www.facepunch.com/threads/891702[/url]
Finally, thank you! [editline]18th August 2013[/editline] Well, doesn't work for me right now, it spits out [code][ERROR] gamemodes/test/gamemode/cl_init.lua:26: attempt to perform arithmetic on a nil value 1. unknown - gamemodes/test/gamemode/cl_init.lua:26 [/code] Line 26 is trace.endpos = trace.startpos etc... [lua] function LPCameraScreenToVector( iScreenX, iScreenY, iScreenW, iScreenH, angCamRot, fFoV ) local d = 4 * iScreenH / ( 6 * math.tan( 0.5 * fFoV ) ) ; local vForward = angCamRot:Forward(); local vRight = angCamRot:Right(); local vUp = angCamRot:Up(); return ( d * vForward + ( iScreenX - 0.5 * iScreenW ) * vRight + ( 0.5 * iScreenH - iScreenY ) * vUp ):Normalize(); end function GM:CalcView(ply,pos,angles,fov) gui.EnableScreenClicker(true) local view = {} view.origin = pos+(angles:Up()*800) view.angles = Angle(90,0,0) view.fov = fov local trace = {} trace.startpos = view.origin trace.endpos = trace.startpos + LPCameraScreenToVector(gui.MouseX(), gui.MouseY(), ScrW(), ScrH(), view.angles, math.rad(view.fov)) * 50000 trace.filter = ply local tr = util.TraceLine(trace) return view end [/lua]
Bump :(
[QUOTE=Persious;41881571]Finally, thank you! [editline]18th August 2013[/editline] Well, doesn't work for me right now, it spits out [code][ERROR] gamemodes/test/gamemode/cl_init.lua:26: attempt to perform arithmetic on a nil value 1. unknown - gamemodes/test/gamemode/cl_init.lua:26 [/code] Line 26 is trace.endpos = trace.startpos etc... [/QUOTE] That's obviously becausae LPCameraScreenToVector isn't returning anything, because the only arithmetic is on the next line, and trace.startpos has to be correct in that context, so the only logical answer is that the function is returning no value. EDIT: Copy pasted your code, and sure enough, I put this in the function: [code] local vector = d * vForward + ( iScreenX - 0.5 * iScreenW ) * vRight + ( 0.5 * iScreenH - iScreenY ) * vUp local noramal = vector:Normalize() print(vector, normal)[/code] And it prints a vector followed by nil. I'm not great at math but finding the problem was easy, how to solve it? I've got no clue. EDIT2: wait isn't that vector already normalized? You don't even need :Normalize() in the code, that's your problem? I'm like 99% sure attempting to Normalize a normal vector returns a nil value.
[QUOTE=OzymandiasJ;41890899]I'm like 99% sure attempting to Normalize a normal vector returns a nil value.[/QUOTE] Attempting to normalize a [b]normal[/b] vector returns a normalized normal vector. If you meant normalized vector, then attempting to normalize a normalized vector returns the same vector (does not alter it).
[QUOTE=MakeR;41892955]Attempting to normalize a [b]normal[/b] vector returns a normalized normal vector. If you meant normalized vector, then attempting to normalize a normalized vector returns the same vector (does not alter it).[/QUOTE] ??? [IMG]http://gyazo.com/f9631519e47a48d1d2461601e92e4b7f.png[/IMG] I guess I don't understand the difference.
Vector.Normalize doesn't return anything, it modifies the original vector. Vector.GetNormalized returns the normalized input vector. Vector.GetNormal does the same thing but is incorrectly named. [editline]19th August 2013[/editline] A normal vector is one that is perpendicular to a surface. A normalized vector is one with its length equal to one.
[QUOTE=MakeR;41894174]Vector.Normalize doesn't return anything, it modifies the original vector. Vector.GetNormalized returns the normalized input vector. Vector.GetNormal does the same thing but is incorrectly named. [editline]19th August 2013[/editline] A normal vector is one that is perpendicular to a surface. A normalized vector is one with its length equal to one.[/QUOTE] So the problem to the original question would be that the vector is being normalized but not returned so there is no value to return for the function?
I hate this shit.
Just make :Normalize() at the end :GetNormal() and the problem is fixed.
oh, okay. I'll try that. [editline]20th August 2013[/editline] Well, okay, it's doing something now at least. Not the right thing, but I'll show you tomorrow. :P
Sorry, you need to Log In to post a reply to this thread.