Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1819:programmcode [2019/03/04 11:26] lennox99 |
ws1819:programmcode [2019/03/05 17:32] (aktuell) mana16 [Code] |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== Code ====== | + | {{https://i.ibb.co/48VrRGH/Code.png}} |
+ | ===== Main.py: ===== | ||
<code python> | <code python> | ||
Zeile 82: | Zeile 84: | ||
| | ||
world.start(end_time) | world.start(end_time) | ||
+ | </code> | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | |||
+ | =====physics_leapfrog.py:===== | ||
+ | <code python> | ||
+ | # -*- coding: utf-8 -*- | ||
+ | from physics import PhysicsEngine | ||
+ | from itertools import combinations | ||
+ | import numpy as np | ||
+ | |||
+ | class PhysicsEngineLeap(PhysicsEngine): | ||
+ | | ||
+ | dt = None | ||
+ | |||
+ | def __init__(self, world, dt): | ||
+ | super(PhysicsEngineLeap, self).__init__(world) | ||
+ | self.dt = dt | ||
+ | |||
+ | def calc_acceleration(self, wo): | ||
+ | return wo.force / wo.mass | ||
+ | |||
+ | def step(self): | ||
+ | world_objects = self.world.world_objects.values() | ||
+ | | ||
+ | #reset force | ||
+ | for wo in world_objects: | ||
+ | wo.force = np.array([0.0,0.0,0.0]) | ||
+ | | ||
+ | pairs = combinations(world_objects, 2) | ||
+ | |||
+ | #force-loop | ||
+ | for wo1, wo2 in pairs: | ||
+ | force = self.grav_force(wo1.position, wo2.position, wo1.mass, wo2.mass) | ||
+ | wo1.force += force | ||
+ | wo2.force -= force | ||
+ | | ||
+ | #first velocity-loop | ||
+ | for wo in world_objects: | ||
+ | wo.velocity += self.calc_acceleration(wo) * self.dt / 2 | ||
+ | |||
+ | #position-loop | ||
+ | for wo in world_objects: | ||
+ | wo.position += wo.velocity * self.dt | ||
+ | |||
+ | for wo in world_objects: | ||
+ | wo.force = np.array([0.0,0.0,0.0]) | ||
+ | | ||
+ | pairs = combinations(world_objects, 2) | ||
+ | |||
+ | #force-loop | ||
+ | for wo1, wo2 in pairs: | ||
+ | force = self.grav_force(wo1.position, wo2.position, wo1.mass, wo2.mass) | ||
+ | wo1.force += force | ||
+ | wo2.force -= force | ||
+ | |||
+ | #second velocity-loop: | ||
+ | for wo in world_objects: | ||
+ | wo.velocity += self.calc_acceleration(wo) * self.dt / 2 | ||
+ | |||
+ | self.world.time += self.dt | ||
</code> | </code> |