My throttle/speed code, what is up with this...

Blitz3D Forums/Blitz3D Programming/My throttle/speed code, what is up with this...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Probably a result of floating point inaccuracy.

try this old bit of code ....

Graphics3D 640,480,16,1

Global plane = CreatePlane():EntityColor plane, 50,50,100:EntityAlpha plane, .75
texture = CreateTexture( 16,16)
SetBuffer TextureBuffer( texture )
Color 0,0,0:Rect 0,0,16,16,1
Color 50,50,200:Rect 0,0,16,16,0
SetBuffer BackBuffer()
ScaleTexture texture,20,20
EntityTexture plane, texture
FreeTexture texture

Global camera = CreateCamera():PositionEntity camera, 0, 50, -100
Global mirror = CreateMirror()

Global light = CreateLight():RotateEntity light , 15,-45,0

Type Car
	Field Model
	Field Wheel[3]
	Field WheelRotation#
	Field Wheelradius#
	Field TopSpeed#
	Field Drag#
	Field Acceleration#
	Field Brakes#
	Field Speed#
	Field Vx#, Vy#, Vz#
End Type

Global MyCar.car = CARcreate( 4, 3, 6, 2, 3 )

While Not KeyDown(1)

	CARupdate( MyCar )
	PointEntity camera, MyCar\model
	CameraZoom camera, EntityDistance( camera, MyCar\model ) / 100
	RenderWorld()
	Flip

Wend

;==========================================
;==========================================
;==========================================

Function CARupdate( c. car )
	
	;user input
	jx# = KeyDown(203) - KeyDown(205 )
	jb#  = KeyDown( 200)
	
	TurnEntity c\Model, 0, jx * 3.0,0
	For w = 0 To 1
		RotateEntity c\wheel[w], 0 , jx*45, 0
	Next
	
	c\Speed = LIMIT ( c\Speed + ( jb * c\Acceleration ) - ( (Not jb ) * c\Brakes ) , 0, c\TopSpeed )
	TFormVector 0,0,(1.0 - c\drag ), c\Model, 0
	c\Vx = ( c\Vx * c\drag ) + ( TFormedX() * c\Speed )
	c\Vz = ( c\Vz * c\drag ) + ( TFormedZ() * c\Speed )
	c\Vy = ( c\Vy * c\drag ) + ( TFormedY() * c\Speed ) 
	speed# = Sqr( c\Vx*c\Vx + c\Vy*c\Vy + c\Vz*c\Vz )

	TranslateEntity c\Model, c\Vx, c\vy , c\Vz
	
	c\WheelRotation = c\WheelRotation + Speed * 90.0 / ( Pi * c\WheelRadius )  
	For w = 0 To 3
		RotateEntity c\wheel[ w] , c\WheelRotation, EntityYaw( c\wheel[w] ) , 0
	Next

End Function

;==========================================
;==========================================
;==========================================

Function CARcreate.car( w#, h#, d# , tw# , tr# )

	c.car = New car
	c\Acceleration = .0075
	c\Brakes = .005 
	c\Drag = .95
	c\Speed = 0.0
	c\TopSpeed = 1.25
	c\Wheelradius = tw
		
	c\Model = CreateCube()
	FitMesh c\Model , -w,-h,-d, w*2,h*2,d*2
	UpdateNormals c\Model
	EntityColor c\Model,255,255,0
	
	Wheel = CreateCylinder( 5 )
	RotateMesh Wheel,0,0,90
	ScaleMesh Wheel , tw,tr,tr
	UpdateNormals Wheel
	EntityColor Wheel, 64,64,64

	For l= 0 To 3
		dx = ( l >0 And l<3 ) - ( l = 0 Or l = 3 )
		dz = ( l < 2 ) - ( l > 1 )
		c\wheel[l] = CopyEntity(wheel , c\Model )
		PositionEntity c\wheel[l], w*dx , -h , d*dz
	Next
	
	PositionEntity c\Model, 0 , tr+h, 0
	
	FreeEntity Wheel
	
	Return c
	
End Function

;==========================================
;==========================================
;==========================================

Function LIMIT#(n#,n1#,n2#)
	
	If n>n2 Then n=n2
	If n<n1 Then n=n1	
	Return n

End Function