I've been pulling my hair out over this one. Basically, I need to get the 2D coordinates for a 3D vector.
[b]Vector:ToScreen()[/b] is not an option, because i'm using a custom camera.
I half-found a solution [URL="http://wiki.garrysmod.com/?title=Common_Code_Snippets_Misc#Screen_to_Vector_.2F_Vector_to_Screen_with_custom_screen_.2F_camera"]here,[/URL] but it's noticeably off.
This is somewhat tricky, since unlike XNA, there are no matrix classes to play around with.
All help appreciated :D
You can represent matrices in Lua:
[url]http://www.lua.org/pil/11.2.html[/url]
[url]https://github.com/davidm/lua-matrix[/url]
If you wanted, you could even create your own matrix "classes" in Lua by defining some metatables.
[url]http://www.lua.org/pil/16.1.html[/url]
After you have your matrices, I think you know where to go from there.
[QUOTE=Wizard of Ass;34358640][url]http://wiki.garrysmod.com/?title=Common_Code_Snippets_Misc#Screen_to_Vector_.2F_Vector_to_Screen_with_custom_screen_.2F_camera[/url][/QUOTE]
That's the link he posted. He thinks the result is off.
[QUOTE=wizardsbane;34356302]You can represent matrices in Lua:
[url]http://www.lua.org/pil/11.2.html[/url]
[url]https://github.com/davidm/lua-matrix[/url]
If you wanted, you could even create your own matrix "classes" in Lua by defining some metatables.
[url]http://www.lua.org/pil/16.1.html[/url]
After you have your matrices, I think you know where to go from there.[/QUOTE]
I've never actually used Matrices before, worth learning. Thanks for this.
[QUOTE=Wizard of Ass;34358640][url]http://wiki.garrysmod.com/?title=Common_Code_Snippets_Misc#Screen_to_Vector_.2F_Vector_to_Screen_with_custom_screen_.2F_camera[/url][/QUOTE]
I've already tried this, sadly it's off for me (maybe something to do with aspect, I don't understand it)
Would using matrices to convert the coordinates be slow, or practically unnoticeable?
[QUOTE=Toneo;34347074]I half-found a solution [URL="http://wiki.garrysmod.com/?title=Common_Code_Snippets_Misc#Screen_to_Vector_.2F_Vector_to_Screen_with_custom_screen_.2F_camera"]here,[/URL] but it's noticeably off.[/QUOTE]
Are you sure you got the FOV right? ( It is 75 by default )
Just asking this because it works fine for me:
( At the left, the FOV is correct, at the right it is manually changed to 70 in the script )
[IMG]http://dl.dropbox.com/u/7659139/Facepunch/wrong_FOV.png[/IMG]
What resolution is that? It's still off for me (I'm testing by rotating the camera slowly around an object at [0,0,0], and drawing a marker at what the code thinks is [0,0,0])
The comments say something about 4:3, could it need changing to support other aspect ratios?
[QUOTE=Toneo;34394523]What resolution is that? It's still off for me (I'm testing by rotating the camera slowly around an object at [0,0,0], and drawing a marker at what the code thinks is [0,0,0])
The comments say something about 4:3, could it need changing to support other aspect ratios?[/QUOTE]
You're right, actually. I looked at the original post, and the person who posted that script also created a fix that might have not made it to the wiki. Go to the wiki page for the script, follow the link to the user's post, and scroll all the way down. The script had been fixed for widescreen ratios.
The wiki is currently down, but here is the code for different aspect ratio support:
[lua]
cam_utils = {}
function cam_utils.ToScreen( cPos, cAng, cFov, scrW, scrH, vDir )
local vDir = cPos - vDir
local d = 4 * scrH / ( 6 * math.tan( 0.5 * cFov ) )
local fdp = cAng:Forward():Dot( vDir )
if ( fdp == 0 ) then
return 0, 0, false, false
end
local vProj = ( d / fdp ) * vDir
local x = 0.5 * scrW + cAng:Right():Dot( vProj )
local y = 0.5 * scrH - cAng:Up():Dot( vProj )
return x, y, ( 0 < x && x < scrW && 0 < y && y < scrH ) && fdp < 0, fdp > 0
end
[/lua]
Sorry, you need to Log In to post a reply to this thread.