English Anticipate if you shoot it hits a target or not

2 replies
Goto Page
To the start Previous 1 Next To the start
Up
The Dark Shadow
User
Offline Off
Recently while i was creating custom weapons script I got stuck in this thing, So basically what I want is anticipate whenever my aim (ex) is at a decent position so if i shoot if it could hit a target print an output.

Here, a simple script that prints "Yes" if player Id 1 (mousemapX) and (mousemapY) are within player Id 2 (target) (X,Y) in 16 pixels each side for both coordinates, elsewise it prints a "No". It doesn't work the best though.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
addhook("ms100", "ms100_hook")
function ms100_hook()
     local You = 1
     local Target = 2
     
     local radius = 16

     local mouseMapX = player(You, "x") - player(You, "screenw")/2 + player(You, "mousex")
     local mouseMapY = player(You, "y") - player(You, "screenh")/2 + player(You, "mousey")

     local inRange_X = mouseMapX >= player(Target, "x") - radius and mouseMapX <= player(Target, "x") + radius
     local inRange_Y = mouseMapY >= player(Target, "y") - radius and mouseMapY <= player(Target, "y") + radius
     
     if inRange_X and inRange_Y then
          parse("hudtxt 0 Yes")
     else
          parse("hudtxt 0 No")
     end
end


If my explanation above was poor here an explanation in images:

IMG:https://i.imgur.com/Qc1L0XM.png


So using the code above in the first image it prints "Yes" at this point as If I shoot It will hit the target. √

IMG:https://i.imgur.com/RTJHiaY.png


But in the second image if the code above is being used it prints "No" even though still if I shoot it will hit the target. ×
It should print "Yes".

Note: Only If the target is being seen within the screen.

Does anyone know the "maths" behind this thing? Thanks, Would be appreciated!
edited 6×, last 14.04.21 11:25:16 am
14.04.21 12:04:33 pm
Up
ohaz
User
Offline Off
What you want to do is create a line (y=mx+t) between your character and the cursor.
First you have to calculate m:
m = (y2-y1) / (x2-x1)
Then you can calculate t by inserting x and y of the player in y=mx+t:

y1 = m * x1 + t

Then you can insert m and t into the formula: y = mx+t (you'll get something like y = 2x + 5
After that you need a circle/line or rectangle/line intersection. Both of those are not super trivial, but there are some stackoverflow posts for it: https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
https://ohaz.engineer - Software Engineering
17.04.21 09:27:08 am
Up
The Dark Shadow
User
Offline Off
Thanks @user ohaz, I really appreciate it. I made it work finally after checking the link you redirected, what you said, and learning some geometry on my own
To the start Previous 1 Next To the start