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:
- World intersection - an intersection with objects and nodes.
- Physics intersection - an intersection with shapes and collision objects.
- Game intersection - an intersection with pathfinding nodes like obstacles.
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:
- Functions to find nodes within a bounding volume.
- Functions to find objects within a bounding volume or intersected with a traced line.
- Functions to find first intersected object with a traced line.
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:
- int getIntersection(const UNIGINE_BOUND_BOX & bb, Vector< Ptr<Node> > & nodes)
- int getIntersection(const UNIGINE_BOUND_SPHERE & bs, Vector< Ptr<Node> > & nodes)
- int getIntersection(const UNIGINE_BOUND_BOX & bb, int type, Vector< Ptr<Node> > & nodes)
- int getIntersection(const UNIGINE_BOUND_SPHERE & bs, int type, Vector< Ptr<Node> > & nodes)
- int getIntersection(const UNIGINE_BOUND_FRUSTUM & bf, int type, Vector< Ptr<Node> > & nodes)
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.
// 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:
- int getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, Vector< Ptr<Object> > & objects)
- int getIntersection(const UNIGINE_BOUND_BOX & bb, Vector< Ptr<Object> > & objects)
- int getIntersection(const UNIGINE_BOUND_SPHERE & bs, Vector< Ptr<Object> > & objects)
- int getIntersection(const UNIGINE_BOUND_FRUSTUM & bf, Vector< Ptr<Object> > & objects)
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.
// 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.
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, Math::Vec3 * ret_point, Math::vec3 * ret_normal, Math::vec4 * ret_texcoord, int * ret_index, int * ret_surface)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, Math::Vec3 * ret_point, Math::vec3 * ret_normal, Math::vec4 * ret_texcoord, int * ret_index, int * ret_surface)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Ptr<WorldIntersection> & v)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Ptr<WorldIntersectionNormal> & v)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Ptr<WorldIntersectionTexCoord> & v)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, const Ptr<WorldIntersection> & v)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, const Ptr<WorldIntersectionNormal> & v)
- Ptr<Object> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, int mask, const Vector< Ptr<Node> > & exclude, const Ptr<WorldIntersectionTexCoord> & v)
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:
- The surface is enabled.
- The surface has a material assigned.
- Per-surface Intersection flag is enabled.
You can set this flag to the object's surface by using the Object.setIntersection() function.
- A property inherited from the surface_base is assigned to the surface and the intersection parameter of this property is set to 1.
You can set this parameter via the setParameterInt() method function.
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.
// 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;
}
/* ... */
Physics Intersection
Physics intersection function detects the intersection with a shape of bodies. If the object has no body, this function detects intersection with surfaces (polygons) of the object with intersection flag. When you need to find the intersection with the shape of the object (not with polygons), the intersection function of Physics class is the best way to get it.
There are 4 functions to find physics intersections:
- Ptr<Object> getIntersection(const Math::Vec3 &p0, const Math::Vec3 &p1, int mask, const Vector<Ptr<Node>> &exclude, const Ptr<PhysicsIntersection> &intersection)
- Ptr<Object> getIntersection(const Math::Vec3 &p0, const Math::Vec3 &p1, int mask, const Ptr<PhysicsIntersection> &intersection)
- Ptr<Object> getIntersection(const Math::Vec3 &p0, const Math::Vec3 &p1, int mask, const Ptr<PhysicsIntersectionNormal> &intersection)
- Ptr<Object> getIntersection(const Math::Vec3 &p0, const Math::Vec3 &p1, int mask, const Vector<Ptr<Node>> &exclude, const Ptr<PhysicsIntersectionNormal> &intersection)
Thus, to exclude some obstacles you should use these methods:
- Use an intersection mask. An intersection is found only if an object is matching the intersection mask, otherwise it is ignored.
- Specify the list of objects to exclude and pass to the function as an argument.
Usage Example
The following example shows how you can get the intersection information by using the PhysicsIntersection class. In this example we specify a line from the point of the camera (vec3 p0) to the point of the mouse pointer (vec3 p1). The executing sequence is the following:
- Define and initialize two points (p0 and p1) by using the Player::getDirectionFromScreen() function.
- Create an instance of the PhysicsIntersection class to get the intersection information.
- Check, if there is an intersection with an object with a shape or a collision object. The getIntersection() function returns an intersected object when the object intersects with the traced line.
- In this example, when the object intersects with the traced line, all the surfaces of the intersected object change their material parameters. If the object has a shape, its information will be shown in console. The PhysicsIntersection class instance gets the coordinates of the intersection point and the Shape class object. You can get all these fields by using getShape(), getPoint() functions.
// AppWorldLogic.cpp
#include <UnigineEditor.h>
#include <UniginePhysics.h>
#include <UnigineGame.h>
#include <UnigineApp.h>
#include <UnigineObjects.h>
using namespace Unigine;
using namespace Math;
int AppWorldLogic::init() {
// create a mesh instance with a box surface
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box", vec3(0.2f));
// create a new dynamic mesh from the Mesh instance and add it to editor
ObjectMeshDynamicPtr dynamic_mesh = ObjectMeshDynamic::create(mesh);
dynamic_mesh->release();
Editor::get()->addNode(dynamic_mesh->getNode());
// assign a material and a property to the dynamic mesh
dynamic_mesh->setWorldTransform(translate(Vec3(0.0f, 0.0f, 2.0f)));
dynamic_mesh->setMaterial("mesh_base", "*");
dynamic_mesh->setProperty("surface_base", "*");
// assign a body and a shape to the dynamic mesh
BodyRigidPtr body = BodyRigid::create(dynamic_mesh->getObject());
ShapeBoxPtr shape = ShapeBox::create(body->getBody(), vec3(0.2f));
return 1;
}
// start of the main loop
int AppWorldLogic::update() {
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
PlayerPtr player = Game::get()->getPlayer();
if (player.get() == NULL)
return 0;
// get width and height of the current application window
int width = App::get()->getWidth();
int height = App::get()->getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App::get()->getMouseX();
int mouse_y = App::get()->getMouseY();
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player->getDirectionFromScreen(p0, p1, mouse_x, mouse_y, width, height);
// create the instance of the PhysicsIntersection object to save the information about the intersection
PhysicsIntersectionPtr intersection = PhysicsIntersection::create();
// get an intersection
ObjectPtr object = Physics::get()->getIntersection(p0, p1, 1, intersection);
// if the intersection has been occurred, change the parameter of the object's material
if (object)
{
for (int i = 0; i < object->getNumSurfaces(); i++)
{
object->setMaterialParameter("albedo_color", vec4(1.0f, 1.0f, 0.0f, 1.0f), i);
}
// if the intersected object has a shape, show the information about the intersection
ShapePtr shape = intersection->getShape();
if (shape)
{
Log::message("physics intersection info: point: (%f %f %f) shape: %s\n", intersection->getPoint().x, intersection->getPoint().y, intersection->getPoint().z, typeid(shape->getType()).name());
}
}
return 1;
}
Game Intersection
Game class getIntersection() functions are used to check the intersection with obstacles (pathfinding nodes):
- Ptr<Obstacle> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, float radius, int mask, const Vector< Ptr<Node> > & exclude, Math::Vec3 * intersection)
- Ptr<Obstacle> getIntersection(const Math::Vec3 & p0, const Math::Vec3 & p1, float radius, int mask, const Ptr<GameIntersection> & intersection)
The engine creates an invisible cylinder with specified radius between two points and checks if an obstacle is presented inside of it.
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.
Usage Example
The following example shows how you can get the intersection point (vec3) of the cylinder between two points with an obstacle. In this example we specify a cylinder from the point of the camera (vec3 p0) to the point of the mouse pointer (vec3 p1) with the specified radius. The executing sequence is the following:
- Create an instance of the ObstacleBox class on the application initialization.
- Define and initialize two points (p0 and p1) by using the Player::getDirectionFromScreen() function.
- Create an instance of the GameIntersection class to get the intersection point coordinates.
- Check, if there is an intersection with an obstacle. The Game::getIntersection() function returns an intersected obstacle when the obstacle appears in the area of the cylinder.
- After that GameIntersection instance gets the point of the nearest intersection point and you can get it by using the getPoint() function.
// AppWorldLogic.cpp
#include <UnigineEditor.h>
#include <UnigineGame.h>
#include <UnigineApp.h>
#include <UnigineObjects.h>
#include <UniginePathFinding.h>
using namespace Unigine;
using namespace Math;
int AppWorldLogic::init() {
// create a mesh instance with a box surface
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box", vec3(1.0f));
// create a new dynamic mesh from the Mesh instance and add it to editor
ObjectMeshDynamicPtr dynamic_mesh = ObjectMeshDynamic::create(mesh);
dynamic_mesh->release();
Editor::get()->addNode(dynamic_mesh->getNode());
// assign a material and a property to the dynamic mesh
dynamic_mesh->setWorldTransform(translate(Vec3(0.0f, 0.0f, 2.0f)));
dynamic_mesh->setMaterial("mesh_base", "*");
dynamic_mesh->setProperty("surface_base", "*");
// create an obstacle
ObstacleBoxPtr obstacle = ObstacleBox::create(vec3(1.0f));
obstacle->release();
// set the obstacle transformation so that it coincides with the dynamic mesh transformation
obstacle->setWorldTransform(translate(Vec3(0.0f, 0.0f, 2.0f)));
// add the obstacle as the child node to the dynamic mesh
dynamic_mesh->addChild(obstacle->getNode());
return 1;
}
// start of the main loop
int AppWorldLogic::update() {
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
PlayerPtr player = Game::get()->getPlayer();
if (player.get() == NULL)
return 0;
// get width and height of the current application window
int width = App::get()->getWidth();
int height = App::get()->getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App::get()->getMouseX();
int mouse_y = App::get()->getMouseY();
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player->getDirectionFromScreen(p0,p1,mouse_x, mouse_y, width, height);
// create an instance of the GameIntersection class
GameIntersectionPtr intersection = GameIntersection::create();
// try to get the intersection with an obstacle
// cylinder has radius 1.5f, intersection mask equals to 1
ObstaclePtr obstacle = Game::get()->getIntersection(p0, p1, 1.5f, 1, intersection);
// check, if the intersection of mouse direction with any obstacle was occurred;
if (obstacle)
{
// show the coordinates of the intersection in console
Log::message("The intersection with the obstacle was here: (%f %f %f)\n", intersection->getPoint().x, intersection->getPoint().y, intersection->getPoint().z);
}
return 1;
}