Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: NPC area view  (Read 1200 times)

0 Members and 1 Guest are viewing this topic.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
NPC area view
« on: February 21, 2021, 11:20:06 am »
Hello. tell me how to make vision for an NPC with obstacles.
Example in the picture.
Now the left NPC sees the right one, but the right one is behind the wall, that is, the left one should not see the right one.
For vision, I do this: there is a viewArea sprite (in the image it is red), I check the viewArea collision with the NPC sprite, if true, then the NPC sees another NPC, but how to beat if there is a wall between them?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: NPC area view
« Reply #1 on: February 22, 2021, 12:06:43 am »
There are lots of different ways to achieve this. Really depends to what extend you need it.

A general term you'll find a lot of material under is, if you look for ray casting or view cone.

The idea is that you send out a "ray" from the NPC and if it hits an obstacle, it doesn't continue, if it doesn't hit anything, it continues for the defined view distance. If it hits a player, you then know it can see it.
There are mathematical solutions to this, that are better explained elsewhere and I don't really know anyways. :D
« Last Edit: February 22, 2021, 12:08:42 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything