Class: NavMesh
Lua
C++
Edit

NavMesh::PlotPath

This method is used to plot a path between two points.

Syntax

Parameter Description
origin path starting point
destination path end point

Returns

Returns a variable-sized array containing the plotted path points. If a path could not be plotted between the origin and destination, then the returned array will have a size of zero.

Example

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

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

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

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

    //Create a camera    
    auto camera = CreateCamera(world);
    camera->SetFov(70);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 3, -6);
    camera->SetRotation(35, 0, 0);

    //Create light
    auto light = CreateBoxLight(world);
    light->SetRange(-20, 20);
    light->SetArea(20, 20);
    light->SetRotation(35, 35, 0);
    light->SetColor(3);

    //Create scene
    auto ground = CreateBox(world, 10, 1, 10);
    ground->SetPosition(0, -0.5, 0);
    ground->SetColor(0, 1, 0);
    auto wall = CreateBox(world, 1, 2, 4);

    //Create navmesh
    auto navmesh = CreateNavMesh(world, 5, 4, 4);
    navmesh->Build();

    //Create player
    auto player = CreateCylinder(world, 0.4, 1.8);
    player->SetNavObstacle(false);
    player->SetColor(0, 0, 1);
    auto agent = CreateNavAgent(navmesh);
    player->Attach(agent);
    agent->SetPosition(navmesh->RandomPoint());

    std::vector<shared_ptr<Entity> > nodes;

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->MouseHit(MOUSE_LEFT))
        {
            auto mousepos = window->GetMousePosition();
            auto rayinfo = camera->Pick(framebuffer, mousepos.x, mousepos.y);
            if (rayinfo.success)
            {
                auto path = navmesh->PlotPath(player->position, rayinfo.position);
                nodes.resize(path.size());
                for (int n = 0; n < nodes.size(); ++n)
                {
                    if (nodes[n] == NULL)
                    {
                        nodes[n] = CreateSphere(world, 0.25);
                        nodes[n]->SetNavObstacle(false);
                        nodes[n]->SetColor(1, 0, 0);
                    }
                    nodes[n]->SetPosition(path[n]);
                }
            }
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}
Copyright © 2024 Ultra Software.
All rights reserved.