void CircleCollisionResolution(CircleCollider other) { Vector2 closestPoint = ClosestPoint(parent.Position); Vector2 diff = other.Parent.attachedTo.GetPosition() - closestPoint; float length = diff.Length(); if (Math.Abs(diff.x / (HalfExtents.x * 2)) > Math.Abs(diff.y / (HalfExtents.y * 2))) { //x vector longer than y if (other.Parent.attachedTo.GetPosition().x > parent.attachedTo.GetPosition().x) { //we are right parent.attachedTo.SetPosition(parent.attachedTo.GetPosition().x + (other.Radius - length + 1), parent.attachedTo.GetPosition().y); } else { //we are left parent.attachedTo.SetPosition(parent.attachedTo.GetPosition().x - (other.Radius - length + 1), parent.attachedTo.GetPosition().y); } } else { //y longer than x if (other.Parent.attachedTo.GetPosition().y > parent.attachedTo.GetPosition().y) { //we are below parent.attachedTo.SetPosition(parent.attachedTo.GetPosition().x, parent.attachedTo.GetPosition().y + (other.Radius - length + 1)); } else { //we are above parent.attachedTo.SetPosition(parent.attachedTo.GetPosition().x, parent.attachedTo.GetPosition().y - (other.Radius - length + 1)); } } }
void CircleCollisionResolution(CircleCollider other) { var dist = parent.attachedTo.GetPosition() - other.parent.attachedTo.GetPosition(); var length = (other.parent.Position - parent.Position).Length(); Vector2 unit = new Vector2(dist.x / length, dist.y / length); parent.attachedTo.SetPosition(other.parent.attachedTo.GetPosition().x + (radius + other.radius + 1) * unit.x, other.parent.attachedTo.GetPosition().y + (radius + other.radius + 1) * unit.y); }
//check for collsion with another circle collider bool CircleCollision(CircleCollider other) { //if distance between the 2 circles is less than the 2 circles radie combined to the power of 2 if ((other.parent.Position - parent.Position).LengthSquared() <= (Math.Pow(radius + other.radius, 2))) { //collision has occured parent.DebugColour = Color.BLUE; parent.Collision = true; other.parent.Collision = true; return(true); } parent.Collision = false; parent.DebugColour = Color.RED; return(false); }
//check for collision with a circle collider bool CircleCollision(CircleCollider other) { //get the distance between the closest point on the AABB to the circle Vector2 diff = ClosestPoint(other.Parent.Position) - other.Parent.Position; // if the distance is less or equal to the circle radius^2 if (diff.LengthSquared() <= (other.Radius * other.Radius)) { //collision has occured parent.DebugColour = Color.BLUE; parent.Collision = true; other.Parent.Collision = true; return(true); } parent.DebugColour = Color.RED; parent.Collision = false; return(false); }
//create the collider public Collider(CollisionType colType, GameObject AttachedTo) { attachedTo = AttachedTo; position = attachedTo.GetPosition(); //create the collider and set the collision type switch (colType) { case CollisionType.AABB: aabb = new AABB(this); collisionType = CollisionType.AABB; break; case CollisionType.Circle: circle = new CircleCollider(this); collisionType = CollisionType.Circle; break; } }