Move-on-click system with pandai

Return to Code Snippets

Move-on-click system with pandai

Postby tomallevi » Sat Feb 04, 2012 9:31 am

Hi!!
Than pathfinding c++ post, i write this.
It's move-in-click system using pandai. It's written in c++! Click on the terrain to move ralph! i have used the model founded in the StaticObstacleDemo of the panda's manual ;)
Code: Select all
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "aiWorld.h"
#include "pathFind.h"

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

#include <collisionRay.h>
#include <collisionHandlerQueue.h>
#include <collisionTraverser.h>
#include <collisionSphere.h>
 
PandaFramework framework;
WindowFramework* window;
NodePath ralph;
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();
AIWorld* world;
AIBehaviors* behaviors;

CollisionTraverser trav;
CollisionHandlerQueue* queue;
CollisionRay* ray;
NodePath pointer;

void load_models();
void set_up_ai();

void set_move(const Event* ev, void* data);
AsyncTask::DoneStatus ai_update (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();
   
    pointer = NodePath("pointer");
    pointer.reparent_to(window->get_render());
   
    ray = new CollisionRay();
    CollisionNode* n = new CollisionNode("collisionnode");
    n->add_solid(ray);
    n->set_from_collide_mask(BitMask32::bit(22));
    n->set_into_collide_mask(BitMask32::all_off());
    NodePath p(n);
    p.reparent_to(window->get_camera_group());
   
    queue = new CollisionHandlerQueue();
    trav.add_collider(p, queue);
   
    load_models();
    set_up_ai();
   
    framework.define_key("mouse3", "click", &set_move, (void*) window);
   
    framework.main_loop();
    framework.close_framework();
    return (0);
}

void set_up_ai() {
    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");
    behaviors->path_find_to(pointer);
    taskMgr->add(new GenericAsyncTask("AIUpdate", &ai_update, (void*) NULL));
    // add static obstable
    NodePath box1 = framework.get_window(0)->load_model(framework.get_models(), "models/box");
    box1.reparent_to(framework.get_window(0)->get_render());
    box1.set_pos(-30, 25, 0);
    behaviors->add_static_obstacle(box1);
    NodePath box2 = framework.get_window(0)->load_model(framework.get_models(), "models/box");
    box2.reparent_to(framework.get_window(0)->get_render());
    box2.set_pos(30, 0, 0);
    behaviors->add_static_obstacle(box2);
    NodePath box3 = framework.get_window(0)->load_model(framework.get_models(), "models/box");
    box3.reparent_to(framework.get_window(0)->get_render());
    box3.set_pos(-30, -25, 0);
    behaviors->add_static_obstacle(box3);
}

void set_move(const Event* ev, void* data) {
    WindowFramework* win = framework.get_window(0);
    float x = (2 * win->get_graphics_window()->get_pointer(0).get_x()) / ((float) win->get_graphics_window()->get_x_size()) - 1;
    float y = (2 * win->get_graphics_window()->get_pointer(0).get_y()) / ((float) win->get_graphics_window()->get_y_size()) - 1;
    ray->set_from_lens(win->get_camera(0), x, -y);
    trav.traverse(win->get_render());
    queue->sort_entries();
    if (queue->get_num_entries() == 0) {
        cout << "click on the terrain!!" << endl;
        return;
    }
    pointer.set_pos(queue->get_entry(0)->get_surface_point(win->get_render()));
}

AsyncTask::DoneStatus ai_update (GenericAsyncTask* task, void* data) {
    world->update();
    return AsyncTask::DS_cont;
}

void load_models() {
    window->get_camera_group().set_pos_hpr(0, -210, 135, 0, 327, 0);
   
    NodePath env = window->load_model(framework.get_models(), "models/groundPlane");
    env.reparent_to(window->get_render());
    env.set_pos(0, 0, 0);
    env.set_collide_mask(BitMask32::bit(22));
   
    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);
}


compile it on Linux with
Code: Select all
g++ -c file.cpp -o file.o -fPIC -O2 -I/usr/include/python2.6 -I/usr/include/panda3d -fpermissive && g++ file.o -o file -fPIC -L/usr/lib64/panda3d -lp3framework -lpanda -lpandafx -lpandaexpress -lp3dtoolconfig -lp3dtool -lp3pystub -lp3direct -lpandaai && ./file


Happing code!
tomallevi
 
Posts: 99
Joined: Sat Sep 17, 2011 3:56 am

Postby carharttguy » Wed Feb 22, 2012 2:04 pm

Couldn't get this to work with C++, but what does it do actually? It is move-on-click like RuneScape or something?

any chance on a Python version? Otherwise I try to find it out myself, but my and C++, if you know what I mean. Lol
carharttguy
 
Posts: 34
Joined: Tue Feb 21, 2012 11:33 am
Location: Belgium


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 0 guests