Since my last thread derailed into something other then the title i decided to move it to a new thread with a more accurate title.
Old thread
[URL]https://facepunch.com/showthread.php?t=1502491[/URL]
basically what i need is
[t]http://i.imgur.com/HDaznPb.png[/t]
I am having a hard time converting the screen pos to actual world position
[URL]https://www.facepunch.com/threads/891702-Mouse-position-to-world-vector-from-a-custom-origin[/URL]
^ this thread almost got me where i needed to go but cant seem to get it to work correctly.
So moving the mouse does not currently change the player's aim? Or what's the problem here? Last thread you were asking for aim assist and now you have no real question.
[url]http://wiki.garrysmod.com/page/gui/ScreenToVector[/url] ?
[QUOTE=kulcris;49591028]almost got me where i needed to go but cant seem to get it to work correctly.[/QUOTE]
This is never helpful.
The question is
How do i get the world position of where the mouse is pointing from a top down view?
ScreenToVector returns a number that is < 1 no matter where i point it so im not sure how that will help me, i have tried tracing a line from where the camera is to where the mouse is pointing but my issue is I CAN NOT FIND WHERE THE MOUSE IS ACTUALLY POINTING, instead i get some number that MIGHT help me if i was looking at it from the view of the player, instead i am looking from top down
The wiki page I linked has a goddamn example. Is it that hard to open it and use your eyes for just this once?
UGH
I have tried that example in 100 different ways for the past 2 days.
[QUOTE=kulcris;49591674]no need to be rude, i have tried to use that example multiple times in multiple ways i can not get it to work topdown[/QUOTE]
People are giving you what you need, I gave you code for exactly what you asked for in the previous thread, so if it's not what you wanted it's your fault for explaining it badly. Maybe you just don't understand what you're doing.
[QUOTE=HappyGhetto;49591680]People are giving you what you need, I gave you code for exactly what you asked for in the previous thread, so if it's not what you wanted it's your fault for explaining it badly. Maybe you just don't understand what you're doing.[/QUOTE]
if i understood what i was doing then i wouldnt ask for help doing it.
[QUOTE=kulcris;49591691]if i understood what i was doing then i wouldnt ask for help doing it.[/QUOTE]
I mean people are giving you the tools and you don't know what to do with them.
I have tried each and every way suggested and it either just diesnt work or doesnt work the way intended, your code worked in the other thread i just had no way of finding the enemy i was aiming at when i wasnt actually aiming at them. What robbi suggested in the other thread is exactly what i am asking for but i cant get screen to vector to work correctly from 500 units above the player
[editline]23rd January 2016[/editline]
Looking back at screentovector i may have been trying to use it wrong, i will try again when i get home
[code]function GM:CreateMove( cmd )
local ang, v, w, x, y
ang = cmd:GetViewAngles( )
x, y = gui.MousePos( )
v, w = ScrW( ) * .5, ScrH( ) * .5
ang.y = math.deg( math.atan2( v - x, w - y ) )
cmd:SetViewAngles( ang )
end[/code]
Can you comment that a bit for example, why multiply scrw and h by .5
[QUOTE=kulcris;49592581]Can you comment that a bit for example, why multiply scrw and h by .5[/QUOTE]
Because the player is right in the middle of the screen.
Is it possible to get the z vector (up and down) from a top down view?
[QUOTE=Kogitsune;49592248][code]function GM:CreateMove( cmd )
local ang, v, w, x, y
ang = cmd:GetViewAngles( )
x, y = gui.MousePos( )
v, w = ScrW( ) * .5, ScrH( ) * .5
ang.y = math.deg( math.atan2( v - x, w - y ) )
cmd:SetViewAngles( ang )
end[/code][/QUOTE]
this kinda works.. it just aims at the screen if you go to far in one direction and doesnt actually use the mouse position
[editline]23rd January 2016[/editline]
[QUOTE=soliv;49593207]Is it possible to get the z vector (up and down) from a top down view?[/QUOTE]
This is what i need, to aim at the up down position of the location my mouse is pointing
[editline]23rd January 2016[/editline]
[lua]
function aimassist(ply)
local tr = util.QuickTrace( LocalPlayer():GetShootPos(), gui.ScreenToVector( gui.MousePos() ), LocalPlayer() )
print( tr.HitPos )
local material, white = Material( "sprites/splodesprite" ), Color( 255, 255, 255, 255 )
hook.Add( "HUDPaint", "paintsprites", function()
cam.Start3D()
render.SetMaterial( material )
render.DrawSprite( tr.HitPos, 16, 16, white )
cam.End3D()
end )
end
[/lua]
This draws the sprite right on my players head
[video=youtube;7COQvj-eDL0]https://www.youtube.com/watch?v=7COQvj-eDL0[/video]
I also have this companion code to allow the player to pan the camera away from their body:
[code]function GM:CalcView( pl, origin, angles, fov )
local view, item, tr
view = { }
tr = { }
tr.start = origin
self.CurrentDistance = math.Approach( self.CurrentDistance, self.TargetDistance, FrameTime( ) * Settings.MaximumDistance )
self.FloatingPosition = self.FloatingPosition or Vector( 0, 0, 0 )
view.origin = origin + Vector( 0, 0, Settings.MaximumDistance )
tr.endpos = view.origin
tr.filter = pl
tr.mask = MASK_SHOT
tr = util.TraceLine( tr )
view.origin = tr.HitPos
view.angles = Angle( 90, 0, 0 )
view.fov = fov
item = pl:GetActiveWeapon( ).CurrentItem
if not item and not DEBUG_MODE then
return view
end
if ( item and ( item.Kind & ITEM_RANGED == ITEM_RANGED ) ) or DEBUG_MODE then
local where, dir
where = pl:GetPos( ) + Vector( 0, 0, 256 )
tr = util.TraceLine{ start = where, endpos = where + gui.ScreenToVector( gui.MousePos( ) ) * 256, filter = pl, mask = MASK_SHOT }
tr.HitPos.z = where.z
view.origin = tr.HitPos
end
return view
end[/code]
The logic is that atan2 returns the direction between 2D points. We localize the mouse cursor to the center of the screen so that it is as though we are working from <0,0>, and then get the angle between the point and the "origin".
Things worth noting:
I wrote this code 7 years ago (!)
It absolutely requires the cursor to be visible
All the code does is calculate the angle and make the player look in that angle - adjusting their pitch is another kettle of fish that I never tried to tackle. You may need to move to another hook, though - a lot has changed in the game since that was wrote.
[QUOTE=Kogitsune;49593530][video=youtube;7COQvj-eDL0]https://www.youtube.com/watch?v=7COQvj-eDL0[/video]
I also have this companion code to allow the player to pan the camera away from their body:
[code]function GM:CalcView( pl, origin, angles, fov )
local view, item, tr
view = { }
tr = { }
tr.start = origin
self.CurrentDistance = math.Approach( self.CurrentDistance, self.TargetDistance, FrameTime( ) * Settings.MaximumDistance )
self.FloatingPosition = self.FloatingPosition or Vector( 0, 0, 0 )
view.origin = origin + Vector( 0, 0, Settings.MaximumDistance )
tr.endpos = view.origin
tr.filter = pl
tr.mask = MASK_SHOT
tr = util.TraceLine( tr )
view.origin = tr.HitPos
view.angles = Angle( 90, 0, 0 )
view.fov = fov
item = pl:GetActiveWeapon( ).CurrentItem
if not item and not DEBUG_MODE then
return view
end
if ( item and ( item.Kind & ITEM_RANGED == ITEM_RANGED ) ) or DEBUG_MODE then
local where, dir
where = pl:GetPos( ) + Vector( 0, 0, 256 )
tr = util.TraceLine{ start = where, endpos = where + gui.ScreenToVector( gui.MousePos( ) ) * 256, filter = pl, mask = MASK_SHOT }
tr.HitPos.z = where.z
view.origin = tr.HitPos
end
return view
end[/code]
The logic is that atan2 returns the direction between 2D points. We localize the mouse cursor to the center of the screen so that it is as though we are working from <0,0>, and then get the angle between the point and the "origin".
Things worth noting:
I wrote this code 7 years ago (!)
It absolutely requires the cursor to be visible
All the code does is calculate the angle and make the player look in that angle - adjusting their pitch is another kettle of fish that I never tried to tackle. You may need to move to another hook, though - a lot has changed in the game since that was wrote.[/QUOTE]
thank you for explaining that to me,
My issue is pitch... i actually already had the first bit of code (didnt realize it till a few moments ago) but getting the pitch to aim at based on map height is what im after :/
[editline]24th January 2016[/editline]
i guess i could also do the close the cursor is to the player the lower they aim starting at straight ahead
[editline]24th January 2016[/editline]
if i could traceline at an angle instead of at a position then i could probably do it, all i would need is the angle between the center of the screen and the mouse...
[video]https://youtu.be/H3TJHqffe2s[/video]
[lua]
function myCreateMove( cmd )
local ang, v, w, x, y
gui.EnableScreenClicker(true )
ang = cmd:GetViewAngles( )
x, y = gui.MousePos( )
v, w = ScrW( ) * .5, ScrH( ) * .5
ang.y = math.deg( math.atan2( v - x, w - y ) )
local mousepos = gui.ScreenToVector(gui.MousePos()).z
local mouselerp = LerpAngle(mousepos, Angle(45,0,0), Angle(90,0,0))
ang.p = math.Clamp(-(math.abs((mouselerp.p *4)) - 90 +40 ), 0, 90)
cmd:SetViewAngles( ang )
end
[/lua]
This is probably a horrible way to do this but it gets the job done.... mostly, (sucks if you have 2 screens bc you are likely to click out of gmod by mistake)
Edit, uploaded higher quality vid
[video=youtube;MaR_FB5h-SA]https://www.youtube.com/watch?v=MaR_FB5h-SA&feature=youtu.be[/video]
I win again gmod.
[lua]
function myCreateMove( cmd )
gui.EnableScreenClicker(true )
local campos = LocalPlayer():GetPos( ) + Vector( 0, 0, 650 )
local tr = util.TraceLine{ start = campos, endpos = campos + gui.ScreenToVector( gui.MousePos( ) ) * 5000, filter = LocalPlayer(), mask = MASK_SHOT }
local aimposbit = (tr.HitPos - LocalPlayer():EyePos()):Angle()
cmd:SetViewAngles( aimposbit )
crosshair = tr.HitPos
end
local material, white = Material( "sprites/splodesprite" ), Color( 255, 255, 255, 255 )
hook.Add( "HUDPaint", "paintsprites", function()
cam.Start3D()
render.SetMaterial( material )
render.DrawSprite( crosshair, 16, 16, white )
cam.End3D()
end)
[/lua]
[editline]26th January 2016[/editline]
thank you Kogitsune
You gave me EXACTLY what i needed XD
[lua]
where = pl:GetPos( ) + Vector( 0, 0, 256 )
tr = util.TraceLine{ start = where, endpos = where + gui.ScreenToVector( gui.MousePos( ) ) * 256, filter = pl, mask = MASK_SHOT }
[/lua]
[editline]26th January 2016[/editline]
[DEL]
[lua]
local function createpanelbackground()
if !IsValid(Frame) then
local Frame = vgui.Create( "Panel" )
Frame:SetPos( 0, 0 )
Frame:SetSize( ScrW(), ScrH() )
--Frame:MakePopup()
Frame:SetMouseInputEnabled(true)
Frame:SetCursor( "blank" )
end
end
hook.Add("HUDPaint", "createpanelbackground", createpanelbackground)
[/lua]
Create a panel and set the cursor to "blank" thus removing the cursor[/DEL]
(does not allow you to actually fire :/ )
[QUOTE=kulcris;49608391]
Create a panel and set the cursor to "blank" thus removing the cursor[/DEL]
(does not allow you to actually fire :/ )[/QUOTE]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/PANEL/OnMousePressed]PANEL/OnMousePressed[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/PANEL/OnMousePressed]PANEL/OnMouseReleased[/url]
[code]panel.OnMousePressed = function( this, code )
if code == MOUSE_LEFT then
RunConsoleCommand( "+attack" )
elseif code == MOUSE_RIGHT then
RunConsoleCommand( "+attack2" )
end
end
panel.OnMouseReleased = function( this, code )
if code == MOUSE_LEFT then
RunConsoleCommand( "-attack" )
elseif code == MOUSE_RIGHT then
RunConsoleCommand( "-attack2" )
end
end[/code]
Something like that, perhaps.
hmm, good idea. i might be able to allow mouse wheeling as well with this :)
i will try this when i get off work today
worked perfectly :)
[lua]
local function createpanelbackground()
if !IsValid(Frame) then
local Frame = vgui.Create( "Panel" )
Frame:SetPos( 0, 0 )
Frame:SetSize( ScrW(), ScrH() )
Frame:SetMouseInputEnabled(true)
Frame:SetCursor( "blank" )
function Frame:OnMousePressed(key)
if key == MOUSE_LEFT then
RunConsoleCommand( "+attack" )
elseif key == MOUSE_RIGHT then
RunConsoleCommand( "+attack2" )
end
end
function Frame:OnMouseReleased(key)
if key == MOUSE_LEFT then
RunConsoleCommand( "-attack" )
elseif key == MOUSE_RIGHT then
RunConsoleCommand( "-attack2" )
end
end
end
end
hook.Add("Initialize", "createpanelbackground", createpanelbackground)[/lua]
Thanks again!
Sorry, you need to Log In to post a reply to this thread.