Page 1 of 1

Preview flight simulator

Posted: Sun May 19, 2013 1:42 pm
by Henko
' To be added in final version:
' - scenery
' - check on correct landings
' - wind
'
'
option angle degrees
option base 1
dim xy(2),airports(7,3),mes$(60)
read wl,bmax,b,bs,f,to,k,cdo
read clo,clm,dclf,dcdf,dcdg,fr
read dt,g,x,y,koers,breaks,gears,speed,relax,x,y
for i=1 to 7 ! for j=1 to 3 ! read airport(i,j) ! next j ! next i
data 20000,4000,1400,.0005,120,6000,.025,.033
data .22,2.2,.6,.2,.1,.05
data 1,9.81,0,0,90,3,1,1000,.3,45,50
data 45,50,90,  72.5,50,90,  12.5,82.5,230,  22.5,20,270
data 62.5,75,0,  60,27.5,40,  87.5,10,310

gosub init_prog

loop1:          ' check the touch input methods
xp=touch_x(0)
if xp=-1 then loop2
yp=touch_y(0)
temp=gas_bar(xp,yp,0) ! if temp>-1 then gasn=temp
if b=0 then gasn=0
if att_panel(xp,yp,xy,v,h,0) then
  tetn=40*xy(1) ! gamn=40*xy(2)
  end if
temp=flaps_bar(xp,yp,0) ! if temp>-1 then flaps=temp

loop2:    ' check the buttons input methods and process them
if button_pressed("rev") then
  reverse=1-reverse
  if reverse then
    gas=-0.5
    button "rev" title "REV!" at 20,908 size 50,40
    else
    gas=gas_bar(40,880,0)
    button "rev" title "rev" at 20,908 size 50,40
    end if
  end if
if button_pressed("brk") then
  fill rect 0,0 to maxx,395
  breaks=3-breaks
  if breaks then
    button "brk" title "BREAKS!" at 470,745 size 120,40
    else
    button "brk" title "breaks" at 470,745 size 120,40
  end if
end if
if button_pressed("gear") then
  if gears=1 and h>0 then
    gears=0
    button "gear" title "l.gear" at 610,745 size 120,40
    goto loop3
    end if
  if gears=0 then
    gears=1
    button "gear" title "L.GEAR!" at 610,745 size 120,40
    end if
  end if
loop3:
if button_pressed("spo") then
  dt=1 ! ttt=speed
  end if
if button_pressed("sp+") then
  if ttt>speed/4 then
    ttt=ttt/2
    else 
    ttt=0 ! dt=2*dt ! if dt>4 then dt=4
    end if
  end if
if button_pressed("sp-") then
  if dt>1 then
    dt=dt/2
    else
    if ttt=0 then ttt=speed/2 else ttt=2*ttt
    end if
  end if
for i=1 to 7
  b$="b" & i
  if button_pressed(b$) then
    fill rect 437,876 to 505,894
    fill rect 537,876 to 625,894
    fill rect 657,876 to 735,894
    if vor=0 then
      vor=i ! button b$ title "B" & i at 442+38*i,910 size 29,29
      vor_x=airport(i,1) ! vor_y=airport(i,2) ! appr=airport(i,3)
      else 
      if i=vor then
        vor=0 ! button b$ title i at 442+38*i,910 size 29,29
        fill rect 657,876 to 735,894
        vor_x=0 ! vor_y=0 ! appr=0
        else 
        button "b" & vor title vor at 442+38*vor,910 size 29,29
        vor=i ! button b$ title "B" & i at 442+38*i,910 size 29,29
        vor_x=airport(i,1) ! vor_y=airport(i,2) ! appr=airport(i,3)
        end if
      end if
    end if
  next i

' slow down the iteration time
if timer()<ttt then loop1
reset timer

