November 28, 2020, 11:10:21 AM

Author Topic: [bb] Vehicle pitch and roll without child/pivot entities by MadJack [ 1+ years ago ]  (Read 721 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Vehicle pitch and roll without child/pivot entities
Author : MadJack
Posted : 1+ years ago

Description : Found Darkbasic maddening when it came to simply getting a vehicle to follow a terrain. I'm finding BB3d much easier in this respect, but figured there must be a simpler way to pitch and roll a vehicle on a terrain without resorting to child entities to get terrain height.
This uses sin(yaw+offset),cos(yaw+offset) to get the midfront/back, midleft/right x,y co-ords and then uses Atan2 to calculate angles.
Please feel free to let me know your ideas/improvements.

Note: you'll have to specify your own terrain texture and heightmap, but the 747.x is from Hi-Toro's DeathIsland example project).

EDIT by BlitzSupport (24 Jan 2007). Was just looking at this, so bundled code and media <a href="http://www.hi-toro.com/blitz/vehicle.zip" target="_blank">here[/url] for quick access (249 KB).


Code :
Code: BlitzBasic
  1. ; my car test - Phil Jones
  2. ; a look at calculating vehicle pitch and roll (on a terrain) without
  3. ; child/pivot entities
  4.  
  5. ;set up screen,light & camera
  6. Graphics3D 800,600,16
  7. SetBuffer BackBuffer()
  8. light = CreateLight(1)
  9.  
  10. ;create terrain
  11. terrain = LoadTerrain("heightmap_256.bmp")
  12. TerrainDetail terrain, 3000, True
  13.  
  14. ScaleEntity terrain, 50,1000,50
  15. PositionEntity terrain,-200,0,-200
  16. terraintex = LoadTexture("coolfloor2.bmp")
  17.  
  18. ScaleTexture terraintex,.4,.4
  19. TerrainShading terrain,1
  20. EntityTexture terrain,terraintex
  21.  
  22. ;create car/plane/tank whatever and centre it local co-ords
  23. car = LoadMesh("747.x")
  24. ScaleMesh car,100,100,100
  25.  
  26. carlength# = MeshDepth#(car)/2 ; get length from centre to front of car
  27. carwidth# = MeshWidth#(car)/2 ; get length from centre to left or right of car
  28. carheight# = MeshHeight#(car)/2: ; get height from centre To top Or bottom of car
  29. meshh = MeshHeight#(car)/2
  30. FitMesh car, 0-carwidth,0,0-carlength,carwidth*2,carheight*2,carlength*2; just some general recentring to have car sit on plane
  31.  
  32. pitch# = 0
  33. x# = 200
  34. y# = 600
  35. z# = 480
  36. speed# = 0
  37.  
  38. frontheight#=0 ; height of terrain at midpoint front of vehicle
  39. backheight#=0 ; height of terrain at midpoint back of vehicle
  40. leftheight#=0 ; height of terrain at midpoint left side of vehicle
  41. rightheight#=0 ; height of terrain at midpoint right side of vehicle
  42. th# = 0 ; height of terrain at centre of vehicle (this will averaged from front/back/left/right)
  43.  
  44. PositionEntity car,x,y,z ; set up initial pos of car and camera
  45. ;RotateEntity car,0,180,0,1
  46.  
  47. ;these are just for visual ref - not needed in calculation of rotations
  48. pop = CreateCube()
  49. ScaleEntity pop,5,400,5
  50. pfront = CreateCube()
  51. ScaleEntity pfront,5,5,5
  52. pback = CreateCube()
  53. ScaleEntity pback,5,5,5
  54. pleft = CreateCube()
  55. ScaleEntity pleft,5,5,5
  56. pright = CreateCube()
  57. ScaleEntity pright,5,5,5
  58.  
  59. ;setup camera
  60. camera = CreateCamera()
  61. PositionEntity camera ,300,350,100
  62. PointEntity camera ,car
  63. CameraRange camera,1,900000
  64.  
  65.  
  66. ;MAIN LOOP +++++++++++++++++++++++++++++++++
  67.  
  68. While Not KeyHit(1)
  69.  
  70. ; get current location and overall height of car
  71. x# = EntityX (car)
  72. z# = EntityZ (car)
  73. y# = EntityY (car)
  74.  
  75. ; check for keypress to alter car speed (A,Z)
  76. speed = speed -.5*(KeyDown(208) And speed > -10 ) + .5*(KeyDown(200) And speed < 10 )
  77.  
  78. ; check for keypress to turn car left/right
  79. turn = (KeyDown(203)) - (KeyDown(205))
  80. TurnEntity car,0,turn,0
  81. yaw# = EntityYaw(car)
  82.  
  83.  
  84.  
  85. ; CALCULATE PITCH/ROLL ++++++++++++++++++++++++++++++++
  86. ;work out terrainheights at front/back/left/right of car
  87. anglenew# = yaw -90  ; what must be added to yaw to calculate front of vehicle's global x,z co-ords
  88. xnew# = x+Cos(anglenew)*carlength
  89. znew# = z+Sin(anglenew)*carlength
  90. frontheight# = TerrainY(terrain,xnew,y,znew); get the height at new x,y,z
  91. PositionEntity pfront,xnew,frontheight,znew ; a visual marker only - not neccessary for calculations
  92.  
  93. anglenew# = yaw +90 ; what must be added to yaw to calculate back of vehicle's global x,z co-ords
  94. xnew# = x+Cos(anglenew)*carlength
  95. znew# = z+Sin(anglenew)*carlength
  96. backheight = TerrainY(terrain,xnew,y,znew)
  97. PositionEntity pback,xnew,backheight,znew
  98.  
  99. anglenew# = yaw  ; what must be added to yaw to calculate midleft of vehicle's global x,z co-ords
  100. xnew# = x+Cos(anglenew)*carwidth
  101. znew# = z+Sin(anglenew)*carwidth
  102. Leftheight = TerrainY(terrain,xnew,y,znew)
  103. PositionEntity pleft,xnew,leftheight,znew
  104.  
  105. anglenew# = yaw + 180 ; what must be added to yaw to calculate midright of vehicle's global x,z co-ords
  106. xnew# = x+Cos(anglenew)*carwidth
  107. znew# = z+Sin(anglenew)*carwidth
  108. rightheight = TerrainY(terrain,xnew,y,znew)
  109. PositionEntity pright,xnew,rightheight,znew
  110.  
  111. pitchx# = carlength*2 ; need complete length of car for x
  112. pitchy# = (backheight - frontheight); the height dif between the front and rear of car
  113. pitch = ATan2(pitchx,pitchy); atan2 will return the angle from 0,0 to x,y
  114.  
  115. rollx# = carwidth*2
  116. rolly# = (rightheight - leftheight) ; the height dif between the left and right of car
  117. roll# = ATan2(rollx,rolly); atan2 will return the angle from 0,0 to x,y
  118.  
  119. RotateEntity car, pitch-90,yaw,roll-90 ; rotate car with offsets to adjust Atan2's calculation
  120.  
  121. th=(frontheight+backheight+leftheight+rightheight)/4; get average of terrain heights for cars overall height
  122. PositionEntity car,x,th,z ; adjust car height
  123. MoveEntity car,0,0,speed
  124.  
  125. PositionEntity pop,x,th,z ; this is just a central axis 'pole' to help see rotation angles
  126. RotateEntity pop,pitch-90,yaw,roll-90
  127.  
  128. PositionEntity camera , x+400,y+300,z+200
  129. PointEntity camera,car
  130.  
  131. UpdateWorld
  132. RenderWorld
  133. Flip
  134.  
  135. Wend
  136. Stop


Comments :


Matty(Posted 1+ years ago)

 Very useful..I am going to use this...with some modifications.


Stevie G(Posted 1+ years ago)

 Oops .. wrong thread!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal