For each body, loop through all other bodies to calculate the total force they exert on it. Use that force to update the body's velocity. >>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)]) >>> len(body_system_1) 2 >>> body_system_
(self, delta_time: float)
| 134 | return len(self.bodies) |
| 135 | |
| 136 | def update_system(self, delta_time: float) -> None: |
| 137 | """ |
| 138 | For each body, loop through all other bodies to calculate the total |
| 139 | force they exert on it. Use that force to update the body's velocity. |
| 140 | |
| 141 | >>> body_system_1 = BodySystem([Body(0,0,0,0), Body(10,0,0,0)]) |
| 142 | >>> len(body_system_1) |
| 143 | 2 |
| 144 | >>> body_system_1.update_system(1) |
| 145 | >>> body_system_1.bodies[0].position |
| 146 | (0.01, 0.0) |
| 147 | >>> body_system_1.bodies[0].velocity |
| 148 | (0.01, 0.0) |
| 149 | |
| 150 | >>> body_system_2 = BodySystem([Body(-10,0,0,0), Body(10,0,0,0, mass=4)], 1, 10) |
| 151 | >>> body_system_2.update_system(1) |
| 152 | >>> body_system_2.bodies[0].position |
| 153 | (-9.0, 0.0) |
| 154 | >>> body_system_2.bodies[0].velocity |
| 155 | (0.1, 0.0) |
| 156 | """ |
| 157 | for body1 in self.bodies: |
| 158 | force_x = 0.0 |
| 159 | force_y = 0.0 |
| 160 | for body2 in self.bodies: |
| 161 | if body1 != body2: |
| 162 | dif_x = body2.position_x - body1.position_x |
| 163 | dif_y = body2.position_y - body1.position_y |
| 164 | |
| 165 | # Calculation of the distance using Pythagoras's theorem |
| 166 | # Extra factor due to the softening technique |
| 167 | distance = (dif_x**2 + dif_y**2 + self.softening_factor) ** (1 / 2) |
| 168 | |
| 169 | # Newton's law of universal gravitation. |
| 170 | force_x += ( |
| 171 | self.gravitation_constant * body2.mass * dif_x / distance**3 |
| 172 | ) |
| 173 | force_y += ( |
| 174 | self.gravitation_constant * body2.mass * dif_y / distance**3 |
| 175 | ) |
| 176 | |
| 177 | # Update the body's velocity once all the force components have been added |
| 178 | body1.update_velocity(force_x, force_y, delta_time * self.time_factor) |
| 179 | |
| 180 | # Update the positions only after all the velocities have been updated |
| 181 | for body in self.bodies: |
| 182 | body.update_position(delta_time * self.time_factor) |
| 183 | |
| 184 | |
| 185 | def update_step( |
no test coverage detected