dst1 ={x=100,y=100}
dst3 = {x=-100,y=80}
dst4 = {x=-100,y=-100}
dst2 = {x=100,y=-10}
my = {x=0,y=0,d=20,dr=30}
--o1是中心角色,o2是目标角色
function degree(o1,o2)
local a = (o2.y-o1.y)/(o2.x-o1.x)
local d = math.atan(a)*180.0/math.pi
if o2.x-o1.x < 0 then
d=d+180
elseif o2.y-o1.y < 0 then
d=d+360
end
if d==360 then
d=0
end
return d
end
--o1dr是幅度的一半,在o1d当前朝向的前后
function indegree(o1,o2,o1d,o1dr)
local d = degree(o1,o2)
o1d = o1d - o1dr
--o1d映射到0到360
while o1d < 0 do
o1d = o1d + 360
end
o1d = o1d % 360
if o1d + o1dr*2 > 360 then
return (d>=0 and d <=(o1d+o1dr*2)%360) or (d<=360 and d >= o1d)
else
return o1dr*2+o1d>=d and o1d < d
end
end
print(degree(my,dst1),degree(my,dst3),degree(my,dst4),degree(my,dst2))
print(indegree(my,dst1,my.d,my.dr))
dst3 = {x=-100,y=80}
dst4 = {x=-100,y=-100}
dst2 = {x=100,y=-10}
my = {x=0,y=0,d=20,dr=30}
--o1是中心角色,o2是目标角色
function degree(o1,o2)
local a = (o2.y-o1.y)/(o2.x-o1.x)
local d = math.atan(a)*180.0/math.pi
if o2.x-o1.x < 0 then
d=d+180
elseif o2.y-o1.y < 0 then
d=d+360
end
if d==360 then
d=0
end
return d
end
--o1dr是幅度的一半,在o1d当前朝向的前后
function indegree(o1,o2,o1d,o1dr)
local d = degree(o1,o2)
o1d = o1d - o1dr
--o1d映射到0到360
while o1d < 0 do
o1d = o1d + 360
end
o1d = o1d % 360
if o1d + o1dr*2 > 360 then
return (d>=0 and d <=(o1d+o1dr*2)%360) or (d<=360 and d >= o1d)
else
return o1dr*2+o1d>=d and o1d < d
end
end
print(degree(my,dst1),degree(my,dst3),degree(my,dst4),degree(my,dst2))
print(indegree(my,dst1,my.d,my.dr))