• Print

Author Topic: Entity height/width in view angle?  (Read 17255 times)

0 Members and 1 Guest are viewing this topic.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Entity height/width in view angle?
« on: July 13, 2006, 06:33:44 am »
Using Lua, how would one go about getting the height and width, in standard _GmodRect_SetPos (,,h,w) format, accounting for various screen resolutions, of a Entity in the HUD?
(if you know of a mod/script/function that has been released, and can provide link to it, please do!)

Gmod Rect uses 0 to 1 for its X/Y co-ordinates, so
Rect_Set(.5,.5 would place the start of the rectangle in the center of the screen, and (.5,.5, .5,.5) where the last two are height/width of rectangle, would make the rectangle fill lower-right quadrant of the users screen.

However, how would I adjust the height/width dynamically depending on how close I am to an ent? Things farther away would have much smaller height/width perspective than closer of course.

------------------------------------------------------
|           |2|                                                          |
|                                                                         |
|                                                                         |
|                                                                         |
|                                                                         |
|                                               w                        |
|                                   -|-     |    |                      |
|                                            |  1 | h                   |
|                                            |___|                     |
|                                                                         |
|                                                                         |
|                                                                         |
|                                                                         |
------------------------------------------------------

Say 1 and 2 in my 'hud' above are trashcans/barrels,whatever.
I know I can use EntGetPos to grab vectors, but how would I get the h/w they're using of my screen?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Entity height/width in view angle?
« Reply #1 on: July 13, 2006, 07:12:40 am »
I think that _GModRect_SetEntity and _GModRect_SetEntityOffset are what you're looking for.

Under notes for setentity: "Actually, the rect doesn't technically "hover" over the entity in the game. The command tells the clients its sent to to constantly setpos the floating rect at the position of the entity on the user's screen, or hide it if the entity is not on the user's screen. ".

So I'm not sure if this will make the rect resize automagically based off distance or not, but if it doesn't, you can easily do it yourself.

Let me know how it works out, I was thinking of doing something similar. :P
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #2 on: July 13, 2006, 10:58:54 am »
Megiddo,
 The code I have uses both of those. Unfortunately, the only 'dynamic' they do is attach to the entity, and offset the starting point for the rectangle of entity.
Here's what I'm using (_very_ similiar to my predator vision post, posted this way as to ask a direct question). Currently, this code tracks npc and entities. but doesn't adjust the h/w dynamically.
Code: Lua
  1. --predid, radius and centervec are the playerid, 9999 and _PlayerGetShootPos, respectively
  2. function findtargets(predid, centervec, radius)
  3.     targets = _EntitiesFindInSphere( centervec, radius )
  4.         for i = 1,table.getn(targets) do
  5.             if (string.find (_EntGetType(targets[i]),"npc_",1,true) ~= nil) or
  6.                 (_EntGetType(targets[i]) == "player")
  7.                 then
  8.                 _GModRect_Start("gmod9/white")
  9.                 _GModRect_SetPos(0,0,.04,.02)
  10.                 _GModRect_SetColor(255,20,50,200)
  11.                 _GModRect_SetTime(9999,0,0)
  12.                 _GModRect_SetEntity( targets[i] )
  13.                 _GModRect_SetEntityOffset(vector3(-5,0,34))
  14.                 _GModRect_Send(predid,888 + i + predid)
  15.             end
  16.         end
  17. end
  18.  
SetEntity places the starting point of the rectangle smack dab in the center.
In this case, imagine the start point being the npc/players belly button when facing them.
SetEntityOffset moves that point. The numbers I have there are pretty close to top of the head/left of the ear when facing them.
(I have only had time to test about 2 hours so far, so just realized I hadn't yet tried non humanoids (birds/antlions/etc))

However. I can find no (easy premade function) way to have the height and width adjust.
 There might be one. Unfortunately, my LUA experience has just been what I've learned while converting, and greatly enhancing, a mod for/with a friend, so I don't know if there is a way. I figure there is from some LUA mods I've heard of, but can't seem to find the ones that do it.

EDIT by Megiddo: Instead of just <code>, use <code=lua>, where < is actually "[".
« Last Edit: July 13, 2006, 05:37:13 pm by Megiddo »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: Entity height/width in view angle?
« Reply #3 on: July 13, 2006, 11:48:29 am »
This seems your best bet, http://forums.facepunchstudios.com/showthread.php?t=80322&highlight=Radar , but it still not quite what you want.


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #4 on: July 13, 2006, 03:49:10 pm »
Thanks for trying Golden-Death. I just looked at that radar script. The only rectangle width/height adjusting it does is variant on user-adjustable values, and whether they zoom in or not.
Dynamic depending on what user sets the variables to, but not dynamic depending on player location.
Though the code updating the 'position' of the _GmodText characters on the radar 'rectangle' is nice, and includes more math than I fully understand, its calculated on x/y vector positions of where  the player is.
Basically, I think all the script does is convert the sphere around the player into an area that will be between 0 and 1 for X and Y, and then places text on the rectangle overlay at those X/Y co-ordinates.

I have to figure out a way to determine distance from player to Ent. (figure this is (reasonably) easy, vector-wise)
then figure out height/width of Ent on screen related to distance in 0 to 1 precision (That, I think is going to be painful, if not impossible for me)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Entity height/width in view angle?
« Reply #5 on: July 13, 2006, 05:34:10 pm »
Code: Lua
  1. if timeid then
  2.         HaltTimer( timeid )
  3. end
  4.  
  5. npcid = 97
  6. userid = 1
  7. width = 10
  8. height = 50
  9. co = 1
  10.  
  11. function updateRect()
  12.         local dist = vecLength( vecSub( _EntGetPos( userid ), _EntGetPos( npcid ) ) )
  13.  
  14.         _GModRect_Start( "gmod/white" )
  15.         _GModRect_SetPos( 0, 0, (width/dist)*co, (height/dist)*co )
  16.         _GModRect_SetColor( 255, 20, 25, 200 )
  17.         _GModRect_SetTime( 9999, 0, 0 )
  18.         _GModRect_SetEntity( npcid )
  19.         _GModRect_SetEntityOffset( vector3( -50, 0, 34 ) )
  20.         _GModRect_Send( userid, 888 )
  21. end
  22. timeid = AddTimer( 1/30, 0, updateRect )

That takes care of your immediate problem, but I'm sure you'll notice a few more problems cropped up :P . I'd help you out further, but I have to go. I'm off work tomorrow though, so I'll see what I can do  ;) .
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #6 on: July 13, 2006, 07:00:26 pm »
Megiddo,
Awesome. 'Playing' around with the code now.
Just from looking, I can see that timer could be a problem.
Perhaps an 'onThink' would work better?

I too am off tomorrow, though, I'll probably be up late (I'm 3 hours ahead of you), so will prob hit bed around midnight your time.
Late riser too of course on days I don't work.

Oh, another possibility that I was playing around with. _GmodQuads ... uses a 4 point quad vertices.
I wasn't making much progress with it, but perhaps it would be another solution.

I thought about applying effects to the npcs/players for about 2 seconds, then realized I don't want others who don't have the 'vision' to see the effects.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Entity height/width in view angle?
« Reply #7 on: July 13, 2006, 07:35:28 pm »
I used a timer so I could reload the script easily :P. As far as quads, it's doable, but you'd have to imbed the transparency in the material it seems. The biggest problem is that quads are visible through walls.
Experiencing God's grace one day at a time.

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: Entity height/width in view angle?
« Reply #8 on: July 13, 2006, 07:53:27 pm »
I used a timer so I could reload the script easily :P. As far as quads, it's doable, but you'd have to imbed the transparency in the material it seems. The biggest problem is that quads are visible through walls.

That may work, seeing as it'd be like 'Heat vision', finding heat sources.


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #9 on: July 13, 2006, 08:06:08 pm »
Quads/Rects and GmodText are all visibile through walls.
I realised this was an issue when I first tried the text.
However, considering I'm working on 'predator' vision, it doesn't bother me that much.

Right now, i'm having a problem with getting the rectangles centered.
Much better progress than I was though, thanks to Megiddo.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #10 on: July 13, 2006, 09:20:14 pm »
Figured out that GmodRect_SetEntOffset wasn't working correctly for adjusting left or right, at least in the view screen.
Just trying various stuff, I found that the following works best for centering.
I originally thought in error that SetPos 0,0,w,h needed to be zeros. I was wrong.
Starting from the 'belly button'/center of the entity, use the same formula, but divide by 2 (half the width of ent), and make it negative (so it places itself to the left in your view)
Offset still seems to adjust its top starting point, but I will play around with adjusting the y-coord too and see how well that works.

Code: Lua
  1.  
  2. _GModRect_Start( "gmod/white" )
  3. _GModRect_SetPos( -((width/dist)*co)/2, 0, (width/dist)*co, (height/dist)*co )
  4. _GModRect_SetColor( 255, 20, 25, 200 )
  5. _GModRect_SetTime( 9999, 0, 0 )
  6. _GModRect_SetEntity( targs[i] )
  7. _GModRect_SetEntityOffset( vector3(0,0,35))
  8. _GModRect_Send( predid, 888 + i + predid )
  9.  
(sorry, been playing around with it in my script, variable names have been changed) :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #11 on: July 13, 2006, 10:02:28 pm »
This centers wonderfully for humanoids. Remove/comment out the GmodRect_SetEntOffset

width = 15
height = 60
co=1
_GModRect_SetPos( -((width/dist)*co)/2, -((height/dist)*co)/2, (width/dist)*co, (height/dist)*co )

No idea what I'm going to do for non-humanoids. I'd hate to add if/then statements to adjust for w/h.
Too bad theres no _Gmod_EntGetScreenHWCoords that returns (Height,Width)
« Last Edit: July 13, 2006, 10:09:32 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #12 on: July 14, 2006, 08:50:38 pm »
This is what I'm using at the moment.
Works fine until theres about 6 npcs or players moving about.
I didn't expect miracles, knew it would be slow, but I'm hoping one of you assist me in tweaking it.

Code: [Select]
function findtargets(predid)
 if (checkpredator(predid)) then
   local startvector = _PlayerGetShootPos(predid)
    targs = _EntitiesFindInSphere(startvector, radius )
for i = 1,table.getn(targs) do
    if (string.find (_EntGetType(targs[i]),"npc_",1,true) ~= nil) or
       (_EntGetType(targs[i]) == "player") then
              if (predid ~= targs[i]) then
               local dist = vecLength( vecSub( startvector, _EntGetPos( targs[i] ) ) )
               local Xco = (width/dist)*co
               local Yco = (height/dist)*co
               _GModRect_Start( targetmaterial )
               _GModRect_SetPos( -(Xco/2), -(Yco/2), Xco, Yco )
               _GModRect_SetColor( 200, 0, 150, matalpha )
               _GModRect_SetTime( .5, 0, 0 )
               _GModRect_SetEntity( targs[i] )
               _GModRect_Send( predid, predid + (2*i)  )
           end -- if targ is self
         end -- if entype is npc or player
end -- for loop sphere table
  end -- if connected
 end  --function
Never could get decent 'onThink' results, so, Im using the timer megiddo originally used.
I just rewrote it so that more than one player can use it. PredUser[predid].visiontimer = AddTimer blah

Tweak Idea, that I don't know how to impliment.
Tips and Advice appreciated.
    targs = _EntitiesFindInSphere(startvector, radius )
That looks for all entities in a 360 sphere around me the player (startvector = playergetshootpos)
Which, for rectangles, is useless. They can only be drawn in the FOV.
I believe if I could get what is in my current view it would help speed things up. (I could be wrong)
From the wiki, theres a _Util.EntsInBox(vert1,vert2)
http://gmwiki.garry.tv/index.php/Util.EntsInBox
 Anyone know if that would be quicker.
I presume we could use PlayerGetShootAng to set vert2, PlayerGetShootPos to set vert1, but, how do we add in extra height/distance. I understand vecAdd could be used, but, if Im facing any direction, is -1,0,0 behind, +1, 0,0 in front? I somewhat grasp the 3d points, I'm just trepid about them.
 And how do I tell how many points there are?

Thanks!
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: Entity height/width in view angle?
« Reply #13 on: July 14, 2006, 09:12:43 pm »
What script am I suppsoed to use that with?


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #14 on: July 14, 2006, 10:46:06 pm »
Its just a function I'm working on. Lots more to it, didn't want to clutter up this forum any more than I already had, but if Megiddo doesn't mind, I could place it here. Its ~100 lines counting a few credit comments, and the materials I'm still testing as 'target' rectangles.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print