【问题标题】:UE4 Mesh several collision points detectingUE4 Mesh几个碰撞点检测
【发布时间】:2021-11-24 17:53:00
【问题描述】:

当我的网格体发生碰撞时,我需要找到所有命中点(顶点),因为使用 OnHit,结构中只有 一个 冲击点并且只有一个(红色调试球体)。有没有办法做到这一点? (例如在 Unity 碰撞结构中具有这些点的数组:collision.contacts

这是一个例子,当 2 个立方体与面接触并且有很多接触点(不是 1 个)时

【问题讨论】:

  • 可能有些事情可以用 C++ 完成。看看UPrimitiveComponent::UpdateOverlapsImplUPrimitiveComponent::ComponentOverlapMulti
  • 仅供参考:有一个“游戏开发”堆栈站点,用于询问 UE4 问题 (gamedev.stackexchange.com)

标签: collision-detection collision unreal-engine4 mesh-collider


【解决方案1】:

碰撞会产生重叠事件,因此理论上您可以使用OnComponentBeginOverlap 并获取SweepResult 来处理重叠事件。但是SweepResult 不太可靠,所以我建议在重叠事件中做一个Spherical Sweep

void Pawn::OnComponentBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, 
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
 {
     if (OtherActor && (OtherActor != this))
     {
         TArray<FHitResult> Results;
         auto ActorLoc = GetActorLocation();
         auto OtherLoc = OtherComp->GetComponentLocation();
         auto CollisionRadius = FVector::Dist(Start, End) * 1.2f;
 
         // spherical sweep 
         GetWorld()->SweepMultiByObjectType(Results, ActorLoc, OtherLoc,
             FQuat::Identity, 0,
             FCollisionShape::MakeSphere(CollisionRadius),  
             // use custom params to reduce the search space
             FCollisionQueryParams::FCollisionQueryParams(false)
         );
 
         for (auto HitResult : Results)
         {
             if (OtherComp->GetUniqueID() == HitResult.GetComponent()->GetUniqueID()) {

             // insert your code 

                 break;
             }
         }
     }
 }

您可以尝试使用 FCollisionQueryParams 来加快此过程,但会在几帧碰撞后绘制球面扫描,因此您可以暂停/停止 actor 以获得准确的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多