Vector:ToScreen() returns huge value at certain angles/positions

8 replies, posted

I was experimenting a bit with Vector:ToScreen() and came across a weird bug which makes it return huge values at certain angles and positions. What I've done is to take 2 3DVectors and used Vector:ToScreen() to convert them to 2DVectors. The line you are seeing in the video are the two 2DVectors drawn using Surface.DrawLine(). In the chat, you can see what Vector:ToScreen() returned for the two 3DVectors.
Have anyone experienced this before?
Am I possibly doing something wrong?
Does anyone have an alternative way instead of using Vector:ToScreen()?
https://www.youtube.com/watch?v=-NBjaskA1Pc&feature=youtu.be
local toScr1 = Vector(-47, 368, -12287):ToScreen()
local toScr2 = Vector(-24, 278, -12287):ToScreen()
chat.AddText(Color(200,0,0,255), "------------------------")
chat.AddText(Color(200,0,0,255), "toScr1: X=" .. math.Round(toScr1.x) .. " | Y=" .. math.Round(toScr1.y))
chat.AddText(Color(200,0,0,255), "toScr2: X=" .. math.Round(toScr2.x) .. " | Y=" .. math.Round(toScr2.y))
surface.SetDrawColor(255,0,0,255)
surface.DrawLine(toScr1.x, toScr1.y, toScr2.x, toScr2.y)

are you doing that in a 3D context

I'm 100% sure it's because the object is off-screen, the best solution might be to clamp the values using current screen resolution:
math.Clamp(CurrentValueX, 0, CurrentScreenSizeX)

It seems like it works the same in a 2D-Context and a 3D-Context. I tried to move it into a 3D-Context both yesterday and now again but without any difference.
I just tried this and it won't work as the problem is that the values are nonlinear. It seems like there is a jump in the values returned. The values returned are not "smooth".
Refer to this video and you'll understand what I mean:
https://www.youtube.com/watch?v=g67ZfV5iN14

Does this code have the same issue?
cam.Start3D()
local toScr1 = Vector(-47, 368, -12287):ToScreen()
local toScr2 = Vector(-24, 278, -12287):ToScreen()
toScr1.x = math.Clamp(toScr1.x, 0, ScrW())
toScr1.y = math.Clamp(toScr1.y, 0, ScrH())
toScr2.x = math.Clamp(toScr2.x, 0, ScrW())
toScr2.y = math.Clamp(toScr2.y, 0, ScrH())
chat.AddText(Color(200,0,0,255), "------------------------")
chat.AddText(Color(200,0,0,255), "toScr1: X=" .. math.Round(toScr1.x) .. " | Y=" .. math.Round(toScr1.y))
chat.AddText(Color(200,0,0,255), "toScr2: X=" .. math.Round(toScr2.x) .. " | Y=" .. math.Round(toScr2.y))
surface.SetDrawColor(255,0,0,255)
surface.DrawLine(toScr1.x, toScr1.y, toScr2.x, toScr2.y)
cam.End3D()

mind also printing out the position you're trying to get to screen?

I tried all of these without any luck. My guess would be that the Vector:ToScreen() function never was intended to be used when a vector is outside of the screen. However, it seems to me that there must be something wrong about it in the backend.

It happens because first vector of your line becomes out of screen. If you see your chat, second vector stays same

Yes, that happened to me as well. That's why I do not draw the entire line on the screen.
If one of the points is outside the screen area I don't draw it or I just split it to a bunch of smaller segments that I draw individually.

Sorry, you need to Log In to post a reply to this thread.