-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart.py
149 lines (100 loc) · 4.02 KB
/
part.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Implements airplane parts - of which there are 5, the nose (N), tail (T),
# left wing (L), right wing (R), and pilot (P) (the pilot is the center of
# the airplane).
#
# Each individual part has location, velocity, acceleration, forces acting
# on it, and mass. It's airplane's job to set the forces acting on each
# part, then the part determines its position in the next timestep.
#
# Also implements the constraint class, a simple object that specifies
# a part to constrain to and a distance.
from geometry import *
class part:
instances = []
def __init__(s, pos, vel, mass):
""" Initialize the part """
s.pos = pos # the part's location point
s.vel = vel
s.mass = mass
s.acc = vector(0.0, 0.0, 0.0) # the current acceleration
s.forces = [] # list of forces acting on the part
# Each force is a FUNCION.
s.constraints = [] # List of constraint objects. Each constrait
# is another part and a distance.
part.instances.append(s)
@classmethod
def update(cls):
""" Update the positions, velocities, etc for each part """
for c in cls.instances:
c.computeAcc()
c.updateVelocity()
c.updatePosition()
def computeAcc(s):
""" Compute the acceleration of the plane at this timestep
based on the forces acting on it. Forces are assumed to be
in lbf. """
massinverse = 1/s.mass
fv = vector(0.0, 0.0, 0.0) # Force vector
for f in s.forces:
fv += f().scale(massinverse)
for c in s.constraints:
fv += c.force()
s.acc = fv
def updateVelocity(s):
""" Compute the velocity vector of the plane, based
on acceleration this frame. Uses RK4"""
k1 = s.acc.scale(1/60)
yn = s.vel
# Now, update velocity with k1, and recompute acceleration
s.vel += k1.scale(1/2) # This velocity will be discarded
s.computeAcc()
k2 = s.acc.scale(1/60)
s.vel = yn + k2.scale(1/2)
s.computeAcc()
k3 = s.acc.scale(1/60)
# Finally, get k4:
s.vel = yn + k3
s.computeAcc()
k4 = s.acc.scale(1/60)
# Compute final acceleration value
s.vel = yn + k1.scale(1/6) + k2.scale(1/3) \
+ k3.scale(1/3) + k4.scale(1/6)
def updatePosition(s):
""" Compute the updated position of the plane, based on velocity
this frame. Uses RK4 """
k1 = s.vel.scale(1/60)
yn = s.pos
s.pos += k1.scale(1/2)
s.updateVelocity()
k2 = s.vel.scale(1/60)
s.pos = yn + k2.scale(1/2)
s.updateVelocity()
k3 = s.vel.scale(1/60)
s.pos = yn + k3
s.updateVelocity()
k4 = s.vel.scale(1/60)
s.pos = yn + k1.scale(1/6) + k2.scale(1/3) \
+ k3.scale(1/3) + k4.scale(1/6)
def minus(s, other):
""" Return the vector result of substracting two part positions. """
return s.pos.minus(other.pos)
__sub__ = minus
class constraint:
# Constraints are ridgid connections between two parts, of some length.
# Constraints are one-directional (i.e. you should have one going
# in each direction).
# A constrait is modeled as a very stiff spring between objects.
def __init__(s, this, other, l):
""" Create a constrait between part 'this' and another
part 'other', forcing them to maintaing distance l. """
s.other = other
s.this = this
s.l = l
def force(s):
""" Generates the force of this constraint """
v = s.other.pos - s.this.pos
lv = v.norm() # actual distance between the objects
diff = lv - s.l
return v.unit().scale(diff/5) # this is the force to apply
# to "this". It's actually not very large (because constraints don't
# take mass into account)