truck and trailer

Blitz3D Forums/Blitz3D Beginners Area/truck and trailer

Just wondering how best to make the cab drive around and the trailer follow in the correct way? they are 2 different b3d models.

the truck in question



I think that you need JV-ODE add-on because there's a demo program with tractor-trailer.

You could try this: First, use pointentity to point the trailer to the cab. Then, move the entity with the distance between the trailer and the cab (EntityDistance)minus the allowed distance. .. edit: you should use a pivot on the connection point, parented to the cab instead of the cab itself.
ie:
pivot = CreatePivot(cab)
moveentity pivot, 0, 0, 1

thanks guys

ive been playing with pointentity, but i want the trailer to pivot like a real trailer would in motion. i was hoping not to have to use any physics addons as the programs getting quite complex as it is. ill keep playing. if any one has any examples please post

Pete

	;setup graphics
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
	
	CreateLight()
	
	camera = CreateCamera()
	PositionEntity camera, 0, 2, -8
	

	;create cab	
	cab = CreateCube()
	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, -1, 0
	
	;create trailer	
	trailer = CreateCube()
	ScaleEntity trailer, 0.39, 0.4, 1
	EntityColor trailer, 255, 0, 0
	PositionEntity trailer, 0, -1.6, 0
	
	;main loop	
	Repeat

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


Nice and simple demo, bram32.

Hey, that's pretty fun :)

thanks very much my code was almost the same apart from my align trailer to pivot bit didnt work right with the trailer snapping back to zero rotation. Hehe

Pete

oh sorry one more thing, in the code above how would you limit how far the cab can turn left and right? to stop the cab passing though the trailer and i want a large turning circle.

Pete

>Ive been playing with pointentity, but i want the trailer
>to pivot like a real trailer would in motion. i was hoping
>not to have to use any physics addons as the programs
>getting quite complex as it is. ill keep playing. if any
>one has any examples please post

Remember to set the centers of rotation between the back axles of both cab and trailer, that will make them seem more realistic without physics.


Andy

ive got it looking good with my model but with the code above whats the best way to limit the maximum turning angle of the cab?

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


nice example, but what ment was limiting the cabs turning angle so if you held down the left or right key the cab would stop turning before passing though the trialer. say at about 45 degrees

What I ment to say was, to achieve that, the difference between cab and trailer angle should be limited to -45..+45.
Try this in the main loop:
	;main loop	
	Repeat

		;align trailer to pivot
		PointEntity trailer, pivot
		MoveEntity trailer, 0, 0, EntityDistance(trailer, pivot) - 1

		yw# = AngDist(EntityYaw(cab), EntityYaw(trailer)) 
		
		If Abs(yw) > 45 Then
			TurnEntity trailer, 0, -(yw - (45 * Sgn(yw))), 0
			PositionEntity trailer, EntityX(pivot, 1), EntityY(pivot, 1), EntityZ(pivot, 1)
			MoveEntity trailer, 0, 0, EntityDistance(trailer, pivot) - 1
		End If
		
		;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
			
		RenderWorld		
		
		Text 0,  0, yw
		Flip
		
	Until KeyHit(1)


thanks works great going forward reverse really screws it up, this is harder than i thought. ill keep playing with your code. thanks again bram32