Get screen bounds of an actor

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Get screen bounds of an actor

Post by argv »

I want to draw a rectangle around an actor in ZScript. To do this, I need to know where that actor is on the screen, and its width and height on the screen. Is there a way to find out?
User avatar
Pixel Eater
 
 
Posts: 667
Joined: Wed Aug 02, 2017 12:31 am
Location: In between the Moon and you, between the buried and me.

Re: Get screen bounds of an actor

Post by Pixel Eater »

Apeirogon is a champ at this. They've done exactly that for Fill Spectre (it can be enabled in the menu).
There's also Nash's cube version which could be useful: RadiusDebug.

How it's done is over my head but looking through the code might be helpful :geek:
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: Get screen bounds of an actor

Post by argv »

Those don't actually compute actor bounds at all. Fill Spectre's shader scans the whole screen using chroma key to figure out which pixels contain a spectre. RadiusDebug just places an actor with a cube-shaped model around the target actor. That doesn't solve my problem…
User avatar
Pixel Eater
 
 
Posts: 667
Joined: Wed Aug 02, 2017 12:31 am
Location: In between the Moon and you, between the buried and me.

Re: Get screen bounds of an actor

Post by Pixel Eater »

The last page I linked to has a newer method we've been testing that almost does away with the chroma keying. The chroma is only used to determine what's the foreground/background. The rest is handled by Apeirogon's bounding box code which calculates the on screen vertices of your chosen actor. Here I've enabled the outline from the menu:
Last edited by Pixel Eater on Mon Aug 13, 2018 4:43 pm, edited 1 time in total.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Get screen bounds of an actor

Post by Nash »

I think you want world -> screen projection? Gutamatics helps you do this, but there's currently no documentation.

There's this example that's floating on Discord, should help you figure out how to use the projection API

Code: Select all

class PRJ_ProjectionTestHandler : StaticEventHandler {
    override void renderOverlay(RenderEvent e) {
        if (automapactive) return;
        
        PRJ_Matrix worldToClip = PRJ_Matrix.worldToClip(e.viewPos, e.viewAngle, e.viewPitch, e.viewRoll, players[consoleplayer].FOV);
        
        Actor projActor;
        ThinkerIterator iter = ThinkerIterator.create("Actor", Thinker.STAT_DEFAULT);
        while ((projActor = Actor(iter.next())) != NULL) {
            Vector3 adjustedWorldPos = e.viewPos + LevelLocals.vec3Diff(e.viewPos, projActor.pos);
            Vector3 ndcPos = worldToClip.multiplyVector3(adjustedWorldPos + (0, 0, projActor.height / 2.0)).asVector3();
            if (ndcPos.z <= 1 && ndcPos.z >= -1) {
                // this is the coordinates where you're drawing to:
                Vector2 drawPos = PRJ_GlobalMaths.ndcToViewPort(ndcPos);
                Screen.drawText(smallfont, Font.CR_White, drawPos.x - (smallfont.stringWidth("+") * cleanXFac / 2.0), drawPos.y - (smallfont.getHeight() * cleanYFac / 2.0), "+", DTA_CleanNoMove, true);
            }
        }
    }
}
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: Get screen bounds of an actor

Post by Apeirogon »

Nash wrote:There's this example that's floating on Discord
Marvelous...I spent three days in mathcad, gzdoom console.printf and other stuff, analyzing what is responsible for what, to understand how it work, when there are already ready example how it works...
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Get screen bounds of an actor

Post by Gutawer »

[imgur]https://i.imgur.com/AClSXri[/imgur]
I assume you mean like this. I've done this before, using Gutamatics:
https://gist.github.com/Gutawer/389966d ... 687cdbf52c

This will draw this box on all actors, which will look messy with corner textures, so you'll obviously want to filter out most actors using a hitscan or something to determine what the player's looking at. The corner textures use offsets to be drawn in the correct positions, and are offset by a triangle wave, since this was modelled after how Deus Ex' version of this looks which has fluctuating corners. You can remove that.

