truck and trailer

Blitz3D Forums/Blitz3D Beginners Area/truck and trailer

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.

For that, this example is better. The other one had the directions messed up a bit.
"Yw#" is the difference in angles between the cab and the trailer. When the cab and trailer align, it is around zero:
	;setup graphics
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
	
	CreateLight()
	
	camera = CreateCamera()
	PositionEntity camera, 0, -8, 2
	RotateEntity camera, -90, 0, 0
	
	;create cab	
	cab = CreateCone()
	RotateMesh cab, -90, 180, 0
	ScaleEntity cab, 0.4, 0.4, 0.4
	
	;create pivot
	pivot = CreateSphere(8,cab)
	ScaleEntity pivot, 0.2, 0.2, 0.2
	EntityColor pivot, 0, 255, 0
	EntityOrder pivot, -1
	;pivot=CreatePivot(cab)
	PositionEntity pivot, 0, 0, -1
	
	;create trailer	
	trailer = CreateCone()
	RotateMesh trailer, 90, 0, 0

	ScaleEntity trailer, 0.39, 0.4, 1
	EntityColor trailer, 255, 0, 0
	PositionEntity trailer, 0, 0, -2
	
	;main loop	
	Repeat

		yw# = AngDist(EntityYaw(cab), EntityYaw(trailer)) 
		
		;move cab	
		If KeyDown(203) Then TurnEntity cab, 0, -1, 0
		If KeyDown(205) Then TurnEntity cab, 0, +1, 0
		If KeyDown(200) Then MoveEntity cab, 0, 0, +0.1
		If KeyDown(208) Then MoveEntity cab, 0, 0, -0.1
	
		;align trailer to pivot
		PointEntity trailer, pivot
		MoveEntity trailer, 0, 0, EntityDistance(trailer, pivot) - 1
					
		RenderWorld
		
		
		Text 0,  0, yw
		Flip
		
	Until KeyHit(1)

End

;-----------------------------------------------------------------------------------------------------
;													AngDist()
;-----------------------------------------------------------------------------------------------------
;returns shortest difference between two angles
Function AngDist#(ang1#, ang2#)

	Local c1# = (ang2 - ang1)
	Local c2# = (ang2 + 360 - ang1)
	Local c3# = (ang2 - 360 - ang1)
	
	If Abs(c3) < Abs(c1) Then c1 = c3
	If Abs(c2) < Abs(c1) Then c1 = c2
	
	Return c1
	
End Function