Vector:ToScreen() with render.RenderView

How would I get the correct x and y screen coordinates for a vector in a render.RenderView drawn on a derma panel? E.g. Vector(0, 0, 0):ToScreen() would return the position of Vector(0, 0, 0) on my screen in the renderview in the panel, not in general. Think map icons.

:snip: complete misread, sorry

I’ve seen it done before, I want to make an in-game map.

Sorry, I completely misread your original post. It depends what you set the view origin and angles to be.

Well, they’d be somewhat above the player and looking down.

Let’s say:



// Pos (90 units above localplayer)
LocalPlayer():GetPos()+Vector(0,0,90)

// Angle (looking down)
Angle(90, 0, 0)


I’m assuming you want them AFTER you’ve done the RenderView? In that case, it’s a bit tricky. You have 2 options: calculate the position yourself (fun if you like math, geometry and trigonometry, a huge headache if you don’t, and in either case would make some pretty ugly code), or do you things inside the RenderView rather than after. I’m actually not sure if that works (it should), and I’m not near gmod so you’ll have to test it yourself. The idea is to do this (untested code):
[lua]hook.Add(“PreDrawEffects”, “MiniMapIcons”, function()
cam.Start2D()
– draw your icons here, vector:ToScreen should give you minimap coords like you want
cam.End2D()
return true
end)
render.RenderView(your_view) – make sure the view has
hook.Remove(“PreDrawEffects”, “MiniMapIcons”)[/lua]

[editline]9th October 2017[/editline]

Actually HUDPaint isn’t the ideal hook for this, because you’re not actually drawing the HUD in the minimap and doing so would be a problem. Maybe use PreDrawOverlay instead.

[editline]9th October 2017[/editline]

Okay, looking at the wiki,

GM:PreDrawEffects seems most likely to be what you need - you just have to use Start2D. I’ve updated the code above. Still not tested though.

http://forum.facepunch.com/showthread.php?t=1580420&p=52729315&viewfull=1#post52729315

??

What are you asking for?

Icons on a minimap, given vector positions of them.

This actually does draw something which updates as I move, but the position of the box seems to be completely off relative to my position.
Code:



hook.Add("PreDrawEffects", "Map", function()
    cam.Start2D()
	local pos = LocalPlayer():GetPos():ToScreen()
	surface.SetDrawColor(255, 255, 255, 255)
	surface.DrawRect(pos.x, pos.y, 10, 10)
    cam.End2D()

    return true
end)

render.RenderView({
    origin = (0, 0, 0),
    angles = Angle(90, 0, 0),
    drawviewmodel = false,
    x = x,
    y = y,
    w = w,
    h = h,
    fov = 120
})

hook.Remove("PreDrawEffects", "Map")


Can you make a video of the result?

That’s not going to be right for a minimap. You’ll have to translate them manually – you can’t use ToScreen, nor should you have to trace at all. You can reverse the formula Pyro-Fire linked with some algebra.

That won’t work for a perspective-projection minimap

Here’s a gif:

Welp, there goes my mobile data for the month! I’m not even sure what I’m looking at.

See how I walk on the middle of the road.
The white box displayed on the map.

It’s clearly not on the road.

It will if you just get get corners through some maths with the projection height and FOV.

Try to subtract the minimap’s screen coordinates from the vector:ToScreen ones.