Should by sim class be updating the balls, or should the balls be updating themselves?

Posted by ElegantPoet3386@reddit | learnprogramming | View on Reddit | 6 comments

So, I'm still working on my physics simulation that I started 3 days ago. Right now there's a sim class that controls the simulation. I decided today to also make a ball class so that I can reuse logic for every single ball, that way multiple balls won't become a nightmare to code though. My code is as follows.

import arcade
import random
GRAVITY = -1000;
C = 0.8;

class Sim(arcade.View):
    def __init__(self):
        super().__init__();
        ball = Ball(100,window.height-50,0,0,50,100,(255,255,255))
        self.ball_list = [ball]
    
    def on_draw(self):
        self.clear();
        for i in range(len(self.ball_list)):
            arcade.draw_circle_filled(self.ball_list[i].x,self.ball_list[i].y,self.ball_list[i].radius,self.ball_list[i].color)


    
    def on_update(self, delta_time):
        for i in range(len(self.ball_list)):
            self.ball_list[i].velocity_y+=GRAVITY*delta_time
            self.ball_list[i].y += self.ball_list[i].velocity_y*delta_time
            if(self.ball_list[i].y - self.ball_list[i].radius < 0):
                self.ball_list[i].velocity_y *= -(C**0.5)
                self.ball_list[i].y = self.ball_list[i].radius


class Ball():
    def __init__(self,x,y,v_x,v_y,r,m,color:tuple):
        self.x = x
        self.y = y
        self.velocity_x = v_x
        self.velocity_y = v_y
        self.radius = r
        self.mass = m
        self.color = color;

As you can see, right now the Simulation class controls the drawing, and the updating of every single ball. and loops through all of the balls on every tick. Note that on_update and on_draw are called by arcade not me.

The thing I'm wondering, would it be better practice to move the updating and drawing logic to the ball class? Maybe write methods for them, and the simulation only calls their methods every tick. Is this a good idea or am I just giving myself more work?