Replace #### with your chosen prefix to get this working, and you'll need to provide your own corner textures.
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: Get screen bounds of an actor

Post by argv »

Neat! But I already got it working in my mod, which draws basically that but for items that can be picked up.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Get screen bounds of an actor

Post by Gutawer »

Heh, fair enough, forgot to look at when this thread was made!
User avatar
ramon.dexter
Posts: 1529
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: Get screen bounds of an actor

Post by ramon.dexter »

argv wrote:Neat! But I already got it working in my mod, which draws basically that but for items that can be picked up.
This should really go into the scripting resources as a major resource, cause it's perfect :wub:
argv
Posts: 184
Joined: Tue Aug 30, 2016 4:47 pm

Re: Get screen bounds of an actor

Post by argv »

Gutawer wrote:[imgur]https://i.imgur.com/AClSXri[/imgur]
I assume you mean like this. I've done this before, using Gutamatics:
https://gist.github.com/Gutawer/389966d ... 687cdbf52c

This will draw this box on all actors, which will look messy with corner textures, so you'll obviously want to filter out most actors using a hitscan or something to determine what the player's looking at. The corner textures use offsets to be drawn in the correct positions, and are offset by a triangle wave, since this was modelled after how Deus Ex' version of this looks which has fluctuating corners. You can remove that.

Replace #### with your chosen prefix to get this working, and you'll need to provide your own corner textures.
Although I have it working in my mod, I notice that your version looks like it accounts for the actor's entire 3D volume, rather than assuming that it's a flat sprite (with no billboarding on the pitch axis) like my implementation. This is obviously important if the actor in question is represented by voxels or a model, or if sprites are billboarded on both axes. I'll need to give your code another look. I've posted a GitHub issue to that effect.

By the way, you only need one corner texture, not four. DTA_FlipX and DTA_FlipY can be used to rotate them when drawing the other three corners.

Also, the texture name doesn't have to be in eight-letter form. The engine also accepts full paths like “UseToPickup/bracket.png” as a texture name. I use that to avoid namespace collisions with other mods—“UseToPickup” isn't normally considered a texture folder, so textures in that folder can only be found by their full path, and are therefore very unlikely to collide with anything.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Get screen bounds of an actor

Post by Gutawer »

argv wrote:By the way, you only need one corner texture, not four. DTA_FlipX and DTA_FlipY can be used to rotate them when drawing the other three corners.

Also, the texture name doesn't have to be in eight-letter form. The engine also accepts full paths like “UseToPickup/bracket.png” as a texture name. I use that to avoid namespace collisions with other mods—“UseToPickup” isn't normally considered a texture folder, so textures in that folder can only be found by their full path, and are therefore very unlikely to collide with anything.
Good points, but keep in mind that I made this quickly just to test that Gutamatics could do it, so not a whole lot of effort was put into that sort of thinking. :P

By the way, that's a pretty nice mod. You've nailed the presentation, with all the subtle fades and transitions. :thumb:
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49073
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Get screen bounds of an actor

Post by Graf Zahl »

I know people won't like what I'm saying now, but doing any unprojection from the 3D view to the 2D screen on the script side is a dangerous thing.
Just imagine the HUD code gets rewritten to draw on the upscaled result from the renderer, e.g. do draw the menu in higher resolution than the rest - which is actually seriously being considered. This all would completely fall apart in such a case because the 3D view's extent no longer matches the 2D view.
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: Get screen bounds of an actor

Post by Gutawer »

I understand what you mean, but to be honest, people clearly want to be able to do this (as this thread evidences, plus a large number of times from Discord) - there was a feature suggestion to do this here viewtopic.php?f=54&t=56168 but it was put on hold, so it didn't look like any progress was being made there, hence why I added this functionality to my maths library. I suppose it might be worth considering doing this now as an engine feature, because otherwise, unless there's an in-engine solution, people are going to continue doing this on the scripting side.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: Get screen bounds of an actor

Post by Nash »

I'll stop using scripted solution when engine natively supports projection and deprojection. Otherwise, this is all I've got...
Post Reply

Return to “Scripting”