parking_car

Post Reply
Joel
Posts: 57
Joined: Fri Jan 15, 2016 1:36 pm
My devices: miniipad
Flag: Germany

parking_car

Post by Joel »

Hi
Lately I began writing a funny little car-parking game. Unfortunately I am not longer interested in finishing it.
May be some of you enjoys making something out of it. That's why I left all the commentaries and testing routines in the code.
Bye, joel.

Code: Select all

/* COLORS used in code
'y': comments
'' :main progamm
'g':find rapidly the line
'b':sub-programms
'c':functions
'r':watch it!!special lines that might cause some trouble 
'm':DATA
'y'

***********************************
          **************
          *parking_car *
          **************

GLOBAL VARIABLES:
(t0+dlt_t=t1)
lgth(n_vh)  :length of vehicle
n_vh,:Number of vehicles created by create car()
p1_t0(n_vh,2) :front point of the car (steering axle) ON longitudinal axis of the car @ t0
p2_t0(n_vh,2) :rear point of the car (rear axle) ON longitudinal axis of the car @ t0
p1_t1(n_vh,2) :...@t1
p2_t1(n_vh,2) :...@t1
lmbd(n_vh)  :deflection ANGLE of the steering axle (NORMAL coordinates) (@t0)
 -lmbd=>TO the RIGHT
 +lmbd=>TO the LEFT
v_p1(n_vh)  :(NORMAL-)speed of the FRONT (steering) axle
dlt_t :self explaining
dlt_s(n_vh) : way made within dlt_t of steering axle (NORMAL direction)(=v_p1*dlt_t)
rho_t0(n_vh):ORIENTATION of the steering axle relative TO the system of coordinates)
phi_t0(n_vh):ORIENTATION of the car @ t0 (relative TO system of coordinates)
phi_t1(n_vh):ORIENTATION of the car @t1 (relative TO s.o.c.)
active_car:index number of vehicle to which controls refer
global variables used as arrays for example(n_vh,2) where (n_vh,1) represents  x- and (2)  y-coordinates

***********************************
*/
''
'==============
'MAIN
'==============
OPTION ANGLE DEGREES
OPTION BASE 1
test=1
DEBUG=0
'Dimensioning the global arrays
n_vh=3
DIM p1_t0(n_vh,2),p1_t1(n_vh,2), p2_t0(n_vh,2), p2_t1(n_vh,2),c_t0(n_vh,2)
DIM wdth(n_vh),lgth(n_vh),overh(n_vh),lmbd(n_vh),dlt_s(n_vh), v_p1(n_vh)
DIM rho_t0(n_vh),phi_t0(n_vh), phi_t1(n_vh)
DIM steer_max(n_vh), speed_max(n_vh)
DIM time_last(n_vh)

IF test THEN
'r'
 'REFRESH OFF
''
 GOSUB test_functions
ENDIF

END 'of MAIN

'b'
'==============
'SUB-PROGRAMS contain only code for testing the functions. test9 is the latest one
'==============

