I have created a mesh of first image and i want the collider between the pink shaded region
After writing the following Script i got collider in second image
void InnerOuterCircle () {
vertice = new List<Vector3> ();
triangle = new List<int> ();
for (int x = 0; x <= angle; x ++) {
vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * innerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * innerRadius));
vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * outerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * outerRadius));
}
for (int x = 0; x < vertice.Count - 2; x += 2) {
triangle.Add (x + 0);
triangle.Add (x + 2);
triangle.Add (x + 1);
triangle.Add (x + 2);
triangle.Add (x + 3);
triangle.Add (x + 1);
}
Mesh mesh = new Mesh ();
MeshFilter filter = GetComponent<MeshFilter> ();
filter.mesh = mesh;
mesh.vertices = vertice.ToArray ();
mesh.triangles = triangle.ToArray ();
PolygonCollider2D collider = gameObject.AddComponent<PolygonCollider2D> ();
Vector2[] edgePoints = new Vector2[vertice.Count];
for (int i = 0; i < vertice.Count; i++) {
edgePoints [i] = vertice [i];
}
collider.points = edgePoints;
}
Assuming it's possible to do custom collision detection logic in Unity, collision detection between circles is rather simple; just use the Pythagorean theorem on the vector between their centers:
bool TestCirclesCollision(double x1, double y1, double r1, double x2, double y2, double r2)
{
// Pythagorean theorem to compute distance between two points
var dx = x2 - x1;
var dy = y2 - y1;
var distance = Math.Sqrt(dx * dx + dy * dy);
// compare to combined radii of two circles
// return true if collision, otherwise false
return distance <= r1 + r2;
}
If the circles have holes in them at their centers, you would add an additional test to make sure they're not too close, same as the existing radius test but with a >= instead of a <= on the inner:
return distance <= r1outer + r2outer && distance >= r1inner + r2inner;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.