commit 3889c3d127d96d0330913e86c961324fb985dab9 parent bea04b83bd1a69fce0480d69df68fa07663a40b5 Author: Michael Savage <mikejsavage@gmail.com> Date: Fri Sep 18 13:00:38 +0100 Fix terrain_height Diffstat:
hm.cc | | | 2 | +- |
terrain_manager.cc | | | 29 | ++++++++++++++++++++++------- |
terrain_manager.h | | | 2 | +- |
diff --git a/hm.cc b/hm.cc @@ -228,7 +228,7 @@ extern "C" GAME_FRAME( game_frame ) { state->pos += angles_to_vector_xy( state->angles ) * speed * dt * ( float ) fb; const glm::vec3 sideways = glm::vec3( -cosf( state->angles.y ), sinf( state->angles.y ), 0 ); state->pos += sideways * speed * dt * ( float ) lr; - // state->pos.z = terrain_height( &state->tm, state->pos.x, state->pos.y ) + 2; + // state->pos.z = terrain_height( &state->tm, state->pos ) + 2; state->pos.z += dz * 50.0f * dt; terrain_update( &state->tm, state->pos ); diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -226,13 +226,28 @@ void terrain_render( const TerrainManager * const tm, const glm::mat4 VP, const glUseProgram( 0 ); } -float terrain_height( const TerrainManager * const tm, const float x, const float y ) { - const u32 tx = x / TILE_SIZE; - const u32 ty = y / TILE_SIZE; +// TODO: this is horrible +// it's probably worth using view_x/y instead of view_left/top +// and using signed arithmetic to make this less annoying +// also implement the bounded arithmetic class +float terrain_height( const TerrainManager * const tm, const glm::vec3 position ) { + // see doodles/tile_coords_to_view_coords.png for docs + const u32 player_tile_x = position.x / TILE_SIZE; + const u32 player_tile_y = position.y / TILE_SIZE; + + if( player_tile_x < tm->tile_x ) assert( tm->tile_x - player_tile_x <= VIEW_HALF ); + else assert( player_tile_x - tm->tile_x <= VIEW_HALF ); + if( player_tile_y < tm->tile_y ) assert( tm->tile_y - player_tile_y <= VIEW_HALF ); + else assert( player_tile_y - tm->tile_y <= VIEW_HALF ); + + const u32 dx = player_tile_x - tm->tile_x; + const u32 dy = player_tile_y - tm->tile_y; + + const u32 vx = view_add( tm->view_left, VIEW_HALF + dx ); + const u32 vy = view_add( tm->view_top, VIEW_HALF + dy ); - // TODO: totally wrong - // convert into tile space - const Heightmap * const hm = &tm->tiles[ tx ][ ty ]; + const Heightmap * const hm = &tm->tiles[ vx ][ vy ]; - return hm->bilerp_height( x - tx * TILE_SIZE, y - ty * TILE_SIZE ); + return hm->bilerp_height( position.x - player_tile_x * TILE_SIZE, + position.y - player_tile_y * TILE_SIZE ); } diff --git a/terrain_manager.h b/terrain_manager.h @@ -43,6 +43,6 @@ void terrain_init( TerrainManager * const tm, const char * const tiles_dir, void terrain_teleport( TerrainManager * const tm, const glm::vec3 position ); void terrain_update( TerrainManager * const tm, const glm::vec3 position ); void terrain_render( const TerrainManager * const tm, const glm::mat4 VP, const float sun_slope ); -float terrain_height( const TerrainManager * const tm, const float x, const float y ); +float terrain_height( const TerrainManager * const tm, const glm::vec3 position ); #endif // _TERRAIN_MANAGER_H_