[IMG]http://puu.sh/n7ZZ[/IMG]
(Sorry for the big and badly drawn image.)
So i have a problem:
I want to draw a dot telling the entitys location.
So when the entity is exactly forward of the player the dot will be in the middle.
But how do i "convert" the position to x value? It must be something with players angles..
This problem has been bugging me for long, I hope i get an answer how to do this.
Thanks!
Do you want to draw a dot on the entity? I don't understand what you're asking.
[QUOTE=brandonj4;35360927]Do you want to draw a dot on the entity? I don't understand what you're asking.[/QUOTE]
I want the dot to be drawn on HUD.
Imagine if the | -------------- DOT ----------- | bar is in the right bottom corner of your hud. And the DOT changes location according to the entity.
Do you want it to still show on the screen even though you arn't looking at it?
This is for drawing it with looking at it, it should work if not just post the errors. :)
[lua]
hook.Add("HUDPaint", "DrawDot", function()
if LocalPlayer():Alive() then
if LocalPlayer():GetPos():Distance(pos) > 250 then --it doesnt show when you are 250 units closer
for k,v in pairs(ents.FindByClass(<EntityFolderName>)) do
local pos = v:GetPos()
local ang = v:GetPos() + Vector( 0,0,5)):ToScreen() -- estimate height of the entity and cut in half on z
draw.RoundedBox(8, pos.x, pos.y, 20,20, Color(255,255,255,255))
end
end
end
end
[/lua]
[IMG]http://puu.sh/n8Eo[/IMG]
Your code will make it draw the dot on the entity.
I want it so the dot is in the middle of the bar (check image) when i look straight at the entity.
If i look little bit to the left of the entity the dot will move little bit to right.
Add me on steam and I gonna try to work this out with you.
[lua]local ang = pl:EyeAngles()
ang.p = 0
local target_vec = ent:GetPos() - pl:GetShootPos()
target_vec.z = 0
target_vec = target_vec:GetNormal()
local pos = ang:Right():Dot(target_vec)[/lua]
If I didn't screw up anywhere, pos should be a value between -1 and 1 which gives the position of the dot on your bar. -1 is completely to the left, 1 is completely to the right and 0 is completely in the middle.
Here's how it works.
[img]http://uppix.net/8/1/b/1affddd0585c1c00217c2bb8ec887.png[/img]
If you ignore the vertical direction, you can split your view angle into two vectors, a Forward vector, which points in the direction you are facing, and a Right vector, which will always point 90 degrees to the right of the direction you are facing. Those vectors are normalized, which simply means that their length is 1. It's always easier to work with normalized vectors because it simplifies calculations quite a lot.
In this example code, I grabbed the EyeAngles of the player and set its pitch to 0, getting rid of the vertical component. In this situation, ang:Forward() will give you the Forward vector, and ang:Right() will give you the Right vector. Here we're going to use the Right vector.
[img]http://uppix.net/9/c/c/1cebc70964022e992c4ef82b1456d.png[/img]
I also calculated the normalized vector which points to the entity you want to show on your bar. I called it target_vec and got it by calculating the position of the entity minus the position of the player and removing the vertical (Z) component. Then I normalized it using GetNormal. All it does is divide a vector by its length, that guarantees that the result will have a length of 1.
[img]http://uppix.net/1/9/2/60a260142e088e5e6b5951a67bc22.png[/img]
Now if you don't know how a dot product works, it's pretty simple, very fast, and really useful in a lot of situations. You can see it that way, if you take two vectors A and B, and then project A onto B, you get a vector C, which is parallel to B. Well the dot product of A and B is how many times C you need to get B. If B is normalized then it's really simple, the dot product of A and B is the length of C. Except it's negative if A is "behind" B, and C turns out to face the opposite way.
If A and B are both normalized then you can see it that way: the dot product of A and B is 0 if A and B are perpendicular, 1 if they are parallel and facing the same way, -1 if they are facing opposite directions, and somewhere between -1 and 1 in every other situation.
[img]http://uppix.net/7/6/4/dcc8c3c2fdb83ee6f1c98a9aa8880.png[/img]
So if you apply that with target_vec and ang:Right(), you get this.
If you are looking directly at the target entity, target_vec and ang:Right() are perpendicular, therefore you get 0.
The more you look to the right, the more ang:Right() will go "behind" target_vec, and you will get a result that goes towards -1.
And if you look to the left, it's the opposite, the result goes towards 1.
Hope that wasn't too complicated. :v: It's always good to know how to use dot products, they can usually save you a lot of expensive angle computations.
Thank you so much _Killburn! I don't know if it works but i'll test it later today.
Here you go, one heart! :)
EDIT:
Awesome. It works perfectly!
Sorry, you need to Log In to post a reply to this thread.