• 10
• 10
• 12
• 12
• 14

# Getting height on infinite landscape.

This topic is 883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone,

I am doing infinite procedural landscape in my game engine and I've stuck with one problem.

For example, every chunk is 512x512 size and has local coordinates ( 0....512 with different x,z values ) AND information about its placement in the world: m_chunkPosX and m_chunkPosZ..

So, HOW can I convert camera position to chunk's local space ( 0, 512 ), so I will be able to use the following method to get correct height:

// X and Z can be only local space ( 0.. 512 ), but my camera can be everywhere!!
inline float GetHeightAt( int32_t x, int32_t z )
{
return m_mapData[ z * m_iChunkSize /* 512 */ + x ].y;
}


Thanks!

Edited by NekoCode

##### Share on other sites

for any grid you need to compute 4 bounding points of a grid (corners) then its simple

you project  vector (from A to CAM) onto both side sides,[A,D] [A,B] you calculate the resulting points and then use distance function to get percentage of the distance from A then you multiply that by corresponding grid dimension.

for x coord:

Project RED_LINE on BLUE_LINE -> get position -> CAM_VEC_X_POS (vec3)

int x_coord = (distance(x, cam_vec_x_pos) / distance(A,D) ) * 512'

how to calc B C and D? dont ask me if grid was supposed to be axis algined then its simple if not its more complicated and i dont know
Edited by WiredCat

##### Share on other sites

Thanks for help!

I am trying to figure it out by the method you suggested me, but I also have finished it in different way: drop a ray to the landscape chunk!

float normalRay = dot( vNormal, vRayDir ); // vRayDir in current case is a vector down ( 0.0f, -1.0f, 0.0f ).

if( normalRay > 0.0f )
{
float fDistance = (dot(vNormal, vOrigin) + D) / NdotR; // D = -dot( vNormal, vTriangle[0] );

if( fDistance >= 0.0f && fDistance < fMaxDistance ) // fMaxDistance in current case is 1024.0f
{
vOriginOut = vRay * fDistance + vOrigin;

return IsInside(vOriginOut);
}
}