Jump to content

Problems with Pathroute


photo

Recommended Posts

I am working on making a node move to a destination. I am following the route_02 example and trying to replicate that. For some reason I cannot get it to work. I instance the route, do the updates, and it never seems to hit "if(_route.isReached()) {". Please note I am still using the scratch project.

 

Here are the two important classes:

level1.cpp

#include <unigine.h>
#include <core/scripts/utils.h>
#include <scripts/MousePicking.h>
#include <scripts/MovingObject.h>
UserInterface ui;           	// User interface that loads the UI file
// Widgets that will be automatically constructed on loading menu.ui. To be
// exported into the script, they should be declared in the global scope.
WidgetWindow level_window;  	// Menu window
Node droid;                 	// Droid node
float rotation_speed = 50.0f;   // Speed of Droid rotation
MovingObject box1;   // Box1 node, this will contain it
Vec3 offset = Vec3(0.0f,0.0f,0.0f);
void init_gui() {
// Get the default GUI used by the engine.
Gui gui = engine.getGui();
// Load all widgets from user interface file.
ui = engine.gui.addUserInterface(gui,"project/level1.ui");

// Calculate window size. It is necessary to properly position a widget in
// the same frame.
level_window.arrange();
// Position the window in the left bottom corner of the screen.
level_window.setPosition(0,gui.getHeight() - level_window.getHeight());
// Add the window to be rendered in the default GUI. Only root widgets
// should be added (their children will automatically become visible).
// The window is movable.
gui.addChild(level_window,GUI_ALIGN_OVERLAP);

// Set focus to the window.
level_window.setFocus();
}
void init_nodes() {
// Find a node loaded into the editor from level1.world by its name and
// get its index.
int index = engine.editor.findNode("Droid");

if(index != -1) {
 // Get the node by its index.
 droid = engine.editor.getNode(index);
}
// Make box1 move
index = engine.editor.findNode("Box1");
if (index != -1) {
 box1 = new MovingObject(engine.editor.getNode(index));
}
box1.setDestination(vec3(100.0f,0.0f,100.50f));

// Create a free-flying camera to be used in-game.
PlayerSpectator camera = new PlayerSpectator();
// Set its position used when the world is loaded.
camera.setPosition(vec3(2.0f,0.0f,1.5f));
// Set the viewing direction.
camera.setDirection(vec3(-1.0f,0.0f,-0.5f));

// Set the created camera to render the world when in-game mode. It is necessary
// because the Editor uses its own separate camera.
engine.game.setPlayer(camera);
}
/*
*/
// Callback function executed when "Return to Main menu" button from level1.ui is
// pressed.
void quit_to_main_menu_clicked() {
// Call a system script function that displays a splash screen.
engine.system.call("setMenuSplash");
// Run the console command that shuts down this world and loads the Menu one.
engine.console.run("world_load project/menu");
}
Node add_editor(Node node) {
engine.editor.addNode(node);
return node_remove(node);
}
void remove_editor(Node node) {
engine.editor.removeNode(node);
}

/*
*/
// This function is called when the world is loaded.
int init() {
//init_gui();
init_nodes();
// update scene, used for mouse picking - this is imported from MousePicking.h
if(is_function("update_MousePicking")) {
 thread("update_MousePicking");
}

// Add the pathfinding details
NavigationSector navigation = add_editor(new NavigationSector(vec3(1024.0f,1024.0f,256.0f)));
navigation.setWorldTransform(translate(Vec3(0.0f,0.0f,0.0f) + offset));

return 1;
}
// This function is called when the world is exited.
int shutdown() {
// Remove a user interface instance and delete all its widgets from the memory.
//engine.gui.removeUserInterface(ui);
return 1;
}
// This function is called each frame while the world is running.
int update() {

// Rotate the Droid node around Z axis with correction for the game FPS.
droid.setTransform(droid.getTransform() * rotateZ(rotation_speed * engine.game.getIFps()));
// Update box1
box1.update();
return 1;
}

 

and this is MovingObject.h:

#include <unigine.h>
#include <core/scripts/utils.h>
class MovingObject {
/*-----------------------------------------------
 Local Variables
-----------------------------------------------*/
// this is the controlled object
Node _myControlledNode;
// We need to know if an object can be interactive or not
// assume that objects aren't until they are set to interactive
int _interactive = -1;

// The sphere used to detect collisions
Obstacle _boundingSphere;

// The path for the object to follow, it can be null
PathRoute _route;

// Show the route in 3d for users to see
int _showRoute = 1;

// Route destination
Vec3 _routeDestination;

// Counter for nodes in path
int counter;
/*-----------------------------------------------
 Constructor / Destructors
-----------------------------------------------*/
// Default constructor takes the object being handled
MovingObject(Node node_) {
 _myControlledNode = node_;
 //_boundingSphere = new ObstacleSphere(node_.getBoundRadius());
 //_myControlledNode.addChild(_boundingSphere);
 _route = new PathRoute(2.0f);
 //_route.setExcludeObstacles((_boundingSphere));
}

// Deconstructor to remove objects
~MovingObject() {
 //delete _myControlledNode; // I don't think we need this
 delete _boundingSphere;
 delete _route;
}
/*-----------------------------------------------
 Methods
-----------------------------------------------*/
// Can the object be interacted with by the user?  Default is no
int isInteractive() {
 return _interactive;
}

// Set route for the object to follow
void setRoute(PathRoute route_) {
 _route = route_;
}

// Instruct the object to go to the destination
void setDestination(Vec3 destination_) {
 engine.world.updateSpatial();
 _route.create3D(_myControlledNode.getWorldPosition(), destination_);
 _routeDestination = destination_;
}

// this object was clicked by the user
void clicked() {
 if (_interactive) {
  // do stuff here
 }
}

// Update the object
void update() {
 Vec3 position = _myControlledNode.getWorldPosition();
 quat orientation = _myControlledNode.getWorldRotation();

 //engine.message("Route.isReady() = " + _route.isReady() + ", Route.isReached() = " + _route.isReached() + ", Route.getNumPoints() = " + _route.getNumPoints());
 //engine.message("Destination: " + _routeDestination.xyz);

 if(length(position - _routeDestination) < 2.0f || counter++ > 9000) {
  if(counter > 9000) engine.message(""); //":  PathRoute failed, too far\n");
 }
 else if(_route.isReady()) {
  if(_route.isReached()) {

// get the frames per second
float ifps = engine.game.getIFps();

// determine direction object should point
Vec3 direction = _route.getPoint(1) - _route.getPoint(0);

engine.message(direction.xyz);

if(length(direction) > EPSILON) {
	orientation = lerp(orientation, quat(setTo(Vec3_zero, direction, vec3(0.0f, 0.0f, 1.0f))), ifps * 8.0f);
}

position += _myControlledNode.getWorldDirection() * ifps * 16.0f;
_myControlledNode.setWorldTransform(translate(position) * orientation);

if(_showRoute) _route.renderVisualizer(vec4_one);

// move to the next node
_route.create3D(position, _routeDestination, 1);
  } else {
//engine.message("route has not been reached\n");
  }
 }
 else if(_route.isQueued() == 0) {
  _route.create3D(position, _routeDestination, 1);
 }
}

};

 

Any help in understanding the pathfinding system, or maybe something that I am missing, would be GREATLY appreciated!

Link to comment
×
×
  • Create New...