Created
February 26, 2015 22:55
-
-
Save CapacitorSet/6ce8f5d9d9842ac42b44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
G = 1 | |
class Vector: | |
dX = 0 | |
dY = 0 | |
dZ = 0 | |
def build(self, x1, y1, z1, x2, y2, z2): | |
self.dX = math.fabs(x1 - x2) | |
self.dY = math.fabs(y1 - y2) | |
self.dZ = math.fabs(z1 - z2) | |
def length(self): | |
return math.sqrt(self.dX ** 2 + self.dY ** 2 + self.dZ ** 2) | |
# Sine over the X axis | |
def xComponent(self): | |
return self.dX/self.length() | |
# Sine over the Y axis | |
def yComponent(self): | |
return self.dY/self.length() | |
# Sine over the Z axis | |
def zComponent(self): | |
return self.dZ/self.length() | |
class Asteroid: | |
def __init__(self, mass, x, y, z, xVelocity, yVelocity, zVelocity): | |
self.mass = mass | |
self.x = x | |
self.y = y | |
self.z = z | |
self.xVelocity = xVelocity | |
self.yVelocity = yVelocity | |
self.zVelocity = zVelocity | |
def distanceTo(self, destination): | |
distance = 0 | |
distance += (self.x - destination.x) ** 2 | |
distance += (self.y - destination.y) ** 2 | |
distance += (self.z - destination.z) ** 2 | |
return math.sqrt(distance) | |
def acceleration(self, destination): | |
route = Vector() | |
route.build(self.x, self.y, self.z, destination.x, destination.y, destination.z) | |
result = Vector() | |
result.dX = route.xComponent() * G * destination.mass/(self.distanceTo(destination)**2) | |
result.dY = route.yComponent() * G * destination.mass/(self.distanceTo(destination)**2) | |
result.dZ = route.zComponent() * G * destination.mass/(self.distanceTo(destination)**2) | |
return result | |
def totalAcceleration(self, system): | |
result = Vector() | |
for asteroid in system: | |
if asteroid.mass != self.mass: # Naive check. Is asteroid != self possible? | |
result.dX += self.acceleration(asteroid).dX | |
result.dY += self.acceleration(asteroid).dY | |
result.dZ += self.acceleration(asteroid).dZ | |
return result | |
# v(t+dt) = oldVelocity + dt * totalAcceleration | |
def updateVelocity(self, system, dt): | |
self.xVelocity += dt * self.totalAcceleration(system).dX | |
self.yVelocity += dt * self.totalAcceleration(system).dY | |
self.zVelocity += dt * self.totalAcceleration(system).dZ | |
# x(t+dt) = oldX + dt * xSpeed | |
# Must be called after updateVelocity | |
def updatePosition(self, dt): | |
self.x += dt * self.xVelocity | |
self.y += dt * self.yVelocity | |
self.z += dt * self.zVelocity | |
massa1 = Asteroid(10, 0, 0, 0, 18, 0, 0) | |
massa2 = Asteroid(20, 2, 3, 5, 0, 0, 0) | |
system = [massa1, massa2] | |
dt = 0.1 | |
massa1.updateVelocity(system, dt) | |
massa2.updateVelocity(system, dt) | |
massa1.updatePosition(dt) | |
massa2.updatePosition(dt) | |
print massa1.x, massa1.y, massa1.z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment