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.
Related
I am currently developing an indoor path-finding. I have multiple floors and different rooms. How will I be able to implement a* algorithm in the images of each floor using c# wpf?
I use spatial A* for the game I'm working on.
Spatial A* uses movement "cost" to work out the best route between two points. The cost mentions is supplied by an array. Usually a 2d array of number - float uint or whatever.
Moving through a square/cell at position x,y thus costs the number in that 2d array. EG costs[2,3] would be the cost of movement through the cell 2 cells across from the left and 3 down from the top of an imaginary grid projected onto your "room".
If the move is diagonal then there's also a multiplier to consider but that will be in whichever implementation you go with.
Hence you need a 2d costed array per floor.
You would need to somehow analyse your pictures and work out an appropriate size for a costed cell. This should match the smallest size of a significant piece of terrain in your floor.
You would then translate your picture into a costed array. You've not told us anywhere near enough to tell you specifically how to do that. Maybe that would have to be a manual process though.
Blocked cells get the max number, empty cells get 1. Depending on your requirements that might be that. Or alternatively you might have actors leaping tables and chairs etc.
You give the pathing algorithm start and target location (x,y), the appropriate costed array and it works out the cheapest route.
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?
I'm hoping to create a Voronoi landscape in Unity in C#. I looked at a number of Unity Project files, but they all implement Fortune's algorithm, which is completely over my head. Are there any other methods of generating Voronoi diagram (that is easier to understand)?
Slow performance is completely fine with me.
Much appreciated!
Sidenote: Since I'm working in Unity and need to generate 2D/3D mesh from Voronoi diagram, per-pixel distance check won't work :,(
On second thought, maybe I could use a 2D array of Vector2s instead of pixels, that are 1.0 unit spaced apart in x and z axis.
There is a very simple way to create an approximated Voronoi diagram VD. For every Site s that should define a cell in the VD (2D-plane) you center a cone at s with constant slope and a certain height. Then you look from above onto that landscape of cones (where all the spikes are visible). The boundary where the different cones meet (projected to the 2D-plane) is the (approximated) Voronoi diagram.
(Image Source)
As you requested in the comments, to get the actual edge data seems not so easy. But there could be some graphical routines to generate them by intersecting the cones.
An alternative is to compute a Delaunay triangulation of the given point set. There are some implementation referenced in this related post (also simple approximations are mentioned). Then you compute the dual graph of your triangulation and you have the Voronoi diagram. (Dual graph means that for every for every edge AB in the triangulation there exists an edge in the VD bisecting the space between the two vertices A and B, and for every triangle there exists a vertex in the VD where the dual edges meet.) Othwerwise there are also many C# Voronoi implementations around: Unity-delaunay, but as you mentioned using the Fortune approach.
If you want to code everything yourself you may compute a triangulation of the points with brute force for n points in O(n^2) time. Then apply in-circle tests and edge flips. That is, for every triangle t(abc) create a circle C defined by the three vertices of t. Then check if there lies another point d of your point set inside C. If so, then flip the edge that is in t as well as forms an edge in the triangle with d. This flipping is done until all triangles fulfil the empty circle property (Delaunay condition). Again with brute force will take O(n^2) time. Then you can compute the dual graph as mentioned above.
(Image Source)
"Easiest? That's the brute-force approach: For each pixel in your output, iterate through all points, compute distance, use the closest. Slow as can be, but very simple. If performance isn't important, it does the job."
[1] Easiest algorithm of Voronoi diagram to implement?
I'm using HelixToolkit's ModelImporter class(Helix 3D Toolkit is a collection of custom controls and helper classes for WPF.) for loading 3D objects from STL files (STereoLithography is a file format native to the stereolithography CAD software created by 3D Systems). The 3D models contain ModelGroup3D object with one or several GeometryModel3D objects inside depending on how many parts the model is comprised from. I would like to calculate the volume of the whole 3D model. I searched for similar questions and the only one answered was this one Calculate volume of 3D mesh which I'm not sure how to reform for my solution. Since I'm a newbie any help is greatly appreciated.
Additionally the models I'm loading are all closed meshes.
Thanks
First convert the surface mesh into a volume mesh. For example, you can convert the triangulated surface mesh into a tetrahedral mesh. One way to do this by constructing the constrained Delaunay triangulation of the surface triangles.
Next, you can get a good estimate of the volume enclosed by the surface mesh, by summing the volumes of all the elements in the volume mesh. For example, by summing the volumes of all the tetrahedrons in the mesh.
The easyest way is computing the Gaussian flux of all triangles.
for the "theory" if your surface is closed then imagine that a vector filed is running through it, then what comes in is equal to what comes out and is also equal to the volume inclosed. for the calculus details check "Gauss theorem" and Green-ostrogradsky integrals.
to compute it:
Vertex v1 ;
Vertex v2 ;
Vertex v3 ;
for (int i = 0;i< triangles.Count; i++)
{
v1 = triangles[i].P0;
v2 = triangles[i].P1;
v3 = triangles[i].P2;
Mesh.volume += (((v2.Y - v1.Y) * (v3.Z - v1.Z) - (v2.Z - v1.Z) * (v3.Y - v1.Y)) * (v1.X + v2.X + v3.X)) / 6;
}
If you have any question don't hesitate, i can develop how you get to that function.
Have fun.
Hey this may sound simple but it escapes me,
I have a list of 3D points (including negative positions) that I would like to map onto a 2D Texture.
I'm trying to figure out how to map the points appropriately to the texture and how it differs if it has a specific width/height.
Thanks
The simple way: using ortographic projection.
x_2d = x_3d + z_3d * scale_x
y_2d = y_3d + z_3d * scale_y
Where (scale_x, scale_y) is a vector describing the "direction" of the projection.
If objects with an high position ("far away") should be smaller you should search for perspective projection (e.g. on Wikipedia: 3D Projection)
In my experience, usually you specify the 2D texture coordinates in your model using glTexCoord2f (one for each 3D point), and let OpenGL take care of the rest.
Maybe I am misunderstanding what you are trying to do here.