test_functions:
 'global vars for include lib
 
 ratio=2
 xmin=-SCREEN_WIDTH()/ratio
 xmax=SCREEN_WIDTH()/ratio
 ymin=-SCREEN_HEIGHT()/ratio
 ymax=SCREEN_HEIGHT()/ratio
 
 /*
 xmin=-100
 xmax=100
 ymin=-100
 ymax=100
 */
 test_function_loop1:
 GRAPHICS
 INPUT "testnumber? 9 is latest testnr.":test_nr
 ON test_nr GOSUB test1,test2, test3, test4, test5, test6, test7, test8, test9
 'GOTO return_of_test_function
  GOTO test_function_loop1
  
 test1:
  INPUT phi_t0(n_vh),lmbd(n_vh)
  set_rho_t0(n_vh)
  PRINT rho_t0(n_vh)
 RETURN 'of test1
 
 test2:
  INPUT v_p1(n_vh),dlt_t
  set_dlt_s(n_vh)
  PRINT dlt_s(n_vh)
 RETURN 'of test2
 
 test3:
  'set_p1_t1
  '->dlt_s
  '->rho
  '->p1_t0(n,2)
  'p1_t1->
  INPUT dlt_s(n_vh),rho_t0(n_vh),p1_t0(n_vh,1),p1_t0(n_vh,2)
  set_p1_t1(n_vh)
  'PRINT p1_t1(n_vh,1),p1_t1(n_vh,2)
  GRAPHICS
  DRAW CIRCLE tc_x(p1_t0(n_vh,1)),tc_y(p1_t0(n_vh,2)) SIZE 2
  DRAW CIRCLE tc_x(p1_t1(n_vh,1)),tc_y(p1_t1(n_vh,2)) SIZE 3
  
 RETURN 'of test3
  
 test4:
  'set_p2_t1
  '->dlt_s
  '->lmbd
  '->phi
  '->p2_t0
  'p2_t1->
  INPUT dlt_s(n_vh),lmbd(n_vh), phi_t0(n_vh), p2_t0(n_vh,1),p2_t0(n_vh,2)
  set_p2_t1(n_vh)
  GRAPHICS
  DRAW CIRCLE tc_x(p2_t0(n_vh,1)),tc_y(p2_t0(n_vh,2)) SIZE 1
  DRAW CIRCLE tc_x(p2_t1(n_vh,1)),tc_y(p2_t1(n_vh,2)) SIZE 4
 RETURN 'of test4
 
 test5:
  'set_phi_t1
  'PRINT phi_t1(n_vh)
  INPUT x,y
  PRINT ATAN2(y,x)
  INPUT weiter$
  GOTO test5
 RETURN 'of test 5
 
 test6:
  lgth(n_vh)=20
  phi_t0(n_vh)=0
  lmbd(n_vh)=10
  'rho
  set_rho_t0(n_vh)
  p2_t0(n_vh,1)=0!p2_t0(n_vh,2)=0
  'p1_t0
  set_p1_t0(n_vh) 'front axis
  DRAW CIRCLE tc_x(p2_t0(n_vh,1)),tc_y(p2_t0(n_vh,2)) SIZE 1 'rear axis
  DRAW CIRCLE tc_x(p1_t0(n_vh,1)),tc_y(p1_t0(n_vh,2)) SIZE 3 'front axis
  v_p1(n_vh)=10
  dlt_t=1
  set_dlt_s(n_vh)
  
  'p1_t1
  set_p1_t1(n_vh)
  DRAW CIRCLE tc_x(p1_t1(n_vh,1)),tc_y(p1_t1(n_vh,2)) SIZE 7 'front axis after dt
  set_p2_t1(n_vh)
  DRAW CIRCLE tc_x(p2_t1(n_vh,1)),tc_y(p2_t1(n_vh,2)) SIZE 5 'rear axis after dt
  set_phi_t1(n_vh)
  FOR n= 1 TO 2
  
  p2_t0(n_vh,n)=p2_t1(n_vh,n)
  phi_t0(n_vh)=phi_t1(n_vh)
  set_p1_t0(n_vh)
  NEXT n
  DRAW CIRCLE tc_x(p1_t0(n_vh,1)),tc_y(p1_t1(n_vh,2)) SIZE 9 'front axis after dt
 RETURN 'of test6
 'g'
 ''
 test7:
  GRAPHICS
  DRAW SIZE 3
  RESTORE TO steerbuttons
  FOR n= 1 TO 4
   READ t$
   BUTTON N TEXT T$ AT SCREEN_WIDTH()*.5+40*n,SCREEN_HEIGHT()*.9 SIZE 30,30
   BUTTON n SHOW
  NEXT n
  FIELD "speed" TEXT "" AT SCREEN_WIDTH()*.3,SCREEN_HEIGHT()*.9 SIZE 30,30 RO
  FIELD "steer" TEXT "" AT SCREEN_WIDTH()*.3+40,SCREEN_HEIGHT()*.9 SIZE 30,30 RO

  'global variables as shown above
  index=1
  lgth(n_vh)=40
  phi_t0(n_vh)=90
  lmbd(n_vh)=0
  v_p1(n_vh)=0
  dlt_t=1
  p2_t0(n_vh,1)=0!p2_t0(n_vh,2)=0
  'create_car(index, p2_t0_x, p2_t0_y, phi_t0,  car_type)',v_p1, lgth, wdth,lmbd, ) 
  
  'further globals not defined above
  wdth(n_vh)=lgth(n_vh)/1.7'width of vehicle
  
  'calculate sim
  test7_loop1:
  set_rho_t0(n_vh)
  set_p1_t0(n_vh)
  FIELD "speed" TEXT v_p1(n_vh)
  FIELD "steer" TEXT lmbd(n_vh)
  overh(n_vh)=lgth(n_vh)/5
  p3_x=p2_t0(n_vh,1)-overh(n_vh)*COS(phi_t0(n_vh))
  p3_y=p2_t0(n_vh,2)-overh(n_vh)*SIN(phi_t0(n_vh))
  p0_x=p1_t0(n_vh,1)+overh(n_vh)*COS(phi_t0(n_vh))
  p0_y=p1_t0(n_vh,2)+overh(n_vh)*SIN(phi_t0(n_vh))
  /*
  p2_rght_x=p2_t0(n_vh,1)+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_rght_y=p2_t0(n_vh,2)-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p2_left_x=p2_t0(n_vh,1)-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_left_y=p2_t0(n_vh,2)+wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_rght_x=p1_t0(n_vh,1)+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_rght_y=p1_t0(n_vh,2)-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_left_x=p1_t0(n_vh,1)-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_left_y=p1_t0(n_vh,2)+wdth(n_vh)/2*COS(phi_t0(n_vh))
  */
  p2_rght_x=p3_x+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_rght_y=p3_y-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p2_left_x=p3_x-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_left_y=p3_y+wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_rght_x=p0_x+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_rght_y=p0_y-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_left_x=p0_x-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_left_y=p0_y+wdth(n_vh)/2*COS(phi_t0(n_vh))
  
  
  GRAPHICS CLEAR 0,0,0
  DRAW LINE tc_x(p2_t0(n_vh,1)),tc_y(p2_t0(n_vh,2)) TO tc_x(p1_t0(n_vh,1)),tc_y(p1_t0(n_vh,2))
  DRAW LINE tc_x(p2_rght_x),tc_y(p2_rght_y) TO tc_x(p1_rght_x),tc_y(p1_rght_y)
  DRAW LINE TO tc_x(p1_left_x),tc_y(p1_left_y)
  DRAW LINE TO tc_x(p2_left_x),tc_y(p2_left_y)
  DRAW LINE TO tc_x(p2_rght_x),tc_y(p2_rght_y)
  FILL CIRCLE tc_x(p1_t0(n_vh,1)),tc_y(p1_t0(n_vh,2)) SIZE 3 'steer axis point
  IF DEBUG THEN FILL CIRCLE tc_x(0),tc_y(0) SIZE 5
  REFRESH
  
  set_dlt_s(n_vh)
  set_p1_t1(n_vh)
  set_p2_t1(n_vh)
  set_phi_t1(n_vh)
  
  'update variables
  FOR n=1 TO 2
   p2_t0(n_vh,n)=p2_t1(n_vh,n) 'rear axis
  NEXT n
  phi_t0(n_vh)=phi_t1(n_vh) 'direction of vehicle
  
  'check buttons
  status_button=0
  FOR n= 1 TO 4
   IF BUTTON_PRESSED (STR$(n))THEN 
    status_button=n
   ENDIF 
  NEXT n
  IF status_button<>0 THEN
   ON  status_button GOSUB test7_label1,test7_label2,test7_label3,test7_label4
  ENDIF
  
  GOTO test7_loop1
  
  test7_label1:
   v_p1(n_vh)=v_p1(n_vh)+1
  RETURN
  
  test7_label2:
   v_p1(n_vh)=v_p1(n_vh)-1
  RETURN
  
  test7_label3:
   lmbd(n_vh)=lmbd(n_vh)-10
  RETURN
  
  test7_label4:
   lmbd(n_vh)=lmbd(n_vh)+10
  RETURN

  steerbuttons:
  DATA "^", "v", "<-", "->" 
 
 RETURN  'of test 7

