# What is the Position of Two Colliding Balls in a 2D Box after 10 Seconds?

• Python
• Arman777
In summary, the problem is to find the position of two identical balls on a 2D box (billiard table) after 10 seconds, taking into account their deceleration rate of 5 m/s^2. The balls may collide during this time.
Colliding_Ball:
from math import sqrt, atan2, sin, cos, pi, inf
from numpy import array

W = 600  # width of the table
H = 300  # height of the table
R = 10  # the radius of the ball
A = 0  # deceleration constant
dt = 10 ** -3
ma = mb = 1  # masses of the particles a and bdef vec_magnitude(V1):
return sqrt(V1[0]**2 + V1[1]**2)def collision_test(V1, V2):
if vec_magnitude(V1 - V2) <= 2 * R:
return Truedef dot_product(V1, V2):
return sum(V1 * V2)def after_collision_velocity(Va, Vb, Ra, Rb):
''' the equation that produces the velocity of the objects after the collision'''
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_newdef check_reflection(P, V_mag, angle, V):
if P[1] < R:
P += array([0, 2 * (R - P[1])])
angle *= -1
return P, V_mag, angle, V
if P[0] < R:
P += array([2 * (R - P[0]), 0])
angle = pi - angle
return P, V_mag, angle, V
if P[1] > H - R:
P += array([0, 2 * (H - R - P[1])])
angle *= -1
return P, V_mag, angle, V
if P[0] > W - R:
P += array([2 * (W - R - P[0]), 0])
angle = pi - angle
return P, V_mag, angle, V
else:
V_mag -= A * dt
Vx = V_mag * cos(angle)
Vy = V_mag * sin(angle)
P += array([Vx * dt, 0])
P += array([0, Vy * dt])
V = array([Vx, Vy])
return P, V_mag, angle, Vfile = open("test_drawing.txt", "w")
for line in open("tex.txt", "r"):
t = 0
Xa, Ya, Xb, Yb, Vxa, Vya, Vxb, Vyb = [
int(i) for i in (line.rstrip()).split(" ")]
Pa = array([Xa, Ya], dtype=float)
Pb = array([Xb, Yb], dtype=float)
Va = array([Vxa, Vya], dtype=float)
Vb = array([Vxb, Vyb], dtype=float)
Va_mag = vec_magnitude(Va)
Vb_mag = vec_magnitude(Vb)
if Vxa == 0:
Vxa = inf
angle_a = atan2(Vya, Vxa)
if Vxb == 0:
Vxb = inf
angle_b = atan2(Vyb, Vxb)
while t <= 10:
Pa, Va_mag, angle_a, Va = check_reflection(Pa, Va_mag, angle_a, Va)
Pb, Vb_mag, angle_b, Vb = check_reflection(Pb, Vb_mag, angle_b, Vb)
if collision_test(Pa, Pb) == True:
Va, Vb = after_collision_velocity(Va, Vb, Pa, Pb)
Va_mag = vec_magnitude(Va)
Vb_mag = vec_magnitude(Vb)
if Va[0] == 0:
Va[0] = inf
angla_a = atan2(Va[1], Va[0])
if Vb[0] == 0:
Vb[0] = inf
angle_b = atan2(Vb[1], Vb[0])
t += dt
file.write(str(Pa[0]) + " " + str(Pa[1]) + " " + str(Pb[0]) + " " + str(Pb[1]) + "\n")
print(Pa[0], Pa[1], Pb[0], Pb[1])
file.close()

data for a simple collision program:
100 200 140 200 4 4 -4 4

Drawing Program:
from pylab import plot, show

Xa = [100]
Ya = [200]
Xb = [140]
Yb = [200]

for line in open("test_drawing.txt", "r"):
data = [float(i) for i in line.split(" ")]
Xa.append(data[0])
Ya.append(data[1])
Xb.append(data[2])
Yb.append(data[3])

plot(Xa, Ya, "-r")
plot(Xb, Yb, "-.g")
show()

Now when you run this you ll get a data of points for each coordinate (Pa, Pb).

The image is like this