' flight model
rho=(1-0.0000226*h)^4.256
gas=(1-relax)*gas+relax*gasn ! th=gas*rho*to
b=b-bs*th*dt ! w=wl+b ! m=w/g ! time=time+dt
tet=(1-relax)*tet+relax*tetn ! gam=(1-relax)*gam+relax*gamn
if vh=0 then bet=0 else bet=atan(vs/vh) ! alf=gam-bet
cl=clo+.1*alf ! if cl>clm then cl=clm ! cl=cl+flaps*dclf
cd=cdo+k*cl*cl+flaps*dcdf+gears*dcdg
lift=rho/16*v*v*f*cl ! drag=lift*cd/cl
kh=th*cos(gam)-lift*sin(bet)*cos(tet)-drag*cos(bet)
if h=0 and lift<w and vh>0 then kh=kh-fr*(w-lift)
ahn=kh/m-breaks ! ah=(1-relax)*ah+relax*ahn
vh=vh+ah*dt ! if vh<0 then vh=0
x=x+vh*sin(koers)*dt/1000 ! y=y+vh*cos(koers)/1000
kv=lift*cos(bet)+th*sin(gam)-d*sin(bet)-w
asn=kv/m ! if h=0 and asn<0 then asn=0 ! as=(1-relax)*as+relax*asn
vs=vs+as*dt ! if h=0 and vs<0 then vs=0
v=sqrt(vh*vh+vs*vs)
h=h+vs*dt ! if h<0 then h=0
if tet and v then koers=koers+57*lift*sin(tet)/m/v
if koers<0 then koers=koers+360 ! if koers>360 then koers=koers-360

' update the instruments
rmeter("km/hr","r",110,507,98,0,500,3.6*v,0)
rmeter("meter","b",340,507,98,0,1000,h,1)
graphics lock ! ti$=t2a$(time)
fill rect 192,422 to 258,438 ! draw text n2a$(h,5,0) at 192,418
fill rect 187,589 to 263,605 ! draw text n2a$(vs,5,1) at 187,585
fill rect 438,427 to 504,443 ! draw text n2a$(koers,4,0) at 438,423
fill rect 428,588 to 514,607 ! draw text ti$ at 428,585
fill rect 722,432 to 748,598
dy=170*b/bmax
if dy<25 then fill color 1,0,0 else fill color 0,.6,0
fill rect 722,598-dy to 748,598 ! fill color .8,.8,.8
pos_meter(bet,gam,tet,x,y,vor_x,vor_y,h,koers,appr)

graphics unlock

goto loop1
end

def pos_meter(bet,gam,tet,xv,yv,vor_x,vor_y,h,koers,appr)
graphics lock
fill color .71,.82,.9 ! fill circle 600,507 size 98
draw size 1 ! draw color .4,.4,.4
draw line 520,507 to 680,507
draw line 600,427 to 600,587
for i=0 to 160 step 20 ! y=427+i ! draw line 590,y to 610,y ! next i
draw size 2 ! draw color 0,0,1
for i=0 to 160 step 20 ! x=520+i ! draw line x,497 to x,517 ! next i
for i=0 to 120 step 20 ! y=447+i ! draw line 650,y to 670,y ! next i
draw color 0,0,0 ! fill color 0,0,0
yc=507-2*gam ! fill circle 600,yc size 6
dx=50*cos(tet) ! dy=50*sin(tet)
draw line 600-dx,yc-dy to 600+dx,yc+dy
draw color 0,.6,0 ! yc=507-2*bet
draw circle 600,yc size 8 ! draw line 592,yc+8 to 608,yc+8
draw color 0,0,0 ! fill color .8,.8,.8

if vor_x then
  fill rect 430,795 to 700,817
  if appr>99 then apr$=int(appr/10) else apr$="0" & int(appr/10)
  draw text "approach - runway " & apr$ at 430,795
  dx=vor_x-xv ! dy=vor_y-yv ! dme=sqrt(dx*dx+dy*dy)
  if dme<60 then
    bear=bearing(dx,dy) ! afw=ap-bear
    vor_uit()
    draw text n2a$(bear,4,0) at 444,873
    draw text n2a$(dme,6,1) at 540,873
    if dx*sin(koers)+dy*cos(koers)>0 then
      ap=appr
      draw text "TO" at 396,872
      else
      ap=appr+180 ! if ap>360 then ap=ap-360
      draw color .7,0,0 ! draw text "FROM" at 384,872
      draw color 0,0,0
      end if
    if dme<16 then
      fill rect 657,876 to 735,894 ! draw text "ON" at 680,873
      xp=600-afw ! if xp<516 then xp=516 ! if xp>684 then xp=684
      a=1000*dme*tan(5.7) ! yp=30*(h-a)/a
      if yp<-30 then yp=-30 ! if yp>30 then yp=30
      yp=507+yp
      else
      xp=516 ! yp=574
      fill rect 657,876 to 735,894 ! draw text "OFF" at 676,873
      end if
    else
    vor_uit() ! xp=516 ! yp=574
    end if
  else
  vor_uit() ! fill rect 430,795 to 700,817 ! xp=516 ! yp=574
  end if
