home *** CD-ROM | disk | FTP | other *** search
/ Amoszine 4 / Amoszine 4 (Disk 1 of 3).adf / READERS_SOURCE.LHA / READERS_SOURCE / Paul_Overy's_code / find_angle.AMOS / find_angle.amosSourceCode
Encoding:
AMOS Source Code  |  1992-02-26  |  3.5 KB  |  130 lines

  1. 'Program: I put a question to the Amoszine #2 (Lee Bamber), his
  2. '         answere is in that issue.
  3. '  Bloke: Paul Overy   
  4. '
  5. '         By the way Lee, are you sure about saying:-  
  6. '         "If you can't use floating point number systems, you 
  7. '          can rule out the use of the Amos commands that will do
  8. '          this in an instant."    
  9. '               =============
  10. '          
  11. '  Floats are painfully slow, I only use them when intialising a program,
  12. '  even then I save them to disk so I only have to do it once. 
  13. '  There also the new binary integer arithmetic routines for fast trig.      
  14. '  
  15. '  This is my own answer to my own question, is hungry on memory but     
  16. '  its very fast! (yes its a look up table)  
  17. '
  18. '  By the way... This is my engine for my future coming game Bolt Thrower. 
  19. '
  20. Dim PX(72),PY(72)
  21. Global PX(),PY(),_MOUSE_X,_MOUSE_Y,CX,CY,MX,MY,FRAME,X,Y,_START10
  22. '
  23. 'Look of data store in Bank 10 
  24. Proc _TO_BE_DELETED
  25. Proc _SET_UP
  26. '
  27. _START10=Start(10) : X=100*512 : Y=100*512
  28. '
  29. Do 
  30.    _MOUSE_X=X Screen(X Mouse)
  31.    _MOUSE_Y=Y Screen(Y Mouse)
  32.    Sprite 2,X Hard(CX),Y Hard(CY),FRAME
  33.    Proc _TEST_MOUSE
  34.    Proc _CALC_POSITION
  35.    Wait Vbl 
  36. Loop 
  37. '
  38. Procedure _TEST_MOUSE
  39.    If Mouse Key
  40.       Add MX,PX(FRAME) : Add MY,PY(FRAME)
  41.    End If 
  42. End Proc
  43. Procedure _CALC_POSITION
  44.    Proc _FIND_ANGLE[_MOUSE_X,_MOUSE_Y,CX,CY]
  45.    TURN=FRAME-Param
  46.    'find & turn in the quickest direction.  
  47.    If Abs(TURN)>36
  48.       Add FRAME,Sgn(TURN),1 To 72
  49.    Else 
  50.       Add FRAME,-Sgn(TURN),1 To 72
  51.    End If 
  52.    CX=X/512 : CY=Y/512
  53.    ' Make some false gravity  
  54.    If Abs(MX)>32 or Abs(MY)>32
  55.       LX=MX/32
  56.       LY=MY/32
  57.    Else 
  58.       LX=MX
  59.       LY=MY
  60.       If Abs(MX)>1 or Abs(MY)>1
  61.          LX=0 : MX=0
  62.          LY=0 : MY=0
  63.       End If 
  64.    End If 
  65.    'move
  66.    Add X,MX : Add Y,MY
  67.    'slow step speed 
  68.    Add MX,-LX : Add MY,-LY
  69. End Proc
  70. Procedure _FIND_ANGLE[X1,Y1,X2,Y2]
  71.    '
  72.    X3=X1-X2 : Y3=Y2-Y1
  73.    ' Re-scale ships off screen to fit look up data  
  74.    While Abs(X3)>320 or Abs(Y3)>224
  75.       X3=X3/2 : Y3=Y3/2
  76.    Wend 
  77.    'Flip data to fit correct axis.
  78.    If X3>0
  79.       If Y3>=0
  80.          ANG=Peek(Y3*320+X3+_START10)
  81.       Else 
  82.          ANG=37-Peek(-Y3*320+X3+_START10)
  83.       End If 
  84.    Else 
  85.       If Y3>=0
  86.          ANG=73-Peek(Y3*320-X3+_START10)
  87.       Else 
  88.          ANG=35+Peek(-Y3*320-X3+_START10)
  89.       End If 
  90.    End If 
  91.    Centre Str$(ANG)
  92. End Proc[ANG]
  93. Procedure _SET_UP
  94.    Screen Open 0,320,200,4,Lowres : Flash Off : Curs Off : Cls 0
  95.    Change Mouse 2
  96.    K=19
  97.    For N#=0.0 To 2.0*Pi# Step Pi#/36.0
  98.       PX(K)=Cos(N#)*64 : PY(K)=Sin(N#)*64
  99.       Draw PX(K)/4+100,PY(K)/4+100 To -PX(K)/4+100,-PY(K)/4+100
  100.       Bar PX(K)/4+98,PY(K)/4+98 To PX(K)/4+102,PY(K)/4+102
  101.       Get Bob K,80,80 To 80+48,80+48 : Cls 0
  102.       Hot Spot K,24,24
  103.       Exit If K=18
  104.       Add K,1,1 To 72
  105.    Next N#
  106.    For K=16 To 31 : Colour K,$FFF : Next K
  107. End Proc
  108. '
  109. Procedure _TO_BE_DELETED
  110.    ' For GENERATING LOOK UP Data For FINDING ANGLES 
  111.    Text 0,100,"This only needs to be done once."
  112.    Degree 
  113.    Erase 10 : Reserve As Data 10,320*200
  114.    ADR=Start(10)
  115.    For Y=0 To 199
  116.       Text 0,Text Base,"Wait..."+Str$(199-Y)+"  "
  117.       For X=0 To 319
  118.          X#=X : Y#=Y
  119.          If Y<>0 : Rem check for devide by 0.             
  120.             PRE_ANG=Atan(X#/Y#)
  121.          Else 
  122.             PRE_ANG=90
  123.          End If 
  124.          ANG=PRE_ANG/5 : Rem needs word storage,cut down to 1-72 byte size  
  125.          Add ANG,1,1 To 72 : Rem Bob frame can't start at zero. 
  126.          Poke ADR,ANG : Inc ADR
  127.       Next X
  128.    Next Y
  129.    Radian 
  130. End Proc