This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
UnigineEditor
界面概述
资产工作流程
设置和首选项
项目开发
调整节点参数
Setting Up Materials
Setting Up Properties
照明
Landscape Tool
Sandworm
使用编辑器工具执行特定任务
Extending Editor Functionality
嵌入式节点类型
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Creating Routes

Unigine has a built-in pathfinding system that includes navigation areas, obstacles and functions of the PathRoute class that are used to calculate the optimal routes among obstacles within navigation areas.

Warning
3D navigation feature is experimental and not recommended for production use.

Via UnigineEditor, you can only add a navigation area (a sector or a mesh) to the scene and place obstacles. The 2D or 3D route that is calculated within the navigation area should be created from the code.

Creating a Route within Navigation Area#

To create a route within a navigation area, in which no obstacles are placed, you can use the following:

Source code (C++)
// create a new route
PathRoutePtr route = PathRoute::create();
// set a radius for the point which will move along the route
route->setRadius(2.0f);

// declare points between which the route should be calculated
Vec3 p0 = Vec3(-60.0f,-60.0f,5.0f);
Vec3 p1 = Vec3( 60.0f, 60.0f,5.0f);

// create a 3D route
route->create3D(p0,p1);

Notice
You can create a 2D route the same way by calling the create2D() function.

To visualize the calculated route, call the renderVisualizer() function of the PathRoute class:

Source code (C++)
route->renderVisualizer(vec4(1.0f));
To visualize the navigation area, call the renderVisualizer() functions of the Node class:
Source code (C++)
sector->renderVisualizer();

Notice
You should enable the engine visualizer by calling Visualizer::setEnabled(1);

You can affect route calculation via UnigineEditor by adjusting parameters of the navigation sector or mesh.

Creating a Route within Navigation Area with Obstacles#

Creating the route within a navigation area with obstacles is similar to creating the route within an empty navigation area. Moreover, the route will be recalculated if the obstacle changes its transformation.

If the obstacle is connected with a dynamically changing node that should be bypassed, this node should be set as a parent node for the obstacle. This will enable simultaneous changing transformation of the node and the obstacle. For example:

Source code (C++)
// AppWorldLogic.h

#include <UnigineLogic.h>
#include <UnigineStreams.h>
#include <UnigineObjects.h>
#include <UniginePathFinding.h>

class AppWorldLogic : public Unigine::WorldLogic {
	
public:
	AppWorldLogic();
	virtual ~AppWorldLogic();
	
	virtual int init();
	
	virtual int update();
	virtual int postUpdate();
	virtual int updatePhysics();
	
	virtual int shutdown();
	
	virtual int save(const Unigine::StreamPtr &stream);
	virtual int restore(const Unigine::StreamPtr &stream);
	
private:
	
	// declare the required variables
	Unigine::ObstacleBoxPtr box;
	Unigine::ObjectMeshStaticPtr mesh;
	Unigine::PathRoutePtr route;
	// declare points between which a route should be calculated
	Unigine::Math::Vec3 p0 = Unigine::Math::Vec3(-60.0f, -60.0f, 5.0f);
	Unigine::Math::Vec3 p1 = Unigine::Math::Vec3(60.0f, 60.0f, 5.0f);

};
Source code (C++)
// AppWorldLogic.cpp

#include <UnigineEditor.h>
#include <UnigineGame.h>
#include <UnigineVisualizer.h>
#include <UnigineLog.h>

using namespace Unigine;
using namespace Math;

int AppWorldLogic::init() {

	// enable the engine visualizer
	Visualizer::setEnabled(1);

	// create a navigation sector within which pathfinding will be performed
	NavigationSectorPtr navigation = NavigationSector::create(vec3(128.0f, 128.0f, 8.0f));
	navigation->setWorldTransform(translate(Vec3(1.0f, 1.0f, 5.0f)));

	// create a mesh by adding a box surface to it
	MeshPtr mesh_0 = Mesh::create();
	mesh_0->addBoxSurface("box_surface", vec3(1.0f));
	// create the ObjectMeshStatic that should be bypassed
	mesh = ObjectMeshStatic::create(mesh_0);

	mesh->setMaterial("mesh_base", "*");
	mesh->setPosition(Vec3(2.0f, 2.2f, 1.5f));
	mesh->setScale(vec3(2.0f, 2.0f, 2.0f));

	// create an obstacle
	box = ObstacleBox::create(vec3(1.2f, 1.2f, 1.2f));

	box->setPosition(Vec3(0.0f, 0.0f, 0.0f));
	// add the obstacle as the child node to the mesh in order to change their transformation simultaneously
	mesh->addChild(box);

	// create a new route
	route = PathRoute::create();
	// set a radius for the point which will move along the route
	route->setRadius(1.2f);

	return 1;
}

// start of the main loop
int AppWorldLogic::update() {
	
	// get the frame duration
	float ifps = Game::getIFps();
	// and define the angle of the object's rotation
	float angle = ifps * 90.0f;

	// change transformation of the mesh
	mesh->setTransform(mesh->getTransform() * Mat4(rotateZ(angle)));
	// render the bounding box of the obstacle
	box->renderVisualizer();

	// recalculate the route in the current frame and render its visualizer
	route->create2D(p0, p1);
	if (route->isReached()) route->renderVisualizer(vec4(1.0f));
	else Log::message("PathRoute failed");

	return 1;
}
Notice
In the example above, the route is recalculated each frame. However, it is non-optimal for application performance. You can calculate the route, for example, once per 10 frames.

In the result, you will have a simple navigation sector, in which the dynamically changing obstacle box is placed.

Here the obstacle is highlighted with red
Last update: 2021-04-09
Build: ()