Page 1 of 1

[≈ solved] Check if an object is in field of view?

PostPosted: Mon Feb 20, 2017 21:30
by AiTechEye
Someone that know how to calculate / or know where to find code, to check if an object is in field of view?

I think it could be something to sneak behind mobs without to be detected.

Image

Re: Check if an object is in field of view?

PostPosted: Mon Feb 20, 2017 23:01
by octacian
VanessaE in IRC wrote:there's a line-of-sight call or two in the API I think
https://github.com/minetest/minetest/blob/master/doc/lua_api.txt#L2323


I decided not to wait for someone to see this and asked there too :P

Re: Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 03:16
by BrandonReese
See if this can help. I think it's mostly on a two plane but it might work.

http://blog.wolfire.com/2009/07/linear- ... rs-part-2/

Re: Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 15:37
by AiTechEye
line-of-sight checks if something is blocking the view.

the tutorial at blog.wolfire.com works as long the bot is looking at same direction (weird), but was usefull.

Re: Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 21:43
by stu
AiTechEye wrote:Someone that know how to calculate / or know where to find code, to check if an object is in field of view?

I think it could be something to sneak behind mobs without to be detected.

It would be almost impossible to determine the exact FOV of any client/view mode. You could possibly make a reasonable approximation but that wouldn't help without a lot of complicated trigonometry. `minetest.line_of_sight` is about the best we've got right now, though I believe there is a PR for better ray-casting support.

If the mob checks the LOS towards you and if that is not blocked by a node then it can 'see' you.

Re: Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 21:49
by AiTechEye
After a few hours of googling and testing, i got this idea, and its a lot easier:
if the object is nearer then the position front of it

so its just to check if the player is front of the mob
Image

so you gets a viewfield that looks like this
Image

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function is_front_of(pos1,pos2)
   local pos1=self.object:getpos()
   local pos2=ob:getpos()
   local l1=distance(pos1,pos2)
   local l2=distance(aliveai.pointat(self,1),pos2)
   return l1>l2
end

function distance(pos1,pos2)
   return math.sqrt((pos1.x-pos2.x)*(pos1.x-pos2.x) + (pos1.y-pos2.y)*(pos1.y-pos2.y)+(pos1.z-pos2.z)*(pos1.z-pos2.z))
end

function pointat(self,d)-- get position front of object
   local pos=self.object:getpos()
   local yaw=self.object:getyaw()
   d=d or 1
   local x =math.sin(yaw) * -d
   local z =math.cos(yaw) * d
   return {x=pos.x+x,y=pos.y,z=pos.z+z}
end

Re: Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 22:09
by stu
You really should use the built-in vector helpers, for distance especially. It still looks to me like a rather inefficient way to accomplish this, though I do not fully understand your requirements.

Re: [≈ solved] Check if an object is in field of view?

PostPosted: Tue Feb 21, 2017 22:39
by AiTechEye
i need them to this mod, but this easy version works too :-)

in my case its too late to use the helpers, because i had no idea there was such features in minetest when i started this mod project a few months ago.

https://forum.minetest.net/viewtopic.php?f=11&t=16083

minetest.line_of_sight checks if you are behind something, so you can hide behind flowers...
(im using a more useful function for that)