Trying to figure out basic enemy AI sight

Hi,

I have been pulling my hair out trying to figure out how to get some basic enemy AI going.
I would gladly use python but I can’t find any good tutorials on getting it working with blender. I don’t have the time to learn Python itself. I barely get enough free time to use Blender itself. I just want to learn how to do scripting in blender. According to the blender python scripting tutorials I did find, I pretty much have to be a python god to do the tutorial.

Anyway…

I can’t use just rays because they can’t cover an area.
http://i39.tinypic.com/x1y81z.jpg
I can’t use just collision boxes because they can see right through walls.
http://i39.tinypic.com/1ok4qq.jpg
I am trying to mix collision and rays using a very basic setup:
http://i43.tinypic.com/16m4z2f.jpg
0. Enemy object spins around.

  1. Collision box parented to enemy object collides with player.
  2. Object tracks to player.
  3. If a ray hits the player without colliding with a box or anything, the enemy is deleted via “end object”.
    3.5. If a ray doesn’t hit the player within 3 seconds, the enemy box forgets the player for almost 2 seconds and then repeats the process.

Unfortunately, I attempted to test this and I can’t even get the collision box to detect the player.
Something’s wrong.
Can you guys take a look at the blend and help me out?

Attachments

test.blend (147 KB)

the blue thingy is blocking the ray :rolleyes:

>.<
I can’t believe I missed that.
I’ll try fixing that when I get home today.

Heres a tutorial for python in the BGE:
http://www.tutorialsforblender3d.com/Python/Tutorials/Python_Tutorial_1.html

Once you get the python down you can use a function I have for determining if an agent can see an object:

from math import degrees
from Mathutils import Vector

def SightCone(object, target, target_width=1.0, ray_dist=0, ray_prop='', primary=80, secondary=90):
    def rayCast():
        p1 = t_pos + (Vector(target_width/2,0,0) * t_ori)
        p2 = t_pos + (Vector(-target_width/2,0,0) * t_ori)
        ray1 = object.rayCast(p1, object, ray_dist, ray_prop)
        ray2 = object.rayCast(p2, object, ray_dist, ray_prop)
        
        return( ray1[0] == target and ray2[0] == target)

    o_pos = object.position
    o_ori = object.orientation
    t_pos = target.position
    t_ori = target.orientation
    
    vec1 = Vector(0,1,0) * o_ori
    vec1.x = -vec1.x
    vec1.z = -vec1.z
    
    vec2 = t_pos - o_pos
    
    angle = degrees(vec1.angle(vec2))
    
    if angle &lt;= secondary:
        if rayCast():
            if angle &lt;= primary:
                return(1)
            else:
                return(2)
    
    return(0)

The arguments of the function are:
-object: The object doing the looking
-target: the target getting looked at
-target_width: the width of the target
-ray_dist: how far the object doing the looking can see
-ray_prop: only detect ray collisions with this property
-primary and secondary: view fields, allows for peripheral vision. Its how wide the object doing the looking can see. The angles are halved, so 90 = 180. In the instance of primary = 80 and secondary = 90 (defualt), the agent has a sight cone of 180 degrees.

A 0 returned means the target can’t be seen. 1 means the target is in its primary view field and 2 means the target is in its secondary view field.