Challenge

Community Forums/Showcase/Challenge

NewtSoup(Posted 2003) [#1]
I got bored again and inspired by the comic antics in the warthog and warthog revisited halo movies I decided to look into a simple real-time physics engine. This little proggy simulates a point mass (m) being accelerated upwards by force (f) for time (t) the resultant force on the object is show on the screen by R, I am sure you will work the rest of the data out. Somewhere along the way I turned it into a very simple game whereby you select the launch parameters and then try to land the thing again with a velocity of less than 1 meter per second. Then I thought well what's the perfect case scenario? so I had a quick attempt at crowbaring in an Auto Pilot. It works for the default settings and any coefficient of friction less than zero. However it fails if the friction is zero or the thrust time is greater than 2 seconds.

Now here's the challenge:
Can someone write an autopilot (lines 56 - 62) such that it can land succesfully for all combinations of uF thrust, burn time and mass.

A positive value of uF may be ignored as that would accelerate the craft in the direction its going :)

No cheating by changing any of the parameters set at launch time. You may however record the ships state (velocity height etc at any time for use in the auto pilot later - I already check the height at which the primary burn finished)
and you may attenuate the ships thrust to any value between the set launch parameter and zero.

now here's the code:
I included the keyconstants so the code was cut'n'paste friendly but at home here I include it as a seperate file.

;Include "key_constants.bb"
Const G#=-9.81 ;m/s/s
Const LANDED=1
Const BURNING=2
Const FLYING=3
Const CRASHED=4
friction#=-0.7
thrust#=1000 ;N
mass=25 ;Kg

thrust_time#=2 ;seconds
start_time=MilliSecs()
last_time=start_time
this_time=start_time
v#=0
u#=0
s#=0
Local df[5]
Local dft#[3]
df[0]=1
df[1]=5
df[2]=10
df[3]=50
df[4]=100
df[5]=1000
dft[0]=0.01
dft[1]=0.1
dft[2]=1
dft[3]=10
fi=4
ti=2
height#=0
f#=0
edit_m=0

