Physics and collision system to make players interact with the generated terraine without falling through the world like lemmings. It uses a shared bilinear interpolation function for terrain height for smooth and consistant height queries on the client and server.
24 lines
754 B
C++
24 lines
754 B
C++
#include <cmath>
|
|
#include "world.h"
|
|
#include "bettola/game/chunk.h"
|
|
#include "bettola/game/terrain.h"
|
|
|
|
float World::get_height(float world_x, float world_z) {
|
|
/*
|
|
* Mesh generation uses (WIDTH-1), so account for it
|
|
* when calculating the chunk and local coords.
|
|
*/
|
|
float chunk_width = (float)(BettolaLib::Game::CHUNK_WIDTH-1);
|
|
float chunk_height = (float)(BettolaLib::Game::CHUNK_HEIGHT-1);
|
|
|
|
int chunk_x = floor(world_x / chunk_width);
|
|
int chunk_z = floor(world_z / chunk_height);
|
|
|
|
float local_x = world_x - (chunk_x * chunk_width);
|
|
float local_z = world_z - (chunk_z * chunk_height);
|
|
|
|
BettolaLib::Game::Chunk& chunk = get_chunk(chunk_x, chunk_z);
|
|
|
|
return BettolaLib::Game::Terrain::get_height_at(chunk, local_x, local_z);
|
|
}
|