test8:
  OPTION TEXT POS CENTRAL
  OPTION SPRITE POS CENTRAL
  GRAPHICS
  DRAW SIZE 3
  SLIDER "speed" VALUE .5 AT SCREEN_WIDTH()*.1,SCREEN_HEIGHT()*.9 SIZE 300 ANGLE -90
  SLIDER "speed" SHOW 
  SLIDER "steer" VALUE .5 AT SCREEN_WIDTH()-300,SCREEN_HEIGHT()*.9 SIZE 300
  SLIDER "steer" SHOW
  
  FIELD "speed" TEXT "" AT SCREEN_WIDTH()*.1-15,SCREEN_HEIGHT()*.9-300 SIZE 30,30 RO
  FIELD "steer" TEXT "" AT SCREEN_WIDTH()-150-15,SCREEN_HEIGHT()*.9-50 SIZE 30,30 RO
  IF DEBUG THEN FIELD "debug" TEXT "" AT SCREEN_WIDTH()*.1+15,SCREEN_HEIGHT()*.9-100 SIZE SCREEN_WIDTH()-200,50
  
  'global variables as shown above
   index=1
   IF DEBUG THEN
    dlt_t=1
   ELSE 
    dlt_t=.3
   ENDIF 
  IF DEBUG THEN
   speed_max(n_vh)=10
   steer_max(n_vh)=50
   lgth(n_vh)=80
   wdth(n_vh)=lgth(n_vh)/1.7'width of vehicle
   overh(n_vh)=lgth(n_vh)/3
   phi_t0(n_vh)=20
   lmbd(n_vh)=0
   v_p1(n_vh)=0
   
   'p2_t0(n_vh,1)=0
   'p2_t0(n_vh,2)=0
  ENDIF
  
  create_car(index, 0, 0, 80, 80/1.7,80/3,20) '(index, p2_t0_x, p2_t0_y, lgth, wdth, overh,phi_t0)
  draw_sprite(1)
  SPRITE 1 SHOW
  
  IF DEBUG THEN
   FILL COLOR 1,0,0
   FILL CIRCLE tc_x(0),tc_y(0) SIZE 4
  ENDIF
  'calculate sim LOOOOOOOP
  test8_loop1:
  set_rho_t0(index) !IF DEBUG THEN set_rho_t0(n_vh)
  set_p1_t0(index) ! IF DEBUG THEN set_p1_t0(n_vh)
  FIELD "speed" TEXT INT(v_p1(index))
  FIELD "steer" TEXT INT(lmbd(index))
  IF DEBUG THEN FIELD "debug" TEXT TOUCH_X(0)
  IF DEBUG THEN
   p3_x=p2_t0(n_vh,1)-overh(n_vh)*COS(phi_t0(n_vh))
   p3_y=p2_t0(n_vh,2)-overh(n_vh)*SIN(phi_t0(n_vh))
   p0_x=p1_t0(n_vh,1)+overh(n_vh)*COS(phi_t0(n_vh))
   p0_y=p1_t0(n_vh,2)+overh(n_vh)*SIN(phi_t0(n_vh))
  ENDIF
  /*->
  p2_rght_x=p2_t0(n_vh,1)+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_rght_y=p2_t0(n_vh,2)-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p2_left_x=p2_t0(n_vh,1)-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p2_left_y=p2_t0(n_vh,2)+wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_rght_x=p1_t0(n_vh,1)+wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_rght_y=p1_t0(n_vh,2)-wdth(n_vh)/2*COS(phi_t0(n_vh))
  p1_left_x=p1_t0(n_vh,1)-wdth(n_vh)/2*SIN(phi_t0(n_vh))
  p1_left_y=p1_t0(n_vh,2)+wdth(n_vh)/2*COS(phi_t0(n_vh))
  */
  IF DEBUG THEN
   p2_rght_x=p3_x+wdth(n_vh)/2*SIN(phi_t0(n_vh))
   p2_rght_y=p3_y-wdth(n_vh)/2*COS(phi_t0(n_vh))
   p2_left_x=p3_x-wdth(n_vh)/2*SIN(phi_t0(n_vh))
   p2_left_y=p3_y+wdth(n_vh)/2*COS(phi_t0(n_vh))
   p1_rght_x=p0_x+wdth(n_vh)/2*SIN(phi_t0(n_vh))
   p1_rght_y=p0_y-wdth(n_vh)/2*COS(phi_t0(n_vh))
   p1_left_x=p0_x-wdth(n_vh)/2*SIN(phi_t0(n_vh))
   p1_left_y=p0_y+wdth(n_vh)/2*COS(phi_t0(n_vh))
  
  
   GRAPHICS CLEAR 0,0,0
   DRAW LINE tc_x(p2_t0(n_vh,1)),tc_y(p2_t0(n_vh,2)) TO tc_x(p1_t0(n_vh,1)),tc_y(p1_t0(n_vh,2))
   DRAW LINE tc_x(p2_rght_x),tc_y(p2_rght_y) TO tc_x(p1_rght_x),tc_y(p1_rght_y)
   DRAW LINE TO tc_x(p1_left_x),tc_y(p1_left_y)
   DRAW LINE TO tc_x(p2_left_x),tc_y(p2_left_y)
   DRAW LINE TO tc_x(p2_rght_x),tc_y(p2_rght_y)
   REFRESH
  ENDIF 'debug
  draw_sprite(1)
  
  set_dlt_s(index)
  set_p1_t1(index)
  set_p2_t1(index)
  set_phi_t1(index)
  IF DEBUG THEN
   set_dlt_s(n_vh)
   set_p1_t1(n_vh)
   set_p2_t1(n_vh)
   set_phi_t1(n_vh)
  ENDIF 'debug
 
  'update variables
  FOR n=1 TO 2
   'rear axis
   p2_t0(index,n)=p2_t1(index,n) ! IF DEBUG THEN p2_t0(n_vh,n)=p2_t1(n_vh,n)
  NEXT n
  'direction of vehicle
  phi_t0(index)=phi_t1(index) ! IF DEBUG THEN phi_t0(n_vh)=phi_t1(n_vh) 
  
  'check buttons
  
  IF SLIDER_CHANGED("steer") OR SLIDER_CHANGED("speed") THEN
   v_p1(index)=(SLIDER_VALUE("speed")-0.5)*2*speed_max(index)
   lmbd(index)=(SLIDER_VALUE("steer")-0.5)*2*steer_max(index)
   IF DEBUG THEN
    v_p1(n_vh)=(SLIDER_VALUE("speed")-0.5)*2*speed_max(n_vh)
    lmbd(n_vh)=(SLIDER_VALUE("steer")-0.5)*2*steer_max(n_vh)
   ENDIF
   
  ENDIF
  
  IF TOUCH_X(0)<>-1 THEN
   SLIDER ("speed") VALUE 0.5
   SLIDER ("steer") VALUE 0.5
   v_p1(index)=0
   lmbd(index)=0
   IF DEBUG THEN
    v_p1(n_vh)=0
    lmbd(n_vh)=0
   ENDIF 

  ENDIF
   
  GOTO test8_loop1
 
 RETURN  'of test 8 
