我的粒子系统的物理更新功能似乎不正确。我的目标是将所有粒子都吸引到鼠标上。

粒子按照预期的那样向鼠标指针移动,直到非常接近为止。当它们靠近时,它们会加速得如此之快,以至于它们远离指针飞行而永不返回。

这是更新功能:

void updateParticle(particle& p,double time){
    const double G=0.000000000066726;
    const double POINTERMASS=1000000000000;

    double squareDistance=pow(p.coords.x-pointerDevice.x,2)+pow(p.coords.y-pointerDevice.y,2)+pow(p.coords.z-pointerDevice.z,2);
    if(squareDistance<0.001)
        squareDistance=0.001;//to fix the possible division by zero

    coords_3d_f accelerationVector={p.coords.x-pointerDevice.x,p.coords.y-pointerDevice.y,p.coords.z-pointerDevice.z};

    accelerationVector=vector_scalar_multiplication(vector_unit(accelerationVector),((G*POINTERMASS)/squareDistance));
    accelerationVector=vector_scalar_multiplication(accelerationVector,time);

    p.velocity=vector_addition(p.velocity,accelerationVector);

    p.coords.x-=p.velocity.x*time;
    p.coords.y-=p.velocity.y*time;
    p.coords.z-=p.velocity.z*time;
}

当squareDistance为常数时,程序看起来不错,但是我知道它是错误的。

那么,我在做什么错呢?

最佳答案

力与距离的平方成反比,因此,当距离接近0时,力(和加速度)接近无穷大。换句话说,如果粒子非常接近,它们也会变得非常快。

如果要物理上准确,请使指针对象具有有限的大小,以使粒子从其弹起。

如果不需要精确,则可以在粒子非常接近时减小作用力。

关于c++ - 粒子系统物理作用很奇怪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11543133/

10-13 08:20