auto = 0
thrust_height=0
Local status=LANDED
Graphics 800,600,32,2
SetBuffer BackBuffer()
SetFont LoadFont("verdana",15,1)
While Not KeyHit(k_escape)
Cls
last_time=this_time
this_time=MilliSecs()-start_time
u#=v#
If KeyHit(k_a) auto=1-auto
period#=Float(this_time-last_time)/1000
If (launch_time>0)
	If (this_time>launch_time+(thrust_time*1000)And status=BURNING)f#=0:status=FLYING
	If (Not auto) And (status=FLYING)
		f=0
		If KeyDown(k_z) f=thrust
		If KeyDown(k_x) f=thrust/2
		If KeyDown(k_c) f=thrust/4
	End If 
	If auto And status=FLYING  And height<=thrust_height
		If v<-height And  v<-1  Then
			f=thrust
		Else
			f=-mass*g
		End If
	End If
	
	a#=(f#+v*friction+mass*g)/mass
	v#=u#+a#*period
	s#=u*period+0.5*a*period*period
	height#=height#+(s)
	If height#>max# Then  max=height#
	If v>max_v# Then
		max_v=v
		If height>thrust_height thrust_height=height
	End If
	flight_time#=Float(this_time-launch_time)/1000
	If (height#<=0) And (status=flying)
		height=0
		s=0
		launch_time=0 
		f=0
		If Abs(v)<1 Then status=LANDED
		If Abs(v)>1 Then status=CRASHED	
		auto=0;	
		thrust_height=0
	End If
		
	
End If
	
Color 70,190,80
Rect 0,590,800,10,1
Color 220,90,80
Rect 398,584-height,5,6,1
Rect 399,581-height,3,3,1
Rect 400,579-height,1,2,1
If f>0
	Color 250,250,0
	Rect 399,588-height+2,3,3
	Color 400,200,0
	Rect 400,590-height+3,1,4
End If
Color 255,255,255
If KeyHit(K_L) And launch_time=0
	v=0:u=0
	launch_time=MilliSecs()-start_time
	f#=thrust
	max#=0
	max_v=0
	flight_time#=0
	status=BURNING
End If

Text 10,40,"a         = "+((f/mass)+g)+"m/s/s"
Text 10,60,"s         = "+height#+"m"
Text 10,80,"R         = "+(f+(v*friction)+(mass*g))+"N" 
Text 300,80,"("+(v*friction)+"N friction)"
If Abs(v)>1 Then 
	Color 255,0,0
Else 
	Color 0,255,0
End If
Text 10,100,"v         = "+v+"m/s"
Color 255,255,255
Text 10,120,"Launched ="+((launch_time)/1000)
Text 10,140,"Time     = "+((this_time)/1000)
Text 10,160,"Period   = "+period
Color 200,90,80
Text 10,180,"Max s    = "+max+"m"
Text 10,200,"Max v    = "+max_v+"m/s"
Text 10,220,"Flight t = "+flight_time+"s"
Color 255,255,255
If edit_m=0
	Color 70,190,80
	Text 10,0,"uf        = "+friction
	Color 255,255,255
	Text 10,240,"Thrust   ="+thrust+"N ("+df[fi]+" Newton adjust)"
	Text 10,260,"Burn     ="+thrust_time +"s ("+dft[ti]+" second adjust)"
	Text 10,20,"m         = "+mass+"kg ("+df[fi]+" Kg adjust)"
End If
If edit_m=2
	Text 10,0,"uf        = "+friction
	Color 70,190,80
	Text 10,240,"Thrust   ="+thrust+"N ("+df[fi]+" Newton adjust)"
	Color 255,255,255
	Text 10,260,"Burn     ="+thrust_time +"s ("+dft[ti]+" second adjust)"
	Text 10,20,"m         = "+mass+"kg ("+df[fi]+" Kg adjust)"

End If
If edit_m=3
	Text 10,0,"uf        = "+friction
	Text 10,240,"Thrust   ="+thrust+"N ("+df[fi]+" Newton adjust)"
	Color 70,190,80
	Text 10,260,"Burn     ="+thrust_time +"s ("+dft[ti]+" second adjust)"
	Color 255,255,255
	Text 10,20,"m         = "+mass+"kg ("+df[fi]+" Kg adjust)"

End If
If edit_m=1
	Text 10,0,"uf        = "+friction
	Text 10,240,"Thrust   ="+thrust+"N ("+df[fi]+" Newton adjust)"
	Text 10,260,"Burn     ="+thrust_time +"s ("+dft[ti]+" second adjust)"
	Color 70,190,80
	Text 10,20,"m         = "+mass+"kg ("+df[fi]+" Kg adjust)"

	Color 255,255,255
End If


If KeyHit(k_r) height=0:v=0:f=Int (0):auto=0
If KeyHit(K_SPACE) edit_m= edit_m+1 :If edit_m>3 edit_m=0

If edit_m=1
	If KeyHit(K_Up) mass=mass+df[fi]
	If KeyHit(k_down)mass=mass-df[fi]
	If KeyHit(k_right) fi=fi+1 :If fi>5 fi=5
	If KeyHit(k_left) fi=fi-1 :If fi<0 fi=0

End If

If edit_m=2
	If KeyHit(K_Up) thrust=thrust+df[fi]
	If KeyHit(k_down)thrust=thrust-df[fi]
	If KeyHit(k_right) fi=fi+1 :If fi>5 fi=5
	If KeyHit(k_left) fi=fi-1 :If fi<0 fi=0
End If

If  edit_m=3
	If KeyHit(K_Up)  thrust_time=thrust_time+dft[ti]
	If KeyHit(k_down)thrust_time=thrust_time-dft[ti]
	If KeyHit(k_right) ti=ti+1 :If ti>3 ti=3
	If KeyHit(k_left) ti=ti-1 :If ti<0 ti=0

End If
If edit_m=0
	If KeyHit(K_Up) friction=friction+0.1
	If KeyHit(k_down)friction=friction-0.1
	If Abs(friction)<0.1 friction=0
End If

Color 100,255,90
Text 10,400,"Z Thrust"
Text 10,420,"X Thrust/2"
Text 10,440,"C Thrust/4"
Text 10,460,"L to launch, Space And arrow "
Text 10,480,"keys to change parameters"
Text 10,500,"R to reset A to engage Autopilot"
Text 10,520, "Esc to quit."
If auto Then Text 10,300,"Auto pilot on"
If status=LANDED Color 0,255,0:Text 280,400,"Landed, well done!!"
If status=CRASHED Color 255,0,0:Text 280,400,"Crashed, bad luck!!"
Color 255,255,255
Flip(1)
Wend

End

;******************** COSNTANTS *************************
Const K_ESCAPE= 1   
Const K_1= 2   
Const K_2= 3   
Const K_3= 4   
Const K_4= 5   
Const K_5= 6   
Const K_6= 7   
Const K_7= 8   
Const K_8= 9   
Const K_9= 10   
Const K_0= 11   
Const K_Minus= 12; On Main Keyboard 
Const K_Equals= 13   
Const K_Backspace= 14; Backspace key 
Const K_Tab= 15   
Const K_Q= 16   
Const K_W= 17   
Const K_E= 18   
Const K_R= 19   
Const K_T= 20   
Const K_Y= 21   
Const K_U= 22   
Const K_I= 23   
Const K_O= 24   
Const K_P= 25   
Const K_Left_Bracket= 26   
Const K_Right_Bracket= 27   
Const K_Enter= 28; Return/Enter on Main Keyboard 
Const K_Left_Control= 29   
Const K_A= 30   
Const K_S= 31   
Const K_D= 32   
Const K_F= 33   
Const K_G= 34   
Const K_H= 35   
Const K_J= 36   
Const K_K= 37   
Const K_L= 38   
Const K_Semi_Colon= 39   
Const K_Apostrophe=40;'   
Const K_Grave= 41; Accent Grave `
Const K_Left_Shift= 42   
Const K_Backslash=43   
Const K_Z= 44   
Const K_X= 45   
Const K_C= 46   
Const K_V= 47   
Const K_B= 48   
Const K_N= 49   
Const K_M= 50   
Const K_Comma= 51   
Const K_Period= 52; On Main keyboard 
Const K_Slash= 53; On Main Keyboard 
Const K_Right_Shift= 54   
Const K_Multiply= 55; On Numeric Keypad 
Const K_Left_Alt= 56   
Const K_Space=57   
Const K_Capslock=58   
Const K_F1= 59   
Const K_F2 =60   
Const K_F3= 61   
Const K_F4= 62   
Const K_F5= 63   
Const K_F6= 64   
Const K_F7= 65   
Const K_F8= 66   
Const K_F9= 67   
Const K_F10= 68   
Const K_NumLock= 69   
Const K_ScrollLock= 70   
Const K_NumPad_7= 71   
Const K_NumPad_8= 72   
Const K_NumPad_9= 73   
Const K_Subtract= 74; On Numeric Keypad 
Const K_NumPad_4= 75   
Const K_NumPad_5= 76   
Const K_NumPad_6= 77   
Const K_Add=78; On Numeric Keypad 
Const K_NumPad_1= 79   
Const K_NumPad_2= 80   
Const K_NumPad_3= 81   
Const K_NumPad_0= 82   
Const K_Decimal= 83; On Numeric Keypad 
Const K_OEM_102= 86; On UK/Germany Keyboards 
Const K_F11= 87   
Const K_F12= 88   
Const K_F13= 100; (NEC PC98) 
Const K_F14= 101; (NEC PC98) 
Const K_F15= 102; (NEC PC98) 
Const K_Kana= 112; Japanese Keyboard 
Const K_ABNT_C1= 115; /? on Portugese (Brazilian) keyboards 
Const K_Convert= 121; Japanese Keyboard 
Const K_NoConvert= 123; Japanese Keyboard 
Const K_Yen= 125; Japanese Keyboard 
Const K_ABNT_C2= 126; Numpad . on Portugese (Brazilian) keyboards 
Const K_NUMPAD_Equals= 141; = on numeric keypad (NEC PC98) 
Const K_PrevTrack= 144; Previous Track (DIK_CIRCUMFLEX on Japanese keyboard) 
Const K_AT= 145; (NEC PC98) 
Const K_Colon= 146; (NEC PC98) 
Const K_Underline= 147; (NEC PC98) 
Const K_Kanji= 148; Japanese Keyboard 
Const K_Stop= 149; (NEC PC98) 
Const K_AX= 150; Japan AX 
Const K_Unlabeled= 151; (J3100) 
Const K_Next_Track= 153; Next Track 
Const K_NUMPAD_Enter= 156; ENTER on Numeric Keypad 
Const K_Right_Control= 157   
Const K_Mute= 160; Mute 
Const K_Calculator= 161; Calculator 
Const K_Play_Pause= 162; Play/Pause 
Const K_Media_Stop= 164; Media Stop 
Const K_Volume_Down= 174; Volume - 
Const K_Volume_Up =176; Volume + 
Const K_Web_Home =178; Web Home 
Const K_NUMPAD_Comma= 179; On Numeric Keypad (NEX PC98) 
Const K_Divide= 181; / On Numeric Keypad 
Const K_SysReq =183;   
Const K_Right_alt= 184; Right Alt 
Const K_Pause= 197; Pause 
Const K_Home= 199; Home on Arrow Pad 
Const K_Up= 200; Up Arrow on Arrow Keypad 
Const K_Page_Up= 201; Page Up on Arrow Keypad 
Const K_Left= 203; Left Arrow on Arrow Keypad 
Const K_Right= 205; Right Arrow on Arrow Keypad 
Const K_End =207; End Key on Arrow Keypad 
Const K_Down= 208; Down Key on Arrow Keypad 
Const K_Next= 209; Next Key on Arrow Keypad 
Const K_Insert= 210; Insert Key on Arrow Keypad 
Const K_Delete= 211; Delete Key on Arrow Keypad 
Const K_Left_Windows= 219; Left Windows Key 
Const K_Right_Windows= 220; Right Windows Key 
Const K_Apps= 221; Apps Menu Key 
Const K_Power= 222; System Power 
Const K_Sleep= 223; System Sleep 
Const K_Wake= 227 ;System Wake 
Const K_Web_Search= 229   
Const K_Web_Favorites= 230   
Const K_Web_Refresh= 231   
Const K_Web_Stop= 232   
Const K_Web_Forward= 233   
Const K_Web_Back= 234   
Const K_My_Computer= 235   
Const K_Mail=  236 

	



NewtSoup(Posted 2003) [#2]
well that produced a fantastic response.. for those of you who are even slightly interested the solution I found was:
[code]
If auto And status=FLYING then
If (height<2*thrust_height) And (v<-1) f#=(((v*v)/(2*height))*mass)-mass*g
End If
[code]

play it safe works the best it seems.

- Luke