Positioning 3D2D Cam

Recently I’ve gotten into that part of Lua I’ve rarely been to - which is SWEPs.
After few days of playing with funcs, hooks and so on I got to the point where everything looks nice and works great… however, it lacks a proper ammo display - which I decided to use 3D2D Cam.

So here is my issue.
I am trying to position the rectangle so it appears right where the muzzleflash appears, I had no issues applying effect and it shows up at the correct position, but somehow the rectangle appears few inches to the back.
Is it caused by FOV not taken under consideration (no proper calculations were included) or do I have to do some calculations related to Cam3D2D’s draw scale?

Here is the image of what I mean (muzzleflash appears at the end of barrel, while the rectangle shows up way behind it):

function SWEP:PostDrawViewModel( vModel, person, weapon )
	local pos = vModel:GetAttachment( 1 ).Pos
	local ang = self.Owner:EyeAngles()

	cam.Start3D2D( pos, Angle( ang.Pitch, ang.Yaw, ang.Roll ), 0.1 ) -- Angle( p, y, r ) instead of just ang so I can increase one of these if needed
		cam.IgnoreZ( true )
		draw.RoundedBox( 0, 0, 0, 200, 250, Color( 255, 0, 0, 25 ) )
		draw.SimpleTextOutlined( "AMMO", "DermaDefaultBold", 0, 0, Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )

I rarely ask for help, so please, be nice, and thanks in advance!

Use Angle:RotateAroundAxis.

I did it before, but it was still in the middle of the barrel.
Let me do it again

For this I strongly recommend you just get the Attachment.Pos and ToScreen it, then draw text at those coordinates. It’s much simpler and achieves a similar effectt.

Similar effect, but I want it to be affected by angles aswell which ToScreen() won’t let me.

Also I rotated it around axis, the result: it’s more to the back than before


It looks exactly where it is supposed to be judging from your previous pic. Now use -Angle:Forward() * somenumber to move it back.

Push it forward and to the left?

Yes, I know how to position it the tricky way, but shouldn’t GetAttachment( 1 ).Pos return the proper position, I didn’t have to push it forward when applying util.Effect() to it.
Also, wouldn’t LocalToWorld() be nicier than pushing through the angles?

[del]It seems Max did some weird shit:[/del]

Those are NAMES, not IDs. It seems that “2” has ID 1 and “1” has ID 2. Try it. I may be wrong.

I want this tool Robotboy… now…

Also, I tried the same attachment I used for muzzleflash but fine I’ll try ID 2.

Err… even more to the back.


Disregard that, the ids are correct, no idea why it attaches to second attachment.

[editline]18th May 2014[/editline]

I had to upgrade the tool for this case btw :smiley:

I really think it has something to do with FOV and draw scale.

@Edit: Nevermind, draw scale has nothing to do with it, I tried changing it from 0.1 to 0.5 and the left upper corner was still in the same position.

That makes me want to have it even more… I could make one but I am way too lazy for that since I’ve got a full-time job.
So it’s good to see someone still making good tools that I and other devs can use.

Funny enough, my tool shows CORRECT positions even though it uses same functions :v It even works properly when moving the view model around.

I am pretty sure that’s just the hook that is malfunctioning.

What other hook do you suggest?

You could try PostDrawTranslucentRenderables or HUDPaint, that will draw the screen above everything though.

Remove all vector and angle offsets and try and rework it from there?

The hook fucks up positions returned by those functions.

I actually do believe it might be something to do with bonemerging.

Yes, the hook messes with the positions, here is the output of print() using 2 different hooks:

print( "GetViewModelPosition: " .. tostring( self.Weapon:GetAttachment( 1 ).Pos ) )

PostDrawViewModel: 1522.029541 3460.080811 163.484558

print( "PostDrawViewModel: " .. tostring( vModel:GetAttachment( 1 ).Pos ) )

GetViewModelPosition: 1508.282471 3460.793213 112.514145

As you can see, Z aswell as Y is always way off.