计算两个移动的 aabb 之间的撞击时间

问题描述 投票:0回答:0

现在,我正在尝试使用 minkowski 和来计算 2d aabb 之间的影响时间。 这是我创建的函数的实现,但它不起作用,因为我的游戏隧道中有一些实体:


// calculate the time of impact between two moving aabbs within a timestep.
float GetTimeOfImpact(aabb aAABB, aabb bAABB, vec2 aVel, vec2 bVel, float Timestep)
{
     // this is an invalid time of impact.
     float TimeOfImpact = -1.0f;
     
     vec2 RelativeVelocity = aVel - bVel;
     float RelativeSpeed = Length(RelativeVelocity);
     if(!Compare(RelativeSpeed, 0.0f))
     {
          vec2 CenterA = GetCenter(aAABB);
          vec2 CenterB = GetCenter(bAABB);
          
          aabb Minkowski = MinkowskiSum(bAABB, aAABB);
          vec2 Dir = RelativeVelocity*Timestep;
          
          float TOI = RayIntersection(CenterA, Dir, Minkowski); // returns a value 0 to 1 or -1 to indicate an invalid time of impact
          if(TOI >= 0.0f && TOI <= 1.0f)
          {
               // i am skeptical about this.
               TimeOfImpact = Timestep * TOI;
          }
     }
     
     return TimeOfImpact;
}

我正在使用一些自定义函数,它们单独运行良好。 你觉得逻辑上有什么问题吗?

c++ collision-detection aabb
© www.soinside.com 2019 - 2024. All rights reserved.