Programming
Fundamentals
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes

Intersections

Unigine has different methods to detect intersections. Intersection is a generic point of the defined area (or line) and an object. This article describes different types of intersections and their usage.

There are three main types of intersections:

Shape and Object classes have their own getIntersection() functions. They are used for detecting intersections with the specific shape or the specific surface of the object.

World Intersection

By using different overloaded World class getIntersection() functions you can find objects and nodes that intersect bounds of the specified bounding volume or find the first intersection of the object with the invisible tracing line.

World class functions for intersection search can be divided into 3 groups:

Intersection with Nodes

To find the intersection of nodes with bounds of the specified volume, you should use functions that have a bounding volume and a vector of nodes in arguments:

Schematic visual representation of intersection with nodes in bounding volume.

These functions return the value indicating the result of the intersection search: 1 if the intersection was found; otherwise, 0. Intersected nodes can be found in the vector that passed as an argument to the function.

To clarify the nodes search, perform the following:

  • Specify the type of node to search.

Usage Example

In the example below, the engine checks intersections with a bounding box and shows the message in the console.

In the update() method: the engine checks the intersection and shows the message about intersection.

Source code (C++)
// AppWorldLogic.cpp
#include "AppWorldLogic.h"
#include "UnigineMathLib.h"
#include "UnigineWorld.h"

using namespace Unigine;
using namespace Unigine::Math;

/* ... */

int AppWorldLogic::update() {
	// initialize a bounding box and vector for intersected nodes
	WorldBoundBox boundBox(Vec3(0.0f), Vec3(1.0f));
	Vector<Ptr<Node>> nodes;

	// check the intersection with nodes
	int result =  World::get()->getIntersection(boundBox, nodes);
	if (result)
	{
		for (int i = 0; i < nodes.size(); i++)
		{
			Log::message("Intersected node: %s \n", nodes.get(i)->getName());
		}
		Log::message("The number of intersected nodes is: %i \n", nodes.size());
	}

	return 1;
}

/* ... */

Intersection with Objects

The following functions can be used to perform the intersection search with objects:

Schematic visual representation of intersection with objects in bounding volume.

These functions return the value indicating the result of the intersection search: 1 if the intersection was found; otherwise, 0. You can pass the start (vec3 p0) and the end (vec3 p1) point of the intersecting line or pass the (BoundBox, BoundFrustum, BoundSphere) to get the intersection of objects with a line or with a bounding volume, respectively. Intersected nodes can be found in the vector that passed as an argument to the function.

Usage Example

In the example below, the engine checks intersections with a bounding box and shows the message in the console.

In the update() method: the engine checks the intersection and shows the message about intersection.

Source code (C++)
// AppWorldLogic.cpp
#include "AppWorldLogic.h"
#include "UnigineMathLib.h"
#include "UnigineWorld.h"

using namespace Unigine;
using namespace Unigine::Math;

/* ... */

int AppWorldLogic::update() {
	// initialize a bounding box and vector for intersected objects
	WorldBoundBox boundBox(Vec3(0.0f), Vec3(1.0f));

	Vector<Ptr<Object>> objects;

	int result = World::get()->getIntersection(boundBox, objects);
	if (result)
	{
		for (int i = 0; i < objects.size(); i++)
		{
			Log::message("Intersected node: %s \n", objects.get(i)->getName());
		}
		Log::message("The number of intersected nodes is: %i \n", objects.size());
	}

	return 1;
}

/* ... */

First Intersected Object

The following functions are used to find the nearest intersected object with the traced line. You should specify the start point and the end point of the line, and the function detects if there are any object on the way of this line.

Schematic visual representation of intersection with objects and traced line.

These functions return an intersection information and a pointer to the nearest object to the start point (p0). Information about the intersection can be presented in standard vectors or in the following format that you pass to functions as arguments:

  • WorldIntersection intersection - the WorldIntersection class instance. By using this class you can get the intersection point (coordinates), the index of the intersected triangle of the object and the index of the intersected surface.
  • WorldIntersectionNormal normal - the WorldIntersectionNormal class instance. By using this class you can get only the normal of the intersection point.
  • WorldIntersectionTexCoord texcoord - the WorldIntersectionTexCoord class instance. By using this class you can get only the texture coordinates of the intersection point.

These functions detect intersection with surfaces (polygons) of meshes. But there are some conditions to detect the intersection with the surface:

  1. A per-surface Intersection flag is enabled.
    Notice
    You can set this flag to the object's surface by using the Object.setIntersection() function.
  2. An Intersection option of the property in the Properties window is enabled.
    Notice
    You can enable this property by using the Property.setIntersection() function.
  3. A surface is enabled.
  4. A surface has an assigned material.
  5. A surface has an assigned property.

To clarify the object search, perform the following:

  • Use an intersection mask. An intersection is found only if an object is matching the intersection mask.
  • Specify the list of objects (nodes) to exclude and pass to the function as an argument.

Usage Example

In the example below, the engine checks intersections with a raytraced line and shows the message in the console.

In the update() method: the engine checks the intersection and shows the message about intersection.

Source code (C++)
// AppWorldLogic.cpp
#include "AppWorldLogic.h"
#include "UnigineMathLib.h"
#include "UnigineWorld.h"

using namespace Unigine;
using namespace Unigine::Math;

/* ... */

int AppWorldLogic::update() {
	WorldIntersectionPtr wi =  WorldIntersection::create();
	ObjectPtr o;

	if (wi)
		o = World::get()->getIntersection(Vec3(0.0f), Vec3(1.0f), 1, wi);

	if (o)
	{
		Log::message("Intersected object is: %s \n", o->getName());
	}
	return 1;
}

/* ... */

Game Intersection

Game class getIntersection() functions are used to check the intersection with obstacles (pathfinding nodes):

The engine creates an invisible cylinder with specified radius between two points and checks if an obstacle is presented inside of it.

Schematic visual representation of intersection with objects and cylinder volume.

These functions return an intersection information and a pointer to the nearest obstacle to the start point (p0). Information about the intersection will be presented in the GameIntersection class instance which you pass to the function as an argument or in the vector.

Use mask and exclude arguments of the overloaded function to specify the obstacle search.

To clarify the obstacle search, perform the following:

  • Use an obstacle intersection mask. An intersection is found only if an object is matching the intersection mask, otherwise it is ignored.
  • Specify the list of obstacles to exclude and pass to the function as an argument.
Last update: 2017-07-03