January 27, 2021, 06:22:46 AM

Author Topic: Terrain object height question  (Read 350 times)

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Terrain object height question
« on: June 16, 2020, 04:29:40 PM »
Hi Guys, I have a car that I want to place on the terrain demo that comes with AGK, but I don't know how to get the Y Co-ords of it's location. When looking at the existing code this seems to be covered quite well with the camera...
// stick camera to the terrain
      x# = GetCameraX(1)
      z# = GetCameraZ(1)
      SetCameraPosition( 1, x#, GetObjectHeightMapHeight(camera,x#,z#)+.2, z# )
but I was wondering how to re-translate this for my car?. Can anyone help me with this?.

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: Terrain object height question
« Reply #1 on: June 16, 2020, 08:50:17 PM »
Still no luck... I found the GetObjectHeightMapHeight command... then created a box object as object 4
I gave it xx# and yy# co-ords and put it on a heightmap.. I then made it move with the first line below, which it does... but it still runs at
a set height totally ignoring the heighmap info!!!..  I thought for sure the below code would work, using YB# to get the X and Z Map co-ords
and sending it to the object position. Could anyone at least tell me if I am on the right track here please.  ???

   yb#=GetObjectHeightMapHeight( 4, xx#, zz# )+30

Offline blinkok

  • Sr. Member
  • ****
  • Posts: 280
Re: Terrain object height question
« Reply #2 on: June 17, 2020, 12:06:09 AM »
Your object id for the height map and your object id for the car are the same (4)
I have some code that simulates a car on a height map here

Offline Kronos

  • Jr. Member
  • **
  • Posts: 37
Re: Terrain object height question
« Reply #3 on: June 17, 2020, 05:47:19 PM »
if you want vehicle to align on terrain and "slide" about on it (no physics) you can do something like this (code adapted from original Blitz3D code by MadJack)

veh.mesh is just the vehicle body. Wheels are attached to veh.pivot.

As AGK has no pivots I use small invisible cubes instead.

4 cubes are attached to veh.pivot in a cross formation matching dimensions of vehicle.
one at front,one at rear, one on left and one on right
veh.FrontHeight, veh.RearHeight etc.

I split out the veh.mesh from the veh.pivot so the vehicle body angle can be independant of the wheels which are always stuck to the ground. It should be possible to take this further so that the body will bounce around or the vehicle can jump off the terrain.

Code: [Select]
       //get current position of vehicle

airborne as Integer

veh.speed = veh.speed *0.99
if abs(veh.speed) < 0.001 then veh.speed = 0.0

        //get heights of the 4 pivots on the terrain
veh.FrontHeight = GetObjectHeightMapHeight(veh.terrain,GetObjectWorldX(veh.FrontPivot),GetObjectWorldZ(veh.FrontPivot))
veh.RearHeight =  GetObjectHeightMapHeight(veh.terrain,GetObjectWorldX(veh.RearPivot),GetObjectWorldZ(veh.RearPivot))
veh.LeftHeight = GetObjectHeightMapHeight(veh.terrain,GetObjectWorldX(veh.LeftPivot),GetObjectWorldZ(veh.LeftPivot))
veh.RightHeight = GetObjectHeightMapHeight(veh.terrain,GetObjectWorldX(veh.RightPivot),GetObjectWorldZ(veh.RightPivot))
//calculate angles based on these heights
pitchx# = veh.carlength
pitchy# = (veh.frontheight - veh.rearheight)
pitch# = ATan2(pitchx#,pitchy#)

rollx# = veh.carwidth
rolly# = (veh.leftheight - veh.rightheight)
roll# = ATan2(rollx#,rolly#)
pitch# = pitch#-90.0

SetObjectRotation(veh.pivot, pitch#,yaw#,roll#)



for i = 1 to 4//numwheels
//do some stuff to flatten out the angle changes so vehicle mesh angle lags behind wheels a bit
pitch# = fmod(pitch#,360)
roll# = fmod(roll#,360)
yaw# = fmod(yaw#,360)

veh.oldrotationx = fmod(veh.oldRotationX,360)
veh.oldrotationy = fmod(veh.oldRotationY,360)
veh.oldrotationz = fmod(veh.oldRotationZ,360)

veh.oldRotationX = veh.oldRotationX + (pitch#-veh.oldRotationX)*0.1
veh.oldRotationY = yaw#
veh.oldRotationZ = veh.oldRotationZ + (roll#-veh.oldRotationZ)*0.1

Offline phodiS

  • Jr. Member
  • **
  • Posts: 52
Re: Terrain object height question
« Reply #4 on: June 17, 2020, 05:51:27 PM »
This is great stuff!!!... The car demo is going to really help!, and I will also look at the code you have posted as well Kronos. I am trying to keep this as simple as it possibly can be; and will slowly expand on this project as I go.
Thank you both very much for your help!.


SimplePortal 2.3.6 © 2008-2014, SimplePortal