十字路口 (Intersections)
Unigine has different methods to detect intersections. Intersection is a shared point of the defined area (or line) and an object. This article describes different types of intersections and their usage.Unigine有不同的方法来检测交叉口。交点是定义的区域(或直线)和一个对象的共享点。本文描述了不同类型的交叉口及其用法。
There are three main types of intersections:主要有三种类型的交叉口:
- World intersection — an intersection with objects and nodes.World intersection — 与 objects和nodes的交集。
- Physics intersection — an intersection with shapes and collision objects.Physics intersection — 与 shapes和碰撞物体的交集。
- Game intersection — an intersection with pathfinding nodes such as obstacles.Game intersection — 与寻路节点相交,例如 障碍 (obstacles)。
The Shape and Object classes have their own getIntersection() functions. These functions are used to detect intersections with a specific shape or a specific surface of the object.Shape和Object类有自己的getIntersection()函数。这些函数用于检测物体的特定形状或特定表面的交点。
See Also另请参阅#
- C# Component Sample demonstrating the use of World IntersectionC#组件示例演示了 World Intersection 的使用
- C++ Sample demonstrating different cases of intersection detectionC++ 示例演示了不同情况下的交集检测
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 identify the first intersection of the object with the invisible tracing line.通过使用不同重载的World类getIntersection()函数,您可以找到与指定的包围体边界相交的对象和节点,或识别对象与不可见跟踪线的第一个交集。
The World class functions for intersection search can be divided into 3 groups:用于交集搜索的World类函数可以分为3组:
- 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查找第一个相交对象的函数与跟踪线
The following objects have individual parameters that control the accuracy of intersection detection:以下对象具有控制交叉口检测精度的单个参数:
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:为了找到节点与指定体积的边界的交集,你应该使用具有边界体积和节点向量的函数参数:
- bool GetIntersection(WorldBoundBox bb, ICollection<Node> nodes)
- bool GetIntersection(WorldBoundSphere bs, ICollection<Node> nodes)
- bool GetIntersection(WorldBoundBox bb, int type, ICollection<Node> nodes)
- bool GetIntersection(WorldBoundSphere bs, int type, ICollection<Node> nodes)
- bool GetIntersection(WorldBoundFrustum bf, int type, ICollection<Node> nodes)
These functions return the value indicating the result of the intersection search: true if the intersection was found; otherwise, false. Intersected nodes can be found in the collection that passed as an argument to the function.这些函数返回指示交集搜索结果的值: true如果找到交集;否则,假。交叉的节点可以在作为参数传递给函数的集合中找到。
To clarify the nodes search, specify the type of node to search.澄清节点搜索,指定要搜索的节点类型。
Usage Example使用的例子#
In the example below, the engine checks intersections with a bounding box and shows the names of intersected objects in the console.在下面的例子中,引擎用一个边界框检查交叉,并在控制台中显示交叉对象的名称。
In the Init() function the bounding box is defined.在Init()函数定义的边界框。
The main logic is in the Update() method, the engine checks the intersection and shows the message about intersection.主要的逻辑是在Update()方法中,引擎检查交叉口并显示关于交叉口的消息。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
using WorldBoundBox = Unigine.BoundBox;
#endif
#endregion
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Intersections : Component
{
// define a bounding box
WorldBoundBox boundBox;
// create a counter to show the message once
int counter = 1;
private void Init()
{
// initialize the bounding box
boundBox = new WorldBoundBox(new Vec3(0.0f), new Vec3(10.0f));
}
private void Update()
{
// check the intersection with the bounding box
List<Node> nodes = new List<Node>();
bool result = World.GetIntersection(boundBox, nodes);
if (result && counter !=0)
{
foreach(Node n in nodes)
{
// show the name of the intersected node in the console
Log.Message("There was an intersection with: {0} \n", n.Name);
}
counter--;
}
}
}
Intersection with Objects十字路口与对象#
The following functions can be used to perform the intersection search with objects:可以使用以下函数对对象进行交集搜索:
- bool GetIntersection(Vec3 p0, Vec3 p1, ICollection<Object> objects, bool check_surface_flags)
- bool GetIntersection(WorldBoundBox bb, ICollection<Object> objects)
- bool GetIntersection(WorldBoundSphere bs, ICollection<Object> objects)
- bool GetIntersection(WorldBoundFrustum bf, ICollection<Object> objects)
- bool GetVisibleIntersection (vec3 camera, WorldBoundFrustum bf, Object[] objects, float max_distance)
- int GetVisibleIntersection( vec3 camera, WorldBoundFrustum bf, Node.TYPE type, Node[] nodes, float max_distance)
These functions return the value indicating the result of the intersection search: true if the intersection was found; otherwise, false. 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 collection that passed as an argument to the function.这些函数返回指示交集搜索结果的值:如果找到交集,则返回true;否则,false。您可以通过相交线的起始点(vec3 p0)和结束点(vec3 p1)或通过(BoundBox, BoundFrustum, BoundSphere)分别获得对象与直线或边界体的交点。交叉的节点可以在作为参数传递给函数的集合中找到。
For WorldBoundFrustum, there are two modes of getting intersections:For WorldBoundFrustum, there are two modes of getting intersections:
- Intersections with the objects that are visible inside the frustum — GetVisibleIntersection()与截锥内 visible的对象相交- GetVisibleIntersection()
- Intersections with all objects inside the frustum, both visible and invisible — GetIntersection()路口截锥体内的所有对象,有形 和无形的 - GetIntersection()
Finding Objects Intersected by a Bounding Box查找对象分割的边界框#
In the example below, the engine checks intersections with a bounding box and shows the message in the console.在下面的示例中,发动机检查路口边界框,在控制台中显示了消息。
In the Init() function the bounding box is defined.在Init()函数中定义了边界框。
The main logic is in the Update() method, the engine checks the intersection and shows the message about intersection.的主要逻辑是Update()方法,发动机检查交叉和显示信息的十字路口。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
using WorldBoundBox = Unigine.BoundBox;
#endif
#endregion
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Intersections : Component
{
// define a bounding box
WorldBoundBox boundBox;
// create a counter to show the message once
int counter = 1;
private void Init()
{
// initialize the bounding box
boundBox = new WorldBoundBox(new Vec3(0.0f), new Vec3(10.0f));
}
private void Update()
{
// check the intersection with the bounding box
List<Unigine.Object> objects = new List<Unigine.Object>();
bool result = World.GetIntersection(boundBox, objects);
if (result && counter != 0)
{
foreach (Unigine.Object o in objects)
{
Log.Message("There was an intersection with: {0} \n", o.Name);
}
counter--;
}
}
}
Finding Objects Intersected by a Bounding Frustum寻找被边界截体相交的物体#
For WorldBoundFrustum, there are two modes of getting intersections:WorldBoundFrustum,十字路口的两种模式:
- Intersections with the objects that are visible inside the frustum (within the rendering visibility distance and the object's LOD is visible by the camera) — GetVisibleIntersection()与物体的交叉是 visible在截体内(在渲染可见距离和物体的LOD在摄像机中可见)- GetVisibleIntersection()
- Intersections with all objects inside the frustum, both visible and invisible (either occluded by something or out of the visibility distance) — GetIntersection()与截锥内的所有对象相交,可见和不可见(被某些东西遮挡或在可见距离之外)- GetIntersection()
In the example below, the engine checks intersections of WorldBoundFrustum with all objects, both visible and invisible. The frustum itself is highlighted blue, and the objects that are inside the frustum are highlighted red for 20 seconds.在下面的示例中,发动机检查WorldBoundFrustum所有对象的十字路口,都可见和不可见。突出显示的平截头体本身是蓝色,截锥体内的对象20 seconds.红色突出显示
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
using WorldBoundBox = Unigine.BoundBox;
#endif
#endregion
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Intersections : Component
{
private void Init()
{
// enable the visualizer
Visualizer.Enabled = true;
}
private void Update()
{
if(Input.IsKeyDown(Input.KEY.SPACE))
{
// get a reference to the camera
Player player = Game.Player;
Camera camera = player.Camera;
// get the size of the current application window
EngineWindow main_window = WindowManager.MainWindow;
if (!main_window)
{
Engine.Quit();
return;
}
ivec2 main_size = main_window.Size;
mat4 projection = camera.GetAspectCorrectedProjection(((float)(main_size.y) / ((float)main_size.x)));
// bound frustrum for the intersection and a list to store intersected objects
var bf = new WorldBoundFrustum(camera.Projection, camera.Modelview);
Visualizer.RenderFrustum(projection, camera.IModelview, vec4.BLUE, 20.0f);
List<Unigine.Object> objects = new List<Unigine.Object>();
// perform the intersection search with the bound frustrum
World.GetIntersection(bf, objects);
// draw the bound spheres of all objects' surfaces found by the search
foreach(var obj in objects)
{
for (int i = 0; i < obj.NumSurfaces; ++i)
{
var bs = obj.GetBoundSphere(i);
Visualizer.RenderBoundSphere(bs, obj.WorldTransform, vec4.RED, 20.0f);
}
}
}
}
}
To search only for intersections with the objects that are within the rendering visibility distance and the LODs of which are visible, use the following method instead of getIntersection():为了只搜索与呈现可见距离和LODs范围内的物体相交,使用以下方法代替getIntersection():
// perform the intersection search with the bound frustrum
World.GetVisibleIntersection(player.WorldPosition, bf, objects, 100.0f);
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.下面的函数用于找到与跟踪线最近的相交对象。您应该指定行的起始点和结束点,该函数会检测这条线的路径上是否有任何对象。
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Vec3[] ret_point, vec3[] ret_normal, vec4[] ret_texcoord, int[] ret_index, int[] ret_surface)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, Vec3[] ret_point, vec3[] ret_normal, vec4[] ret_texcoord, int[] ret_index, int[] ret_surface)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, WorldIntersection v)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, WorldIntersectionNormal v)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, WorldIntersectionTexCoord v)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, WorldIntersection v)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, WorldIntersectionNormal v)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, 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:这些函数返回一个路口信息和最近的对象指针(p0)的起始点。十字路口的信息可以在以下格式,提出了标准向量或作为参数传递给函数:
- 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.WorldIntersection intersection—WorldIntersection类实例。通过使用这个类,您可以得到交点(坐标)、对象的相交三角形的索引和相交曲面的索引。
- WorldIntersectionNormal normal — the WorldIntersectionNormal class instance. By using this class you can get only the normal of the intersection point.WorldIntersectionNormal normal—WorldIntersectionNormal类实例。通过使用这个类,你只能得到交点的法线。
- WorldIntersectionTexCoord texcoord — the WorldIntersectionTexCoord class instance. By using this class you can get only the texture coordinates of the intersection point.WorldIntersectionTexCoord texcoord - WorldIntersectionTexCoord类实例。通过使用这个类可以得到唯一的交点的纹理坐标。
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.启用每个表面的Intersection标志。
You can set this flag to the object's surface by using the Object.SetIntersection() function.你可以设置这个标志对象的表面使用Object.SetIntersection()函数。
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.使用一个Intersection面具。一个十字路口被发现只有一个对象匹配Intersection面具。
- 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.在下面的例子中,引擎用光线追踪线检查路口,并在控制台中显示消息。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
#endif
#endregion
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Intersections : Component
{
// define world intersection instance
WorldIntersection wi;
// create a counter to show the message once
int counter = 1;
private void Init()
{
// initialize the world intersection
wi = new WorldIntersection();
}
private void Update()
{
// check the intersection with the object
Unigine.Object o = World.GetIntersection(new Vec3(0.0f), new Vec3(1.0f), 1, wi);
if (o != null && counter != 0)
{
Log.Message("The name of the obstacle is: {0} \n", o.Name);
counter--;
}
}
}
Physics Intersection物理的十字路口#
Physics intersection function detects intersections with a physical shape or a Fracture Body. If the object has no body, this function detects intersection with surfaces (polygons) of the object having the Physics Intersection flag enabled. 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.物理交叉功能检测与物理形状或Fracture Body十字路口。如果对象没有身体,这个函数检测交叉表面(多边形)的对象有Physics Intersection标志启用。当你需要找到交集与物体的形状与多边形(不),Physics类的交叉功能是最好的方法。
There are 4 functions to find physics intersections:有4个函数来找到物理十字路口:
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, PhysicsIntersection intersection)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, PhysicsIntersectionNormal intersection)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, PhysicsIntersection intersection)
- Object GetIntersection(Vec3 p0, Vec3 p1, int mask, Node[] exclude, PhysicsIntersectionNormal intersection)
These functions perform tracing from the start p0 point to the end p1 point to find a collision object (or a shape) located on that line. Functions use world space coordinates.这些函数执行从起始p0点到结束p1点的跟踪,以找到位于该线上的碰撞对象(或形状)。函数使用世界空间坐标。
Thus, to exclude some obstacles you should use these methods:因此,为了排除一些障碍,你应该使用以下方法:
- Use a Physics Intersection mask. A physics intersection is detected only if the Physics Intersection mask of the surface/shape/body matches the Intersection mask passed as a function argument, otherwise it is ignored.使用Physics Intersection面具。检测到物理的十字路口只有Physics Intersection面具表面/形状/身体匹配Intersection面具作为函数参数传递,否则它将被忽略。
- 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:下面的示例展示了如何通过使用PhysicsIntersection类获取交集信息。在这个例子中,我们指定了一条从相机(vec3 p0)点到鼠标指针(vec3 p1)点的直线。执行顺序如下:
- Define and initialize two points (p0 and p1) by using the Player.GetDirectionFromScreen() function.使用Player.GetDirectionFromScreen()函数定义并初始化两个点(p0和p1)。
- Create an instance of the PhysicsIntersection class to get the intersection information.创建一个PhysicsIntersection类的实例来获得交叉的信息。
- 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.检查是否有一个相交的对象与形状或碰撞对象。当对象与跟踪的直线相交时,GetIntersection()函数返回一个相交的对象。
- When the object intersects with the traced line, all 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 Shape, Point properties.当追踪的对象相交线,所有的表面分割对象的改变他们的材质参数。如果对象有一个形状,其信息将显示在控制台。PhysicsIntersection类实例得到交点的坐标和Shape类对象。你可以得到所有这些字段使用Shape, Point属性。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class PhysicsIntersectionClass : Component
{
private void Init()
{
// create a mesh instance with a box surface
Mesh mesh = new Mesh();
mesh.AddBoxSurface("box", new vec3(2.2f));
// create a new dynamic mesh from the Mesh instance
ObjectMeshDynamic dynamic_mesh = new ObjectMeshDynamic(mesh);
dynamic_mesh.WorldTransform = MathLib.Translate(new Vec3(0.0f, 0.0f, 2.0f));
// assign a body and a shape to the dynamic mesh
BodyRigid body = new BodyRigid(dynamic_mesh);
ShapeBox shape = new ShapeBox(body, new vec3(2.2f));
}
private void Update()
{
// initialize points of the mouse direction
Vec3 p0, p1;
// get the current player (camera)
Player player = Game.Player;
// get the size of the current application window
EngineWindow main_window = WindowManager.MainWindow;
if (!main_window)
{
Engine.Quit();
return;
}
ivec2 main_size = main_window.Size;
// get the current X and Y coordinates of the mouse pointer
int mouse_x = Gui.GetCurrent().MouseX;
int mouse_y = Gui.GetCurrent().MouseY;
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player.GetDirectionFromScreen(out p0, out p1, mouse_x, mouse_y, 0, 0, main_size.x, main_size.y);
// create the instance of the PhysicsIntersection object to save the information about the intersection
PhysicsIntersection intersection = new PhysicsIntersection();
// get an intersection
Unigine.Object obj = Physics.GetIntersection(p0, p1, 1, intersection);
// if the intersection has occurred, change the parameter of the object's material
if (obj != null)
{
for (int i = 0; i < obj.NumSurfaces; i++)
{
obj.SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 1.0f, 0.0f, 1.0f), i);
}
// if the intersected object has a shape, show the information about the intersection
Shape shape = intersection.Shape;
if (shape != null)
{
Log.Message("physics intersection info: point: ({0} {1} {2}) shape: {3}\n", intersection.Point.x, intersection.Point.y, intersection.Point.z, shape.TypeName);
}
}
}
}
Game Intersection游戏的十字路口#
The GetIntersection() functions of the Game class are used to check the intersection with obstacles (pathfinding nodes):GetIntersection() Game类的函数用来检查障碍(寻路节点)的交点:
- Obstacle GetIntersection(Vec3 p0, Vec3 p1, float radius, int mask, Node[] exclude, Vec3[] intersection)
- Obstacle GetIntersection(Vec3 p0, Vec3 p1, float radius, int mask, GameIntersection intersection)
The engine creates an invisible cylinder with specified radius between two points and checks if an obstacle is inside 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.这些函数返回一个路口信息和一个指针到最近的障碍起点( p0 )。十字路口的信息将在GameIntersection类实例作为参数传递给函数或向量。
Use the mask and exclude arguments of the overloaded function to specify the obstacle search.使用重载函数的mask和exclude参数来指定障碍搜索。
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.使用障碍Intersection遮罩。只有当对象匹配Intersection掩码时才会发现交集,否则将被忽略。
- 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. We'll do the following:下面的例子显示了如何得到交点(vec3)汽缸两点之间的一个障碍。我们会做以下:
- Create an instance of the ObstacleBox class on the application initialization.在应用程序初始化时创建ObstacleBox类的实例。
- Define two points (p1 and p2) that specify the beginning and the end of the cylinder.定义两个点(p1和p2),指定开始和结束的缸。
- Create an instance of the GameIntersection class to get the intersection point coordinates.创建GameIntersection类的实例以获得交点坐标。
- 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.检查交叉路口是否有障碍物。当障碍物出现在圆柱区域时,Game::getIntersection()函数返回一个相交的障碍物。
- When the GameIntersection instance intersects with an obstacle, you can get its name using the getName() function or any other details using the corresponding functions.当GameIntersection实例相交与一个障碍,你可以得到它的名字使用getName()函数或任何其他细节使用相应的功能。
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
#endif
#endregion
[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class Intersections : Component
{
// define an obstacle box and game intersection instance
ObstacleBox obstacleBox;
GameIntersection gi;
// parameters of the virtual cylinder defining the volume
// inside which to check for intersections
float radius = 1.0f; // radius of the cylinder
vec3 p1 = new vec3(0, -3.0f, 3.0f); // starting point of the ray
vec3 p2 = new vec3(0, 3.0f, 3.0f); // ending point of the ray
private void Init()
{
// initialize the obstacle box and specify the name
obstacleBox = new ObstacleBox(new vec3(1.0f));
obstacleBox.Name = "main_obstacle";
// initialize the game intersection object
gi = new GameIntersection();
// enable the visualizer
Visualizer.Enabled = true;
}
private void Update()
{
// make the obstacle box move inside and outside the game intersection area
Vec3 new_pos = new Vec3(0,0,2 + MathLib.Sin(Game.Time));
obstacleBox.WorldPosition = new_pos;
Visualizer.RenderPoint3D(new_pos, 0.15f, vec4.MAGENTA);
// calculate the cylinder height (equals to the distance between the starting and ending points)
Visualizer.RenderPoint3D(p1, 0.15f, vec4.BLUE);
Visualizer.RenderPoint3D(p2, 0.15f, vec4.YELLOW);
// create an instance of the GameIntersection class
Obstacle obstacle = Game.GetIntersection(p1, p2, radius, 1, gi);
//visualize the cylinder that represents the game intersection area
Visualizer.RenderCylinder(radius, (p2-p1).Length, MathLib.SetTo(p1 + (p2 - p1) / 2, p2, vec3.UP, MathLib.AXIS.Z), vec4.WHITE);
// check if the intersection of the cylinder with the obstacle has occurred
if (obstacle != null)
{
obstacle.RenderVisualizer();
Log.Message("The name of the obstacle is: {0} \n", obstacle.Name);
}
}
}