using UnityEngine; public class CollisionDetection : MonoBehaviour { private void OnCollisionEnter(Collision collision) { Debug.Log("Collision detected with " + collision.gameObject.name); } }
using System; using Box2DX.Collision; public class CollisionDetection { public static void Main() { // Create two circles with different positions and radii CircleShape circle1 = new CircleShape(); circle1.Position.Set(0.0f, 0.0f); circle1.Radius = 1.0f; CircleShape circle2 = new CircleShape(); circle2.Position.Set(2.0f, 2.0f); circle2.Radius = 2.0f; // Check whether the circles are intersecting bool isIntersecting = Collision.TestOverlap(circle1, circle2, Transform.Identity, Transform.Identity); if (isIntersecting) { Console.WriteLine("Circles are intersecting"); } else { Console.WriteLine("Circles are not intersecting"); } } }
using FarseerPhysics.Collision; using FarseerPhysics.Collision.Shapes; using Microsoft.Xna.Framework; public class CollisionDetection { public static void Main() { // Create two rectangles with different positions and sizes PolygonShape rect1 = new PolygonShape(PolygonTools.CreateRectangle(1.0f, 1.0f)); rect1.Position = Vector2.Zero; PolygonShape rect2 = new PolygonShape(PolygonTools.CreateRectangle(2.0f, 2.0f)); rect2.Position = new Vector2(3.0f, 3.0f); // Check whether the rectangles are intersecting bool isIntersecting = AABB.TestOverlap(ref rect1.AABB, ref rect2.AABB); if (isIntersecting) { Console.WriteLine("Rectangles are intersecting"); } else { Console.WriteLine("Rectangles are not intersecting"); } } }This code uses Farseer Physics package to create two rectangles with different positions and sizes, and then checks whether they are intersecting or not using the TestOverlap() method.