粒子碰撞
Particle collision
我正在尝试编写一个代码,给定一维框中N个粒子的质量,速度,位移和半径,将以0.03s的小增量返回它们的位置。该计划符合规定。但是,不会生成正确的值。
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
struct particle {
double x; //position of particle
double im; //inverse mass
double v; //velocity of particle
double r; //radius of particle
};
void bonkers(vector<particle> &array, int NN, double t, double ddt, int T);
double time(vector<particle> &a, int NN, int &whichn);
void collision(particle &a, particle &b);
void position(particle &a, double ddt);
void position(particle &a, double ddt)//position of particles
{
a.x += ddt * a.v; //position = t * velocity
}
void collision(particle &a, particle &b)//velocities after collision
{
double realativeV = a.v - b.v;
double af = a.im / (a.im + b.im);
double bf = b.im / (a.im + b.im);
a.v -= 2.0 * af * realativeV;
b.v += 2.0 * bf * realativeV;
}
double time(vector<particle> &a, int NN, int &whichn)//Finds time of first collision.
{
double dt = 1e100; //really large number
for (int n = 0; n < NN - 1 ; n++)
{
double RelativeV = a[n].v - a[n + 1].v;
if (RelativeV > 0)//this means dt won't be 0, no negative times
{
double Collisiont = ((a[n + 1].x - a[n + 1].r) - (a[n].x + a[n].r))/ RelativeV;//gives time of collision
//time of collision worked out as time when the distance between the two balls centre is equal to their combined radi
if (Collisiont < dt)//finds smallest possible time of collision
{
dt = Collisiont; //returns time of first collision
whichn = n; //gives which particles collide. Therefore which particles velocities need to change
}
}
}
return dt;
}
void bonkers(vector<particle> &array, int NN, double t, double ddt, int T)
{
ofstream myfile;
myfile.open("example.txt");//sends all information to file called example.txt
int whichn = 0;
double k;
double l = 0;
for (int i = 0; i <= T; i++)//set arbitrary number of collision i.e. T
{
k = t;//after every collision k reset to 0 as time function works from 0
while( k <= time(array, NN, whichn) )
{
myfile << l; //prints the time
k += ddt; //incriments time
l += ddt; //increments time, but doesn't reset time so gives total time
for (int n = 1; n < NN -1 ; n++)
{
position(array[n], ddt);//calculates new position
myfile << "t" << array[n].x;//prints the positions of all the particles at said time
}
myfile << endl;
}
collision(array[whichn], array[whichn + 1]);//asings new velocities to the two particles which collided
}
myfile.close();
}
int main()
{
int N; //number of balls
double m; //mass of balls
int T = 50; //number of collisions
double t = 0.0; //starts from 0 time
double ddt = 0.03; //invrements by 0.03 seconds each time
cout << "Input number of balls" << endl;
cin >> N;
vector<particle> a(N + 2); //extra two for the two walls, wall1 at 0 and wall2 at 20
for (int k = 1; k <= N; k++)
{
cout << "Input mass of ball " << k << endl;
cin >> m;
a[k].im = 1.0 / m; //finds inverse mass
cout << "Input position of ball " << k << " between 0 and 20" << endl;
cin >> a[k].x;
cout << "Input speed of ball " << k << endl;
cin >> a[k].v;
cout << "Input radius of ball " << k << endl;
cin >> a[k].r;
}
//asign wall variables
a[0].im = 0; //infinte mass, so walls don't move
a[0].r = 0;
a[0].x = 0; //wall1 at 0
a[0].v = 0;
a[N + 1].im = 0;
a[N + 1].r = 0;
a[N + 1].x = 20; //wall2 at 20
a[N + 1].v = 0;
bonkers(a, N + 2, t, ddt, T);
return 0;
}
当你只有一个球时,有一个非常明显的问题。半径、质量和速度全部 1.由于某种原因,球的速度在 10.03 的位置发生变化,而不是在 20 时反弹墙壁。
我相信问题出在代码的"时间"功能部分。通过输出"k"来更明显.txt在疯狂函数中而不是"l"。
非常感谢:)
问题是这一行。
while( k <= time(array, NN, whichn) )
您将在 while 循环的每次迭代中重新计算碰撞时间,并增加已用时间 k
,然后进行比较。事件之前的时间和经过的时间没有意义进行比较。您可以计算碰撞时间并增加时间,k
,直到到达那里,也可以重新计算碰撞时间并在每次迭代中与 0 进行比较。
相关文章:
- 落砂模拟碰撞检测C++和SFML
- SFML 碰撞永远不会在我的系统中注册
- 在均匀网格中处理碰撞
- 在 Cocos2dx 中初始化粒子效果后的警告 - "libpng warning: iCCP: known incorrect sRGB profile"
- 粒子系统:所有粒子都朝同一方向运动
- 平台游戏 - 下车时避免与平台碰撞
- AABB 碰撞使实际精灵的大小翻倍
- 具有随机值的多个对象及其碰撞的 SFML
- SFML 中的重力和碰撞检测
- 我的碰撞检测中的奇怪行为
- Box2D Contact Listener碰撞仅间歇性工作
- 如何为球形物体和三角形地形提供高效的碰撞检测和响应
- 同时与两个片段着色器发生碰撞
- 弹性粒子在斜坡上弹跳
- 2D 障碍物碰撞
- 未定向到相机的粒子
- 可破坏的网格块会导致奇怪的碰撞
- ptrdiff_t typedef 碰撞 - 谷歌测试和英特尔蟒蛇
- 粒子碰撞
- 带有嵌套循环的矢量粒子-未检测到碰撞