コード例 #1
0
ファイル: satellite.py プロジェクト: lol-cubes/physics
def run_satellite_simulation(duration=100):
    import time

    from physics.utils import atan, cos, distance, sin
    from physics.vectors import Vector
    from physics.bodies import Body
    from physics.canvas import canvas, root

    earth = Body(
        canvas,
        canvas.create_oval(350, 225, 600, 475, fill="red"),
        475,
        350,  # center of gravity
        velocity=Vector(x=0, y=0),
        mass=100000  # arbitrary number... no units
    )

    satellite = Body(canvas,
                     canvas.create_oval(300, 312.5, 325, 337.5, fill="red"),
                     312.5,
                     325,
                     velocity=Vector(x=0, y=0),
                     mass=100)

    for frame in range(int(duration / 0.01)):

        # if frame == 0:
        #     satellite.push(Vector(r=1000, theta=180), 0.01)

        # distance between statellite and earth (centers of gravity)
        d = distance(satellite.coords, earth.coords)
        # angle between x-axis and vector of satellite to earth (centers of gravity)
        theta = atan((satellite.y - earth.y) / (satellite.x - earth.x))
        f = ((satellite.mass * earth.mass) / d)
        gravitational_force = Vector(r=f, theta=theta)

        support_force = Vector(r=0, theta=0)
        if d <= 12.5 + 125:
            # circles are intersecting (distance between centers is greater than or equal to sum of radii)

            # support force of earth's surface
            # has opposite direction, but same magnitude as earth's gravity
            support_force = gravitational_force * -1
        Body.push(satellite, gravitational_force + support_force, 0.01)

        root.update()
        time.sleep(0.01)


# TODO:
# calculate required support force to stop satellite.
コード例 #2
0
 def __rmul__(self, multiplicand):
   # Introducing... dot product!
   result = Vector.__rmul__(self, multiplicand)
   if isinstance(result, Vector):
     return Vertex(*result.components)
   else:
     return result
コード例 #3
0
 def __init__(self, l1, l2, m1, m2, bob_r1, bob_r2, initial_state, history_size = 100):
     self.m1 = m1
     self.m2 = m2
     self.simulation = RungeKuttaSimulation(self.motion, initial_state, history_size = history_size)
     self.state = initial_state
     
     self.bob_size1 = bob_r1
     self.bob_size2 = bob_r2
     self.l1 = l1
     self.l2 = l2
     
     #These are in POLAR coordinates [r, theta]
     self.bob_pos_rest1 = Vector(l1, -HALF_PI)
     self.bob_pos_rest2 = Vector(l2, -HALF_PI)
     
     self.history_origin1 = self.bob_pos_rest1
     self.history_origin2 = self.bob_pos_rest2
コード例 #4
0
ファイル: baller.py プロジェクト: misspellted/enlightened
 def __init__(self, timer, length, height):
     PyGameScene.__init__(self, timer, length, height)
     Gravitational.__init__(
         self, 10, Vertex(DEMO_WINDOW_LENGTH / 2, DEMO_WINDOW_HEIGHT << 4))
     self.ball = Ball(
         32, Vertex(DEMO_WINDOW_LENGTH / 3, DEMO_WINDOW_HEIGHT / 3),
         Vector(BALL_VELOCITY, BALL_VELOCITY))
     self.wind = Wind(0.125)
     self.addEntity(self.ball)
コード例 #5
0
ファイル: baller.py プロジェクト: misspellted/enlightened
    def influence(self, entity):
        # force = mass * acceleration
        # acceleration = force / mass
        acceleration = Vector(
            self.speed / entity.mass, 0
        )  # Wind blows from the left to the right, imparting positive velocity.
        # TODO: Introduce a directional component?

        entity.velocity += acceleration
コード例 #6
0
 def draw(self, origin, scale, colors):
     bob_theta1, _, bob_theta2, _ = self.state
     bob_delta_polar1 = Vector(0, bob_theta1)
     bob_delta_polar2 = Vector(0, bob_theta2)
     
     pushMatrix()
     translate(*origin)
     stroke(colors[0])
     x1, y1 = scale * polar2rect(bob_delta_polar1 + self.bob_pos_rest1, flip_y = True)
     line(0, 0, x1, y1)
     circle(x1, y1, self.bob_size1)
     translate(x1, y1)
     
     stroke(colors[1])
     x2, y2 = scale * polar2rect(bob_delta_polar2 + self.bob_pos_rest2, flip_y = True)
     line(0, 0, x2, y2)
     circle(x2, y2, self.bob_size2)
     popMatrix()
