球到固定球碰撞

时间:2017-06-07 17:22:25

标签: c++ 2d physics glm-math

我正在努力实现在击中障碍物时改变方向的球(在这种情况下是一个固定的球)。我可以检测到碰撞发生的时间但我不知道如何在撞到障碍物时修改球的方向。这是一些代码:

struct Vec2
{
    float x, y;
};

struct Ball
{
     void onCollision(const Ball& fixedBall)
     {
         // This function will be called when a collision occurs
         // Speed will be constant, only direction needs to change
     }

     void update()
     {
         position += direction * speed; 
     }

     Vec2 position, direction; // direction is a normalized vector
     float speed, radius; 
};

1 个答案:

答案 0 :(得分:1)

你需要通过否定它来反转速度。

if (collision)
  speed = speed * -1