简体   繁体   中英

3d collision on matlab

I'm developing a system like this: I have a sphere with many molecules inside it. If the molecules collide, their new directions need to be recalculated, as well as if they collide with the walls of the sphere.
I have already two matrices: one with the coordinates of all the particles and another with the coordinates of the walls of the sphere. Here is a part of my algorithm.

% Coordinates of the wall of the sphere
theta=linspace(0, 2*pi, 25);
phi=linspace(0, pi, 25);
x_sph=r_sph.*cos(theta).*sin(phi);
y_sph=r_sph.*sin(theta).*sin(phi);
z_sph=r_sph.*cos(phi);
[x_sph' y_sph' z_sph'];


itmax=100
for it=(1:itmax);
    for i3=1:500
        for j3=1:500
            if i3~=j3
               dist1(i3,j3,it)=sqrt((balls_in_sphere(i3,1)-balls_in_sphere(j3,1))^2+(balls_in_sphere(i3,2)-balls_in_sphere(j3,2))^2+(balls_in_sphere(i3,3)-balls_in_sphere(j3,3))^2);
               if dist1(i3,j3,it)<=d

                %recalculate the new directions   ???

               end
            end
        end
        for j3=1:25
            dist2(i3,j3,it)=sqrt((balls_in_sphere(i3,1)-cs(j3,1)^2)+(balls_in_sphere(i3,2)-cs(j3,2)^2)+(balls_in_sphere(i3,3)-cs(j3,3)^2)); 
%comparative between the coordinates of the balls inside the sphere and the points of the sphere

            if dist2(i3,j3,it)<=d
              %if there is a collision, recalculate the directions   ???

            end
        end
    end
    balls_in_sphere1=balls_in_sphere2;
end

I'd be very thankful if someone helps me. I've been trying to solve for weeks, without success.

I can just provide you with some "suggestions":

  • the problem is complex, you might start with a 2D sccenario with few particles and a square or a pentagon (instead of a sphere)
  • consider elastic collision
  • you should give the particles a mass and a speed (to be decomposed on its X and Y component)
  • you should add an external loop iterating with respect to the simulation time (at each iteration t=t+dt )
  • at each iteration, compute the new position of the particles
  • the collision condition could be defined with respect to a minimum distance between two particles (as it seems you've already done)
  • instead of defining the sphere (or, in the semplified case, the square) by points consider a set of meshes (like a soccer balls) and evaluate the distance with respect to them
  • to determine the new direction of the particles, following a collision, consider the composition of the velocity vectors: You can easily find the formulas on Internet (eg http://bolvan.ph.utexas.edu/~vadim/Classes/2014s/collisions.pdf

Once you have a stable solution for 2D you can add the third dimension.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM