**Introduction**

This tutorial is an introduction about collision detection and in this first part we will see how to detect collision between two circles. I will be using Python programming language to write the code and hope you are already familiar with Python language ðŸ™‚

**Collision between two circles**

Here we will take two circles as our input, for each circle we will take the position and radius. Using this three values from each of the circle we can easily check the collision.

- Calculate the horizontal distance between the circle centers
- Calculate the vertical distance between the circle centers
- Form a right angle triangle using the horizontal distance as base and vertical distance as height
- Calculate the length of the hypotenuse using the base and height
- Calculate the distance between the center of the circles
- If hypotenuse calculated is less than the distance between the center of the circles we can say a collision occurred.

The below given image describes three different cases of collision between two circles

**How can we make use of the circle collision detection**

Consider two objects having random shapes and you have to do some collision collision check between this two objects. Yes the exact collision detection between random shaped objects are not easy to achieve. But you can do the collision detection between this two with circles

- Define a mask for each of the shapes. Let the mask be a circle of a specific radius. The radius selected should be enough to cover (at least 80%) the random shaped object. Do the same for the second object
- Now all you have to do is processing this mask while checking collision. Since the mask defined for each object is circular in shape and cover 80% of the area, the collision checking will be easy and enough. ðŸ™‚

**The Program**

The below given program is written in Python language and the things that we saw above is written step by step.

import math # Find the absolute value of a variable def absolute_value(x): if x < 0: return 0 - x return x # Read parameters of a circle, x_position, y_position and radius def read_circle(number): radius = int(input("Enter circle " + str(number) + " radius: ")) x_position = int(input("Enter circle " + str(number) + " x position: ")) y_position = int(input("Enter circle " + str(number) + "y position: ")) print() return (radius, x_position, y_position) # Create a right angle triangle and check for collision # Take two circles as arguments and do the calculation def check_collision(circle1, circle2): base = absolute_value(circle1[1] - circle2[1]) height = absolute_value(circle1[2] - circle2[2]) hypotenuse = math.sqrt(base * base + height * height) center_distance = circle1[0] + circle2[0] if hypotenuse <= center_distance: return True return False # The main method # Print success message on collision def main(): circle1 = read_circle(1) circle2 = read_circle(2) collision_status = check_collision(circle1, circle2) if collision_status: print("Success") else: print("Failed") main()

Hope this tutorial was helpful ðŸ™‚

Thanks for Reading