【发布时间】:2017-10-24 13:22:03
【问题描述】:
我编写了一个代码来模拟二维盒子中圆形粒子的运动。每当他们搬出盒子时,我都会把它们放在盒子里靠近墙壁的地方。我想在代码中添加粒子的直径(2R),这意味着当两个圆的中心之间的距离变得小于 2R 时,它们沿着连接它们的中心的线分开,使得圆心之间的距离变为等于 2R。
谁能建议一个防止粒子重叠的代码?
这是我的代码,其中不考虑重叠:
clear all
close all
l = 224; nn = 800; %number of particles
time = 1000; dd = 1;
x= l*rand(1,nn);
y= l*rand(1,nn);
for t = 1:time;
x= x + rand(1,nn)-0.5* ones(1,nn);
y=y+rand(1,nn)-0.5* ones (1,nn);
index = (x < 0); x(index) = abs(normrnd(0,1,1,nnz(index)));
index = (y < 0); y(index) = abs(normrnd(0,1,1,nnz(index)));
index = (x > l); x(index) = l-abs(normrnd(0,1,1,nnz(index)));
index = (y > l); y(index) = l-abs(normrnd(0,1,1,nnz(index)));
end
【问题讨论】:
-
所以每个粒子都沿直线行进,直到它与墙壁接触,当它“反弹”时,您还想包括粒子间的碰撞吗?
-
随着时间的推移,每一步的方向和长度步长没有变化(我使用随机数来做到这一点)。是的,当两个圆的中心之间的距离小于 2R 时,它们会沿着连接它们的中心和圆心距离的线等于@Wolfie
-
你能帮忙吗? @Wolfie
-
3 个或更多粒子同时碰撞(可能不太可能)会发生什么?
-
你看到的问题叫做Collision Detection
标签: matlab random collision-detection simulation