优化圆圈碰撞响应

时间:2016-06-22 10:19:15

标签: c++ sfml

使用SFML,我制作了一个计算碰撞后两个球的轨迹的算法;它工作正常,但如果我尝试超过30个球,它立即冻结或10-20秒后冻结。 我试图避免多次进行相同的计算,但它不起作用。

有什么建议吗?(我有一台高端PC,问题不在那里)

Phi是碰撞角度,dis是距离;

void collisionResponse(Circle &a, Circle &b)
{
    float mass1 = a.getMass();
    float mass2 = b.getMass();
    float disX = a.pos.x - b.pos.x;
    float disY = a.pos.y - b.pos.y;
    float phi = atan2(disY, disX);
    float speed1 = a.getSpeed();
    float speed2 = b.getSpeed();
    float angle1 = a.getAngle();
    float angle2 = b.getAngle();
    float v1x = speed1*cos((angle1 - phi));
    float v1y = speed1*sin((angle1 - phi));
    float v2x = speed2*cos((angle2 - phi));
    float v2y = speed2*sin((angle2 - phi));
    float f1x = ((mass1 - mass2)*v1x + (mass2 + mass2)*v2x) / (mass1+mass2);
    float f2x = ((mass1 + mass1)*v1x + (mass2 - mass1)*v2x) / (mass1+mass2);
    float f1y = v1y;
    float f2y = v2y;
    float cosphi = cos(phi);
    float sinphi = sin(phi);
    float cosphiPI = cos(phi + PI / 2);
    float sinphiPI = sin(phi + PI / 2);
    a.speed.x = cosphi*f1x + cosphiPI*f1y;
    a.speed.y = sinphi*f1x + sinphiPI*f1y;
    b.speed.x = cosphi*f2x + cosphiPI*f2y;
    b.speed.y = sinphi*f2x + sinphiPI*f2y;
    while (sqr(a.pos.x - b.pos.x) + sqr(a.pos.y -  b.pos.y) <= sqr(a.getRadius()+ b.getRadius()))
    {
        a.Move();
        b.Move();
    }
}

0 个答案:

没有答案
相关问题