-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphysics.py
74 lines (61 loc) · 2.24 KB
/
physics.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
#!/usr/bin/env python
# encoding: utf-8
"""
physics.py
CS266 Ant Sim
"""
import sys
import random, math
import numpy as np
from param import G
from error import SimulationError
np.set_printoptions(threshold=np.nan)
class Physics(object):
def checkPhysics():
"""Updates shaking of ants"""
#input: array or blocks, joint vectors
ants = 0
joints = 0
CC = {}
for y in range(G.numBlocksY):
for x in range(G.numBlocksX):
if G.state[(x,y)]:
CC[(x,y)] = ants
for J in G.jointRef[(x,y)]:
joints += 1
ants += 1
trans = np.zeros((2*ants,joints), dtype=np.double)
weight = np.zeros((2*ants), dtype=np.double)
for i in range(2*ants):
if i % 2 == 1:
weight[i] = -1.0
an = 0; jn = 0
for y in range(G.numBlocksY):
for x in range(G.numBlocksX):
if G.state[(x,y)]:
for J in G.jointRef[(x,y)]:
if J.at[1] >= 0:
trans[2*CC[J.at]][jn] += J.vector[0]
trans[2*CC[J.at]+1][jn] += J.vector[1]
if J.to[1] >= 0:
trans[2*CC[J.to]][jn] += -J.vector[0]
trans[2*CC[J.to]+1][jn] += -J.vector[1]
jn += 1
an += 1
output, res, rank, s = np.linalg.lstsq(trans, -weight)
an = 0; jn = 0
for y in range(G.numBlocksY):
for x in range(G.numBlocksX):
if G.state[(x,y)]:
for J in range(len(G.jointRef[(x,y)])):
G.jointRef[(x,y)][J].add(output[jn])
jn += 1
an += 1
checkPhysics = staticmethod(checkPhysics)
def resetPhysics():
# reset forces
G.jointData = np.zeros((G.numBlocksX * G.numBlocksY * 3))
resetPhysics = staticmethod(resetPhysics)
def linComb(c,v):
return np.apply_along_axis(sum, 0, np.transpose(c*np.transpose(v)))
linComb = staticmethod(linComb)