public static void SetPolygon(ref SBPConvexData data, int corner, float size, float startAngle, InnerEdgeConstructionType innerEdgeConstructionType) { if (data == null) { return; } corner = Mathf.Max(corner, 3); size = Mathf.Max(size, 1e-3f); if (corner % 2 == 0) { // 偶数角の場合 int h = corner / 2; data.nodes = new SBPNodeData[corner]; data.edges = new SBPEdgeData[corner + h]; float angleDelta = 360f / corner; for (int i = 0; i < corner; ++i) { float angle = angleDelta * i + startAngle; data.nodes[i] = new SBPNodeData(Utilities.GetAngledVectorWithDegree(angle)); } for (int i = 0, j = corner - 1; i < corner; j = i++) { data.edges[i] = new SBPEdgeData(i, j); } for (int i = 0; i < h; ++i) { data.edges[corner + i] = new SBPEdgeData(i, i + h); } data.helperNodeOffset = corner; } else { // 奇数角の場合は中心点が必要 data.nodes = new SBPNodeData[corner + 1]; data.edges = new SBPEdgeData[corner * 2]; float angleDelta = 360f / corner; for (int i = 0; i < corner; ++i) { float angle = angleDelta * i + startAngle; data.nodes[i] = new SBPNodeData(Utilities.GetAngledVectorWithDegree(angle)); } data.nodes[corner] = new SBPNodeData(Vector2.zero); for (int i = 0, j = corner - 1; i < corner; j = i++) { data.edges[i * 2] = new SBPEdgeData(i, j); data.edges[i * 2 + 1] = new SBPEdgeData(i, corner); } data.helperNodeOffset = corner; } }
private void OnWizardCreate() { SBPConvexData obj = SBPConvexData.CreateInstance("SBPConvexData") as SBPConvexData; SetPolygon(ref obj, corner, 1f, 0f, InnerEdgeConstructionType.ConnectWithCenter); // SetTriangle(ref obj, 1f, 0f); AssetDatabase.CreateAsset(obj, path + "/SBPConvexData.asset"); }
public static Convex LoadFromConvexData(SBPConvexData data, Vector2 translation, float rotation, Vector2 scale) { if (data == null) { return(null); } // ノードを作成 List <Node> nodes = new List <Node>(); for (int i = 0; i < data.nodes.Length; ++i) { SBPNodeData nodeData = data.nodes[i]; Vector2 position = nodeData.position; position *= scale; position = Utilities.RotateVector(position, rotation); position += translation; Node n = new Node(position, nodeData.mass, nodeData.damping); nodes.Add(n); } // エッジを作成 List <Edge> edges = new List <Edge>(); for (int i = 0; i < data.edges.Length; ++i) { SBPEdgeData edgeData = data.edges[i]; Edge e = new Edge(nodes[edgeData.aIdx], nodes[edgeData.bIdx]); edges.Add(e); } // 凸包を作成 Convex convex = new Convex(); int offset = data.helperNodeOffset; for (int i = 0; i < nodes.Count; ++i) { if (i < offset) { convex.AddCollisionNode(nodes[i]); } else { convex.AddHelperNode(nodes[i]); } } convex.edges = edges; convex.RecalculateBounds(); return(convex); }