コード例 #7
0
    def __init__(self, k, m, l, w, initial_state):
        self.k = k
        self.m = m
        self.simulation = RungeKuttaSimulation(self.motion,
                                               initial_state,
                                               history_size=100)
        self.state = initial_state

        self.wall_pos = Vector(-w / 4.0, -w * 3.0 / 2.0)
        self.wall_dims = Vector(w / 4.0, w * 2.0)
        self.floor_pos = Vector(0, w / 2.0)
        self.floor_dims = Vector(l * 2.0, w / 4.0)
        self.spring_pos = Vector(0, -w / 2.0)
        self.spring_dims_rest = Vector(l, w)
        self.bob_pos_rest = Vector(l, -w / 2.0)
        self.bob_dims = Vector(w, w)
        self.history_origin = Vector(l + w / 2.0, 0)
コード例 #8
0
 def motion(self, state):
     theta1, theta_dot1, theta2, theta_dot2 = state
     
     #Localize these for convenience
     m1 = self.m1
     m2 = self.m2
     l1 = self.l1
     l2 = self.l2
     
     #convenience substitutions
     M = m1 + m2
     m2_half = m2 / 2.0
     
     #Do trig stuff once
     a = theta1 - theta2
     cos_a = cos(a)
     sin_a = sin(a)
     cos_sq_a = cos_a * cos_a
     b = theta1 - 2.0 * theta2
     sin_b = sin(b)
     th1_dot_sq = theta_dot1 * theta_dot1
     th2_dot_sq = theta_dot2 * theta_dot2
     sin_th1 = sin(theta1)
     sin_th2 = sin(theta2)
     
     #denominators for the two equations are very similar
     denom = (M - m2 * cos_sq_a)
     denom1 = -denom * l1
     denom2 = denom * l2
     
     #Numerator for angular accel 1:
     term1 = l1 * m2 * th1_dot_sq * sin_a * cos_a
     term2 = th2_dot_sq * l2 * m2 * sin_a
     term3 = g * m1 * sin_th1
     term4 = g * m2_half * sin_th1
     term5 = g * m2_half * sin_b
     num1 = term1 + term2 + term3 + term4 + term5
     
     #Numerator for angular accel 2:
     term1 = -th1_dot_sq * l1 * sin_a
     term2 = g * sin_th2
     half_one = - M * (term1 + term2)
     term1 = th2_dot_sq * l2 * m2 * sin_a
     term2 = g * M * sin_th1
     half_two = (term1 + term2) * cos_a
     num2 = half_one + half_two
     
     #Finally, we have our angular acceleration values!
     #Good luck debugging this if the formula is wrong...
     alpha1 = num1 / denom1
     alpha2 = num2 / denom2 
     
     return Vector(theta_dot1, alpha1, theta_dot2, alpha2)
コード例 #9
0
ファイル: projectile.py プロジェクト: lol-cubes/physics
def run_projectile_simulation(angle=45, initial_speed=500, duration=2):
    """
    initial_speed * sin(angle) must be greater than 1, or else the ball can't get off the ground
    """
    import time

    from physics.vectors import Vector
    from physics.bodies import Body
    from physics.canvas import canvas, root

    canvas.create_line(0, 521, 1000, 521)

    r = int(initial_speed)
    theta = int(angle)
    t = int(duration)
    ball = Body(canvas,
                canvas.create_oval(20, 500, 40, 520, fill="red"),
                20,
                500,
                velocity=Vector(x=Vector.get_horizontal_component(r, theta),
                                y=Vector.get_vertical_component(r, theta)))
    root.update()
    time.sleep(0.5)

    g = Vector(x=0, y=-9.8)

    fallen = False
    for frame in range(int(t / 0.01)):
        if ball.y >= 500 and frame != 0:
            if fallen == False:
                canvas.move(ball.id, 0, 500 - ball.y)
                fallen = True
            ball.velocity = Vector(x=0, y=0)
        else:
            ball.velocity += g

        ball.move(0.01)
        root.update()
        time.sleep(0.01)
コード例 #10
0
 def __init__(self,
              canvas,
              identifier,
              x,
              y,
              velocity=Vector(x=0, y=0),
              mass=1):
     self.x = x
     self.y = y
     self.canvas = canvas
     self.id = identifier
     self.velocity = velocity
     self.mass = mass
コード例 #11
0
    def draw(self, origin, scale, colors):
        bob_theta, _ = self.state
        bob_delta_polar = Vector(0, bob_theta)

        pushMatrix()
        translate(*origin)
        stroke(colors[0])
        x, y = scale * polar2rect(bob_delta_polar + self.bob_pos_rest,
                                  flip_y=True)

        line(0, 0, x, y)
        circle(x, y, self.bob_size)
        popMatrix()
コード例 #12
0
    def __init__(self, l, m, bob_r, initial_state, history_size=100):
        self.m = m
        self.simulation = RungeKuttaSimulation(self.motion,
                                               initial_state,
                                               history_size=history_size)
        self.state = initial_state

        self.bob_size = bob_r
        self.l = l

        #These are in POLAR coordinates [r, theta]
        self.bob_pos_rest = Vector(l, -HALF_PI)

        self.history_origin = self.bob_pos_rest
