Vehicle over terrain prob

Blitz3D Forums/Blitz3D Beginners Area/Vehicle over terrain prob

Gazza69(Posted 2012) [#1]
Hi guys, can anybody provide any insight as to why my car in this code is slowly getting higher above the ground ?
Down the track, I will put it into separate files and clean it up a bit, but for now, I can't seem to work out why the car gets a little higher.
I have borrowed and modified some code from the examples that come with Blitz3d and the forums - its all a bit of a mix, but hopefully enough comments to explain it all. I can see theres various ways of doing this, I've gone with trying to use align to vector based on height of wheels over terrain - I like the mathematical approach, makes me use the grey matter a little more:)

Cheers

Gazza69


;Include "cargamefunctions.bb"
Global skybox, terr 
;set up screen,light & camera
Graphics3D 640,480,16,2
SetBuffer BackBuffer()

SetBuffer BackBuffer()light = CreateLight(1)
terr=LoadTerrain( "media/heightmap_256.bmp" )
ScaleEntity terr,1000/TerrainSize(terr),70,1000/TerrainSize(terr)
TerrainDetail terr,1000,True
TerrainShading terr,True
PositionEntity terr,-500,0,-500
tex=LoadTexture( "media/terrain-1.jpg" )
ScaleTexture tex,50,50
EntityTexture terr,tex
EntityType terr,SCENE


MakeSkybox()
;camera

cam2=CreateCamera()
PositionEntity cam2,0,100,-20
CameraRange cam2,1,900000

; Build the Car

body_pivot=CreatePivot() ; everything moves with this
body = CreateCube(body_pivot)  ; actual body of car (rectangle)
ScaleEntity body,3,.5,3.5
EntityColor body,255,0,0
PositionEntity body, 0,1.5,0


;make the wheels
rl_wheel = CreateWheel(0,body_pivot)
rr_wheel = CreateWheel(0,body_pivot)
fl_wheel = CreateWheel(1,body_pivot)
fr_wheel = CreateWheel(1,body_pivot)

PositionEntity rl_wheel,-3.5,1.3,-3  ;position all wheels relative to 0,0,0
PositionEntity rr_wheel,3.5,1.3,-3
PositionEntity fl_wheel,-3.5,1.3,4
PositionEntity fr_wheel,3.5,1.3,4



PositionEntity body_pivot,0,TerrainY(terr,0,0,70),70  ; re position body_pivot in world, wheels and body follow
  

While Not KeyHit(1)

   If KeyDown(200) ; forward     
     MoveEntity body_pivot,0,0,1 ; move it forward    
     TurnEntity body_pivot, 0,x#/15,0 ; turn car   
     y=y Mod 360 +1  ;keep between 1 and 360 ;update heights of wheels
   End If
 
   If KeyDown(208)    ;backward
     MoveEntity body_pivot,0,0,-.2 
     TurnEntity body_pivot, 0,-x#/15,0 ; turn the car the right amount
     y=y Mod 360 -1  
   End If
  
   If KeyDown(203) x#=x#+1 ; turning left 
   If KeyDown(205)  x#=x#-1 ;turn right

   If x# >0     ;this code slowly returns front wheels to straight position 
     x#=x#-.2
   ElseIf x# <0
     x#=x#+.2
   Else
     x#=0
   End If
  
   If x#>15 Then x#=15 ; keep steering constraints
   If x#<-15 Then x#=-15   


   PointEntity cam2, body
   ;update camera position to follow vehicle and stay at constant height above terr point
   PositionEntity cam2, EntityX(body,True), TerrainY(terr, EntityX(body,True),0,EntityZ(body,True))+10,EntityZ(body,True)-25
   ;update rear and front wheel rotations and yaw based on y for local y rotation and x for local yaw   
    RotateEntity fl_wheel,y,x,0
    RotateEntity  fr_wheel,y,x,0
    RotateEntity rl_wheel,y,0,0
    RotateEntity  rr_wheel,y,0,0
   
    ;use these for mak driver below
    fl_wheel_height# = TerrainY(terr, EntityX(fl_wheel,True),0,EntityZ(fl_wheel,True))
    fr_wheel_height# = TerrainY(terr, EntityX(fr_wheel,True),0,EntityZ(fr_wheel,True))
    rl_wheel_height# = TerrainY(terr, EntityX(rl_wheel,True),0,EntityZ(rl_wheel,True))
    rr_wheel_height# = TerrainY(terr, EntityX(rr_wheel,True),0,EntityZ(rr_wheel,True))
 
    ;borrowed from mak driver example - align x and z axis of body pivot based on wheel locations	
	
	zx#=(EntityX( fl_wheel,True )+EntityX( fr_wheel,True ))/2
	zx=zx-(EntityX( rl_wheel,True )+EntityX( rr_wheel,True ))/2
	zy#=(fl_wheel_height#+fr_wheel_height#)/2
	zy=zy-(rl_wheel_height#+rr_wheel_height#)/2
	zz#=(EntityZ( fl_wheel,True )+EntityZ( fr_wheel,True ))/2
	zz=zz-(EntityZ( rl_wheel,True )+EntityZ( rr_wheel,True ))/2
    AlignToVector body_pivot,zx,zy,zz,3,1
	
	
	zx#=(EntityX( fr_wheel,True )+EntityX( rr_wheel,True ))/2
	zx=zx-(EntityX( fl_wheel,True )+EntityX( rl_wheel,True ))/2
	zy#=(fr_wheel_height+rr_wheel_height)/2
	zy=zy-(fl_wheel_height+rl_wheel_height)/2
	zz#=(EntityZ( fr_wheel,True )+EntityZ( rr_wheel,True ))/2
	zz=zz-(EntityZ( fl_wheel,True )+EntityZ( rl_wheel,True ))/2
	AlignToVector body_pivot,zx,zy,zz,1 

    PositionEntity skybox,EntityX(cam2),EntityY(cam2),EntityZ(cam2);  tie skybox to camera location
    UpdateWorld

    RenderWorld


    Text 10,70 ,"front left wheel is at " +EntityX(fl_wheel,True) + ", " + EntityY(fl_wheel,True) + ", " + EntityZ(fl_wheel,True)
    Text 10,90 ,"front right wheel is at " +EntityX(fr_wheel,True) + ", " + EntityY(fr_wheel,True) + ", " + EntityZ(fr_wheel,True)
    Text 10,110 ,"rear left wheel is at " +EntityX(rl_wheel,True) + ", " + EntityY(rl_wheel,True) + ", " + EntityZ(rl_wheel,True)
    Text 10,130 ,"rear right wheel is at " +EntityX(rr_wheel,True) + ", " + EntityY(rr_wheel,True) + ", " + EntityZ(rr_wheel,True)
    Text 10,150, "zx zx and zz end up being " + zx + ", " + zy +", "+zz 
    Text 10, 170 ,"front left, front right, rear left and rear height " + fl_wheel_height +", " + fr_wheel_height +", " +  rl_wheel_height + ", " + rr_wheel_height
    Text 10,190 ,"pitch yaw and roll are " + EntityPitch(body_pivot) + ", "+EntityYaw(body_pivot) +", " +EntityRoll(body_pivot)
    Text 10,200 ,"entityy at car_pivot" + TerrainY(terr,EntityX(body_pivot),0,EntityZ(body_pivot))
    Flip
  
  Wend 


End



Function CreateWheel(size,p)
	
	the_wheel = CreateCylinder(6,1,p)
	RotateMesh the_wheel,90,90,0
	If size=1
	ScaleMesh the_wheel,.9,.9,.9
	Else
	ScaleMesh the_wheel,.9,.9,.9
	End If
	EntityFX the_wheel,16
	EntityColor the_wheel,50,50,50
	Return the_wheel
End Function


Function MakeSkybox()
	skybox=CreateMesh()
	;front face
	b=LoadBrush( "media/front.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( "media/left.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( "media/back.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( "media/right.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( "media/up.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;bottom face	
	b=LoadBrush( "media/down.jpg",49 )
	s=CreateSurface( skybox,b )
	AddVertex s,-1,-1,-1,1,0:AddVertex s,+1,-1,-1,1,1
	AddVertex s,+1,-1,+1,0,1:AddVertex s,-1,-1,+1,0,0
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	ScaleMesh skybox,100,100,100
	FlipMesh skybox
	EntityFX skybox,1
	EntityOrder skybox,10
end function



Last edited 2012

Last edited 2012


Floyd(Posted 2012) [#2]
I don't know about your car positioning, but everybody should avoid the new bbcode tag until further notice. It breaks code.

Here is your line 68 with the old and new code tags.
   If x# >0     ;this code slowly returns front wheels to straight position 

[bbcode] If x# >0 ;this code slowly returns front wheels to straight position [/bbcode]


Gazza69(Posted 2012) [#3]
Ok, thanks Floyd. Will take that on board for future posts.