2D elastic collision using Matlab

2.4k Views Asked by At

I recently started using Matlab and am modeling hard spheres in 2D. I have n particles, their positions are given by two arrays x(n,1) and y(n,1). The simulation has N time steps.

I set up a distance matrixt:

Distance: x(1)-x(1), x(2)-x(1), x(3)-x(1), 
          x(2)-x(1), x(2)-x(2), x(3)-x(2), 
          x(3)-x(1), x(3)-x(2), x(3)-x(3), 

The diagonal is zero. I tried to use a for loop in combination with a for command to see if a collision has taken place, by comparing particle distances to 2*radius. If they do collide I want to find the collision angle to create a new reference frame and treat the collision as a 1D collision in that frame.

for i=1:number_particles
  for j=(i+1):number_particles

    if distance_2D(i,j) < 2*radius             %Collision with other particle

    dy = position_y(i)-position_y(j);
    dx = position_x(i)-position_x(j);

    P(i,j) = atan2(dy,dx);      

    end
 end
end

However, I am not sure if atan2 is the correct command to use to extract the angle that I am looking for. Also it seems like there should be a much neater way of setting the solution up in Matlab.

Should I be using atan(y/x) or atan2d(y,x). And then from there?