/// <summary> /// Given an AABB Box Entity and a CircleEntity, checks if these objects are overlapping /// </summary> /// <returns> returns true if 'Circle' and 'Box' are overlapping each other </returns> public static bool DetectCollision(CircleEntity Circle, BoxEntity Box) { Vector Edge = new Vector(Circle.x, Circle.y); //if the circle is on the left if (Circle.x < Box.Min.x) { //edge x to check is set to left side Edge.x = Box.Min.x; } //if the circle is on the right else if (Circle.x > Box.Max.x) { //edge x to check is set to the right side Edge.x = Box.Max.x; } //if the circle is above box if (Circle.y < Box.Min.y) { //edge y to check is set on top Edge.y = Box.Min.y; } //if the circle is below else if (Circle.y > Box.Max.y) { //edge y to check is set on the bottom Edge.y = Box.Max.y; } Vector Distance = new Vector(Edge.x - Circle.x, Edge.y - Circle.y); float RadiusSquared = Circle.Radius * Circle.Radius; Console.WriteLine(); return(Mathf.Pow(Distance.x, 2) + Mathf.Pow(Distance.y, 2) < RadiusSquared); }
/// <summary> /// Given 2 CircleEntities, generates a Manifold containing the information required to resolve a collision (if there was one to begin with). /// </summary> public static Manifold GenerateManifold(CircleEntity A, CircleEntity B) { Manifold Result; Result.A = A; Result.B = B; float totalRadius = A.Radius + B.Radius; float dx = A.x - B.x; float dy = A.y - B.y; float distancePowered = (dx * dx) + (dy * dy); if (totalRadius * totalRadius <= distancePowered) { return(GenerateEmptyManifold()); } float distance = Mathf.Sqrt(distancePowered); Result.PenetrationDepth = totalRadius - distance; Vector BtoA = (B.position - A.position).Normalized(); Vector contactPoint = A.position + BtoA * A.Radius; Result.ContactPoint = new List <Vector>(); Result.ContactPoint.Add(contactPoint); Result.Normal = BtoA; Result.IsEmpty = false; return(Result); }
protected override void OnMouseDown(Vector3 pos, ICADObject sko) { if (current != null) { if (!canCreate) { return; } current.c.isSelectable = true; current.isSelectable = true; current = null; return; } if (DetailEditor.instance.currentSketch == null) { return; } current = new CircleEntity(DetailEditor.instance.currentSketch.GetSketch()); current.center.pos = pos; AutoConstrainCoincident(current.center, sko as IEntity); current.isSelectable = false; current.c.isSelectable = false; }
/// <summary> /// Given a CircleEntity and the vertices and position of a 2D Polygon, checks if the circle and the polygon are overlapping /// </summary> /// <param name="circle"> The circle entity in question </param> /// <param name="polyPosition"> The position of the polygon in question </param> /// <param name="poly"> The world space position of the vertices of the polygon </param> /// <param name="localPoly"> The local space position of the vertices of the polygon </param> /// <returns></returns> public static bool DetectCollision(CircleEntity circle, Vector polyPosition, Vector[] poly, Vector[] localPoly) { Vector relativeCirclePos = circle.position - polyPosition; float seperation = -10000; int face = -1234; //Console.WriteLine("MOVEMNT TICK"); for (int i = 0; i < poly.Length; i++) { //get the normal Vector normal = poly[i].GetNormal().Normalized(); Vector circleToPoint = relativeCirclePos - localPoly[i]; //dot the normal with the circle position float currentSeperation = Vector.Dot(normal, circleToPoint); //if result of dot is bigger than radius if (currentSeperation >= circle.Radius) { //Console.WriteLine("NO COL"); return(false); } //return //if its bigger than stored penetration if (currentSeperation > seperation) { //store current face //store current penetration face = i; seperation = currentSeperation; } } Vector v1 = poly[face]; int secondIndex = face + 1 >= poly.Length ? 0 : face + 1; Vector v2 = poly[secondIndex]; float v1Dot = Vector.Dot(circle.position - v1, v2 - v1); float v2Dot = Vector.Dot(circle.position - v2, v1 - v2); if (v1Dot <= 0) { if (Mathf.DistanceSquared(v1.x, v1.y, circle.position.x, circle.position.y) > circle.Radius * circle.Radius) { //Console.WriteLine("NO COL"); return(false); } } else if (v2Dot <= 0) { if (Mathf.DistanceSquared(v2.x, v2.y, circle.position.x, circle.position.y) > circle.Radius * circle.Radius) { //Console.WriteLine("NO COL"); return(false); } } return(true); }
void AddCircle(netDxf.Entities.Circle c) { var ce = c.Center; CircleEntity circle = new CircleEntity(DetailEditor.instance.currentSketch.GetSketch()); circle.c.SetPosition(new UnityEngine.Vector3((float)ce.X, (float)ce.Y, (float)ce.Z)); circle.radius.value = c.Radius; }
protected override void OnDeactivate() { if (current != null) { current.Destroy(); current = null; } canCreate = true; }
/// <summary> /// Given 2 circle entities, checks if the circles are overlapping /// </summary> /// <returns> returns true if CircleA and CircleB are overlapping each other </returns> public static bool DetectCollision(CircleEntity CircleA, CircleEntity CircleB) { float CollisionDistance = CircleA.Radius + CircleB.Radius; float dx = CircleA.x - CircleB.x; float dy = CircleA.y - CircleB.y; return(CollisionDistance * CollisionDistance >= (dx * dx) + (dy * dy)); }
/// <summary> /// Given a CircleEntity and a PolygonEntity, checks if their bounding volumes overlap each other /// </summary> /// <returns></returns> public static bool DetectBoundingVolumeCollision(CircleEntity A, PolygonEntity B) { float CollisionDistance = A.Radius + B.GetVolumeRadius(); float dx = A.x - B.x; float dy = A.y - B.y; return(CollisionDistance * CollisionDistance >= (dx * dx) + (dy * dy)); }
/// <summary> /// /// </summary> /// <param name="Circle"></param> /// <param name="poly"></param> /// <returns></returns> public static bool DetectCollision(CircleEntity Circle, Vector[] poly) { Vector Edge = new Vector(Circle.position.x, Circle.position.y); float minX = poly[1].x; float maxX = poly[0].x; float minY = poly[2].y; float maxY = poly[1].y; //if the circle is on the left if (Circle.x < minX) { //edge x to check is set to left side Edge.x = minX; } //if the circle is on the right else if (Circle.x > maxX) { //edge x to check is set to the right side Edge.x = maxX; } //if the circle is above box if (Circle.y < minY) { //edge y to check is set on top Edge.y = minY; } //if the circle is below else if (Circle.y > maxY) { //edge y to check is set on the bottom Edge.y = maxY; } Vector Distance = new Vector(Edge.x - Circle.x, Edge.y - Circle.y); float RadiusSquared = Circle.Radius * Circle.Radius; MyGame.Points.Add(new Vector(Edge.x, Edge.y)); return(Mathf.Pow(Distance.x, 2) + Mathf.Pow(Distance.y, 2) < RadiusSquared); }
/// <summary> /// Given a PolygonEntitiy and a Circle Entity, generates a manifold containing the information required to /// resolve the collision (if there was one to begin with). /// </summary> public static Manifold GenerateManifold(PolygonEntity poly, CircleEntity circle) { Manifold Result; Result.A = poly; Result.B = circle; Result.IsEmpty = false; Result.ContactPoint = new List <Vector>(); Vector relativeCirclePos = circle.position - poly.position; float seperation = -10000; int face = -1234; for (int i = 0; i < poly.GetVertices().Length; i++) { //get the normal Vector normal = poly.GetFaceNormal(i).Normalized(); Vector circleToPoint = relativeCirclePos - poly.GetLocalSpaceVertices()[i]; //dot the normal with the circle position float currentSeperation = Vector.Dot(normal, circleToPoint); //if result of dot is bigger than radius if (currentSeperation > circle.Radius) { return(GenerateEmptyManifold()); } //return //if its bigger than stored penetration if (currentSeperation > seperation) { face = i; seperation = currentSeperation; } } Vector v1 = poly.GetVertices()[face]; int secondIndex = face + 1 >= poly.GetVertices().Length ? 0 : face + 1; Vector v2 = poly.GetVertices()[secondIndex]; //if point is inside polygon if (seperation < EPSILON) { //normal is negative face normal Result.Normal = poly.GetFaceNormal(face).Normalized() * -1f; Result.PenetrationDepth = circle.Radius; Result.ContactPoint.Add(Result.Normal * circle.Radius + circle.position); //penetration depth is radius //contact point is -normal*radius + position } //determine which voronoi region the circle lies in //dot with first point float v1Dot = Vector.Dot(circle.position - v1, v2 - v1); float v2Dot = Vector.Dot(circle.position - v2, v1 - v2); Result.PenetrationDepth = circle.Radius - seperation; //dot with second point //penetration depth is radius-seperation //if dot with first point is negative if (v1Dot <= 0) { if (Mathf.DistanceSquared(v1.x, v1.y, circle.position.x, circle.position.y) > circle.Radius * circle.Radius) { return(GenerateEmptyManifold()); } Result.Normal = (v1 - poly.position).Normalized(); Result.ContactPoint.Add(v1); //normal is point-center //contact point is point } else if (v2Dot <= 0) { if (Mathf.DistanceSquared(v2.x, v2.y, circle.position.x, circle.position.y) > circle.Radius * circle.Radius) { return(GenerateEmptyManifold()); } Result.Normal = (v2 - poly.position).Normalized(); Result.ContactPoint.Add(v2); } else { Result.Normal = poly.GetFaceNormal(face).Normalized(); Result.ContactPoint.Add(Result.Normal * circle.Radius + circle.position); } return(Result); }
public override bool CheckCollision(CircleEntity other) { return(CollisionDetection.DetectBoundingVolumeCollision(other, this)); }
private void Awake() { _circle = GetComponent <CircleEntity>(); _setting = App.Instance.Settings; }
public override Manifold GenerateManifold(CircleEntity other) { return(CollisionResolution.GenerateManifold(this, other)); }
public override bool CheckCollision(CircleEntity other) { return(true); }
void Awake() { _setting = App.Instance.Settings; _timer = App.Instance.TimerManager; _circle = GetComponent <CircleEntity>(); }
public abstract bool CheckCollision(CircleEntity other);
public Diameter(Sketch sk, CircleEntity c) : base(sk) { AddEntity(c); Satisfy(); }
//COLISION WITH RADIO BROADCASTING /// <summary> /// true if colides with any radio signal except given signal /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool CollisionRadio(CircleEntity entity) { throw new NotImplementedException(); }
//COLISION AND MOVEMENTS //COLLISION WITH OTHER ROBOTS OR PASSIVE ENTITY /// <summary> /// Collision of circle to the newMiddle /// </summary> /// <param name="entity"></param> /// <param name="newMiddle"></param> /// <returns></returns> public bool Collision(CircleEntity entity, Vector2 newMiddle) { throw new NotImplementedException(); }
public abstract Manifold GenerateManifold(CircleEntity other);