-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmotion.py
69 lines (50 loc) · 1.61 KB
/
motion.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
import numpy as np
class Motion:
def __init__(self):
pass
def cal_acc(self, ball, curve):
"""Calculate acceleration"""
raise NotImplementedError
def cal_vel(self, ball, curve):
"""Calculate velocity"""
raise NotImplementedError
def cal_total_time_vel(self, ball, curve, steps=10000):
"""Calculate total time from integral"""
raise NotImplementedError
class MotionSimple(Motion):
"""Simplest motion calculations
Conditions:
1. Frictionless
2. Conservation of mechanical energy.
3. The ball cannot detach from curve.
"""
def __init__(self, params):
super().__init__()
self.g = params.g # can be in base __init__() if dynamic g not needed
def cal_vel(self, ball, curve):
"""Calculate velocity vector from the law of conservation of mechanical energy."""
x, y = ball.x, ball.y
m = curve.cal_slope(x)
v = np.sqrt(2 * self.g * (curve.h - y))
deno = np.sqrt(1 + m * m)
vx = v / deno
vy = v * m / deno
return vx, vy
def cal_acc(self, ball, curve):
"""Calculate acceleration vector from gravitational and constraint force."""
x = ball.x
vx = ball.vx
m = curve.cal_slope(x)
g = self.g
curv = curve.cal_curvature(x)
curv_term = curv * (vx * vx)
deno = 1 + m * m
ax = -(curv_term + g) * m / deno
ay = (curv_term - g * m * m) / deno
return ax, ay
def cal_total_time_vel(self, ball, curve, steps=10000):
dx = curve.w / steps
x = np.arange(dx, curve.w + dx, dx)
y = curve.cal_height(x)
m = curve.cal_slope(x)
return np.sum(np.sqrt((1 + m ** 2) / (2 * self.g * (curve.h - y)))) * dx