Hey there,
I'm trying to setup a basic gamemode where players are in an isometric view. They can see their players running around a grid. What I am trying to do, and struggling a lot with, is to have a tile return its placement in a grid when a players mouse moves/hovers over it.
Ive checked the wiki for this and have attempted to do such a thing. However, I seem to be getting incorrect coords displacing by a few gmod vector points. Ive been working on this for quite a while and have no idea what the heck is going on.
This is my think function on client side for entity Tile:
function ENT:Think()
local pos = self:GetPos()
local tr = util.QuickTrace( LocalPlayer():EyePos(), gui.ScreenToVector( gui.MousePos() ), LocalPlayer() )
print( tr.HitPos )
print(pos)
end
And this is how I changed local players view:
function MyCalcView( ply, pos, angles, fov )
local viewType = tostring(ply:GetPData("view"))
if viewType == "ISO" then
local view = {}
view.origin = Vector(-399, -400, 400)
view.angles = Angle(60, 45, 0)
view.fov = fov
view.drawviewer = true
return view
elseif viewType == "DEFAULT" then
end
end
hook.Add("CalcView", "MyCalcView", MyCalcView)
This is some of the output im getting
190.000000 -95.000000 -444.000000 -- odds are tile coord
-71.200233 -112.077797 -378.843933 -- evens are mouse to 3d pos
190.000000 -47.500000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 0.000000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 47.500000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 95.000000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 142.500000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 190.000000 -444.000000
-71.200233 -112.077797 -378.843933
190.000000 237.500000 -444.000000
-71.200233 -112.077797 -378.843933
which looks correct. But upon moving my mouse across the grid. the mouse pos only shifts by around 1 or so
Any ideas? I greatly appreciate any help you guys have for me. Thank you
Try using the more generic function to get mouse position. (I forget the name but there's a link on the gui.ScreenToVector wiki page)
You can set your camera position, mouse pos, fov, etc.
util.AimVector
Thank you so much for the help. I'm getting closer. I've since replaced it with this:
local ang = Angle(45, 45, 0 )
local aimVec = util.AimVector(ang, LocalPlayer():GetFOV() , gui.MouseX(), gui.MouseY(), ScrW(), ScrH())
print((aimVec * 1000) - Vector( 449, 450, -200)) --added so easier to compare(vector is my cam pos)
Ive also updated my camera so its more isometric.
I couldn't use LocalPlayer:EyeAngles() so i replaced it with a local angle (same as the calcview angle). Also noted that i might be able to use renderangles or eyeangles. (seems to give same results)
Anyways, I'm getting values much closer than before but it seems my 0,0 position is off. I should be getting a value between 285 and -285 for both x and y axis.
But instead im getting:
bottom left: (-205, 256)
top left: (256, -205)
bottom right: (-143, -143)
top right (133, 133)
Like I said. close! But still slightly off. As I mentioned before My 0,0 pos is off aswell.
Any Ideas?
vector:ToScreen() has problems if you don't do cam.Start3D() cam.End3D() before it. Maybe it's similar to that:
cam.Start3D()
cam.End3D()
local ang = Angle(45, 45, 0 )
local aimVec = util.AimVector(ang, LocalPlayer():GetFOV() , gui.MouseX(), gui.MouseY(), ScrW(), ScrH())
print((aimVec * 1000) - Vector( 449, 450, -200)) --added so easier to compare(vector is my cam pos)
Didnt seem to do anything
Good haha because I just realized I was having the same issue! :P
Its still slightly off. I noticed the further away my tile the large the search area. I just turn the "20s" into a search offset variable and tweaked the number until it only selected 1 tile at the furthest point. However if someone has a solution for that i would be very happy to hear it as i may have to scale up my grid in the future.
This is how i fixed the above:
function ENT:GetMouseTile()
cam.Start3D()
cam.End3D()
local tilePos = self:GetPos()
local posF = tilePos + (Vector(0, 25, 0))
local toScreenPosF = posF:ToScreen()
local posB = tilePos - (Vector(0, 25, 0))
local toScreenPosB = posB:ToScreen()
local posR = tilePos + (Vector(25, 0, 0))
local toScreenPosR = posR:ToScreen()
local posL = tilePos - (Vector(25, 0, 0))
local toScreenPosL = posL:ToScreen()
if (gui.MouseX() >= (toScreenPosF.x )) and (gui.MouseX() <= (toScreenPosB.x)) then
if (gui.MouseY() >= (toScreenPosR.y )) and (gui.MouseY() <= (toScreenPosL.y)) then
self:SetColor(Color(0,255,0))
return true
else
self:SetColor(Color(255,255,255))
return false
end
else
self:SetColor(Color(255,255,255))
return false
end
end
That works well for entities with rectangular prism collision bounds, like cubes or boxes. Nice snippet.
Sorry, you need to Log In to post a reply to this thread.