Check out this example:
set display mode 1024,768,32,1
sync on
autocam off
hide mouse
a=make vector3(1)
a=make vector3(2)
make object plane 1,10,10,0
rotate object 1,0,rnd(360),0
make object sphere 2,4,20,20
position object 2,rnd(30)-15,-3,rnd(30)-15
xrotate camera 0,30
Speed as float : Speed=0.1
do
if keystate(17) then position object 2,object position x(2)+Speed*sin(camera angle y(0)),-3,object position z(2)+Speed*cos(camera angle y(0))
if keystate(31) then position object 2,object position x(2)-Speed*sin(camera angle y(0)),-3,object position z(2)-Speed*cos(camera angle y(0))
if keystate(30) then position object 2,object position x(2)+Speed*sin(camera angle y(0)-90),-3,object position z(2)+Speed*cos(camera angle y(0)-90)
if keystate(32) then position object 2,object position x(2)+Speed*sin(camera angle y(0)+90),-3,object position z(2)+Speed*cos(camera angle y(0)+90)
xrotate camera 0,wrapvalue(camera angle x(0)+mousemovey()/3.0)
yrotate camera 0,wrapvalue(camera angle y(0)+mousemovex()/3.0)
position camera 0,0,0
move camera 0,-30
set point light 0,camera position x(0),camera position y(0),camera position z(0)
turn object right 1,0.2
move object 1,3
line screen width()/2,screen height()/2,object screen x(1),object screen y(1)
move object 1,-2
set vector3 1,object position x(1),object position y(1),object position z(1)
position object 1,0,0,0
set vector3 2,object position x(2)-object position x(1),object position y(2)-object position y(1),object position z(2)-object position z(1)
normalize vector3 2,2
set cursor 0,0
print "Mouse - Look"
print "WASD - Strafe"
print
if dot product vector3(1,2)>=0
print "Player is in front of the door"
else
print "Player is behind the door"
endif
sync
loop
This uses more or less the same technique as Neuro Fuzzy's, but I hate the
atanfull() command with a passion. This is more true to the math behind it and would work with any door oriented any way.
In vector 1, we store the normal of the door. Basically, that's just the direction in 3D space that the door is facing. Next, in vector 2, we store the vector from the door to the player by subtracting the door's position from the player's. After that, we normalize it, which just scales it down so that the length of the vector equals one (read on for why).
Next, a little math lesson.
The dot product of vector A and B gives us: |A|*|B|*cos(Angle)
That can be read as: The length of the first vector times the length of the second vector times the cosine of the angle between them. Now, since both of our vectors have a length of 1, this is just the cosine of the angle between them. Using what we know about the cosine function, we can tell that when 0<Angle<90, 1>cos(Angle)>0, and when 90<Angle<180, 0<cos(Angle)<-1.
In summary, when the dot product of our two vectors is greater than zero, we know that the player is in front of the door, and when it's negative, we know that the player is behind it.
Whew!
And so concludes the math lesson for the day!
Edit: It just occurred to me that we don't even have to normalize the second vector because even if we don't, the dot product will still be positive or negative. It just won't necessarily be the cosine of the angle between the vectors.