Jump to content


Photo

Finding Z height in heightmap terrain


  • Please log in to reply
2 replies to this topic

#1 AquaFerno

AquaFerno

    GMC Member

  • GMC Member
  • 6 posts
  • Version:GM:Studio

Posted 02 June 2012 - 03:38 AM

Hi GMC,

I have a rather complicated problem that I can't get my head around. Maybe one of you can help.

Ok, I am working on a 3D racing game in GM8 that borrows techniques from JMike's city rendering example and Percsich.hu's car and terrain example. It features terrain created from heightmap coordinates and GM tiles can be mapped onto the terrain. First, the engine reads the 128x128 heightmap and then stores it into 16x16 cells in a 2048x2048 (same size as the room) ds_grid. Then the tile scanner reads all of the 16x16 tiles that are in the room and upscales them to 256x256 and then maps them onto the terrain (hope your still with me).

This all works fine but I can't seem to make the cameras z value move on the terrain's z value. I am using a modified version of Percsich.hu's "find_height" script from his car and terrain example to get the z from the heightmap but I am having trouble because the heightmap image is 128x128, the heightmap ds_grid is 2048x2048 and because everything gets scaled up 16 times in the room the camera is moving around in a room that is 32768x32768!

I've tried different combinations and all I get is every so often the camera jumps up to about 100 z and then comes back down to 0 z.


get_heightmap_z script:

var tempx, tempy, output, x_offset, y_offset, z1, z2, z3, z4;

tempx = argument0;
tempy = argument1;


//determine which grid and where it is
x_grid = floor(tempx/256);
y_grid = floor(tempy/256);
x_offset = tempx-16*x_grid;
y_offset = tempy-16*y_grid;

//get the z values from the ds list
z1 = ds_grid_get(obj_tile_creator.heightmap_grid,x_grid,y_grid);
z2 = ds_grid_get(obj_tile_creator.heightmap_grid,x_grid+16,y_grid);
z3 = ds_grid_get(obj_tile_creator.heightmap_grid,x_grid+16,y_grid+16);
z4 = ds_grid_get(obj_tile_creator.heightmap_grid,x_grid,y_grid+16);

//pinpoint the final location
if ( x_offset > y_offset )
{
    output = z1 - x_offset * (z1-z2) /16 - y_offset * (z2-z3) /16;
}
else
{
    output = z1 - x_offset * (z4-z3) /16 - y_offset * (z1-z4) /16;
}

return(output);

If I'm not making any sense and you need more info, please let me know and I will provide the GMK file if needed.

Thanks to anyone who will help.
  • 0

#2 xshortguy

xshortguy

    GMC Member

  • Global Moderators
  • 4186 posts
  • Version:GM:Studio

Posted 02 June 2012 - 02:30 PM

How are you computing the camera's z coordinate? Keep in mind that your camera should typically store all of the display parameters as variables (such as from, to, and up vectors). From there, simply move the camera to be relative to the height map (i.e. simply add the camera position offsets).
  • 0

#3 AquaFerno

AquaFerno

    GMC Member

  • GMC Member
  • 6 posts
  • Version:GM:Studio

Posted 03 June 2012 - 08:43 AM

How are you computing the camera's z coordinate? Keep in mind that your camera should typically store all of the display parameters as variables (such as from, to, and up vectors). From there, simply move the camera to be relative to the height map (i.e. simply add the camera position offsets).

I use the above script to calculate the z from the terrain. I have figured it out though, it was due to the fact that the data grid was not the correct size - It needed to be 128x128 instead. Thank you for your reply anyway.

Edited by AquaFerno, 03 June 2012 - 08:48 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users