• Get HitPos of Mouse in World Coordinates in Renderview
    14 replies, posted
Basically, I want to get where the mouse is hitting in world coordinates (x,y,z) I tried alot of things but to no avail. I'm also using renderview, might make a difference.
RenderView doesn't create an alternative world with alternate coordinates, just make a https://wiki.garrysmod.com/page/util/TraceLine starting on the camera and have it ignore entities except for the world.
ubre, is it okay if you accept my friend request on steam? I would really like your help.
I'm at work, can't help much. After looking at util.AimVector I don't think it's going to be useful in this, sorry. I'll try to think of some way to translate the mouse position over to the 3D world.
gui.ScreenToVector Looking at the examples, I believe this is what you are looking for.
This return the direction, this will not help me, I have tried tracing a line with this as help, but it hasn't worked.
You tried this example and it still didn’t work? If not with this particular example, you should at the very least be able to use the direction vector to get a trace going (or use math to get a rough estimate or something perhaps). local tr = util.QuickTrace( LocalPlayer():GetShootPos(), gui.ScreenToVector( gui.MousePos() ), LocalPlayer() ) print( tr.HitPos )
It's a renderview, I'm not trying to get the trace of where the player is looking in-game, I'm trying to get a trace of where the player's cursor is looking in a renderview https://files.facepunch.com/forum/upload/412718/a901d0a4-2ab8-4d52-b63b-c86586b7fd46/008a1cf80a871510275f0464817e2f1f.mp4
I love the passive aggressive mouse wobbling in the video. I think ButterKing5000 is onto something, we could try having the position of the camera being the source of the trace. local tr = util.QuickTrace(camPos, gui.ScreenToVector(gui.MousePos()), ents.GetAll()) print(tr.HitPos) Where campos is the position of the camera. Try playing around with it and the zoom, same with ScreenToVector because I'm sure it's better to have the coordinates local to the panel it's being rendered on instead of the screen.
The campos I'm using is the campos you provided as the origin being: Vector(renderPosX, renderPosY, 0) However, once I do this, it never reaches any endpos and instead ends where it started local dir = gui.ScreenToVector( gui.MousePos() ) local trace = util.TraceLine{ start = Vector(renderPosX, renderPosY, 0), endpos = (Vector(renderPosX, renderPosX, 0) + dir * 10000), filter = { }, }
Could be that the position is outside of the map, try lowering it. I just put 0 as the Z because I didn't see a problem with it before.
Made the Z axis extremely high and extremely low, and it seems to work just a little bit, but it still hasn't fixed the X and Y problem https://files.facepunch.com/forum/upload/412718/15e19076-b4dc-4a5c-9d0f-e126bc87c452/f1841d610003db1125ada6419e2b4b7c.mp4
local _, worldBounds = game.GetWorld():GetModelBounds() local worldHeight = worldBounds.Z - 1000 function frame:Paint(w, h) //This function is updated every frame local posX, posY = self:GetPos() renderPosX, renderPosY = renderPosX + velX*zoomLevel, renderPosY + velY*zoomLevel //The more zoomed in we are the slower we'll move drawAllEntities(false) //Before running the renderview we stop all entities from being drawn hook.Add("PostDrawTranslucentRenderables", "DrawThePoint", function() //Let's draw a 3D sphere on the place we want it to appear local dir = util.AimVector(Angle(90, 0, 0), LocalPlayer():GetFOV(), mousePosX + renderPosX, mousePosY + renderPosY, w, h) local dir = Vector(dir.X*zoomLevel*math.pi, dir.Y*zoomLevel*math.pi, dir.Z) local tr = util.QuickTrace(Vector(renderPosX, renderPosX, worldHeight), Vector(renderPosX, renderPosX, worldHeight) + dir*worldHeight*10, ents.GetAll()) render.SetColorMaterial() render.DrawSphere(tr.HitPos, zoomLevel*500, 30, 30, Color(255, 0, 0, 125 + math.cos(CurTime()*10)*125)) //The size of the sphere depends on how zoomed in you are, also I make it glow by changing its alpha with a simple cosine function end) render.RenderView({ origin = Vector(renderPosX, renderPosX, worldHeight), //Set the position to the variables set earlier angles = Angle(90, 0, 0), x = posX, //This is the position on the screen, so we set it to follow the panel y = posY, //Same w = w, //Size of the panel h = h, //Same drawviewmodel = false, offcenter = { //This is what we use to ZOOM, what you used before was just moving the Z pos, with this you won't clip into anything and will actually zoom like in real life or the camera tool left = -w*zoomLevel + w/2 + renderPosX, right = w*zoomLevel + w/2 + renderPosX, top = -h*zoomLevel + h/2 + renderPosY, bottom = h*zoomLevel + h/2 + renderPosY //I would like to explain this part but I really just kept writing shit until I found what worked, sorry :-) } }) hook.Remove("PostDrawTranslucentRenderables", "DrawThePoint") //Let's remove the sphere before anybody notices anything drawAllEntities(true) //Now let's make sure it's back to normal, this all happens in less than a frame being rendered on your actual game so you see no changes end This is a modified part of the code I gave you before, I added the height of the world (minus 1000 units just to make sure) as the origin of the camera so it's always on top of everything else. The rest is easy to understand (I hope) I don't know why math.pi was the magical number that worked to multiply, I'm kinda learning while doing this as well. Ask if you need help with the code.
I fixed it: local dir = Vector((dir.X*zoomLevel*math.pi) / 1.3, (dir.Y*zoomLevel*math.pi) / 1.3, dir.Z) Just needed to divide the values a bit, thank you.
I code for fun, I don't take in pupils because I'm not an expert. Also I'm trying to fix all problems, I'll respond when I fix the stuff.
Sorry, you need to Log In to post a reply to this thread.