How to get NavMesh polygons in Unity 3d - c#

Question is simple, but i can't find answer on the land of internet. How to get all polys of navmesh, how to find closest ones, how to find centers of these polys, how to find polys connected to specific one, and so on?
Does unity even allow this kind of staff or you need again to download something from github or buy some on asset store?
I find this post https://forum.unity.com/threads/accessing-navmesh-vertices.130883/
but it help find only vertices, not polygons or something..
So, does anyone know anything about this?

The solution:
var navMesh = NavMesh.CalculateTriangulation();
Vector3[] vertices = navMesh.vertices;
int[] polygons = navMesh.indices;
Source: https://docs.unity3d.com/ScriptReference/AI.NavMeshTriangulation-indices.html
Triangle indices for the navmesh triangulation.
Contains 3 integers for each triangle. These integers refer to the vertices array.
Should be the points in each polygon if I'm not mistaken.
Other useful methods:
https://docs.unity3d.com/540/Documentation/ScriptReference/NavMesh.SamplePosition.html
From this source: "Finds the closest point on NavMesh within specified range."
https://docs.unity3d.com/530/Documentation/ScriptReference/NavMesh.Raycast.html
"Trace a line between two points on the NavMesh.
bool True if the ray is terminated before reaching target position. Otherwise returns false. "
https://docs.unity3d.com/530/Documentation/ScriptReference/NavMesh.FindClosestEdge.html
"Locate the closest NavMesh edge from a point on the NavMesh."
What exactly are you needing the navmesh data for?

Related

Looking for an Algorithm for getting Polygons in List of open Pathes(Lists of Vector2d )

i´m wondering if there is an algorithm to all get the closed Polygons in a set of Pathes, like in the Image. I know there is the Intersection algorithm and als intesection of two closed Polygons, but I have just these vector of lines as an input. I´m working in C# and Unity. But of course this is a general question.
Thanks ahead
How about something like this:
Intersect each polyline with all others.
Split each polyline at the intersection points. Creating separate segments between each intersection point.
Put each segment in a structure representing a graph, with a node at each intersection, and edges to describe how nodes are connected.
Start any arbitrary segment
Traverse the graph, selecting the first clockwise edge at each node.
When the starting edge is reached, Output the traversed polygon
Mark each traversed edge.
Repeat from step 5, selecting the next un-marked edge.
There are multiple ways to describe a graph in this way. I would suggest using half-edge structure, where each edge is stored two times, in opposite directions. So the nodes would have a list of outgoing edges, and each edge would have a single target node. This makes it easier to mark traversed edges since you do not need to keep track of the direction you are traveling in.

Creating Custom Convex Hull & Mesh in Unity

I'm currently trying to implement a complex search field into a unity project I'm working on, where a user has the ability to spawn points into a scene. My goal is to create a custom shape for that user based on the points they created, and then I'd like to detect whether or not other objects are inside that shape, similar to the detection of a point inside a complex hull (still shaky on the theory behind that, but an example can be found here). If possible, I'd also like the shape to update itself if the points are later moved, giving it an almost elastic, stretchy feel.
So far, every tutorial or resource I've found online does the exact same basic example, where a script assigns new verts, UVs and triangles to a custom mesh to make a plane using two triangles, but this is frustratingly simple, and decidedly unhelpful when I simply don't know what the final shape will look like, or what triangles to actually draw even when the user has as little as five points in the scene.
As of right now, the closest visual representation I could come up with has a List keep track of the user's points, and a script that just draws a bunch of pseudo-triangles using LineRenderers to connect every point, even ones that aren't exterior faces, by iterating through the List multiple times. While this looks close to what I want, it isn't actually useful in any way, as I don't know how to 'fill' those faces, and I'm still relatively lost when it comes to whether or not an object is inside that hull, like the red sphere shown in the example below.
I can also destroy and redraw those lines repeatedly during the Update() method, which allows me to grab a point and move it around, resulting in the shape dynamically changing, but this results in an undesirable flashing effect that I'd sooner avoid for now.
As this is such a broad question, I've also included the method I'm using to draw these lines below, which parents a bunch of lines shaped like triangles to an empty game object for easy destruction and recreation:
void drawHull()
{
if (!GameObject.Find("hullHold"))
{
hullHold = new GameObject();
hullHold.name = "hullHold";
}
foreach(GameObject point in points)
{
for (int i = points.IndexOf(point); i < points.Count - 2; i++)
{
lineEdge = Instantiate(lineReference);
lineEdge.name = "Triangle" + i;
lineEdge.GetComponent<LineRenderer>().startColor = Color.black;
lineEdge.GetComponent<LineRenderer>().endColor = Color.black;
lineEdge.GetComponent<LineRenderer>().positionCount = 3;
lineEdge.GetComponent<LineRenderer>().SetPosition(0, points[points.IndexOf(point)].transform.position);
lineEdge.GetComponent<LineRenderer>().SetPosition(1, points[i + 1].transform.position);
lineEdge.GetComponent<LineRenderer>().SetPosition(2, points[i + 2].transform.position);
lineEdge.GetComponent<LineRenderer>().loop = true;
lineEdge.SetActive(true);
lineEdge.transform.SetParent(hullHold.transform);
}
}
}
If anyone has encountered a similar problem somewhere else that I simply couldn't find, please let me know! Anything from more knowledge on creating a custom mesh to a more in depth and beginner-friendly explanation on determining if a point is inside a convex hull would be quite helpful. If it's at all relevant, I am working in VR and running version 2018.2.6f1 to ensure that the Oculus rift package and Unity play nice, but I haven't been having any issues working in an environment a few months behind.
Thanks!
You do mention assinging verts and tris to a mesh - this is the 'right' way to do it in unity, as it ties in with highly optimized MeshRenderer, and also I believe Colliders are able to use meshes where given the shapes like that, so you should be able to just plug into PhysX and query it for colliders overlaping with your mesh. Doing it by hand, aka iterating through faces and establishing actual bounds of your object is actually pretty hard to do effectively

