U++ 碰撞函数结构

U++ 碰撞函数结构
碰撞函数UFUNCTION() void OnBoxBeginOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult SweepResult );参数详解UPrimitiveComponent* OverlappedComponent表示自己这边哪个组件发生了重叠。碰撞检测组件都继承自UPrimitiveComponent大致的继承关系为UPrimitiveComponent├── UShapeComponent│ ├── UBoxComponent│ ├── USphereComponent│ └── UCapsuleComponent├── UStaticMeshComponent└── USkeletalMeshComponent什么时候使用它当有多个碰撞组件被绑定到同一个函数就可以判断是哪一个触发了事件if (OverlappedComponent FrontBox) { UE_LOG(LogTemp, Warning, TEXT(进入前方区域)); } else if (OverlappedComponent BackBox) { UE_LOG(LogTemp, Warning, TEXT(进入后方区域)); }AActor* OtherActor表示与之发生重叠的另一个完整Actor使用指针前一般先检查是否有效也经常排除自己if (!OtherActor || OtherActor this) { return; }UPrimitiveComponent* OtherComp对方Actor中具体是哪一个组件参与了重叠。OtherComp和OtherActor的区别OtherActor 对方整个Actor OtherComp 对方Actor中的具体组件OtherBodyIndex该参数一般用不上但也不能删除。后续有需求再记录该参数的作用。bFromSweep该参数是返回bool值表示这次重叠是否来自带Sweep的移动检测。什么是Sweep如果物体从A点移动到B点不使用Sweep的话可以理解为直接把物体放到新位置不沿途扫描障碍物。使用Sweep可以理解为从旧位置移动到新位置时检测图中是否与碰撞体发生接触。const FHitResult SweepResult如果本次重叠由Sweep移动产生这个参数保存对应的查询结果。FHitResult是 UE 中保存碰撞或 Trace 命中信息的结构体可以包含位置、碰撞法线、命中的 Actor 和组件等信息。常见的成员包括SweepResult.Location SweepResult.ImpactPoint SweepResult.Normal SweepResult.ImpactNormal SweepResult.GetActor() SweepResult.GetComponent() SweepResult.bBlockingHit SweepResult.bStartPenetratingvoid OnBoxEndOverlap( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex );EndOverlap中的四个参数和BeginOverlap中的前四个参数完全对应。