Calculate height on Z axis

  • Hi.


    I want to make a function in API to teleport player on location (x,z), let say function teleport_to_surface(x,z). For this i need to calculate a safe Y value so player don't teleport underground or on high and fall to death. So require is to teleport on ground on terrain or if is any building block here, on top of it.


    Any can have a idea to accomplish that?

    First try was to use function raycastFromWorldPosition(...) but this have serious limitless like chunk when i want to teleport need to be rendered on client side and if teleport far away or on unexplored world .. this approach do not function.

    For second try i think to read somehow position of Z direct from chunk data, but here i am stuck i don't have idea how i can do that. Moreover what happen if i want to work with a chunk which was not yet generated by server ...


    Thx in advance for hints!

  • For second try i think to read somehow position of Z direct from chunk data, but here i am stuck i don't have idea how i can do that

    Hi, size of blocks in terrain is actually the same as units that player position have (1 block = 1 unit), so you only need to get terrain from specific chunk and iterate it from above until you will find any solid tile (anything with ID greater than 0 - air, and less then 200 - water). Chunk coordinates can be generated from desired position - there is an utility function for that (in ChunkUtils class), or you can divide your position on chunk width and height with flooring the result. Block coordinates inside chunk are relative to chunk position, so their calculation is also not very hard (you need to subtract chunkPos * chunkSize from your absolute block position to get relative position in chunk), and there is also utility function for that.


    what happen if i want to work with a chunk which was not yet generated by server

    I think that server will just return you null

  • To get the surface lvl at a certain position, you could use the getLODSurfaceLevel() method of the Chunk class - it returns the elevation at a given position, but only takes terrain into account. For example, if you have a particular world position and want to get the surface elevation, you can use the ChunkUtils, as mentioned by paulevs :



    Moreover what happen if i want to work with a chunk which was not yet generated by server ...

    If the chunk doesn't exist, the server will create a new chunk (but it's only generated into RAM - so unless it gets changed, the server won't save it in the database). So basically the World.getChunk() method never returns null (unless an error occurs). Unfortunately this isn't mentioned in the documentation... we'll change that ;)

  • Thx red51.


    Lol i don't read until late after i figure how to make alone.

    BTW i encountered server crash when i try to use function getLODSurfaceLevel() :(

    I will try example from you to see if server still crash when i use function.


    So i make something like this:



    I tested your code. No crash! Function seems to work properly .

    After i tested again my first variant. All worked fine. :thumbup:


    I am not sure why earlier test server crashed many time with exception UNCAUGHT ...



  • I tested your code. No crash! Function seems to work properly .

    After i tested again my first variant. All worked fine. :thumbup:


    I am not sure why earlier test server crashed many time with exception UNCAUGHT ...

    Hmm... maybe the x or z coordinate was out of bounds? I just realized that the getLODSurfaceLevel() method has no safety checks :wat: The game works with native memory here, so if x or z is out of bounds (i.e. < 0 or >= 32), it accesses an invalid memory address which may result in a hard crash...

    We'll change that with the next update, so the API will just throw a proper exception then^^


    However, if you run into any other crashes (or if the method mentioned above still causes crashes despite using valid indices), please let me know ;)

  • Hmm... maybe the x or z coordinate was out of bounds? I just realized that the getLODSurfaceLevel() method has no safety checks :wat: The game works with native memory here, so if x or z is out of bounds (i.e. < 0 or >= 32), it accesses an invalid memory address which may result in a hard crash...

    We'll change that with the next update, so the API will just throw a proper exception then^^


    However, if you run into any other crashes (or if the method mentioned above still causes crashes despite using valid indices), please let me know ;)

    For sure that was. i think i messed initially calculate x and y of block position and because that i get out of bounds and game no check and got server crash.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!