annotate grease/controller/integrator.py @ 11:fa8bb78832e6

Added basic components
author KarstenBock@gmx.net
date Mon, 25 Jul 2011 13:39:01 +0200
parents bc88f7d5ca8b
children
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 __version__ = '$Id$'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17 class EulerMovement(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 """System that applies entity movement to position using Euler's method
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 :param position_component: Name of :class:`grease.component.Position`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 component to update.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 :param movement_component: Name of :class:`grease.component.Movement`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 component used to update position.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 def __init__(self, position_component='position', movement_component='movement'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 self.position_component = position_component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 self.movement_component = movement_component
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 def set_world(self, world):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 """Bind the system to a world"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 self.world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 def step(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 """Apply movement to position"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 assert self.world is not None, "Cannot run with no world set"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 for position, movement in self.world.components.join(
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 self.position_component, self.movement_component):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 movement.velocity += movement.accel * dt
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 position.position += movement.velocity * dt
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 position.angle += movement.rotation * dt
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42