-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.py
38 lines (33 loc) · 1.14 KB
/
physics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- coding: utf-8 *-*
import numpy as np
from world import Planet, Projectile
def updateProjectileMomentum(planets, projectile, dt):
f = np.array([0.0, 0.0])
old = np.array(projectile.velocity)
for planet in planets:
dr = np.array(projectile.pos) - np.array(planet.pos)
f -= planet.mass * dr / np.power(np.linalg.norm(dr), 3)
new = old + f * dt
projectile.velocity = (new[0], new[1])
return projectile
def letProjectilesInteract(projectiles, dt):
for p1 in projectiles[:]:
f = np.array([0.0, 0.0])
old = np.array(p1.velocity)
for p2 in projectiles[:]:
if p1.id is p2.id: continue
dr = np.array(p1.pos) - np.array(p2.pos)
f -= p2.mass * dr / np.power(np.linalg.norm(dr), 3)
new = old + f * dt
projectiles[projectiles.index(p1)].velocity = (new[0], new[1])
return projectiles
def moveProjectile(projectile, dt):
old = np.array(projectile.pos)
v = np.array(projectile.velocity)
new = old + v * dt
projectile.pos = (new[0], new[1])
return projectile
def doesProjectilePenetratePlanet(projectile, planet):
dr = np.linalg.norm(np.array(projectile.pos) - np.array(planet.pos))
return (dr < planet.radius)