draw color .6,.35,0 ! fill color .6,.35,0
fill circle xp,530 size 4 ! draw line xp,530 to xp,460
draw line 648-10,yp-6 to 648,yp ! draw line 648-10,yp+6 to 648,yp
fill color .8,.8,.8 ! draw color 0,0,0
graphics unlock
end def

def bearing(dx,dy)
if dy=0 then 
  if dx>0 then bear=90 else bear=270
  else 
  bear=atan(dx/dy) ! if dy<0 then bear=bear+180
  if bear<0 then bear=bear+360
  end if
bearing=bear
end def  

def vor_uit()
fill rect 437,876 to 505,894 ! fill rect 537,876 to 625,894
fill rect 384,870 to 434,892 ! fill rect 657,876 to 735,894
end def

def gas_bar(x,y,mode)
xt=30 ! yt=660 ! xb=60 ! yb=900
if mode=1 then 
  draw text "thrust" at 14,630
  draw rect xt,yt to xb,yb
  goto loop_gas1
  end if
if x<xt or x>xb or y<yt or y>yb then
  gas_bar=-1 ! return
  end if
loop_gas1:
graphics lock
draw color 0,0,0
fill rect xt+2,yt+2 to xb-2, yb-2
draw size 2
for yl=680 to 880 step 20 ! draw line 37,yl to 53,yl ! next yl
draw size 1
for yl=690 to 870 step 20 ! draw line 40,yl to 50,yl ! next yl
if y<680 then y=680 ! if y>880 then y=880
draw size 6 ! 
if y<716 then draw color 1,0,0 else draw color 0,.6,0
draw line 35,y to 55,y ! draw color 0,0,0
graphics unlock
gas_bar=(880-y)/200
end def

def att_panel(x,y,xy(),v,h,mode)
xt=100 ! yt=660 ! xb=340 ! yb=900
graphics lock
if mode=1 then
  draw size 4
  draw text "pitch / bank" at 150,630
  draw rect xt,yt to xb,yb
  goto loop_att1
  end if
if x<xt or x>xb or y<yt or y>yb then
  att_panel=0 ! return
  else
  att_panel=1
  if x>214 and x<226 then x=220 ! if y>774 and y<786 then y=780
  if v<30 then y=780 ! if h=0 then x=220
  end if
loop_att1:
draw color 0,0,0
fill rect xt+2,yt+2 to xb-2,yb-2
draw size 2
for yl=680 to 880 step 20 ! draw line 210,yl to 230,yl ! next yl
for xl=120 to 320 step 20 ! draw line xl,770 to xl,790 ! next xl
fill rect 198,758 to 242,802 ! draw circle 200,760 to 240,800
fill color 0,0,0 ! fill circle x-3,y-3 to x+3,y+3
fill color .8,.8,.8
xy(1)=(x-220)/120 ! xy(2)=(780-y)/120
graphics unlock
end def

def flaps_bar(x,y,mode)
xt=380 ! yt=660 ! xb=430 ! yb=780
graphics lock
if mode=1 then 
  draw text "flaps" at 375,630
  draw size 4 ! draw rect xt,yt to xb,yb
  goto loop_flap
  end if
if x<xt or x>xb or y<yt or y>yb then
  flaps_bar=-1 ! return
  end if
loop_flap:
fill rect xt+2,yt+2 to xb-2, yb-2
draw size 2
for yl=680 to 760 step 20 ! draw line 390,yl to 420,yl ! next yl
draw size 1
for yl=690 to 750 step 20 ! draw line 395,yl to 415,yl ! next yl
if y<680 then y=680 ! if y>760 then y=760
draw size 6 ! draw color 0,0,1 
draw line 385,y to 425,y ! draw color 0,0,0
flaps_bar=(760-y)/80
graphics unlock
end def

def cls()
graphics clear .8,.8,.8
end def

