English Define the back or the front of player

10 replies
Goto Page
To the start Previous 1 Next To the start
Up
KenVo
User
Offline Off
IMG:http://img96.imageshack.us/img96/1631/30784162.png


Let's say if enemies run into the "front"(red area) of the player, then they will get killed. Otherwise nothing happens.
07.04.12 10:22:18 am
Up
MAX-russia
User
Offline Off
What do you want to say this way ? You need help or what ?
07.04.12 11:38:37 am
Up
3RROR
User
Offline Off
Define what? Sorry I didn't get it.
IMG:http://gamerswins.com/images/ins/logo.png
07.04.12 11:49:44 am
Up
dENd
User
Offline Off
Lua request?
You want to kill all players in your view line?
© Unreal Software, 2003-2018 | This website is using cookies because they are delicious | Disclaimer | Site Notice
07.04.12 11:58:05 am
Up
EngiN33R
Moderator
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function findPlyAngle(ply1,ply2)
     local x1,y1=player(ply1,"x"),player(ply1,"y")
     local x2,y2=player(ply2,"x"),player(ply2,"y")
     local ang=-math.deg(math.atan2(x1-x2,y1-y2))
     return ang
end

function isInFront(ply1,ply2,f) --ply1 is the player in front of ply2
     local ang=findPlyAngle(ply1,ply2)
     local rot2=player(ply2,"rot")
     if rot2>180 then
          rot2=player(ply2,"rot")-180
     end
     if ang>=rot2-f and ang<=rot2+f then
          return true
     end
     return false
end

addhook("ms100","checkforinfront")
function checkforinfront()
     for _,id in pairs(player(0,"table")) do
          for _,iid in pairs(player(0,"table")) do
               if id~=iid then
                    if isInFront(iid,id,45) then
                         parse("customkill "..id.." Magic "..iid)
                    end
               end
          end
     end
end


I guess the thing that actually kills people could be done better, but whatever. I think this is what you asked for. Also the function might not work always.
edited 1×, last 07.04.12 12:09:38 pm
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
07.04.12 12:03:11 pm
Up
Flacko
User
Offline Off
I'm guessing you want to know if a point is in the player's field of vision.
This code could work
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function normalizerad(r)
     if r>math.pi then
          r = -math.pi+r%math.pi
     elseif r<-math.pi then
          r = math.pi-(math.pi-r%math.pi)
     end
     return r
end
function torad(degrees)
     return normalizerad(math.rad(degrees+90))
end

local fovw = 120
local fovd = 60
fovconst = math.atan2(fovw/2,fovd)
--fovconst is pi/2 (or 90 degrees)
function isinplayerfov(id,x,y)
     local a = math.atan2(player(id,"y")-y,player(id,"x")-x)
     local rot = torad(player(id,"rot"))
     return (a>(rot-fovconst) and a<(rot+fovconst))
end

I haven't tested it too much so I cant really tell.
Btw, the human FOV isn't exactly 90°, it's actually about 95° according to wikipedia.
To set the exact value set fovconst to 95*math.pi/180
Where 95 is any angle you wish.

Edit: Whoops, 5 minutes too late
07.04.12 12:09:12 pm
Up
EngiN33R
Moderator
Offline Off
No, user Flacko's code is much better - use it instead.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
07.04.12 07:52:36 pm
Up
KenVo
User
Offline Off
Lol thank you! However, Engineer's script look somewhat less complicated to me.
@Those who didn't understand what i was asking for
07.04.12 07:58:09 pm
Up
PyKemis
User
Offline Off
user EngiN33R has written:
No, user Flacko's code is much better - use it instead.


Hmm well, I see what you did, it's much easier to see how it works from your script.

Since lua is a learning language it's always best to know what it's doing...however if you're just leeching it doesn't matter.
07.04.12 08:02:50 pm
Up
KenVo
User
Offline Off
Well I actually wanted to know how it works, because I'm not working on LUA scripting
08.04.12 12:03:34 am
Up
Flacko
User
Offline Off
Whoops, seems like I made a big mistake, now it's (hopefully) fixed:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function normalizerad(r)
     if r>math.pi then
          r = -math.pi+r%math.pi
     elseif r<-math.pi then
          r = math.pi-(math.pi-r%math.pi)
     end
     return r
end
function torad(degrees)
     return normalizerad(math.rad(degrees+90))
end

local fovw = 120
local fovd = 60
fovconst = math.atan2(fovw/2,fovd)

function isinplayerfov(id,x,y)
     local a = math.atan2(player(id,"y")-y,player(id,"x")-x)
     local rot = torad(player(id,"rot"))
     local dif = normalizerad(rot-a)
     return math.abs(dif)<fovconst
end


To understand how it works it's better to use an image:
IMG:http://img705.imageshack.us/img705/7443/dibujoxlp.png


fovconst is half of the FOV angle
in the script:
Code:
1
fovconst = math.atan2(fovw/2,fovd)


a is the angle from the player to the object
in the script:
Code:
1
local a = math.atan2(player(id,"y")-y,player(id,"x")-x)


The player rot is the player's rotation converted to radians (CS2D uses a bizarre angle system)
in the script:
Code:
1
local rot = torad(player(id,"rot"))


To determine if the target is inside the player's FOV, then the difference between rot and a must be smaller than fovconst
in the script:
Code:
1
2
local dif = normalizerad(rot-a)
return math.abs(dif)<fovconst


normalizerad is just a helper function that makes sure that the angle you pass ranges between [-pi;pi] if it doesn't, then it's transformed. Otherwise, radian operations might become buggy.
To the start Previous 1 Next To the start