static public int GetPath(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.PolygonCollider2D self = (UnityEngine.PolygonCollider2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPath(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
private void createWithCollider2d(Collider2D coll) { b2FixtureDef fixtureDef = new b2FixtureDef(); PhysicsMaterial2D material = coll.sharedMaterial; if (material != null) { fixtureDef.restitution = material.bounciness; fixtureDef.friction = material.friction; } fixtureDef.isSensor = coll.isTrigger; if (coll is BoxCollider2D) { BoxCollider2D boxColl = coll as BoxCollider2D; b2PolygonShape s = b2PolygonShape.AsOrientedBox(boxColl.size.x * 0.5f, boxColl.size.y * 0.5f, new b2Vec2(boxColl.offset.x, boxColl.offset.y), 0 /*transform.eulerAngles.z*Mathf.Deg2Rad*/); scaleShape(s); fixtureDef.shape = s; _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) }; } else if (coll is CircleCollider2D) { CircleCollider2D circleColl = coll as CircleCollider2D; b2CircleShape s = new b2CircleShape(circleColl.radius); s.SetLocalPosition(new b2Vec2(circleColl.offset.x, circleColl.offset.y)); scaleShape(s); fixtureDef.shape = s; _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) }; } else if (coll is PolygonCollider2D) { int i, j; PolygonCollider2D polyColl = coll as PolygonCollider2D; List <b2Fixture> fixtureList = new List <b2Fixture>(); int pathCount = polyColl.pathCount; for (i = 0; i < pathCount; i++) { Vector2[] path = polyColl.GetPath(i); b2Vec2[] vertices = new b2Vec2[path.Length]; for (j = 0; j < path.Length; j++) { vertices[j] = new b2Vec2(path[j].x, path[j].y); } b2Separator sep = new b2Separator(); b2Fixture[] fixtures = sep.Separate(_body, fixtureDef, vertices, 100, polyColl.offset.x, polyColl.offset.y); //必须放大100倍进行计算 for (j = 0; j < fixtures.Length; j++) { scaleShape(fixtures[j].GetShape()); } fixtureList.AddRange(fixtures); } _fixtureDict[coll] = fixtureList.ToArray(); } }
public List<Vector2> GetVerts() { polygon = GetComponent<PolygonCollider2D>(); List<Vector2> verts = new List<Vector2>(polygon.GetPath(0)); //transform to world space for(int i = 0; i < verts.Count; ++i) { verts[i] = transform.TransformPoint((Vector3)verts[i]); } return verts; }
static public int GetPath(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.PolygonCollider2D self = (UnityEngine.PolygonCollider2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPath(a1); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 3) { UnityEngine.PolygonCollider2D self = (UnityEngine.PolygonCollider2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Collections.Generic.List <UnityEngine.Vector2> a2; checkType(l, 3, out a2); var ret = self.GetPath(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetPath to call"); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
static public int GetPath(IntPtr l) { try { UnityEngine.PolygonCollider2D self = (UnityEngine.PolygonCollider2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPath(a1); pushValue(l, ret); return(1); } catch (Exception e) { return(error(l, e)); } }
// Use this for initialization void Start() { polygon = GetComponent<PolygonCollider2D> (); Vector2[] points = polygon.GetPath (0); // Get sides sides = InitializeSides (points); // Set up game object with mesh; gameObject.AddComponent(typeof(MeshRenderer)); MeshFilter filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; Triangulator tr = new Triangulator(points); filter.mesh = tr.GenerateMesh (); }
static public int GetPath(IntPtr l) { try{ UnityEngine.PolygonCollider2D self = (UnityEngine.PolygonCollider2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); UnityEngine.Vector2[] ret = self.GetPath(a1); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
void OnDrawGizmos() { polygon = GetComponent<PolygonCollider2D> (); Vector2[] points = polygon.GetPath (0); Vector2 prevPoint = transform.TransformPoint(points[points.Length-1]); Vector2 nextPoint; foreach(Vector2 point in points) { nextPoint = transform.TransformPoint (point); Gizmos.DrawLine (prevPoint, nextPoint); prevPoint = nextPoint; } }
static int QPYX_GetPath_YXQP(IntPtr L_YXQP) { try { ToLua.CheckArgsCount(L_YXQP, 2); UnityEngine.PolygonCollider2D QPYX_obj_YXQP = (UnityEngine.PolygonCollider2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.PolygonCollider2D)); int QPYX_arg0_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 2); UnityEngine.Vector2[] QPYX_o_YXQP = QPYX_obj_YXQP.GetPath(QPYX_arg0_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
static int GetPath(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); UnityEngine.PolygonCollider2D obj = (UnityEngine.PolygonCollider2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.PolygonCollider2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); UnityEngine.Vector2[] o = obj.GetPath(arg0); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
void OptimizePolygonCollider2D(GameObject gameobject, PolygonCollider2D polygonCollider2D) { int origVertCount = 0; int cleanVertCount = 0; float angleThreshold = 90.0f; List<Vector2> newVerts = new List<Vector2>(); for (int i = 0; i < polygonCollider2D.pathCount; i++) { newVerts.Clear(); Vector2[] path = polygonCollider2D.GetPath(i); if (path.Length < 4) { continue; } origVertCount += path.Length; float angle1 = 0; float angle2 = 0; newVerts.Clear(); int mx = path.Length; Vector2 currentDir = (path[0] - path[1]).normalized; angle1 = Vector2.Angle(path[0] - path[1], currentDir); for (int j = 0; j < mx; j++) { int sPrev = (((j - 1) % mx) + mx) % mx; int sNext = (((j + 1) % mx) + mx) % mx; angle1 = Vector2.Angle(path[sPrev] - path[j], currentDir); angle2 = Vector2.Angle(path[j] - path[sNext], currentDir); if (angle1 > angleThreshold || angle2 > angleThreshold) { currentDir = (path[j] - path[sNext]).normalized; newVerts.Add(path[j]); } } polygonCollider2D.SetPath(i, newVerts.ToArray()); cleanVertCount += newVerts.Count; } }
private Vector2 GetNearestToMesh(Vector2 vertex, PolygonCollider2D poly) { // Test to make sure starting on the collision mesh RaycastHit2D hit = Physics2D.Raycast (vertex - new Vector2 (0.005f, 0f), new Vector2 (1f, 0f), 0.01f, 1 << KickStarter.sceneSettings.navMesh.gameObject.layer); if (!hit) { Transform t = KickStarter.sceneSettings.navMesh.transform; float minDistance = -1; Vector2 nearestPoint = vertex; for (int i=0; i<poly.pathCount; i++) { Vector2[] path = poly.GetPath (i); for (int j=0; j<path.Length; j++) { Vector2 startPoint = t.TransformPoint (path[j]); Vector2 endPoint = Vector2.zero; if (j==path.Length-1) { endPoint = t.TransformPoint (path[0]); } else { endPoint = t.TransformPoint (path[j+1]); } Vector2 direction = endPoint - startPoint; for (float k=0f; k<=1f; k+=0.1f) { float distance = Vector2.Distance (vertex, startPoint + (direction * k)); if (distance < minDistance || minDistance < 0f) { minDistance = distance; nearestPoint = startPoint + (direction * k); } } } } return nearestPoint; } return (vertex); }
private Vector2[] CreateVertexArray(PolygonCollider2D poly) { List <Vector2> vertexData = new List<Vector2>(); Transform t = KickStarter.sceneSettings.navMesh.transform; for (int i=0; i<poly.pathCount; i++) { Vector2[] _vertices = poly.GetPath (i); foreach (Vector2 _vertex in _vertices) { Vector3 vertex3D = t.TransformPoint (new Vector3 (_vertex.x, _vertex.y, t.position.z)); vertexData.Add (new Vector2 (vertex3D.x, vertex3D.y)); } } return vertexData.ToArray (); }
private void RebuildVertexArray(Transform navMeshTransform, PolygonCollider2D poly) { List<Vector2> _vertexData = new List<Vector2>(); for (int i=0; i<poly.pathCount; i++) { Vector2[] _vertices = poly.GetPath (i); foreach (Vector2 _vertex in _vertices) { Vector3 vertex3D = navMeshTransform.TransformPoint (new Vector3 (_vertex.x, _vertex.y, navMeshTransform.position.z)); _vertexData.Add (new Vector2 (vertex3D.x, vertex3D.y)); } } vertexData = _vertexData.ToArray (); }
public static void FromPolygonCollider(VLSObstructor _obstructor, PolygonCollider2D _collider, int _pathIndex = 0) { pntCount = _collider.GetPath(_pathIndex).Length; for (int i = pntCount - 1; i >= 0; i--) _obstructor.LocalVertex(10000, _collider.GetPath(_pathIndex)[i]); if (!_obstructor.VertsAreCounterClockwise()) _obstructor.ReverseNormals(); }
/// <summary> /// generates a list of points from a polygon collider /// </summary> /// <param name="collider">source polygon collider</param> /// <returns>list of points</returns> private static List<Vector2> getPoints(PolygonCollider2D collider) { List<Vector2> points = new List<Vector2>(); foreach (Vector2 point in collider.GetPath(0)) { points.Add(point); } return points; }