Its clear that ball b reflects but ball a does not. Thats really strange... I mean ball b acts like normal but not the a..

Who can spot the error , cause I cannot.

P is the position vector so Pa and Pb are the position vectors of the ball a and ball b respectively. V is the velocity.

I wrote angla_a instead of angle_a ... that was the reason.. So simple ..
FactChecker said:
It's my experience that it is possible to stare at code for a long, long time without spotting a simple error
The case has been proved :)

Last edited:
Klystron, jedishrfu and FactChecker
Each of these kinds of mistakes that you've made and fixed gives you a personalized list of things to check in your code.

Interpretive languages often fall down in this respect where any variable name can be defined incorrectly and it will use it.

I once tried to teach a coworker on how to write programs. My first assignment to him was to enter some code I wrote on paper. I told him to see if he could get it working. He successfully got it to compile but it was most certainly wrong. Due to spelling mistakes in variable names, partly my fault and partly his typing, the compiler would indicate variable xyyzy was undefined and so he defined it not realizing that it was already defined as xyzzy. However, he didn't understand the flow of the program and so added a new variable instead of correcting the spelling mistake.

I know in python if you call a variable before it's defined you will get a warning. However, I've seen cases in my own code where I accidentally reused the wrong indexing variable in a loop and nothing was said. It's only through knowing what you wrote and why you wrote it that the error is spotted quickly.

Lastly, try not to get bogged down in the accuracy of your numbers early on. I had a boss once who was always suspicious of the COBOL compiler math that he would insist on looking at the machine code it generated. Later, I found this was due to some hardware issues with the extended math processor that computed using packed decimal format which COBOL routinely used for some datatypes. However, he never found anything wrong.

Klystron, Arman777 and FactChecker
jedishrfu said:
I know in python if you call a variable before it's defined you will get a warning.
This is a good reason to take a very hard look at anything that gives a warning. Otherwise, typos are very hard to spot.

Klystron, Arman777 and jedishrfu
FactChecker said:
Otherwise, typos are very hard to spot.
Indeed they are...I guess I should have noticed it before.
jedishrfu said:
Lastly, try not to get bogged down in the accuracy of your numbers early on.
I agree..Well I run the code and It worked yey!

So the problem is solved. Thank you all for your help

Klystron
Arman777 said:
Indeed they are...I guess I should have noticed it before.
My advice for the future is to get in the habit of following the coding standards that an editor like PyCharm uses. That will minimize the PyCharm warnings due to cosmetic things and it will be easier to spot the remaining serious errors. I brought your code into PyCharm (free version) and it did flag the angla_a typo, but it was among so many other warnings that it was easy to overlook. I am not familiar with PyCharm, but a good editor will allow you to control the types of things that generate a warning. I didn't see a way to control that in the PyCharm free version.

EDIT: The free version of PyCharm has an option under Code => Inspect Code that immediately spotted the 'angla' typo as a serious problem.

Arman777
FactChecker said:
My advice for the future is to get in the habit of following the coding standards that an editor like PyCharm uses. That will minimize the PyCharm warnings due to cosmetic things and it will be easier to spot the remaining serious errors. I brought your code into PyCharm (free version) and it did flag the angla_a typo, but it was among so many other warnings that it was easy to overlook. I am not familiar with PyCharm, but a good editor will allow you to control the types of things that generate a warning. I didn't see a way to control that in the PyCharm free version.

EDIT: The free version of PyCharm has an option under Code => Inspect Code that immediately spotted the 'angla' typo as a serious problem.
When I started coding a year ago I started with python IDE then I switched to PyCharm. PyCharm was great for me back then but it seemed slow to me. Then this year I updated the Pycharm and it was really slow( I guess there was some kind of a bug) then, I discovered VS code. VS code seems ligther and somewhat faster. I noticed that I can easily switch files, create different files just easily in the VS Code etc. But I guess PyCharm is more focused on the python. Maybe I should start to use it. Thanks

FactChecker

• Introductory Physics Homework Help
Replies
7
Views
11K
• Programming and Computer Science
Replies
2
Views
4K
• Introductory Physics Homework Help
Replies
4
Views
3K