Little example of creating a radar system
sync on
sync rate 40
hide mouse
randomize timer()
rem show us a ground plane
make matrix 1,500,500,40,40
rem player object
make object sphere 2,1
color object 2,rgb(255,0,0)
`make other objects
for t = 3 to 20
make object cone t,1
position object t, rnd(500),0,rnd(500)
color object t, rgb(255,0,0)
next t
width = screen width()/2
height = screen height()/2
rem store these values for drawing our view cone at center of radar
rem default FOV is 90 degrees
global vx1 : vx1 = cos(-135)*10
global vy1 : vy1 = sin(-135)*10
global vx2 : vx2 = cos(-45)*10
global vy2 : vy2 = sin(-45)*10
ink rgb(255,0,0),0
do
camera()
yrotate object 2,camera angle y()
position object 2, camera position x(),0.5,camera position z()
draw_radar(width,height,100,500,2,3,20)
sync
loop
function camera()
speed# = 1
cx# = camera position x()
cz# = camera position z()
a# = camera angle y()
cxa# = camera angle x()
if shiftkey() then speed# = 3
if upkey()=1
cx#=newxvalue(cx#,a#,speed#)
cz#=newzvalue(cz#,a#,speed#)
endif
if downkey()=1
cx#=newxvalue(cx#,a#,-speed#)
cz#=newzvalue(cz#,a#,-speed#)
endif
if leftkey()=1
cx#=newxvalue(cx#,wrapvalue(a#-90.0),speed#)
cz#=newzvalue(cz#,wrapvalue(a#-90.0),speed#)
endif
if rightkey()=1
cx#=newxvalue(cx#,wrapvalue(a#+90.0),speed#)
cz#=newzvalue(cz#,wrapvalue(a#+90.0),speed#)
endif
a#=wrapvalue(a#+(mousemovex()/3.0))
cxa#=cxa#+(mousemovey()/3.0)
if cxa#<-90.0 then cxa#=-90.0
if cxa#>90.0 then cxa#=90.0
position camera cx#,1.5,cz#
rotate camera cxa#,a#,0
endfunction
rem X screen position of radar
rem Y screen position of radar
rem radar's radius
rem radar's range
rem player object
rem starting object number to include in radar checking
rem ending object number to include in radar checking
function draw_radar(x,y,radius,range,obj,aobj,bobj)
ink rgb(255,0,0),0
rem get player's position
px# = object position x(obj)
pz# = object position z(obj)
rem loop through all wanted object
for t = aobj to bobj
rem get object's position
ox# = object position x(t)
oz# = object position z(t)
rem calculate squared distance
sqDist# = (px#-ox#)*(px#-ox#) + (pz#-oz#)*(pz#-oz#)
rem is object within range?
if sqDist# <= range*range
rem get angle between object and player (offset angle)
offAng# = wrapvalue(atanfull(ox#-px#,oz#-pz#))
ang# = (object angle y(obj) - offAng#)+180
rem calculate object's distance from player on radar screen
rem because the distance is squared, we square the other values and avoid
rem using the sqrt() function. This way, we only have to use 1 instead of 2.
rDist# = sqrt((sqDist# * (radius*radius)) / (range*range))
rx# = x + sin(Ang#)*rDist#
ry# = y + cos(Ang#)*rDist#
circle rx#, ry#, 1
endif
next t
circle x, y, radius
ink rgb(0,255,0),0
line x,y,x+vx1,y+vy1
line x,y,x+vx2,y+vy2
endfunction