def n2a$(num,lang,dec)
b$="               "
fac=10^dec
num$=int(fac*num+.5)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def

def w_open (title$,xtop,ytop,xbot,ybot)
r=10 ! draw color 0,0,0 ! draw size 4
graphics lock
draw circle xtop,ytop to xtop+20,ytop+20
draw circle xbot-20,ytop to xbot,ytop+20
draw circle xtop,ybot-20 to xtop+20,ybot
draw circle xbot-20,ybot-20 to xbot,ybot
draw line xtop+r,ytop to xbot-r,ytop
draw line xtop+r,ybot to xbot-r,ybot
draw line xtop,ytop+r to xtop,ybot-r
draw line xbot,ytop+r to xbot,ybot-r
fill rect xtop+r,ytop+2 to xbot-r,ybot-2
fill rect xtop+2,ytop+r to xbot-2,ybot-r
if title$<>"" then
  l=(xbot-xtop-12*len(title$))/2
  draw line xtop,ytop+24 to xbot,ytop+24
  draw color 0,0,1
  draw text title$ at xtop+l,ytop-2
  draw color 0,0,0
end if
graphics unlock
end def

def test (in$,in)
fill rect 30,900 to 400,940
a$="test " & in$ & in
draw text a$ at 30,900
button "cont" title "doorgaan ?" at 440,890
loopje:
if button_pressed("cont")=0 then goto loopje
button "cont" delete
fill rect 30,900 to 400,940
end def

def rmeter(tit$,col$,xc,yc,rad,minval,maxval,val,mode)
if mode=-1 then
  fill circle xc,yc size rad+2
  return
  end if
graphics lock
  if mode=0 then mode_0
  set_col("n")
  draw size 3 ! draw circle xc,yc size rad
  draw size 4 ! draw circle xc,yc size rad-6
  set_col(col$)
  draw size 3 ! draw circle xc,yc size rad-3
  set_col("n")
  draw arc xc,yc,rad-50,130,410
  draw size 3
  for a=130 to 410 step 28
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-42)*cos(a) ! ye=yc+(rad-42)*sin(a)
    draw line xs,ys to xe,ye
    next a
  draw size 1
  for a=130 to 410 step 7
    xs=xc+(rad-50)*cos(a) ! ys=yc+(rad-50)*sin(a)
    xe=xc+(rad-44)*cos(a) ! ye=yc+(rad-44)*sin(a)
    draw line xs,ys to xe,ye
    next a
  delta=(maxval-minval)/10 ! sval=minval
  draw font size 15
  for a=130 to 410 step 28
    xs=xc+(rad-25)*cos(a)-23 ! ys=yc+(rad-25)*sin(a)-10
    draw text n2a$(sval,3,0) at xs,ys
    sval=sval+delta
    next a
  draw font size 20
mode_0:
  fill circle xc,yc size rad-52
  xs=xc+(rad-40)*cos(123) ! ys=yc+(rad-40)*sin(123)
  set_col(col$)
  fill circle xc,yc size 10 ! draw text tit$ at xs,ys
  set_col("n")
  if val<minval then val=minval
  if val>maxval then val=maxval
  beta=130+280*(val-minval)/(maxval-minval)
  xs=xc-30*cos(beta) ! ys=yc-30*sin(beta)
  xe=xc+(rad-54)*cos(beta) ! ye=yc+(rad-54)*sin(beta)
  draw size 6 ! draw line xs,ys to xe,ye
graphics unlock
end def

def set_col(col$)
if col$="r" or col$="R" then
  draw color 1,0,0 ! fill color 1,0,0
  end if
if col$="g" or col$="G" then
  draw color 0,1,0 ! fill color 0,1,0
  end if
if col$="b" or col$="B" then
  draw color 0,0,1 ! fill color 0,0,1
  end if
if col$="n" or col$="N" then
  draw color 0,0,0 ! fill color .8,.8,.8
  end if
enddef

def put_mes(nmes,tt$,m$())
nmes=nmes+1 ! if nmes>60 then nmes=60
for k=nmes-1 to 1 step -1
  m$(k+1)=m$(k)
  next k
m$(1)=tt$
putmes=nmes
end def

