Jump to content

Terrain GetElevation returns 0


Andy90
 Share

Go to solution Solved by Josh,

Recommended Posts

For some reasons the terrain elevation returns allways 0. Im not sure if its a bug or if i did something wrong.

 

void LootSpawner::SpawnLoot(shared_ptr<Terrain> terrain, shared_ptr<Map> scene, std::weak_ptr<Entity> player)
{
    auto entity = GetEntity();

    CreateDebugSpehere(entity->GetWorld(), scene, entity->position, Vec4(1.00, 0.07, 0.07, 1.0));

    if (!prefabList.empty()) {
        for (int i = 0; i < this->itemCount; i++) {
            float degress = 360 / this->itemCount * i;
            auto radians = Radians(degress);
            auto distance = Random(diameterMin, diameterMax);
            auto sin = Sin(degress);
            auto cos = Cos(degress);

            float x = entity->GetPosition(true).x + distance * cos;
            float z = entity->GetPosition(true).z + distance * sin;
            float y = terrain->GetElevation(x, z);

            Print("Terrain height " + String(y));

            int prefabIndex = Random(0, prefabList.size());
            auto prefabName = prefabList[prefabIndex];
            auto prefab = LoadPrefab(entity->GetWorld(), prefabName);
            prefab->SetPosition(x, y, z);

            auto lootCrate = prefab->GetComponent<LootCrate>();
            lootCrate->SetPlayer(player);

            scene->AddEntity(prefab);
            CreateDebugSpehere(entity->GetWorld(), scene, Vec3(x, y, z));
        }
    }
    this->spawned = true;
}

void LootSpawner::CreateDebugSpehere(shared_ptr<World> world, shared_ptr<Map> scene, Vec3 location, Vec4 color)
{
    if (debug) {
        auto sourceSphere = CreateSphere(world);
        sourceSphere->SetColor(color);
        sourceSphere->SetPosition(location);
        scene->AddEntity(sourceSphere);
    }
}

bool LootSpawner::Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Map> scene, const LoadFlags flags)
{
    itemCount = properties["itemCount"];
    prefabs = properties["prefabs"];
    diameterMin = properties["diameterMin"];
    diameterMax = properties["diameterMax"];
    auto player = scene->GetEntity(properties["player"]);
    prefabList = prefabs.Split(";");

    for (auto entity : scene->entities) {
        if (entity->As<Terrain>()) {
            SpawnLoot(entity->As<Terrain>(), scene, player);
        }
    }

    return true;
}

 

Link to comment
Share on other sites

I cannot produce this error in my own testing:

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto cl = ParseCommandLine(argc, argv);

    //Load FreeImage plugin (optional)
    auto fiplugin = LoadPlugin("Plugins/FITextureLoader");

    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1920, 1080, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    auto sz = window->ClientSize();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a world
    auto world = CreateWorld();

    //Load the map
    WString mapname = "Maps/test.ultra";
    if (cl["map"].is_string()) mapname = std::string(cl["map"]);
    auto scene = LoadMap(world, mapname);

    world->RecordStats(true);

    shared_ptr<Terrain> terrain;

    for (auto entity : scene->entities)
    {
        terrain = entity->As<Terrain>();
        if (terrain) break;
    }

    Print(terrain->GetElevation(0, 0, 0));
    return 0;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • Solution

The overload you are using above expects integer coordinates but you are giving it a world position. If either of those values is less than zero or greater than the terrain resolution, the returned value will be 0.0.

You can do this instead:

float x = entity->GetPosition(true).x + distance * cos;
float z = entity->GetPosition(true).z + distance * sin;
float y = terrain->GetElevation(Vec3(x, 0.0f, z));

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...