DO
x# = object position x(1) : y# = object position y(1) : z# = object position z(1)
set cursor 0,0 : print x# : print y# : print z#
sync
LOOP
ok.. this is gonna take the position of the object 1 in the 3D space and print it on screen..
Now lets make a function to detect if the object is in a certain
area..
function ObjectInBox(obj,x#,y#,z#,x2#,y2#,z2#)
xo#=object position x(obj) : yo#=object position y(obj) : zo#=object position z(obj)
if ((xo#>x#) and (xo#<x2#) and (yo#>y#) and (yo#<y2#) and (zo#>z#) and (zo#<z2#))
inarea=1
else
inarea=0
endif
endfunction inarea
If there is some problem to understand the function above, please tell..
It just checks if the object is in a
3D box in the world,
then returns 1 if the object is in or 0 if the object isn't in..
now, lets implement it to example:
sync on : sync rate 0
make object sphere 1, 50
position object 1, 0,25,0
`make a dummy box: ignore it
make object box 2, 500,100,500
position object 2, 250,50,350
color object 2, RGB(200,0,0)
ghost object on 2
DO
gosub ControlObjectUsingArrowkeys
x# = object position x(1) : y# = object position y(1) : z# = object position z(1)
set cursor 0,0 : print int(x#) : print int(y#) : print int(z#)
if ObjectInBox(1,0,0,100,500,500,600)=1
print "Object is inside the imaginary box.."
else
print "Object is outside the imaginary box.."
endif
position camera x#,y#+100,z#-200
sync
LOOP
ControlObjectUsingArrowkeys:
if upKey()=1 THEN move object 1, 2
if downKey()=1 THEN move object 1, -2
if rightKey()=1 THEN yrotate object 1, wrapValue(object angle y(1)+1)
if leftKey()=1 THEN yrotate object 1, wrapValue(object angle y(1)-1)
return
function ObjectInBox(obj,x#,y#,z#,x2#,y2#,z2#)
xo#=object position x(obj) : yo#=object position y(obj) : zo#=object position z(obj)
if ((xo#>x#) and (xo#<x2#) and (yo#>y#) and (yo#<y2#) and (zo#>z#) and (zo#<z2#))
inarea=1
else
inarea=0
endif
endfunction inarea
:EASIER WAY:
of course you could just put a dummy object in the world and check
the collision to it..
But the fastest way is using the distance formula:
sqrt((xpositionofcharacter-xposition2)^2 + (ypositionofcharacter-yposition2)^2 + (zpositionofcharacter-zposition2)^2)
well..
if sqrt((xpositionofcharacter-xposition2)^2 + (ypositionofcharacter-yposition2)^2 + (zpositionofcharacter-zposition2)^2) < 200
print "Inside the imaginary sphere.."
else
print "Outside the imaginary sphere.."
endif
It checks if the character is at less than 200 points from the position2..
Implementing it to the example above (it is much more clear):
sync on : sync rate 0
make object sphere 1, 50
position object 1, 0,25,0
`make a dummy sphere: ignore it
make object sphere 2, 300
position object 2, 250,50,350
color object 2, RGB(200,0,0)
ghost object on 2
DO
gosub ControlObjectUsingArrowkeys
x# = object position x(1) : y# = object position y(1) : z# = object position z(1)
set cursor 0,0 : print int(x#) : print int(y#) : print int(z#)
`[REMEMBER]
if sqrt((x#-250)^2 + (y#-50)^2 + (z#-350)^2) < 150
print "Inside the imaginary sphere.."
else
print "Outside the imaginary sphere.."
endif
position camera x#,y#+100,z#-200
sync
LOOP
ControlObjectUsingArrowkeys:
if upKey()=1 THEN move object 1, 2
if downKey()=1 THEN move object 1, -2
if rightKey()=1 THEN yrotate object 1, wrapValue(object angle y(1)+1)
if leftKey()=1 THEN yrotate object 1, wrapValue(object angle y(1)-1)
return
[REMEMBER]: You need not to create the sphere (I created it just to show the area)..
Note that you check a distance between character and the point:
P(x,y,z) => P(250,50,350)
Hope that helps
The worst foe lies within the self..