seanmtracey / Games-with-Pygame

The code for my "Making games with PyGame" series for Raspberry Pi's The MagPi.
https://smt.codes/writings
85 stars 71 forks source link

Zero distance crash #3

Open DaddyTheRunner opened 9 years ago

DaddyTheRunner commented 9 years ago

The solar system simulator (part 6) crashes if two planets happen to have zero distance between them. Here's the error message Python generates:

$ python simulator.py
Traceback (most recent call last):
  File "simulator.py", line 144, in <module>
    calculateMovement()
  File "simulator.py", line 66, in calculateMovement
    nDirection = (direction[0] / magnitude, direction[1] / magnitude) # Normalis
ed Vector pointing in the direction of the force
ZeroDivisionError: float division by zero
seanmtracey commented 9 years ago

Ah, these are always fun! I'll write some code in a little bit to check for a 0 before we try and divide anything.