Driving Code

Blitz3D Forums/Blitz3D Programming/Driving Code

jeffmorris(Posted 2006) [#1]
Here's code for "walking" using keyboard, driving a "vehicle" using a wheel/pedal set (Logitech Driving Force Pro), and flying an "airplane" using a joystick (Microsoft Force Feedback 2 Joystick). Any suggestions for simple flight physics using the simple driving physics code?

While Not KeyDown(1)
	If KeyDown(59)= True Then mode=0:modestring="Walk"
	If KeyDown(60)= True Then mode=1:modestring="Drive"
	If KeyDown(61)= True Then mode=2:modestring="Fly"
	If mode=0 Then
		If KeyDown(205)=True Then TurnEntity camera,0,-0.25,0
		If KeyDown(203)=True Then TurnEntity camera,0,0.25,0
		If KeyDown(208)=True Then MoveEntity camera,0,0,-0.1
		If KeyDown(200)=True Then MoveEntity camera,0,0,0.1
	EndIf
	If mode=1 Then
		If JoyHit(13,1) Then gear=gear-1:If gear<-1 Then gear=-1
		If JoyHit(14,1) Then gear=gear+1:If gear>1 Then gear=1
		If gear=-1 Then gearstring="Reverse"
		If gear=0 Then gearstring="Neutral"
		If gear=1 Then gearstring="Drive"
		If JoyY(1) < -0.05
			If gear=1
	  			speed=speed+0.005
	  			If speed>2 Then speed=2
				MoveEntity camera,0,0,speed
			End If
			If gear=-1
	  			speed=speed-0.005
      			If speed<-0.5 Then speed=-0.5
				MoveEntity camera,0,0,speed
			EndIf
		End If
		If JoyY(1) > 0.05
			speed=speed-0.01
			If speed<0 Then speed=0
			If speed>0 Then speed=speed*0.97
			MoveEntity camera,0,0,speed
		End If
		If JoyY(1) > -0.05 And JoyY(1) < 0.05
			speed=speed*0.99
			MoveEntity camera,0,0,speed
		End If	
		If JoyX(1) < -0.05
    		steer=-JoyX(1)/2
    		If steer>0.5 Then steer=0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(1) > 0.05
    		steer=-JoyX(1)/2
    		If steer<-0.5 Then steer=-0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(1) > -0.05 And JoyX(1) < 0.05 Then steer=0
	EndIf
	If mode=2 Then
		throttle=(JoyU(0)-1) * -0.5
  		If (throttle > 0.1 And JoyX(0) < -0.1) Then TurnEntity camera,0,-JoyX(0),0
  		If (throttle > 0.1 And JoyX(0) > 0.1) Then TurnEntity camera,0,-JoyX(0),0
  		If (throttle > 0.1 And JoyY(0) < -0.1) Then MoveEntity camera,0,JoyY(0),0
  		If (throttle > 0.1 And JoyY(0) > 0.1) Then MoveEntity camera,0,JoyY(0),0
  		If throttle > 0.1 Then MoveEntity camera,0,0,throttle * 2
	EndIf
	UpdateWorld
	RenderWorld
	Text 0,0,"Mode:"+modestring
	If mode=1 Then Text 0,15,"Car speed:"+speed*100
	If mode=1 Then Text 0,30,"Gear:"+gearstring
	Flip
Wend



D4NM4N(Posted 2006) [#2]
Change the maths :P

These may help: (or not)
http://www.ucmp.berkeley.edu/vertebrates/flight/physics.html
http://user.uni-frankfurt.de/~weltner/Flight/PHYSIC4.htm

Lol dont ask me how tho' im crap at maths


jeffmorris(Posted 2006) [#3]
When I change modes to walk, drive, or fly, I need to reset the values to 0 the first time in a loop after changing the mode. For example, I'm driving a car to the airport and switch to an airplane. I need to save the values for the modes that I switch from, reset the values to 0 the first time in a loop, and when I switch from airplane to car, restore the values. Here's the latest code:

While Not KeyDown(1)
	If KeyDown(59)= True Then mode=0:modestring="Walk"
	If KeyDown(60)= True Then mode=1:modestring="Drive"
	If KeyDown(61)= True Then mode=2:modestring="Fly"
	If mode=0 Then
		If KeyDown(205)=True Then TurnEntity camera,0,-0.25,0
		If KeyDown(203)=True Then TurnEntity camera,0,0.25,0
		If KeyDown(208)=True Then MoveEntity camera,0,0,-0.1
		If KeyDown(200)=True Then MoveEntity camera,0,0,0.1
	EndIf
	If mode=1 Then
		If JoyHit(13,1) Then gear=gear-1:If gear<-1 Then gear=-1
		If JoyHit(14,1) Then gear=gear+1:If gear>1 Then gear=1
		If gear=-1 Then gearstring="Reverse"
		If gear=0 Then gearstring="Neutral"
		If gear=1 Then gearstring="Drive"
		If JoyY(1) < -0.05
			If gear=1
	  			speed=speed+0.005
	  			If speed>2 Then speed=2
				MoveEntity camera,0,0,speed
			End If
			If gear=-1
	  			speed=speed-0.005
      			If speed<-0.5 Then speed=-0.5
				MoveEntity camera,0,0,speed
			EndIf
		End If
		If JoyY(1) > 0.05
			speed=speed-0.01
			If speed<0 Then speed=0
			If speed>0 Then speed=speed*0.99
			MoveEntity camera,0,0,speed
		End If
		If JoyY(1) > -0.05 And JoyY(1) < 0.05
			speed=speed*0.99
			MoveEntity camera,0,0,speed
		End If	
		If JoyX(1) < -0.05
    		steer=-JoyX(1)/2
    		If steer>0.5 Then steer=0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(1) > 0.05
    		steer=-JoyX(1)/2
    		If steer<-0.5 Then steer=-0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(1) > -0.05 And JoyX(1) < 0.05 Then steer=0
	EndIf
	If mode=2 Then
		throttle=(JoyU(0)-1) * -0.5
		If throttle>0
			speed=speed+0.1
			MoveEntity camera,0,0,speed
		End If
		If JoyY(0) < -0.05
  			height=height-0.01
			MoveEntity camera,0,height,speed
		End If
		If JoyY(0) > 0.05
			height=height+0.01
			MoveEntity camera,0,height,speed
		End If
		If JoyX(0) < 0.05
    		steer=-JoyX(0)
    		If steer>0.5 Then steer=0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(0) > 0.05
    		steer=-JoyX(0)
    		If steer<-0.5 Then steer=-0.5
			TurnEntity camera,0,steer,0
		End If
		If JoyX(0) > -0.05 And JoyX(0) < 0.05 Then steer=0
		If JoyY(0) > -0.05 And JoyY(0) < 0.05
			speed=speed*0.99
			MoveEntity camera,0,0,speed
		End If	
		If throttle=0
			speed=speed*0.99
			height=height*0.99
			MoveEntity camera,0,height,speed
		End If
	EndIf
	UpdateWorld
	RenderWorld
	Text 0,0,"Mode:"+modestring
	If mode=1 Then Text 0,15,"Car speed:"+speed*100
	If mode=2 Then Text 0,15,"Air speed:"+speed*100
	If mode=1 Then Text 0,30,"Gear:"+gearstring
	If mode=2 Then Text 0,30,"Height:"+height
	If mode=2 Then Text 0,45,"Throttle:"+throttle
	Flip
Wend



Bobysait(Posted 2006) [#4]
- use arrays
- Change the 'mode' system
=> include an increment

if keydown(...) lastmode=mode:mode=10; walk
if keydown(...) lastmode=mode:mode=20; drive
if keydown(...) lastmode=mode:mode=30; fly

select mode
case 10
;record values in the array
value1[LastMode]=value ...
etc...
mode=1
case 20
; record values ...
etc...
end select