def t2a$(t)
u=int(t/3600) ! t=t-3600*u
m=int(t/60) ! s=t-60*m
t2a$=u & "|" & m & "|" & s
end def

init_prog:
maxx=screen_width() ! maxy=screen_height()
graphics ! cls ! fill color .8,.8,.8 ! draw color 0,0,0
w_open("",5,620,maxx-5,maxy-5)
w_open("",5,400,maxx-5,615)
w_open("Instrument flight VOR/ILS",380,824,maxx-14,946)
gas=gas_bar(0,880,1)
att_panel(0,0,xy,0,0,1)
flaps_bar(0,760,1)
graphics lock
button "rev" title "rev" at 20,908 size 50,40
draw text "sim speed" at 540,630
button "sp-" title "/2" at 470,660 size 80,40
button "spo" title "live" at 560,660 size 80,60
button "sp+" title "x2" at 650,660 size 80,40
button "brk" title "BREAKS!" at 470,745 size 120,40
button "gear" title "L.GEAR!" at 610,745 size 120,40

for i=1 to 7
  button "b" & i title i at 442+38*i,910 size 29,29
  next i
draw size 4 ! draw circle 600,507 size 100
draw size 2 ! draw rect 190,420 to 260,440
draw text "alt" at 208,398
draw rect 185,587 to 265,607
draw text "climb" at 194,563
draw rect 436,425 to 506,445
draw text "heading" at 430,400
draw rect 426,587 to 516,608
draw text "time" at 447,563
draw size 3 ! draw rect 720,430 to 750,600
draw text "fuel" at 710,402
draw font size 15 ! draw size 2
draw text "airport ->" at 384,915
draw rect 436,875 to 506,895
draw text "bearing" at 438,855
draw rect 536,875 to 626,895
draw text "DME" at 565,855
draw rect 656,875 to 736,895
draw text "ILS on/off" at 650,855
draw font size 20
graphics unlock
rmeter("km/hr","r",110,507,98,0,500,0,1)
rmeter("meter","b",340,507,98,0,1000,0,1)
ttt=speed
reset timer
tekst()
return

def tekst()
draw text "This is a preview of my flightsim, it's about 2/3 finished." at 20,20
draw text "It can by far not compete with commercial app's like x-plane," at 20,40
draw text "but it's just fun to program a flightsim." at 20,60
draw text "The text here will be replaced by a (rather simple) scenery," at 20,80
draw text "wich must be designed carefully, because of the calculation " at 20,100
draw text "load imposed. So, without a scenery you can only make " at 20,120
draw text "IFR instrument flights." at 20,140
draw text "At the moment the flying area is a 100x100 km world," at 20,160
draw text "containing 7 airfields. The position (x and y km) and the" at 20,180
draw text "approach direction of the runway can be found in the DIM" at 20,200
draw text "statement." at 20,220
draw text "To start a flight, release the breaks and hit the throttle" at 20,250
draw text "At 100+ km/hr you can use the stick (pitch/bank) to" at 20,270
draw text "rotate the aircraft. At about 160 km/hr you can get airborne," at 20,290
draw text "and bank the craft to change the heading into the desired" at 20,310
draw text "destination. Change the simulation speed only during rather" at 20,330
draw text "stable flight conditions." at 20,350
draw text "Well, no more space for explanations for the time beiing." at 20,370
end def

Re: Preview flight simulator

Posted: Sun May 19, 2013 5:32 pm
by Mr. Kibernetik
This will be something great...

Re: Preview flight simulator

Posted: Sat Jun 14, 2014 10:36 am
by P92
Great job!!!. It includes a FPV (Flight path vector!!!). Really hard work. I like it!!!

Re: Preview flight simulator

Posted: Mon Jun 16, 2014 6:38 am
by Mr. Kibernetik
Probably when 3D graphics support will be available, your simulator will get new opportunities...

Re: Preview flight simulator

Posted: Mon Jun 16, 2014 9:13 am
by Henko
It surely would. What kind of 3d support are you thinking of? Translation/rotation/scaling of poly-planes in 3D?

Re: Preview flight simulator

Posted: Mon Jun 16, 2014 9:17 am
by Mr. Kibernetik
Actually we can discuss it.
But right now I can tell that 3D objects loading (something like .OBJ format) and its render in graphics view will be available.