pathfinding c++


#1

Hi!!
I haven’t found a example of pathfinding in c++. i found only in python. i translate this http://www.panda3d.org/manual/index.php/Static_Obstacles to c++ language.

#include "pandaFramework.h"
#include "pandaSystem.h"
#include "aiWorld.h"

#include "genericAsyncTask.h"
#include "asyncTaskManager.h"

#define speed 0.75

#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
int keys[4] = {0, 0, 0, 0};
 
PandaFramework framework;
WindowFramework* window;
NodePath ralph;
NodePath pointer;
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();
AIWorld* world; 
AIBehaviors* behaviors;
NodePath box;

bool pointer_move = false;

void load_models();
void set_up_keys();
void set_up_ai();

void set_up_key(const Event* ev, void* data);
void set_down_key(const Event* ev, void* data);

void set_move(const Event* ev, void* data);
void add_block(const Event* ev, void* data);
void add_big_block(const Event* ev, void* data);
void add_obstacle(const Event* ev, void* data);

AsyncTask::DoneStatus ai_update (GenericAsyncTask* task, void* data);
AsyncTask::DoneStatus mover (GenericAsyncTask* task, void* data);

int main(int argc, char *argv[]) {
    framework.open_framework(argc, argv);
    framework.set_window_title("My Panda3D Window");
    window = framework.open_window();
    framework.get_window(0)->enable_keyboard();
    // load models
    load_models();
    set_up_keys();
    // load ai
    set_up_ai();
    
    framework.main_loop();
    framework.close_framework();
    return (0);
}

void set_up_ai() {
    // create AI world
    world = new AIWorld(window->get_render());
    AICharacter* ai_char = new AICharacter("ralph", ralph, 60, 0.05, 15);
    world->add_ai_char(ai_char);
    behaviors = ai_char->get_ai_behaviors();
    behaviors->init_path_find("models/navmesh.csv");
    // AI world update
    taskMgr->add(new GenericAsyncTask("AIUpdate", &ai_update, (void*) NULL));
}

void set_move(const Event* ev, void* data) {
    behaviors->path_find_to(pointer);
    window->loop_animations(0);
}

void add_block(const Event* ev, void* data) {
    pointer_move = true;
    box = window->load_model(framework.get_models(), "models/box");
    box.set_pos(0, -60, 0);
    box.set_scale(1, 1, 1);
    box.reparent_to(window->get_render());
}

void add_big_block(const Event* ev, void* data) {
    pointer_move = true;
    box = window->load_model(framework.get_models(), "models/box");
    box.set_pos(0, -60, 0);
    box.set_scale(2, 2, 2);
    box.reparent_to(window->get_render());
}

void add_obstacle(const Event* ev, void* data) {
    if (pointer_move) {
        behaviors->add_static_obstacle(box);
        pointer_move = false;
    }
}

AsyncTask::DoneStatus ai_update (GenericAsyncTask* task, void* data) {
    cout << "is possible" << behaviors->_path_find_obj->_path_finder_obj->_closed_list.empty() <<  endl;
    world->update();
    return AsyncTask::DS_cont;
}

AsyncTask::DoneStatus mover (GenericAsyncTask* task, void* data) {
    LPoint3f st = pointer.get_pos();
    LPoint3f d;
    if (keys[LEFT])
        pointer.set_pos(st + LPoint3f(-speed, 0, 0));
    if (keys[RIGHT])
        pointer.set_pos(st + LPoint3f(speed, 0, 0));
    if (keys[UP])
        pointer.set_pos(st + LPoint3f(0, speed, 0));
    if (keys[DOWN])
        pointer.set_pos(st + LPoint3f(0, -speed, 0));
    
    if (pointer_move)
        box.set_pos(pointer.get_pos());
    return AsyncTask::DS_cont;
}

void set_up_keys() {
    framework.define_key("enter", "enter", &set_move, (void*)NULL);
    framework.define_key("1", "1", &add_block, (void*)NULL);
    framework.define_key("2", "2", &add_big_block, (void*)NULL);
    framework.define_key("space", "add_obstacle", &add_obstacle, (void*)NULL);
    
    framework.define_key("arrow_left", "l", &set_down_key, (void*) keys +LEFT);
    framework.define_key("arrow_right", "r", &set_down_key, (void*)(keys +RIGHT));
    framework.define_key("arrow_up", "u", &set_down_key, (void*)(keys +UP));
    framework.define_key("arrow_down", "d", &set_down_key, (void*)(keys +DOWN));
    
    framework.define_key("arrow_left-up", "l", &set_up_key, (void*)(keys +LEFT));
    framework.define_key("arrow_right-up", "r", &set_up_key, (void*)(keys +RIGHT));
    framework.define_key("arrow_up-up", "u", &set_up_key, (void*)(keys +UP));
    framework.define_key("arrow_down-up", "d", &set_up_key, (void*)(keys +DOWN));
    
    taskMgr->add(new GenericAsyncTask("Mover", &mover, (void*) NULL));
}

void set_up_key(const Event* ev, void* data) {
    *((int*) data)  = 0;
}
void set_down_key(const Event* ev, void* data) {
    *((int*) data)  = 1;
}

void load_models() {
    window->get_camera_group().set_pos_hpr(0, -210, 135, 0, 327, 0);
    
    NodePath env1 = window->load_model(framework.get_models(), "models/skydome");
    env1.set_pos(0, 0, 0);
    env1.set_scale(1, 1, 1);
    env1.reparent_to(window->get_render());
    NodePath env2 = window->load_model(framework.get_models(), "models/skydome");
    env2.reparent_to(window->get_render());
    env2.set_p(180);
    env2.set_h(270);
    env2.set_scale(1, 1, 1);
    NodePath env = window->load_model(framework.get_models(), "models/groundPlane");
    env.reparent_to(window->get_render());
    env.set_pos(0, 0, 0);
    
    LPoint3f start_point(-51, -64, 0);
    ralph = window->load_model(framework.get_models(), "models/ralph");
    ralph.reparent_to(window->get_render());
    ralph.set_scale(2, 2, 2);
    ralph.set_pos(start_point);
    window->load_model(ralph, "models/ralph-run");
    window->loop_animations(0);
    
    pointer = window->load_model(framework.get_models(), "models/arrow");
    pointer.set_color(1, 0, 0);
    pointer.set_pos(0, -0, 0);
    pointer.set_scale(3, 3, 3);
    pointer.reparent_to(window->get_render());
}

to get the model you follow the link of at the end of the linked page!

UPDATE: I have changed this line

behaviors->_path_find_obj->_path_finder_obj->_closed_list.empty()

It’s printed 0 if there’s a calculated path or if the pathfinding is not enable.