How do I find points around a partial circle intersected by line segment

I am working on an obstacle avoidance solution for my square grid pathfinding project. I begin by performing an A* search to find the "long" path. Next, I take my unit's current position and iterate the "long" path until I no longer have line of sight, this produces the "short" path (unit's position->furthest position w/ LoS). When I reach the end of the "short" path, I repeat to find another "short" path until reaching the goal.
I want to modify my short path to add additional points unaligned to the grid if there are obstacles in the way. I find the obstacles intersecting the short path by their center position & radius, and my plan is to add additional points that go around the circumference of these obstacles. The points must be added on the side of the circle that the line intersects so that the path doesn't end up going all the way around the other side.
Here are some screenshots to explain a little better:
http://puu.sh/xFHw5/0e8f32da7a.png
http://puu.sh/xFHwU/04a4b1fe27.png
http://puu.sh/xFHxk/56f3051cc5.png
http://puu.sh/xFHxO/fcc6151a02.png
You can find path consisting of tangents to circle from both sides and arc connecting points where tangents touch the circle. As variant - use point of intersection of tangents, in this case path will contain only two straight segments.

Can Unity NavMesh be used to implement an A* algorithm?

I'm looking forward to use A* pathfinding for a game I'm working on. (I'm actually making a game for myself to learn about this). I am wondering how the Unity NavMesh can be used with a custom A* algorithm, instead of using a NavMeshAgent.
No
Or at least, not easily (why would you want to?).
Unity's builtin NavMesh is intended to be used by Unity's builtin NavMeshAgent utilizing a builtin pathfinder. I don't know what algorithm it uses, but A* implementations typically operate on networks. That is, nodes connected by edges. It does not consider the interior volume (the mesh 'faces').
As Unity's builtins are intended to be used as such, it is very difficult to get access to any of the information directly for use with your own pathfinding algorithms.
If you want to write your own pathfinder, then I recommend writing your own mesh as well.
You can do the following:
var navMesh = NavMesh.CalculateTriangulation() // get baked Navigation Mesh Data;
Vector3[] vertices = navMesh.vertices;
int[] polygons = navMesh.indices;
vertices (obviously) are the vertices of your navigation mesh indicated by their position in unity space. The meshes are defined by polygons. The polygon array shows which vertice belongs to which polygon.
polygons:
0 0
1 0
2 1
3 1
4 0
5 1
This array would indicate that vertices with indices 0,1,4 belongs to polygon number 0 and vertices with indices 2,3,5 belongs to polygon number 1.
There you have your navmesh as polygons. You can define your search graph and run the search algorithm of your choice.

Simple 3D AABB-Line Segment collision detection (intersection)

I just need a method to tell me whether an axis aligned bounding box in 3D intersects a line segment (not a ray) or not. I do not need the points of intersection.
The box is defined by 2 opposite corners, and the line segment by its start and end points, something like this:
Boolean intersection(Vector3 boxStart, Vector3 boxEnd, Vector3 segmentStart, Vector3 segmentEnd){...}
I've done a lot of research, and havent been able to find a code (in C# or Java hopefully) that I can understand or at least use. I need the method and not a library that will do the job...
My problem is that it needs to be 100% precise, and if the segment just touches the box (i.e. they share a single point), it has to return false. For example, if the segment is one of the edges of the box or passes though a corner, they DO NOT intersect.
Thanks
In Java, either of the intersects() methods is a candidate; but due to implementation limitations, you'd need to test it with Line2D.

Categories

Resources