'g'
test9:
  OPTION TEXT POS CENTRAL
  OPTION SPRITE POS CENTRAL
  GRAPHICS
  DRAW SIZE 3
  SLIDER "speed" VALUE .5 AT SCREEN_WIDTH()*.1,SCREEN_HEIGHT()*.9 SIZE 300 ANGLE -90
  SLIDER "speed" SHOW 
  SLIDER "steer" VALUE .5 AT SCREEN_WIDTH()-300,SCREEN_HEIGHT()*.9 SIZE 300
  SLIDER "steer" SHOW
  
  FIELD "speed" TEXT "" AT SCREEN_WIDTH()*.1-20,SCREEN_HEIGHT()*.9-300 SIZE 40,30 RO
  FIELD "steer" TEXT "" AT SCREEN_WIDTH()-150-20,SCREEN_HEIGHT()*.9-50 SIZE 40,30 RO
  IF DEBUG THEN FIELD "debug" TEXT "" AT SCREEN_WIDTH()*.1+15,SCREEN_HEIGHT()*.9-100 SIZE SCREEN_WIDTH()-200,50
  
  'global variables as shown above
   'index=1
   active_car=1
   num_cars=3  
  
  'creating three cars
  
  create_car(1, 0, 0, 80, 80/1.7,80/3,20) '(index, p2_t0_x, p2_t0_y, lgth, wdth, overh,phi_t0)
  draw_sprite(1)
  SPRITE 1 SHOW
  
  create_car(2, 50, 50, 60, 40,80/4,40) '(index, p2_t0_x, p2_t0_y, lgth, wdth, overh,phi_t0)
  draw_sprite(2)
  SPRITE 2 SHOW
  
  create_car(3, 190, 190, 80, 80/1.7,80/3,20) '(index, p2_t0_x, p2_t0_y, lgth, wdth, overh,phi_t0)
  draw_sprite(3)
  SPRITE 3 SHOW
  
   'dlt_t=.01
  
  IF DEBUG THEN
   FILL COLOR 1,0,0
   FILL CIRCLE tc_x(0),tc_y(0) SIZE 4
  ENDIF
  
  'calculate sim LOOOOOOOP
  test9_loop1:
  
  FOR index= 1 TO num_cars
   set_rho_t0(index) 
   set_p1_t0(index) 
  NEXT index 
  
  
   FIELD "speed" TEXT INT(v_p1(active_car))
   FIELD "steer" TEXT INT(lmbd(active_car))
  'IF DEBUG THEN FIELD "debug" TEXT p2_t0(1,1)&" / "&p2_t0(1,2)&"---"&p2_t0(2,1)&" / "&p2_t0(2,2)
  IF DEBUG THEN FIELD "debug" TEXT btc_x(TOUCH_X(0))&" / "&btc_y(TOUCH_Y(0))
  
  FOR index = 1 TO num_cars
   draw_sprite(index)
   
   set_dlt_s(index)
   set_p1_t1(index)
   set_p2_t1(index)
   set_phi_t1(index)
  NEXT index
  
  'update variables
  FOR index=1 TO num_cars
   FOR n=1 TO 2
    'rear axis
    p2_t0(index,n)=p2_t1(index,n)
   NEXT n
  NEXT index
  
  FOR index= 1 TO num_cars
   'direction of vehicle
   phi_t0(index)=phi_t1(index) 
  NEXT index
  'check buttons
  
  IF SLIDER_CHANGED("steer") OR SLIDER_CHANGED("speed") THEN
   'FOR index=1 TO num_cars
    v_p1(active_car)=(SLIDER_VALUE("speed")-0.5)*2*speed_max(active_car)
    lmbd(active_car)=(SLIDER_VALUE("steer")-0.5)*2*steer_max(active_car)
   'NEXT index
  ENDIF
  touch_x0=TOUCH_X(0) ! touch_y0=TOUCH_Y(0)
  IF touch_x0<>-1 THEN
   SLIDER ("speed") VALUE 0.5
   SLIDER ("steer") VALUE 0.5
   FOR index = 1 TO num_cars
    v_p1(index)=0
    lmbd(index)=0
   NEXT index
   set_active_car(touch_x0,touch_y0)
   ENDIF
   
  GOTO test9_loop1
 
 RETURN  'of test 9 

