-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.py
More file actions
71 lines (57 loc) · 1.95 KB
/
physics.py
File metadata and controls
71 lines (57 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Physics Engine
"""
# Copyright (C) 2008 James Fargher
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
import vector
class Space(object):
"""Physics simulation space"""
def __init__(self):
self.bodies = []
def add(self, body):
"""Add body to simulation"""
self.bodies.append(body)
def remove(self, body):
"""Remove body from simulation"""
self.bodies.remove(body)
def update(self, dt):
"""Update all bodies"""
for body in self.bodies:
body.update(dt)
class Body(object):
"""A physical object"""
def __init__(self, mass, moment):
# Positional
self.mass = mass
self.position = vector.Vec2()
self.velocity = vector.Vec2()
self.force = vector.Vec2()
# Angular
self.moment = moment
self.angle = 0.0
self.angular_velocity = 0.0
self.torque = 0.0
def reset_force(self):
self.force = vector.Vec2()
def add_force(self, force):
self.force = self.force + force
def reset_torque(self):
self.torque = 0.0
def add_torque(self, torque):
self.torque = self.torque + torque
def update_positional(self, dt):
acceleration = vector.Vec2(self.force.x / self.mass,
self.force.y / self.mass)
self.velocity += acceleration.scale(dt)
self.position += self.velocity.scale(dt)
def update_angular(self, dt):
acceleration = self.torque / self.moment
self.angular_velocity += acceleration * dt
self.angle += self.angular_velocity * dt
def update(self, dt):
self.update_positional(dt)
self.update_angular(dt)