old_gravity/mecha.py

100 lines
3.0 KiB
Python
Raw Permalink Normal View History

2020-01-28 11:00:33 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 16:32:02 2020
@author: suwako
"""
import numpy as np
2020-02-03 00:31:31 +01:00
import visual
2020-01-28 11:00:33 +01:00
class Mass():
2020-01-31 17:44:20 +01:00
def __init__(self, pos=(0, 0), speed=(0, 0), mass=1):
2020-01-28 11:00:33 +01:00
self.pos = np.array(pos, dtype='float64')
self.speed = np.array(speed, dtype='float64')
self.mass = mass
2020-02-02 01:04:17 +01:00
self.G = 0.00000000001
2020-02-03 00:31:31 +01:00
self.ticks=0
2020-01-28 11:00:33 +01:00
2020-02-01 12:29:45 +01:00
def tick(self):
self.pos += self.speed
2020-02-03 00:31:31 +01:00
self.ticks+=1
2020-02-01 12:29:45 +01:00
def ref(self,
origin=np.array((0, 0), dtype='float64'),
speed=np.array((0, 0), dtype='float64')):
2020-02-02 01:04:17 +01:00
self.pos -= origin
self.speed -= speed
2020-02-01 18:24:24 +01:00
pass
2020-02-02 01:04:17 +01:00
def apply_forces(self, elements):
self.apply_gravity(elements)
2020-01-28 11:00:33 +01:00
def apply_gravity(self, elements):
for element in elements:
d = element.pos - self.pos
norm_d = np.linalg.norm(d)
if norm_d != 0:
u = d/norm_d
norm = self.G * element.mass * (d[0]**2 + d[1]**2)
self.speed += norm*u
2020-02-03 00:31:31 +01:00
def fire(*a):
pass
class Projectile(Mass):
def __init__(self,
orientation=0,
pos=(0, 0),
speed=(0, 0),
mass=0.000000001):
Mass.__init__(self, pos=pos, speed=speed, mass=mass)
self.orientation = orientation
2020-01-28 11:00:33 +01:00
class Player(Mass):
def __init__(self, keys={},
orientation=0,
pos=(0, 0),
speed=(0, 0),
2020-02-02 01:04:17 +01:00
mass=1,
2020-02-03 00:31:31 +01:00
thrust=0.01,
fire_force=1):
2020-01-28 11:00:33 +01:00
Mass.__init__(self, pos=pos, speed=speed, mass=mass)
self.orientation = orientation
self.actions = {"thrust": False, "fire": False}
2020-02-02 01:04:17 +01:00
self.thrust=thrust
2020-02-03 00:31:31 +01:00
self.fire_force=fire_force
2020-01-31 17:44:20 +01:00
2020-02-02 01:04:17 +01:00
def action(self, **kwargs):
print(kwargs)
self.actions.update(kwargs)
def apply_thrust(self):
if self.actions['thrust']:
orientation = np.deg2rad(self.orientation + 90)
or_tuple=(np.cos(orientation), -np.sin(orientation))
norm = self.thrust/self.mass
self.speed += norm * np.array(or_tuple, dtype='float64')
def apply_forces(self, elements):
Mass.apply_forces(self, elements)
self.apply_thrust()
2020-02-03 00:31:31 +01:00
def fire(self, elements):
if self.actions['fire']:
projectile = visual.Projectile(orientation=self.orientation,
pos=self.pos,
speed=self.speed)
orientation = np.deg2rad(self.orientation + 90)
or_tuple=(np.cos(orientation), -np.sin(orientation))
pnorm = self.fire_force/projectile.mecha.mass
snorm = self.fire_force/self.mass
projectile.mecha.speed += snorm * np.array(or_tuple, dtype='float64')
self.speed -= snorm * np.array(or_tuple, dtype='float64')
elements.append(projectile)
self.actions['fire'] = False
2020-01-31 17:44:20 +01:00
2020-01-28 11:00:33 +01:00
if __name__ == "__main__":
2020-01-31 17:44:20 +01:00
pass