public TestBoss() { agentParams = new SharpNav.Crowds.AgentParams(); SetAgentParams(); var tpos = new SharpNav.Geometry.Vector3(3f, -4.62f, 65.35f); World.Instance.crowd.GetAgent(agentID).Position = tpos; }
public void SetTargetPosition(Vector3 position) { NavMeshQuery q = new NavMeshQuery(World.Instance.navMesh, 2048); var poly = q.FindNearestPoly(position, new Vector3(5, 5, 5)); SharpNav.Geometry.Vector3 v = SharpNav.Geometry.Vector3.Zero; q.ClosestPointOnPoly(poly.Polygon, position, ref v); Agent.RequestMoveTarget(poly.Polygon, v); }
bool Hit(SVector3 position, SVector3 direction, SVector3 enemyPosition) { if ((enemyPosition - position).Length() <= 3f) { var enDirection = (enemyPosition - position).Normalized(); var dot = SVector3.Dot(direction, enDirection); if (dot >= Math.Sin(180 * 60 / Math.PI)) { return(true); } } return(false); }
private bool GetSteerTarget(NavMeshQuery navMeshQuery, SVector3 startPos, SVector3 endPos, float minTargetDist, SharpNav.Pathfinding.Path path, ref SVector3 steerPos, ref StraightPathFlags steerPosFlag, ref NavPolyId steerPosRef) { StraightPath steerPath = new StraightPath(); navMeshQuery.FindStraightPath(startPos, endPos, path, steerPath, 0); int nsteerPath = steerPath.Count; if (nsteerPath == 0) { return(false); } //find vertex far enough to steer to int ns = 0; while (ns < nsteerPath) { if ((steerPath[ns].Flags & StraightPathFlags.OffMeshConnection) != 0 || !InRange(steerPath[ns].Point.Position, startPos, minTargetDist, 1000.0f)) { break; } ns++; } //failed to find good point to steer to if (ns >= nsteerPath) { return(false); } steerPos = steerPath[ns].Point.Position; steerPos.Y = startPos.Y; steerPosFlag = steerPath[ns].Flags; if (steerPosFlag == StraightPathFlags.None && ns == (nsteerPath - 1)) { steerPosFlag = StraightPathFlags.End; // otherwise seeks path infinitely!!! } steerPosRef = steerPath[ns].Point.Polygon; return(true); }
public override void Update() { base.Update(); if (targetPlayer != -1) { if (SVector3.Subtract(Agent.Position, World.Instance.players[targetPlayer].position).Length() < 3f) { SetTargetPosition(Agent.Position); } else { SetTargetPosition(World.Instance.players[targetPlayer].position); } } else { GetTarget(); } }
public Vector2[] GetPath(Vector2 start, Vector2 end) { NavPoint startPt = navMeshQuery.FindNearestPoly(new SVector3(-start.X, 0, start.Y), new SharpNav.Geometry.Vector3(2f, 2f, 2f)); NavPoint endPt = navMeshQuery.FindNearestPoly(new SVector3(-end.X, 0, end.Y), new SharpNav.Geometry.Vector3(2f, 2f, 2f)); Path path = new Path(); navMeshQuery.FindPath(ref startPt, ref endPt, new NavQueryFilter(), path); List <SVector3> smoothPath; //find a smooth path over the mesh surface int npolys = path.Count; SVector3 iterPos = new SVector3(); SVector3 targetPos = new SVector3(); navMeshQuery.ClosestPointOnPoly(startPt.Polygon, startPt.Position, ref iterPos); navMeshQuery.ClosestPointOnPoly(path[npolys - 1], endPt.Position, ref targetPos); smoothPath = new List <SVector3>(2048); smoothPath.Add(iterPos); float STEP_SIZE = 0.5f; float SLOP = 0.01f; while (npolys > 0 && smoothPath.Count < smoothPath.Capacity) { //find location to steer towards SVector3 steerPos = new SVector3(); StraightPathFlags steerPosFlag = 0; NavPolyId steerPosRef = NavPolyId.Null; if (!GetSteerTarget(navMeshQuery, iterPos, targetPos, SLOP, path, ref steerPos, ref steerPosFlag, ref steerPosRef)) { break; } bool endOfPath = (steerPosFlag & StraightPathFlags.End) != 0 ? true : false; bool offMeshConnection = (steerPosFlag & StraightPathFlags.OffMeshConnection) != 0 ? true : false; //find movement delta SVector3 delta = steerPos - iterPos; float len = (float)Math.Sqrt(SVector3.Dot(delta, delta)); //if steer target is at end of path or off-mesh link //don't move past location if ((endOfPath || offMeshConnection) && len < STEP_SIZE) { len = 1; } else { len = STEP_SIZE / len; } SVector3 moveTgt = new SVector3(); VMad(ref moveTgt, iterPos, delta, len); //move SVector3 result = new SVector3(); List <NavPolyId> visited = new List <NavPolyId>(16); NavPoint startPoint = new NavPoint(path[0], iterPos); navMeshQuery.MoveAlongSurface(ref startPoint, ref moveTgt, out result, visited); path.FixupCorridor(visited); npolys = path.Count; float h = 0; navMeshQuery.GetPolyHeight(path[0], result, ref h); result.Y = h; iterPos = result; //handle end of path when close enough if (endOfPath && InRange(iterPos, steerPos, SLOP, 1.0f)) { //reached end of path iterPos = targetPos; if (smoothPath.Count < smoothPath.Capacity) { smoothPath.Add(iterPos); } break; } //store results if (smoothPath.Count < smoothPath.Capacity) { smoothPath.Add(iterPos); } } return(smoothPath.Select(x => new Vector2(-x.X, x.Z)).ToArray()); }
private void VMad(ref SVector3 dest, SVector3 v1, SVector3 v2, float s) { dest.X = v1.X + v2.X * s; dest.Y = v1.Y + v2.Y * s; dest.Z = v1.Z + v2.Z * s; }
void AddTerrainTriangles(List <Triangle3> triangles, Terrain terrain) { var data = terrain.terrainData; var terrainSize = data.size; var terrainWidth = data.heightmapWidth; var terrainHeight = data.heightmapHeight; var terrainScale = data.heightmapScale; int incrementX = Mathf.Max(1, Mathf.RoundToInt(terrainCellSize / terrainScale.x)); int incrementZ = Mathf.Max(1, Mathf.RoundToInt(terrainCellSize / terrainScale.z)); var multiplierX = terrainSize.x / (terrainWidth - 1); var multiplierZ = terrainSize.z / (terrainHeight - 1); var heights = data.GetHeights(0, 0, terrainWidth, terrainHeight); var optimizedWidth = Mathf.Floor(terrainWidth / incrementX); var optimizedHeight = Mathf.Floor(terrainHeight / incrementZ); var worldHeights = new SVector3[terrainWidth, terrainHeight]; var resolution = terrain.terrainData.size.y; int ox = 0; for (var hx = 0; hx < terrainWidth; hx += incrementX) { int oz = 0; for (var hz = 0; hz < terrainHeight; hz += incrementZ) { var x = hx * multiplierX + terrain.transform.position.x; var z = hz * multiplierZ + terrain.transform.position.z; var y = heights[hx, hz] * resolution + terrain.transform.position.y; worldHeights[ox, oz] = new SVector3(z, y, x); oz++; } ox++; } var vertices = new SVector3[4]; for (var hx = 0; hx < optimizedWidth - 1; hx++) { for (var hz = 0; hz < optimizedHeight - 1; hz++) { vertices[0] = worldHeights[hx, hz]; vertices[1] = worldHeights[hx, hz + 1]; vertices[2] = worldHeights[hx + 1, hz + 1]; vertices[3] = worldHeights[hx + 1, hz]; triangles.Add(new Triangle3( vertices[0], vertices[1], vertices[2] )); triangles.Add(new Triangle3( vertices[2], vertices[3], vertices[0] )); } } }
// Copyright (c) 2013-2016 Robert Rouhani <*****@*****.**> and other contributors (see CONTRIBUTORS file). // Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE public static List <Vector3> GetPath(actors.area.Zone zone, Vector3 startVec, Vector3 endVec, float stepSize = 0.70f, int pathSize = 45, float polyRadius = 0.0f, bool skipToTarget = false) { var navMesh = zone.tiledNavMesh; var navMeshQuery = zone.navMeshQuery; // no navmesh loaded, run straight to player if (navMesh == null) { return(new List <Vector3>() { endVec }); } // no need to waste cycles finding path to same point if (startVec.X == endVec.X && startVec.Y == endVec.Y && startVec.Z == endVec.Z && polyRadius == 0.0f) { return(null); } var smoothPath = new List <Vector3>(pathSize) { }; NavQueryFilter filter = new NavQueryFilter(); NavPoint startPt, endPt; try { SharpNav.Geometry.Vector3 c = new SharpNav.Geometry.Vector3(startVec.X, startVec.Y, startVec.Z); SharpNav.Geometry.Vector3 ep = new SharpNav.Geometry.Vector3(endVec.X, endVec.Y, endVec.Z); SharpNav.Geometry.Vector3 e = new SharpNav.Geometry.Vector3(5, 5, 5); navMeshQuery.FindNearestPoly(ref c, ref e, out startPt); navMeshQuery.FindNearestPoly(ref ep, ref e, out endPt); //calculate the overall path, which contains an array of polygon references int MAX_POLYS = 256; var path = new SharpNav.Pathfinding.Path(); navMeshQuery.FindPath(ref startPt, ref endPt, filter, path); //find a smooth path over the mesh surface int npolys = path.Count; SharpNav.Geometry.Vector3 iterPos = new SharpNav.Geometry.Vector3(); SharpNav.Geometry.Vector3 targetPos = new SharpNav.Geometry.Vector3(); navMeshQuery.ClosestPointOnPoly(startPt.Polygon, startPt.Position, ref iterPos); navMeshQuery.ClosestPointOnPoly(path[npolys - 1], endPt.Position, ref targetPos); // set target to random point at end of path if (polyRadius != 0.0f) { var randPoly = navMeshQuery.FindRandomPointAroundCircle(endPt, polyRadius); targetPos = randPoly.Position; } if (skipToTarget) { return(new List <Vector3>() { new Vector3(targetPos.X, targetPos.Y, targetPos.Z) }); } smoothPath.Add(new Vector3(iterPos.X, iterPos.Y, iterPos.Z)); //float STEP_SIZE = 0.70f; float SLOP = 0.15f; while (npolys > 0 && smoothPath.Count < smoothPath.Capacity) { //find location to steer towards SharpNav.Geometry.Vector3 steerPos = new SharpNav.Geometry.Vector3(); StraightPathFlags steerPosFlag = 0; NavPolyId steerPosRef = NavPolyId.Null; if (!GetSteerTarget(navMeshQuery, iterPos, targetPos, SLOP, path, ref steerPos, ref steerPosFlag, ref steerPosRef)) { break; } bool endOfPath = (steerPosFlag & StraightPathFlags.End) != 0 ? true : false; bool offMeshConnection = (steerPosFlag & StraightPathFlags.OffMeshConnection) != 0 ? true : false; //find movement delta SharpNav.Geometry.Vector3 delta = steerPos - iterPos; float len = (float)Math.Sqrt(SharpNav.Geometry.Vector3.Dot(delta, delta)); //if steer target is at end of path or off-mesh link //don't move past location if ((endOfPath || offMeshConnection) && len < stepSize) { len = 1; } else { len = stepSize / len; } SharpNav.Geometry.Vector3 moveTgt = new SharpNav.Geometry.Vector3(); VMad(ref moveTgt, iterPos, delta, len); //move SharpNav.Geometry.Vector3 result = new SharpNav.Geometry.Vector3(); List <NavPolyId> visited = new List <NavPolyId>(pathSize); NavPoint startPoint = new NavPoint(path[0], iterPos); navMeshQuery.MoveAlongSurface(ref startPoint, ref moveTgt, out result, visited); path.FixupCorridor(visited); npolys = path.Count; float h = 0; navMeshQuery.GetPolyHeight(path[0], result, ref h); result.Y = h; iterPos = result; //handle end of path when close enough if (endOfPath && InRange(iterPos, steerPos, SLOP, 1000.0f)) { //reached end of path iterPos = targetPos; if (smoothPath.Count < smoothPath.Capacity) { smoothPath.Add(new Vector3(iterPos.X, iterPos.Y, iterPos.Z)); } break; } //store results if (smoothPath.Count < smoothPath.Capacity) { smoothPath.Add(new Vector3(iterPos.X, iterPos.Y, iterPos.Z)); } } } catch (Exception e) { Program.Log.Error(e.Message); Program.Log.Error("Start pos {0} {1} {2} end pos {3} {4} {5}", startVec.X, startVec.Y, startVec.Z, endVec.X, endVec.Y, endVec.Z); // todo: probably log this return(new List <Vector3>() { endVec }); } return(smoothPath); }
/// <summary> /// Scaled vector addition /// </summary> /// <param name="dest">Result</param> /// <param name="v1">Vector 1</param> /// <param name="v2">Vector 2</param> /// <param name="s">Scalar</param> private static void VMad(ref SharpNav.Geometry.Vector3 dest, SharpNav.Geometry.Vector3 v1, SharpNav.Geometry.Vector3 v2, float s) { dest.X = v1.X + v2.X * s; dest.Y = v1.Y + v2.Y * s; dest.Z = v1.Z + v2.Z * s; }
public static Vector3 ToV3(SharpNav.Geometry.Vector3 v) { return(new Vector3(v.X, v.Y, v.Z)); }
public static Vector3 ToV3(SVector3 v) { return(new Vector3(v.X, v.Y, v.Z)); }