import math
from random import randint

class Vec2D:
    def __init__(self, x=50, y=50):
        self.x = x
        self.y = y
    
class Entity:
    def __init__(self, x = 0, y = 50, vx = 0.5, vy = 0, mass = 0.1):
        self.pos = Vec2D(x, y)
        self.vel = Vec2D(vx, vy)
        self.mass = mass
        self.radius = 10
        self.restitution = -0.7
        self.circle = Circle(x=self.pos.x, y=self.pos.y, width=self.radius, height=self.radius)
    def draw(self):
        self.circle.x = self.pos.x
        self.circle.y = self.pos.y
        self.width = self.radius
        self.height = self.radius
    
FRAMERATE  = 1 / 60
    
Cd = 0.47
rho = 1.22
"TODO: Make part of loop"
radius = 10

A = math.pi * radius * radius / (10000) 
ag = 9.81

def gravity(entity):
    "TODO: Make this not awful"
    if entity.vel.x == 0:
        entity.vel.x += pow(0.1, 0.1)
    if entity.vel.y == 0:
        entity.vel.y = pow(0.1, 0.1)
        
    Fx = -0.5 * Cd * A * rho * entity.vel.x * entity.vel.x * entity.vel.x / abs(entity.vel.x)    
    Fy = -0.5 * Cd * A * rho * entity.vel.y * entity.vel.y * entity.vel.y / abs(entity.vel.y)  
    
    if math.isnan(Fx):
        Fx = 0
        
	if math.isnan(Fy):
		Fy = 0
    
    ax = Fx / entity.mass
    ay = ag + (Fy / entity.mass)
    
    entity.vel.x += ax * FRAMERATE
    entity.vel.y += ay * FRAMERATE
    
    entity.pos.x += entity.vel.x * FRAMERATE * 100
    entity.pos.y += entity.vel.y * FRAMERATE * 100
    return entity

def collision(entity, entities):
    if entity.pos.y > 100 - entity.radius / 2:
        entity.pos.y = 100 - entity.radius / 2
        entity.vel.y *= entity.restitution
    return entity

entities = [Entity(), Entity(randint(10, 90), randint(0, 90), randint(-1, -1), randint(-1, -1))]
for i in range(175):
    with animation(FRAMERATE / 2):
        for entity in entities:
            gravity(entity)
            collision(entity, entities)
            entity.draw()

physics

by brecert

Created 3 months, 3 weeks ago.