Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

AI field of view: How to ?  [SOLVED]

All about the StoneScript

AI field of view: How to ?

Postby etXtra » 06 Aug 2012, 10:47

Hello ,

I'm trying to give an "AI" the ability to see the player depending on its own field of view. basically if the player is behind the AI then he is not visible. but if the player is in front then the player is visible...

I 'm writing a script but I'm not able to make it work as expected as I have some difficulties, here it is :

Code: Select all
-- if there is no obstacle between player and AI then "not hHitObject"
    if ( not hHitObject )
    then
        if ( AI_Head_Angle_Y <= 0 )
        then
            if ( direction_to_player <= 0 )
            then
                nAngleA = direction_to_player + AI_Head_Angle_Y
                nAngleB = math.abs ( 0 - nAngleA )
            else
                nAngleA = direction_to_player + AI_Head_Angle_Y
                nAngleB = math.abs ( 0 - nAngleA )
            end
        else
            if ( direction_to_player <= 0 )
            then
                nAngleA = direction_to_player - AI_Head_Angle_Y
                nAngleB = math.abs ( 0 - nAngleA )
            else
                nAngleA = direction_to_player - AI_Head_Angle_Y
                nAngleB = math.abs ( 0 - nAngleA )
            end
        end
        if ( nAngleB <= 95 )
        then
            log.message ( "Visible" )
        else
            log.message ( "Not Visible" )
        end
    else
        log.message ( "Not Visible" )
    end


I try to compute the angles "direction to look at the player"
I really need help, to solve this...
thanks...
etXtra
Junior Boarder
Junior Boarder
 
Posts: 39

Re: AI field of view: How to ?

Postby broozar » 06 Aug 2012, 11:47

i suppose you could use one of these 3 methods, or combine them for better accuracy:

1. sensors. you can attach sensors to your player and youe enemy model. sensors do not have to be centered to the player object, instead, you can attach them e.g. in front of your enemy with an offset and check for a sensor collision with the player ID sensor. advantage: fast and easy to set up. disadvantage: enemies can see through walls with it.

2. rayrast. peridocially send out a ray in front of the enemy and check if you firsthitcollider/firsthitsensor. disadvantage: slow. advantage: very precise.

3. angle calculations (like the one you did) in connection with distance calculations. quite fast and reliable if you know what you are doing, but trigonometry is evil. you hardly get all 360 degrees, but values between -180 to 0 to 180. avoid whenever you can.

tip: combine #1 with #2. wait for a sensor event, then do a raycast to check if there is a wall between the enemy and the player.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3483
Location: Berlin - Germany

Re: AI field of view: How to ?

Postby etXtra » 06 Aug 2012, 15:41

yeah, I already chose the raycast method to know if there are obstacles between, but the question is how to know if the enemy is facing or not the player.
I know angles are in degrees and range from -180 to 180 ; the above script is my attempt to compute field of view using these data. But results aren't good, so I thought I would save time by asking here what was wrong with it?: sometimes the player is behind the AI but the "log.message" I wrote says "visible" ; right now, I can't figure out the error.

as it is not a big code and not complex maybe someone could tell me where I got wrong ???
thanks.
etXtra
Junior Boarder
Junior Boarder
 
Posts: 39

Re: AI field of view: How to ?  [SOLVED]

Postby etXtra » 06 Aug 2012, 17:33

hello !!!

ok, maybe I was tired before but now I finally found my way :
Code: Select all
if ( not hHitObject )
    then
        --log.message ( "VU" )
        if ( ajy <= 0 )
        then
            if ( head <= 0 )
            then
                --nAngleX = head + ajy
                object.rotateTo ( test, 0.0, head, 0.0, object.kGlobalSpace, 1.0 )
                object.rotateTo ( test, 0.0, 0 + math.abs ( ajy ), 0.0, object.kLocalSpace, 1.0 )
                x, nAngleX, z = object.getRotation ( test, object.kGlobalSpace )
                nAngleY = math.abs ( nAngleX )
            else
                --nAngleX = head + ajy
                object.rotateTo ( test, 0.0, head, 0.0, object.kGlobalSpace, 1.0 )
                object.rotateTo ( test, 0.0, 0 + math.abs ( ajy ), 0.0, object.kLocalSpace, 1.0 )
                x, nAngleX, z = object.getRotation ( test, object.kGlobalSpace )
                nAngleY = math.abs ( nAngleX )
            end
        else
            if ( head <= 0 )
            then
                --nAngleX = head - ajy
                object.rotateTo ( test, 0.0, head, 0.0, object.kGlobalSpace, 1.0 )
                object.rotateTo ( test, 0.0, 0 - math.abs ( ajy ), 0.0, object.kLocalSpace, 1.0 )
                x, nAngleX, z = object.getRotation ( test, object.kGlobalSpace )
                nAngleY = math.abs ( nAngleX )
            else
                --nAngleX = head - ajy
                object.rotateTo ( test, 0.0, head, 0.0, object.kGlobalSpace, 1.0 )
                object.rotateTo ( test, 0.0, 0 - math.abs ( ajy ), 0.0, object.kLocalSpace, 1.0 )
                x, nAngleX, z = object.getRotation ( test, object.kGlobalSpace )
                nAngleY = math.abs ( nAngleX )
            end
        end
        --nAngleY = this.nCap ( ) - head
        --hud.setLabelText ( huds, "" .. this.nCap ( ) )
        --hud.setLabelText ( huds, "" .. nAngleY )
        if ( nAngleY <= 91 )
        then
            log.message ( "VU" )
        else
            log.message ( "NON" )
        end
    else
        log.message ( "NON" )
    end


the point is that I had to ensure that the angles computed remain between 180 and -180...
I guess you won't understand my script at first look... because I didnt pasted the layout of it... but if someday someone (maybe a newbie) ask for explanation on how to implement "field of vision" in their AI scripts then I will try to help and explain my idea:
- just post here ! (for the moment I have to go on; so I won't explain today...)

- CHEERS !!! :D -
etXtra
Junior Boarder
Junior Boarder
 
Posts: 39


Return to Game programming