return_of_test_function:
RETURN 'of test_functions

'c'
/*
==============
FUNCTIONS
==============
'y'
set_rho_t0(n_vh) 
using global variables:
.phi_t0
.lmbd(n_vh)

set_p1_t1()
using global variables:
.dlt_s
.rho
.p1_t0

set_p2_t1()
using global variables:
.dlt_s
.lmbd(n_vh)
.p2_t0
get_phi(p1,p2) valid AT any TIME

set_c_t0(n_vh) center of vehicle

get_p1()
using global variables:
.p2
.phi
.lgth(n_vh)
==============
*/
'c'


DEF set_rho_t0(n_vh)
 .rho_t0(n_vh)=.phi_t0(n_vh) - .lmbd(n_vh)
END DEF
'=================
DEF set_dlt_s(n_vh)
  time_gap=TIME()-.time_last(n_vh) 'TIME() helps to smoothen the movement 
 '.dlt_s(n_vh)=.v_p1(n_vh)*.dlt_t
 .dlt_s(n_vh)=.v_p1(n_vh)*time_gap'.dlt_t instead of time_gap dlt_t makes movement unsteady
 .time_last(n_vh)=TIME()
END DEF  
'=================
DEF set_p1_t1(n_vh)
 ds_x=.dlt_s(n_vh)*COS(.rho_t0(n_vh))
 ds_y=.dlt_s(n_vh)*SIN(.rho_t0(n_vh))
 .p1_t1(n_vh,1)=ds_x+.p1_t0(n_vh,1)
 .p1_t1(n_vh,2)=ds_y+.p1_t0(n_vh,2)
