Simulation and Modeling (CSCI 3010U)
Faculty of Science, Ontario Tech University
http://vclab.science.ontariotechu.ca
Discuss in class
Consider the following piece of code that pretends to detect if two cirles are colliding. Complete this code to make it work.
import matplotlib.pyplot as plt
import numpy as np
def plot_circles(circle1, circle2, collision):
= plt.subplots(figsize=(6, 6))
fig, ax 0, 100)
ax.set_xlim(0, 100)
ax.set_ylim('equal')
ax.set_aspect(
# Draw circles
= plt.Circle((circle1['x'], circle1['y']), circle1['radius'], color='red' if collision else 'green', alpha=0.5, label='Circle 1')
circle1_patch = plt.Circle((circle2['x'], circle2['y']), circle2['radius'], color='red' if collision else 'green', alpha=0.5, label='Circle 2')
circle2_patch
ax.add_patch(circle1_patch)
ax.add_patch(circle2_patch)
# Draw center points
'x'], circle2['x']], [circle1['y'], circle2['y']], color='black', marker='x', label='Centers')
ax.scatter([circle1[
# Collision state text
= 'Collision!' if collision else 'No Collision'
state_text =14, fontweight='bold', color='red' if collision else 'black')
ax.set_title(state_text, fontsize
plt.legend()True)
plt.grid(
plt.show()
def check_circle_collision(circle1, circle2):
# TO DO
return False
# Define circles for collision case
= {'x': 30, 'y': 50, 'radius': 20}
circle1_collision = {'x': 50, 'y': 50, 'radius': 20}
circle2_collision = check_circle_collision(circle1_collision, circle2_collision)
collision
plot_circles(circle1_collision, circle2_collision, collision)
# Define circles for non-collision case
= {'x': 20, 'y': 50, 'radius': 15}
circle1_no_collision = {'x': 70, 'y': 50, 'radius': 15}
circle2_no_collision = check_circle_collision(circle1_no_collision, circle2_no_collision)
no_collision plot_circles(circle1_no_collision, circle2_no_collision, no_collision)
Now complete the following code that detects whether or not two convex polygons are colliding with each other.
# Collision detection in polygons - 2D case
# Faisal Z. Qureshi
# faisal.qureshi@ontariotechu.ca
#
# Adapted from ChatGPT
import numpy as np
import matplotlib.pyplot as plt
def polygons_collide(poly1, poly2):
"""Check for collision between two convex polygons using SAT."""
# TO DO
return False
def plot_polygons(poly1, poly2, collision):
"""Visualize two polygons and indicate collision status."""
= plt.subplots(figsize=(6, 6))
fig, ax 0, 100)
ax.set_xlim(0, 100)
ax.set_ylim('equal')
ax.set_aspect(
# Convert polygon arrays to lists for plotting
= np.vstack([poly1, poly1[0]]) # Close the shape
poly1 = np.vstack([poly2, poly2[0]]) # Close the shape
poly2
# Draw polygons
0], poly1[:, 1], 'b-', linewidth=2, label="Polygon 1")
ax.plot(poly1[:, 0], poly1[:, 1], 'blue', alpha=0.3)
ax.fill(poly1[:,
0], poly2[:, 1], 'r-', linewidth=2, label="Polygon 2")
ax.plot(poly2[:, 0], poly2[:, 1], 'red', alpha=0.3)
ax.fill(poly2[:,
# Collision state text
= "Collision!" if collision else "No Collision"
state_text =14, fontweight="bold", color="green" if collision else "red")
ax.set_title(state_text, fontsize
plt.legend()True)
plt.grid(
plt.show()
# Example polygons
= np.array([[10, 10], [30, 10], [30, 30]]) #, [10, 30]]) # Square
polygon1 = np.array([[25, 25], [45, 25], [45, 45], [25, 45]]) # Overlapping Square
polygon2 = np.array([[50, 50], [70, 50], [70, 70], [50, 70]]) # Non-overlapping Square
polygon3
# Check and plot collision case
= polygons_collide(polygon1, polygon2)
collision1
plot_polygons(polygon1, polygon2, collision1)
# Check and plot non-collision case
= polygons_collide(polygon1, polygon3)
collision2 plot_polygons(polygon1, polygon3, collision2)
Via course Canvas.