• ToScreen() from another position
    8 replies, posted
How can I use ToScreen() (or a replacement) but from another position and angle than what the local player is currently seeing? Tried messing with LocalToWorld and WorldToLocal stuff... with no avail.
It's hard to understand what you mean. Can you explain what you need this for?
A radar system. [lua] local vec = Vector(0, 0, 128); local ang = Angle(90, 0, 0); function frame:Paint(w, h) draw.RoundedBox(0, 1, 1, w-2, h-2, Color(0, 0, 0, 100)); surface.SetDrawColor(Color(10, 10, 10, 50)); surface.DrawOutlinedRect(0, 0, w, h); surface.SetDrawColor(Color(255, 255, 255)); local x, y = self:GetPos(); vec = LerpVector(0.05, vec, Vector(LocalPlayer():GetPos().x, LocalPlayer():GetPos().y, LocalPlayer():GetPos().z+600)); for _, v in pairs(ents.FindByClass("npc_combine_s")) do local pos = WorldToLocal(v:GetPos(), v:GetAngles(), vec, ang); local vp = pos:ToScreen(); print(vp.x); -- ridiculously large numbers draw.RoundedBox(0, vp.x-8, vp.y-8, 16, 16, Color(255, 0, 0, 100)); end end [/lua] Currently... not working.
[QUOTE=RonanZer0;50379430]A radar system. [lua] local vec = Vector(0, 0, 128); local ang = Angle(90, 0, 0); function frame:Paint(w, h) draw.RoundedBox(0, 1, 1, w-2, h-2, Color(0, 0, 0, 100)); surface.SetDrawColor(Color(10, 10, 10, 50)); surface.DrawOutlinedRect(0, 0, w, h); surface.SetDrawColor(Color(255, 255, 255)); local x, y = self:GetPos(); vec = LerpVector(0.05, vec, Vector(LocalPlayer():GetPos().x, LocalPlayer():GetPos().y, LocalPlayer():GetPos().z+600)); for _, v in pairs(ents.FindByClass("npc_combine_s")) do local pos = WorldToLocal(v:GetPos(), v:GetAngles(), vec, ang); local vp = pos:ToScreen(); print(vp.x); -- ridiculously large numbers draw.RoundedBox(0, vp.x-8, vp.y-8, 16, 16, Color(255, 0, 0, 100)); end end [/lua] Currently... not working.[/QUOTE] you need to use sin and cos for a radar, not toscreen
im not trying to make a circle im trying to make a square radar here
What he means is translating the x and y potion of a point on to a 2D plane on the screen to draw the radar. I just wrote this example up here that shows any props within a radius on the map relative to the player [CODE]function WorldToRadar(pos, origin) --Retuns a vector 2 of the position, Multiply this to scale it local p = pos - origin return Vector(p.x , p.y,0) end local scale = 0.05 hook.Add("HUDPaint" , "ExampleRadar" , function() draw.RoundedBox(0,10,10,100,100 , Color(40,40,40,255)) for k ,v in pairs(ents.FindByClass("prop_physics")) do local pos = WorldToRadar(v:GetPos() , LocalPlayer():GetPos()) * scale if pos.x > -50 and pos.x < 50 and pos.y > -50 and pos.y < 50 then --Only draw if in the bounds of the radar draw.RoundedBox(0,60 - pos.x , 60 - pos.y , 3,3,Color(120,255,120,255)) --We do 60 - pos.x to offset it from the center of the radar end end end)[/CODE] Please note that the code above does not account for rotation relative to the origin, You could do that yourself or ask again if you need it.
Yeah I don't see how I can rotate it relative to origin... something like this? [lua]function WorldToRadar2(ang, origin) local a = ang - origin; return Angle(0 , a.y, a.r); -- pitch is z right? end[/lua] EDIT: I'm going to have to use sin and cos aren't I?
[QUOTE=RonanZer0;50384284]Yeah I don't see how I can rotate it relative to origin... something like this? [lua]function WorldToRadar2(ang, origin) local a = ang - origin; return Angle(0 , a.y, a.r); -- pitch is z right? end[/lua][/QUOTE] You cant subtract an angle to a vector, but you can do this: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Vector/Rotate]Vector:Rotate[/url]
I think I got it. [lua]pos:Rotate(LocalPlayer():GetAngles());[/lua] Is this correct? It seems to work, but looks like it's a little bit off... EDIT: Yep, definitely wrong. Completely wrong. I can't tell what though, seems like the radar itself is not relative to the player's view angles or something... it's hard to explain.
Sorry, you need to Log In to post a reply to this thread.