Hey y'all, I'm doing a simple simulation of a falling polygon using matplot, however I don't really understand why the code I have isn't working out, it plots the polygon all over the grid.
I assume it might have to do with modifying the x and y list variables directly? Please help me understand.
import matplotlib.pyplot as plt
import math
x_list = [1.5, -0.5, -0.5, 1.5]
y_list = [5, 6, 4, 5]
x_cm = sum(x_list[:-1])/ 3
y_cm = sum(y_list[:-1])/ 3
g = 9.81 # m/s^2
dt = 0.05 #s
vx = 5.0 # m/s
vy = 5.0 # m/s
rotation = math.radians(0)
ang_velocity = math.pi # rad/s
plt.plot(x_list, y_list, 'b')
while y_cm > 0:
# physics values first
vy -= g * dt
x_cm += vx * dt
y_cm += vy * dt
rotation += ang_velocity * dt
# then the rotation of each corner points
rotated_x_list = []
rotated_y_list = []
for i, item in enumerate(x_list):
new_x = x_list[i] * math.cos(rotation) - y_list[i] * math.sin(rotation)
new_y = x_list[i] * math.sin(rotation) + y_list[i] * math.cos(rotation)
rotated_x_list.append(new_x + x_cm)
rotated_y_list.append(new_y + y_cm)
for i, item in enumerate(rotated_x_list):
x_list[i] = rotated_x_list[i]
y_list[i] = rotated_y_list[i]
plt.plot(x_list, y_list, 'g')
#plt.plot(x_list, y_list, 'b')
plt.gca().set_aspect('equal') # set aspect ratio equal
plt.show()