- #1
- 2,168
- 193
We have a 2D box (billiard table) which have size of Width = 600 and Height = 300. We have two identical ball a radius of 20. Also the balls decelerate with a rate of 5 m/s^2. The problem is to find the position of the ball after 10 second. ( The balls may have collide)
In file the data is `235 124 365 176 181 34 -34 -14` and answer should be approximately `399 104 106 62` but I get `480 147 426 169` I guess there's a logic problem somewhere.Debug data for dt = 0.1
Python:
from math import sqrt, atan, sin, cos, pi, inf
from numpy import array
W = 600 # width of the table
H = 300 # height of the table
radius = 20 # the radius of the ball
A = 5 # deceleration constant
dt = 0.1
ma = mb = 1
def collision_test(Xa, Ya, Xb, Yb):
if sqrt((Yb - Ya) ** 2 + (Xb - Xa)**2) <= 2 * radius:
return True
def dot_product(V1, V2):
return sum(V1 * V2)
def vec_magnitude(V1):
return sqrt(V1[0]**2 + V1[1]**2)
def after_collision_velocity(Va, Vb, Ra, Rb):
Va_new = Va - ((2 * mb * dot_product(Va - Vb, Ra - Rb)) / ((ma + mb) * vec_magnitude(Ra - Rb)**2)) * (Ra - Rb)
Vb_new = Vb - ((2 * ma * dot_product(Vb - Va, Rb - Ra)) / ((ma + mb) * vec_magnitude(Rb - Ra)**2)) * (Rb - Ra)
return Va_new, Vb_new
def check_reflection(R, angle):
if R[1] < radius:
R[1] += 2 * (radius - R[1])
angle *= -1
R = array([R[0], R[1]])
return R, angle
if R[0] < radius:
R[0] += 2 * (radius - R[0])
angle = pi - angle
R = array([R[0], R[1]])
return R, angle
if R[1] > H - radius:
R[1] += 2 * (H - radius - R[1])
angle *= -1
R = array([R[0], R[1]])
return R, angle
if R[0] > W - radius:
R[0] += 2 * (W - radius - R[0])
angle = pi - angle
R = array([R[0], R[1]])
return R, angle
else:
return R, angle def motion(V, R, angle):
V_mag = vec_magnitude(V)
V_mag -= A * dt
Vx = V_mag * cos(angle)
Vy = V_mag * sin(angle)
R[0] += Vx * dt
R[1] += Vy * dt
V = array([Vx, Vy])
R = array([R[0], R[1]])
return V, R for line in open("test1.txt", "r"):
t = 0
Xa, Ya, Xb, Yb, Vxa, Vya, Vxb, Vyb = [int(i) for i in (line.rstrip()).split(" ")]
Ra = array([Xa, Ya])
Rb = array([Xb, Yb])
Va = array([Vxa, Vya])
Vb = array([Vxb, Vyb])
angle_a = atan(Vya / Vxa)
angle_b = atan(Vyb / Vxb)
while t < 10:
Va, Ra = motion(Va, Ra, angle_a)
Vb, Rb = motion(Vb, Rb, angle_b)
Ra, angle_a = check_reflection(Ra, angle_a)
Rb, angle_b = check_reflection(Rb, angle_b)
if collision_test(Xa, Ya, Xb, Yb) == True:
Va, Vb = after_collision_velocity(Va, Vb, Ra, Rb)
angle_a = atan(Va[1] / Va[0])
angle_b = atan(Vb[1] / Vb[0])
t += dt
print(Ra[0], Ra[1], Rb[0], Rb[1])
Code:
0.1: 253 127 362 175
0.2: 271 131 358 173
0.3: 289 134 355 172
0.4: 307 138 352 171
0.5: 323 139 350 171
0.6: 329 129 359 183
0.7: 335 120 368 194
0.8: 340 110 377 206
0.9: 346 101 385 218
1.0: 352 91 394 229
1.1: 358 82 403 241
1.2: 363 73 411 252
1.3: 369 63 420 263
1.4: 375 54 429 275
1.5: 380 45 437 274
1.6: 386 35 446 263
1.7: 391 26 454 251
1.8: 397 23 463 240
1.9: 402 32 471 229
2.0: 408 41 479 218
2.1: 413 50 488 207
2.2: 419 59 496 196
2.3: 424 68 505 185
2.4: 429 76 513 174
2.5: 435 85 521 163
2.6: 440 94 529 152
2.7: 445 103 537 141
2.8: 451 111 546 131
2.9: 456 120 554 120
3.0: 461 129 562 109
3.1: 466 137 570 98
3.2: 471 146 578 88
3.3: 477 154 574 77
3.4: 482 163 566 67
3.5: 487 171 558 56
3.6: 492 179 550 46
3.7: 497 188 542 35
3.8: 502 196 535 25
3.9: 507 204 527 26
4.0: 512 212 519 36
4.1: 517 220 511 46
4.2: 522 228 503 56
4.3: 526 236 496 67
4.4: 531 244 488 77
4.5: 536 252 480 87
4.6: 541 260 473 97
4.7: 546 268 465 107
4.8: 550 276 458 117
4.9: 555 276 450 127
5.0: 560 268 443 137
5.1: 564 261 435 147
5.2: 569 253 428 156
5.3: 574 245 420 166
5.4: 578 238 413 176
5.5: 577 230 406 186
5.6: 573 223 398 195
5.7: 568 215 391 205
5.8: 564 208 384 215
5.9: 559 201 377 224
6.0: 555 193 369 234
6.1: 550 186 362 243
6.2: 546 179 355 253
6.3: 542 172 348 262
6.4: 537 165 341 271
6.5: 533 158 334 279
6.6: 529 150 327 270
6.7: 524 143 320 261
6.8: 520 136 313 252
6.9: 516 130 306 243
7.0: 512 123 299 233
7.1: 508 116 292 224
7.2: 504 109 285 215
7.3: 499 102 279 206
7.4: 495 96 272 197
7.5: 491 89 265 188
7.6: 487 82 258 180
7.7: 483 76 252 171
7.8: 479 69 245 162
7.9: 475 63 238 153
8.0: 472 56 232 144
8.1: 468 50 225 136
8.2: 464 43 219 127
8.3: 460 37 212 119
8.4: 456 31 206 110
8.5: 452 25 199 101
8.6: 449 22 193 93
8.7: 445 28 187 85
8.8: 441 34 180 76
8.9: 437 40 174 68
9.0: 434 46 168 59
9.1: 430 52 161 51
9.2: 427 58 155 43
9.3: 423 64 149 35
9.4: 419 70 143 27
9.5: 416 75 137 22
9.6: 412 81 131 30
9.7: 409 87 125 38
9.8: 406 93 118 46
9.9: 402 98 112 54
10.0: 399 104 106 62
Attachments
Last edited: