- #36
MatinSAR
- 651
- 196
This is what I did except that I did not use relative velocity.haruspex said:
- calculate the direction of that line
- resolve the velocities into coordinates parallel to and normal to that line
- convert velocities along the line into being relative to one of the discs
- compute the new velocities along that line
- convert back to the lab frame
- take the velocities normal to the line of centres to be unchanged
- resolve the velocities back into your standard xy coordinates
Code:
function update_velocities!(vx1, vy1, vx2, vy2, x1, y1, x2, y2, r1, r2, m1, m2)
# Relative position
Δx = x2 - x1
Δy = y2 - y1
distance = sqrt(Δx^2 + Δy^2)
# Normal vector (unit vector from particle 1 to 2)
nx = Δx / distance
ny = Δy / distance
# Tangent vector (perpendicular to the normal vector)
tx = -ny
ty = nx
# Dot products with normal vector
vn1 = vx1 * nx + vy1 * ny
vn2 = vx2 * nx + vy2 * ny
# Dot products with tangent vector (remain unchanged)
vt1 = vx1 * tx + vy1 * ty
vt2 = vx2 * tx + vy2 * ty
# Update normal components based on elastic collision principles
new_vn1 = (vn1 * (m1 - m2) + 2 * m2 * vn2) / (m1 + m2)
new_vn2 = (vn2 * (m2 - m1) + 2 * m1 * vn1) / (m1 + m2)
# Update velocities
vx1_new = tx * vt1 + nx * new_vn1
vy1_new = ty * vt1 + ny * new_vn1
vx2_new = tx * vt2 + nx * new_vn2
vy2_new = ty * vt2 + ny * new_vn2
return vx1_new, vy1_new, vx2_new, vy2_new
end
I wanted to consider a situation where the velocity changes in both directions, even perpendicular to the line connecting the centers of mass of two objects.
But as I mentioned in post #34, I regret it, and it's a waste of time in my opinion.
I can't see how it might happen in my code.haruspex said:You also need to watch out for overflow when doing divisions or using the tan function. E.g. when coding the use of a division you will need to think about the physical meaning of the denominator being extremely small.
Last edited: