Steve C,
I would suggest not using a cone for your enemy's line of sight. Instead, try using your angles and distances. Try these steps to make a boundary of sight for your a.i.
1.)Give your enemy a limit in how far away he can spot objects. To do this, measure the distance from the enemy to the player, or whatever he needs to spot, with this formula, distance# = sqrt((e_xpos - p_xpos)^2 + (e_zpos - p_zpos)^2). Where "e_xpos" is the enemy's position on the x coordinate, and "e_zpos" is the enemy's position on the z coordinate, and the "p" stands for player. To give the enemy a limit to how far he can see, just use the code
if distance# =< 300 then...
.
2.)Now, give it angle boundaries, because a person can only see so far to his left and right and up and down, until he turns his head or body. Use "atanfull()" to get the angle between the enemy and the player. Try this
REM get angle between characters
anglebetween# = atanfull(e_xpos - p_xpos,e_zpos - p_zpos)
REM calculate if player is within 90o of enemy's current angle of direction
if angle# =< e_currentangle + 45 and angle# => e_currentangle - 45
withinboundaries = 1
else
withinboundaries = 0
endif
. So, if the enemy's current angle of direction(from left to right) is 90, then the player would have to be between the angles of 45o and 135o for "withinbounds" to = 1.
Here is how I would write a simple 2D program of circles and lines, to get the enemy to chase the player, if within the view limits of the enemy.
set display mode 800,600,32
sync on
sync rate 80
REM setup beginning coords of characters
p_xpos# = rnd(800)
p_ypos# = rnd(600)
e_xpos# = 400
e_ypos# = 300
REM setup beginning angles of characters
p_curangle = 0
e_curangle# = 0
REM main program loop
do
REM player controls
if leftkey() = 1 then p_curangle = wrapvalue(p_curangle + 1)
if rightkey() = 1 then p_curangle = wrapvalue(p_curangle - 1)
if upkey() = 1
p_xpos# = p_xpos# + (cos(p_curangle) * 2)
p_ypos# = p_ypos# + (sin(p_curangle) * 2)
endif
if downkey() = 1
p_xpos# = p_xpos# - (cos(p_curangle) * 2)
p_ypos# = p_ypos# - (sin(p_curangle) * 2)
endif
REM by steps, check to see if player is within enemy's view site
REM 1.)is player within 300 pixels of enemy?
if sqrt((e_xpos# - p_xpos#)^2 + (e_ypos# - p_ypos#)^2)) =< 300 then distance = 1
REM 2.)if distance = 1, then is player within 90o of enemy's current angle?
if distance = 1
anglebetween# = atanfull(e_xpos# - p_xpos#,e_ypos# - p_ypos#)
if anglebetween# =< wrapvalue(e_curangle# + 45) and anglebetween# => wrapvalue(e_curangle# - 45)
withinsight = 1
else
withinsight = 0
endif
endif
REM make enemy chase player if withinsight = 1, swapping sin and cos to make enemy go towards player
if withinsight = 1
e_curangle# = anglebetween#
e_xpos# = e_xpos# + (sin(e_curangle#) * 2)
e_ypos# = e_ypos# + (cos(e_curangle#) * 2)
endif
REM draw everything to the screen
REM player
ink rgb(255,255,255),0
circle p_xpos#,p_ypos#,10
REM draw line coming from circle, to show visual of current angle of player
line p_xpos# + (cos(p_curangle) * 10),p_ypos# + (sin(p_curangle) * 10),p_xpos# + (cos(p_curangle) * 20),p_ypos# + (sin(p_curangle) * 20)
REM enemy
ink rgb(255,0,0),0
circle e_xpos#,e_ypos#,10
REM draw line coming from circle, to show visual of current angle of enemy
line e_xpos# + (cos(e_curangle) * 10),e_ypos# + (sin(e_curangle) *10),e_xpos# + (cos(e_curangle) * 20),e_ypos# + (sin(e_curangle) * 20)
REM end of loop and program
sync
cls
loop
This program might be a little buggy, as it was written without testing. If it is not working correctly, let me know and I will debug it. If you have specific questions, feel free to ask.
+NanoBrain+