END DEF
'=================
DEF set_p2_t1(n_vh)
 dlt_sn=.dlt_s(n_vh)*COS(.lmbd(n_vh))
 .p2_t1(n_vh,1)=.p2_t0(n_vh,1)+dlt_sn*COS(.phi_t0(n_vh))
 .p2_t1(n_vh,2)=.p2_t0(n_vh,2)+dlt_sn*SIN(.phi_t0(n_vh))
END DEF
'================= 
DEF set_p1_t0(n_vh)
 .p1_t0(n_vh,1)=.p2_t0(n_vh,1)+.lgth(n_vh)*COS(.phi_t0(n_vh)) 
 .p1_t0(n_vh,2)=.p2_t0(n_vh,2)+.lgth(n_vh)*SIN(.phi_t0(n_vh))
END DEF
'=================
DEF draw_sprite(index)
 sprite_x=.p2_t0(index,1)+.lgth(index)/2*COS(.phi_t0(index))
 sprite_y=.p2_t0(index,2)+.lgth(index)/2*SIN(.phi_t0(index))
 SPRITE index AT tc_x(sprite_x),tc_y(sprite_y) ANGLE -.phi_t0(index)
 END DEF

'=================
DEF set_phi_t1(index)
 .phi_t1(index)=ATAN2(.p1_t1(index,2)-.p2_t1(index,2),.p1_t1(index,1)-.p2_t1(index,1))
END DEF
'=================
DEF create_car(index, p2_t0_x, p2_t0_y, lgth, wdth, overh,phi_t0)
  .speed_max(index)=400
  .steer_max(index)=50
  .lgth(index)=lgth
  .wdth(index)=wdth'width of vehicle
  .overh(index)=overh
  .phi_t0(index)=0
  .lmbd(index)=0
  .v_p1(index)=0
  .p2_t0(index,1)=.xmin+overh
  .p2_t0(index,2)=.ymax-wdth/2
  
  set_p1_t0(index)
  
  p3_x=.p2_t0(index,1)-.overh(index)*COS(.phi_t0(index))
  p3_y=.p2_t0(index,2)-.overh(index)*SIN(.phi_t0(index))
  p0_x=.p1_t0(index,1)+.overh(index)*COS(.phi_t0(index))
  p0_y=.p1_t0(index,2)+.overh(index)*SIN(.phi_t0(index))
  
  p2_rght_x=p3_x+.wdth(index)/2*SIN(.phi_t0(index))
  p2_rght_y=p3_y-.wdth(index)/2*COS(.phi_t0(index))
  p2_left_x=p3_x-.wdth(index)/2*SIN(.phi_t0(index))
  p2_left_y=p3_y+.wdth(index)/2*COS(.phi_t0(index))
  p1_rght_x=p0_x+.wdth(index)/2*SIN(.phi_t0(index))
  p1_rght_y=p0_y-.wdth(index)/2*COS(.phi_t0(index))
  p1_left_x=p0_x-.wdth(index)/2*SIN(.phi_t0(index))
  p1_left_y=p0_y+.wdth(index)/2*COS(.phi_t0(index))
  
  SPRITE index BEGIN tc_x(.p1_t0(index,1)+overh),tc_y(.ymax-wdth)
   'GRAPHICS CLEAR 0,0,0
   DRAW LINE tc_x(.p2_t0(index,1)),tc_y(.p2_t0(index,2)) TO tc_x(.p1_t0(index,1)),tc_y(.p1_t0(index,2))
   DRAW LINE tc_x(p2_rght_x),tc_y(p2_rght_y) TO tc_x(p1_rght_x),tc_y(p1_rght_y)
   DRAW LINE TO tc_x(p1_left_x),tc_y(p1_left_y)
   DRAW LINE TO tc_x(p2_left_x),tc_y(p2_left_y)
   DRAW LINE TO tc_x(p2_rght_x),tc_y(p2_rght_y)
   FILL CIRCLE tc_x(.p1_t0(index,1)),tc_y(.p1_t0(index,2)) SIZE 3
  SPRITE END
  IF DEBUG THEN DRAW RECT 0,0 TO tc_x(.p1_t0(index,1)+overh),tc_y(.ymax-wdth)
  
  
  .phi_t0(index)=phi_t0
  .p2_t0(index,1)=p2_t0_x ! .p2_t0(index,2)=p2_t0_x
  
