This repository has been archived by the owner on May 12, 2021. It is now read-only.
forked from acoope/BouncingBall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReboundingBallModel.py
94 lines (72 loc) · 2.01 KB
/
ReboundingBallModel.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
#!/usr/bin/env python
import math
import ReboundingBallView
objects = {}
left = 0.0
right = 0.0
bottom = 0.0
top = 0.0
pause = False
def addObjects(objName, position, time, velocity):
obj = {'position':position,'time':time,'velocity':velocity}
objects[objName] = obj
def setScreenBoundries(l,r,b,t):
global left,right,bottom,top
left = l
right = r
bottom = b+0.02
top = t
def setPause(p):
global pause
pause = p
#print pause
def changeVelocity(obj, v, dir):
objects[obj]['velocity'][dir] = v
def updateObject(obj,t):
# set new time
objects[obj]['time'] = t
v = objects[obj]['velocity']
# print v
#current position of ball
x = objects[obj]['position'][0]
y = objects[obj]['position'][1]
z = objects[obj]['position'][2]
#calculate new position based on velocity
if not pause:
x = x + v[0]
y = y + v[1]
z = z + v[2]
#parameters to govern the motion's simulation
gravity=0.02
frictionfactor=20
absorbreboundy=0.18
absorbreboundx=0.08
#print v[0]
#print y
#print "bottom="+`bottom`
#if direction=='up':
#decrease velocity by gravity in each loop.
v[1]=v[1]-gravity
#print v[1]
if x > right: #check for right wall
x = right
v[0] = -v[0]+absorbreboundx
elif x < left:#check for left wall
x = left
v[0] = -v[0]-absorbreboundx
if y > top:
y = top
v[1] = -v[1]
# direction='down'
elif y < bottom+0.02: #bottom wall
y = bottom+0.02
v[1] = -v[1]-absorbreboundy #rebound absorption with each bounce
if v[0]!=0:
v[0]=v[0]-v[0]/frictionfactor-(v[0]/frictionfactor)**2 #friction when rolling on the floor
else:
v[0]=0
#print v[0]
objects[obj]['position'] = [x,y,z]
#print objects[obj]['position']
return objects[obj]['position']
#