#๐Ÿ”’ Rigid polygon in a gravitational field simulation

5 messages ยท Page 1 of 1 (latest)

near remnant
#

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()
vernal sableBOT
#

@near remnant

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

near remnant
#

I figured it out, it was because the rotations were being applied to already rotating points, so the points got scattered because of that

vernal sableBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.