END DEF
'=================
DEF set_active_car(touch_x0,touch_y0)
 min_dist=SQR((.xmax-.xmin)*(.xmax-.xmin)+(.ymax-.ymin)*(.ymax-.ymin))
 touch_btx=btc_x(touch_x0) ! touch_bty=btc_y(touch_y0)
 index=1
 FOR n=1 TO .num_cars
  center_car_x=.p2_t0(n,1)+.lgth(n)/2*COS(.phi_t0(n))
  center_car_y=.p2_t0(n,2)+.lgth(n)/2*SIN(.phi_t0(n))
  curr_dist=SQR((touch_btx-center_car_x)*(touch_btx-center_car_x)+(touch_bty-center_car_y)*(touch_bty-center_car_y))
  'DEBUG PAUSE
  IF curr_dist<min_dist THEN 
   min_dist=curr_dist
   index=n
  ENDIF  
 NEXT n
 .active_car=index
END DEF
'=================


'***********exerpt from my library: trans_coord***************
/*
'c'-description
'y'-testing
'b'-definitions
''

'c'
returns the x- and y-value of a point on the ipad-screen. Basis is a defined coordinate-system given by xmin, xmax, ymin, ymax 

===========
global Variables
===========
xmin:smallest number of x-range within new coordinate system
xmax:biggest number of x-range
ymin:smallest number of y-range
ymax:biggest number of y-range

by omitting one pair (e.g.:ymin,ymax) those values are then calculated with respect to screen ratio
==========
functions
==========

coord_trans():

returns a list of all available functions
----------

tc_x(x):

returns the x-parameter of ipad-screen. usable with DRAW LINE etc. 
When using PIXEL then call function as:2*tc_x(x) etc.
sizes like width or height must be treated like : SIZE tc_x(10)-tc_x(0) which 
corresponds to SIZE 10 

x: x-value within defined coordinate-system
----------

btc_x(x):

returns x-parameter of the defined coordinate-system
x: x-value of the screen
----------

tc_y(y):

analogue
----------

btc_y(y):

analogue
----------

draw_sys():

draws the two axis of the defined coordinate-system
----------

draw_ticks(tick_x,tick_y):

draws ticks in a defined coordinate-system
tick_x: x-value of interval for each tick
tick_y: analogue
----------

draw_grid(dx,dy):

draws a grid in a defined coordinate-system
dx: x-value of interval
dy: analogue
----------

get_unit_x():

returns the smallest element on x-scale in respect of displayable points (DRAW-command etc. NOT PIXEL
----------
 
get_unit_y():

analogue
----------

get_ratio():

returns ratio: dy/dx
''
*/

