Support for thrust
This commit is contained in:
parent
cd5300030b
commit
a2e76bde51
31
mecha.py
31
mecha.py
@ -14,7 +14,7 @@ class Mass():
|
||||
self.pos = np.array(pos, dtype='float64')
|
||||
self.speed = np.array(speed, dtype='float64')
|
||||
self.mass = mass
|
||||
self.G = 0.000001
|
||||
self.G = 0.00000000001
|
||||
|
||||
def tick(self):
|
||||
self.pos += self.speed
|
||||
@ -22,9 +22,12 @@ class Mass():
|
||||
def ref(self,
|
||||
origin=np.array((0, 0), dtype='float64'),
|
||||
speed=np.array((0, 0), dtype='float64')):
|
||||
#self.pos -= origin
|
||||
#self.speed -= speed
|
||||
self.pos -= origin
|
||||
self.speed -= speed
|
||||
pass
|
||||
def apply_forces(self, elements):
|
||||
self.apply_gravity(elements)
|
||||
|
||||
def apply_gravity(self, elements):
|
||||
for element in elements:
|
||||
d = element.pos - self.pos
|
||||
@ -40,15 +43,27 @@ class Player(Mass):
|
||||
orientation=0,
|
||||
pos=(0, 0),
|
||||
speed=(0, 0),
|
||||
mass=1):
|
||||
mass=1,
|
||||
thrust=0.01):
|
||||
Mass.__init__(self, pos=pos, speed=speed, mass=mass)
|
||||
self.orientation = orientation
|
||||
self.actions = {"thrust": False, "fire": False}
|
||||
self.actions.update(keys)
|
||||
|
||||
def action(self, keys):
|
||||
self.actions.update(keys)
|
||||
self.thrust=thrust
|
||||
|
||||
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()
|
||||
|
||||
if __name__ == "__main__":
|
||||
pass
|
||||
|
24
visual.py
24
visual.py
@ -65,13 +65,13 @@ if __name__ == "__main__":
|
||||
pygame.init()
|
||||
black = (0, 0, 0)
|
||||
size = width, height = 1366, 720
|
||||
TICK_RATE = 30
|
||||
TICK_RATE = 60
|
||||
keys = set()
|
||||
tick = (time.time(), time.time(), 0)
|
||||
screen = pygame.display.set_mode(size,
|
||||
flags=pygame.RESIZABLE)
|
||||
elements = [Player(speed=(5, 0), pos=(10, -200)), Player(speed=(-5, 0), pos=(10, 200), mass = 1), Planet(mass=200)]
|
||||
reference = 1
|
||||
elements = [Player(speed=(5, 0), pos=(10, -200)), Player(speed=(-5, 0), pos=(10, 200), mass = 1), Planet(mass=200), Planet(mass=200, pos=(0,20), speed=(-1,0)), Planet(mass=150, pos = (0, 1000), speed = (15, 0)), Player(mass=5, pos = (0, 1010), speed = (15, 1))]
|
||||
reference = 2
|
||||
joy=[1, 1]
|
||||
while 1:
|
||||
dtick = tick
|
||||
@ -91,24 +91,32 @@ if __name__ == "__main__":
|
||||
if event.type == pygame.KEYDOWN:
|
||||
keys.update({event.key})
|
||||
print(event.key)
|
||||
# if event.key == 102:
|
||||
# follow = (follow + 1)%len(elements)
|
||||
if event.key == 102:
|
||||
reference = (reference + 1)%len(elements)
|
||||
if event.key == (32):
|
||||
elements[1].mecha.action(thrust=True)
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key in keys:
|
||||
keys.remove(event.key)
|
||||
if event.key == (32):
|
||||
elements[1].mecha.action(thrust=False)
|
||||
screen.fill(black)
|
||||
for key in keys:
|
||||
if key in [275,276,273,274]:
|
||||
exec("joy" + ("[0]+","[0]-","[1]+","[1]-")[[275,276,273,274].index(key)] + "=1")
|
||||
elements[1].mecha.orientation=joy[0]*(-3)
|
||||
for element in elements:
|
||||
element.mecha.apply_gravity([element.mecha for element in elements])
|
||||
element.mecha.apply_forces([element.mecha for element in elements])
|
||||
for element in elements:
|
||||
element.mecha.tick()
|
||||
ori,speed=origin=elements[0].mecha.pos, elements[0].mecha.speed
|
||||
ori,speed=origin=np.array(elements[0].mecha.pos), np.array(elements[0].mecha.speed)
|
||||
for element in elements:
|
||||
element.mecha.ref(origin=ori, speed=speed)
|
||||
if i < 2:
|
||||
while 10**(joy[1]*0.05) > max(size)/60:
|
||||
joy[1] -= 1
|
||||
while 10**(joy[1]*0.05) < 0.08 :
|
||||
joy[1] += 1
|
||||
for element in elements:
|
||||
element.draw(scale=10**(joy[1]*0.05), reference=elements[reference].mecha.pos, size=screen.get_size())
|
||||
pygame.display.flip()
|
||||
@ -116,4 +124,4 @@ if __name__ == "__main__":
|
||||
# element.apply(camera=camera, origin=origin)
|
||||
# screen.blit(element.image, element.position)
|
||||
time.sleep(0.001)
|
||||
if tick[2]%60 == 0 : print(elements[2].mecha.pos, elements[2].mecha.speed)
|
||||
#if tick[2]%60 == 0 : print(elements[2].mecha.pos, elements[2].mecha.speed)
|
Loading…
Reference in New Issue
Block a user