コード例 #13
0
ファイル: testing.py プロジェクト: misspellted/enlightened
from camera.mount.circular import CircularMount

cameraMount = CircularMount(circumscribed.radius)

print(
    f"Circumscribed cameraSensor radius: [{circumscribed.radius}]: cameraMount diameter: {cameraMount.diameter}"
)

from utilities import millisecondsPerFrame

print(f"msPf: {millisecondsPerFrame(60)}")

from physics.vectors import Vector

linear = Vector(3)
print(f"Magnitude of {linear}: {linear.magnitude()}")
uLinear = linear.unit()
print(f"Unit vector of {linear}: {uLinear}")
print(f"Magnitude of unit vector of {linear}: {uLinear.magnitude()}")

planar = Vector(3, 4)
print(f"Magnitude of {planar}: {planar.magnitude()}")
uPlanar = planar.unit()
print(f"Unit vector of {planar}: {uPlanar}")
print(f"Magnitude of unit vector of {planar}: {uPlanar.magnitude()}")

volumetric = Vector(3, 4, 5)
print(f"Magnitude of {volumetric}: {volumetric.magnitude()}")
uVolumetric = volumetric.unit()
print(f"Unit vector of {volumetric}: {uVolumetric}")
コード例 #14
0
 def motion(self, state):
     x, v = state
     return Vector(v, -self.k / self.m * x)
コード例 #15
0
 def __idiv__(self, divisor):
   Vector.__idiv__(self, divisor)
   return self
コード例 #16
0
 def motion(self, state):
     theta, theta_dot = state
     return Vector(theta_dot, -g / self.l * sin(theta))
コード例 #17
0
 def __floordiv__(self, divisor):
   return Vertex(*Vector.__floordiv__(self, divisor).components)
コード例 #18
0
 def __sub__(self, subtrahend):
   return Vertex(*Vector.__sub__(self, subtrahend).components)
コード例 #19
0
 def __radd__(self, augend):
   return Vertex(*Vector.__radd__(self, augend).components)
コード例 #20
0
 def __imul__(self, multiplier):
   Vector.__imul__(self, multiplier)
   return self
コード例 #21
0
 def __init__(self, x, y):
   Vector.__init__(self, x, y)
コード例 #22
0
 def motion(self, state):
     x1, v1, x2, v2 = state
     a1 = (self.k2 * x2 - self.k1 * x1 - self.k2 * x1) / self.m1
     a2 = (self.k2 * x1 - self.k2 * x2) / self.m2
     return Vector(v1, a1, v2, a2)
コード例 #23
0
 def __rsub__(self, minuend):
   return Vertex(*Vector.__rsub__(self, minuend).components)
コード例 #24
0
 def __init__(self, k1, k2, m1, m2, l1, l2, w1, w2, initial_state, history_size = 100):
     self.k1 = k1
     self.k2 = k2
     self.m1 = m1
     self.m2 = m2
     self.simulation = RungeKuttaSimulation(self.motion, initial_state, history_size = history_size)
     self.state = initial_state
     
     w = max(w1, w2)
     l = l1 + l2
     
     self.wall_pos= Vector(-w / 4.0, -w * 3.0 / 2.0)
     self.wall_dims = Vector(w / 4.0, w * 2.0)
     self.floor_pos = Vector(0, w / 2.0)
     self.floor_dims = Vector(l * 2.0, w / 4.0)
     
     self.spring1_pos = Vector(0, -w1 / 2.0)
     self.spring1_dims_rest = Vector(l1, w1)
     self.spring2_pos_rest = Vector(l1 + w1, -w2 / 2.0)
     self.spring2_dims_rest = Vector(l2, w2) 
     
     self.bob1_pos_rest = Vector(l1, -w1 / 2.0)
     self.bob1_dims = Vector(w1, w1)
     self.bob2_pos_rest = Vector(l1 + w1 + l2, -w2 / 2.0)
     self.bob2_dims = Vector(w2, w2)
     
     self.history_origin1 = Vector(l1 + w / 2.0, 0)
     self.history_origin2 = Vector(l1 + w1 + l2 + w2 / 2.0, 0)
コード例 #25
0
 def __rdiv__(self, dividend):
   return Vertex(*Vector.__rdiv__(self, dividend).components)
コード例 #26
0
 def __isub__(self, subtrahend):
   # TODO: Make this return self instead.
   return Vertex(*Vector.__isub__(self, subtrahend).components)
コード例 #27
0
 def __iadd__(self, addend):
   # TODO: Make this return self instead.
   return Vertex(*Vector.__iadd__(self, addend).components)