/*
'y'
'testing
OPTION ANGLE DEGREES
GRAPHICS CLEAR 1,1,1
GRAPHICS
ratio =5
xmin=-SCREEN_WIDTH()/ratio
xmax=SCREEN_WIDTH()/ratio
ymin=-SCREEN_HEIGHT()/ratio
ymax=SCREEN_HEIGHT()/ratio

'DRAW_ticks(10,10)

REFRESH OFF
DRAW COLOR 0.9,0.8,1
DRAW SIZE 1
draw_grid(10,10)
REFRESH ON

'csys
SHADOW ON
SHADOW OFFSET 2,2
SHADOW BLUR 2
DRAW SIZE 3
DRAW DASH 10
DRAW COLOR 0,0,0
draw_sys

'graph
SHADOW OFFSET 4,4
SHADOW BLUR 4
DRAW SIZE 5
DRAW COLOR 1,0,0
DRAW TO -10,-10
FOR x= xmin TO xmax STEP 1
 DRAW LINE TO tc_x(x),tc_y(50*SIN(x))
NEXT x
draw_ticks(90,10)

LOOP:
SLOWDOWN
PAUSE 1
'GOTO LOOP
TEXT
coord_trans
PRINT "xmin= ";xmin
PRINT "xmax= ";xmax
PRINT "xmin= ";ymin
PRINT "ymax= ";ymax
PRINT "get_ratio()";
get_ratio
PRINT "Screen_height()/screen_width()";SCREEN_HEIGHT()/SCREEN_WIDTH()

PRINT "get_unit_x: ";
get_unit_x
PRINT "get_unit_y: ";
get_unit_y
INPUT x
PRINT "tc_x(x)= ";tc_x(x)
tx=tc_x(x)
PRINT "btc_x(x)= ";btc_x(tx) 
GOTO LOOP
''
*/
'b'
DEF coord_trans
 WHILE DATA_EXIST()
  READ t$
  PRINT t$
 ENDWHILE

DATA "tc_x(x)","tc_y(y)","btc_x(x)","btc_y(y)","draw_sys()","draw_ticks(dx,dy)","draw_grid(dx,dy)","get_unit_x","get_unit_y","get_ratio()"
END DEF  
'=================
DEF tc_x(x)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 mx=SCREEN_WIDTH() /(.xmax-.xmin)
 x0=(-1)*.xmin*mx
 tc_x=x*mx+x0
END DEF
'=================
DEF tc_y(y)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF
 my=SCREEN_HEIGHT() / (.ymin-.ymax)
 y0=(-1)*.ymax*my
 tc_y=y*my+y0
END DEF
'=================
DEF btc_x(x)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 mx=SCREEN_WIDTH() /(.xmax-.xmin)
 x0=(-1)*.xmin*mx
 btc_x=(x-x0)/mx
END DEF 
'=================
DEF btc_y(y)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF
 my=SCREEN_HEIGHT() / (.ymin-.ymax)
 y0=(-1)*.ymax*my
 btc_y=(y-y0)/my
END DEF
'=================
DEF draw_sys()
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF
 DRAW LINE tc_x(.xmin),tc_y(0) TO  tc_x(.xmax),tc_y(0)
 DRAW LINE tc_x(0),tc_y(.ymin) TO tc_x(0),tc_y(.ymax)
END DEF 
'================= 
DEF draw_ticks(tick_x,tick_y)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 FOR n=tick_x TO .xmax STEP tick_x
  DRAW LINE tc_x(n),tc_y(0)-5 TO tc_x(n), tc_y(0)+5
 NEXT n
 
 FOR n= -tick_x TO .xmin STEP -tick_x
  DRAW LINE tc_x(n),tc_y(0)-5 TO tc_x(n), tc_y(0)+5 
 NEXT n
 
 
 FOR n=tick_y TO .yMAX STEP tick_y 
  DRAW LINE tc_x(0)-5,tc_y(n) TO tc_x(0)+5, tc_y(n)
 NEXT n
 
 FOR n=-tick_y TO .ymin STEP -tick_y
  DRAW LINE tc_x(0)-5,tc_y(n) TO tc_x(0)+5, tc_y(n)
 NEXT n
END DEF 
'=================
DEF draw_grid(d_x, d_y)
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF
 
 FOR x=d_x TO .xmax STEP d_x
  DRAW LINE tc_x(x),tc_y(.ymin) TO tc_x(x), tc_y(.ymax)
 NEXT x 
 
 FOR x=d_x TO .xmin STEP -d_x
  DRAW LINE tc_x(x),tc_y(.ymin) TO tc_x(x), tc_y(.ymax)
 NEXT x
 
 FOR y=d_y TO .ymax STEP d_y
  DRAW LINE tc_x(.xmin),tc_y(y) TO tc_x(.xmax),tc_y(y)
 NEXT y 
 
 FOR y=d_y TO .ymin STEP -d_y
  DRAW LINE tc_x(.xmin),tc_y(y) TO tc_x(.xmax),tc_y(y)
 NEXT y 
 
END DEF
'=================
DEF get_unit_x
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 get_unit_x= (.xmax-.xmin)/SCREEN_WIDTH() 
END DEF
'=================
DEF get_unit_y
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 get_unit_y= (.ymax-.ymin)/SCREEN_HEIGHT()
END DEF 
'=================
DEF get_ratio
 IF .ymin=0 AND .ymax=0 THEN 
  .ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
 ENDIF
 IF .xmin=0 AND .xmax=0 THEN 
  .xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
 ENDIF 
 get_ratio= (.ymax-.ymin)/(.xmax-.xmin)
END DEF
 
''

Post Reply