【UE4 C++】碰撞检测与事件绑定
2021/5/6 22:25:29
本文主要是介绍【UE4 C++】碰撞检测与事件绑定,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
概念
碰撞对象通道与预设
-
默认提供碰撞对象类型,如 WorldStatic、WorldDynamic等。允许用户自定义
-
默认提供碰撞预设,如 NoCollision、BloackAll、OverlapAll。允许用户自定义
碰撞响应设置
-
可以用来设置是否模拟物理碰撞以及触发 Overlap 事件
碰撞响应类型
-
物体发生碰撞后,会有三种碰撞相响应类型
C++ 实现
Component 碰撞
-
注意 Overlap Begin/End 的函数参数
-
注意 OnHit 的函数参数
-
注意甚至 Generated Hit Event 的函数名
-
绑定函数可以用 AddDynamic,也可以用 FScriptDelegate 委托
-
部分设置可不写,蓝图使用时再手动设置
-
写法支持 UShapeComponent及其派生类,如 USphereComponent 、UBoxComponent 等
UPROPERTY(EditAnywhere) USceneComponent* Root; UPROPERTY(EditAnywhere) UStaticMeshComponent* Cube; UFUNCTION() virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); UFUNCTION() virtual void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); UFUNCTION() virtual void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
ACollisionActor::ACollisionActor() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; Root = CreateDefaultSubobject<USceneComponent>(TEXT("RootScene")); SetRootComponent(Root); Cube = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Cube")); Cube->SetupAttachment(Root); static ConstructorHelpers::FObjectFinder<UStaticMesh> mesh(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'")); if (mesh.Succeeded()) { Cube->SetStaticMesh(mesh.Object); } // 设置是否开启物理模拟 Cube->SetSimulatePhysics(false); // 开启 Generated Hit Event Cube->SetNotifyRigidBodyCollision(true); // 开启CCD Continuous collision detection (CCD) 连续式碰撞检测 Cube->BodyInstance.SetUseCCD(true); // 开启Generate Overlap Events Cube->SetGenerateOverlapEvents(true); // 设置碰撞预设 Cube->SetCollisionProfileName(TEXT("OverlapAll")); //Cube->SetCollisionResponseToAllChannels(ECR_Overlap); // 设置碰撞响应设置 Cube->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); // 绑定函数 Cube->OnComponentBeginOverlap.AddDynamic(this, &ACollisionActor::OnOverlapBegin); // 绑定函数 使用委托 FScriptDelegate OverlapEndDelegate; OverlapEndDelegate.BindUFunction(this, TEXT("OnOverlapEnd")); Cube->OnComponentBeginOverlap.Add(OverlapEndDelegate); // 绑定碰撞函数 Cube->OnComponentHit.AddDynamic(this, &ACollisionActor::OnHit); } void ACollisionActor::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { UE_LOG(LogTemp, Warning, TEXT("Overlap Begin")); } void ACollisionActor::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) { UE_LOG(LogTemp, Warning, TEXT("Overlap End")); } void ACollisionActor::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) { UE_LOG(LogTemp, Warning, TEXT("Hit")); Destroy(); }
ATriggerBox 碰撞
-
OnActorBeginOverlap
-
OnActorEndOverlap
UFUNCTION() void HandleOverlap(AActor* OverlappedActor, AActor* OtherActor );
void AMyTriggerBox::BeginPlay() { //放在构造函数好像不起作用 OnActorBeginOverlap.AddDynamic(this, &AMyTriggerBox::HandleOverlap); } void AMyTriggerBox::HandleOverlap(AActor* OverlappedActor, AActor* OtherActor ) { UClass* ActorClass = OtherActor->GetClass(); // 其他处理逻辑 }
参考
- 碰撞响应参考
这篇关于【UE4 C++】碰撞检测与事件绑定的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程
- 2024-12-20Svg Sprite Icon实战:从入门到上手的全面指南
- 2024-12-20LCD1602显示模块详解
- 2024-12-20利用Gemini构建处理各种PDF文档的Document AI管道