最简单的 UE 4 C++ 教程 —— 给 Actor 添加径向推力【十八】

2023-10-16 21:30

本文主要是介绍最简单的 UE 4 C++ 教程 —— 给 Actor 添加径向推力【十八】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原教程是基于 UE 4.18,我是基于 UE 4.25】

英文原地址

接上一节教程,本教程非常有趣,我们将通过在设定范围内的所有物体上添加径向推力来模拟爆炸。

创建一个名为 AddRadialForce 的新角色。我们不需要对头文件做任何操作。下面是由虚幻生成的默认头文件。

AddRadialForce.h

#pragma once#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AddRadialForce.generated.h"UCLASS()
class UNREALCPP_API AAddRadialForce : public AActor
{GENERATED_BODY()public:	// Sets default values for this actor's propertiesAAddRadialForce();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;};

为了在调试过程中提供帮助,让我们将 DrawDebugHelpers 头文件添加到代码中。

#include "AddRadialForce.h"
// add debug helpfers
#include "DrawDebugHelpers.h"

在这个例子中,我们将在 BeginPlay() 函数中执行所有的逻辑。我们想要收集我们范围内的所有命中结果,并从范围的扫描中获得结果。为此,我们将使用 TArray 跟踪重叠的 actor。

void AAddRadialForce::BeginPlay()
{Super::BeginPlay();// create tarray for hit resultsTArray<FHitResult> OutHits;// crate tarray for sweep actorsTArray<AActor*> SweepActors;}

接下来我们要声明 TArray 数组 OutHits 。我们希望扫描范围从 actor 的位置开始同时也从该位置结束,并让 CollisionShape 是一个 500 单位的球体。您可以使用 GetActorLocation() 来获取 actor 的位置,它返回一个向量。我们使用 FCollisionShape:: makephere (500.0f) 来创建一个CollisionShape

// Called when the game starts or when spawned
void AAddRadialForce::BeginPlay()
{Super::BeginPlay();// create tarray for hit resultsTArray<FHitResult> OutHits;// get actor locationsFVector MyLocation = GetActorLocation();// start and end locations. The sphere will create the radial sweep.FVector Start = MyLocation;FVector End = MyLocation;// create a collision sphereFCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);}

为了可视化扫描的球体,我们将绘制一个调试球体。

// Called when the game starts or when spawned
void AAddRadialForce::BeginPlay()
{Super::BeginPlay();// create tarray for hit resultsTArray<FHitResult> OutHits;// get actor locationsFVector MyLocation = GetActorLocation();// start and end locations. The sphere will create the radial sweep.FVector Start = MyLocation;FVector End = MyLocation;// create a collision sphereFCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);// draw collision sphereDrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);
}

接下来我们要检查我们的 actor 在我们 BeginPlay() 时是否击中了任何东西。每个 actor 都有GetWorld 功能。从 ,我们将使用从 GetWorld() 函数得到的 SweepMultiByChannel() 函数并对其设置上面刚创建的变量作为参数。这将返回一个 bool,指示是否有其他 actor 在该 actor 的范围内。

void AAddRadialForce::BeginPlay()
{Super::BeginPlay();// create tarray for hit resultsTArray<FHitResult> OutHits;// get actor locationsFVector MyLocation = GetActorLocation();// start and end locations. The sphere will create the radial sweep.FVector Start = MyLocation;FVector End = MyLocation;// create a collision sphereFCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);// draw collision sphereDrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);// check if something got hit in the sweepbool isHit = GetWorld()->SweepMultiByChannel(OutHits, Start, End, FQuat::Identity, ECC_WorldStatic, MyColSphere);}

如果 isHit 为真,我们将通过遍历数组 OutHits ,并向每个成功 cast 的 actor 的根组件添加径向推力。

if (isHit){// loop through TArrayfor (auto& Hit : OutHits){UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>((Hit.GetActor())->GetRootComponent());if (MeshComp){// alternivly you can use  ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin.// set the float radius to 500 and the float strength to 2000.MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true);}}}

最终完整 cpp 代码如下

#include "AddRadialForce.h"
// add debug helpfers
#include "DrawDebugHelpers.h"// Sets default values
AAddRadialForce::AAddRadialForce()
{// 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;}// Called when the game starts or when spawned
void AAddRadialForce::BeginPlay()
{Super::BeginPlay();// create tarray for hit resultsTArray<FHitResult> OutHits;// get actor locationsFVector MyLocation = GetActorLocation();// start and end locations. The sphere will create the radial sweep.FVector Start = MyLocation;FVector End = MyLocation;// create a collision sphereFCollisionShape MyColSphere = FCollisionShape::MakeSphere(500.0f);// draw collision sphereDrawDebugSphere(GetWorld(), GetActorLocation(), MyColSphere.GetSphereRadius(), 50, FColor::Cyan, true);// check if something got hit in the sweepbool isHit = GetWorld()->SweepMultiByChannel(OutHits, Start, End, FQuat::Identity, ECC_WorldStatic, MyColSphere);if (isHit){// loop through TArrayfor (auto& Hit : OutHits){UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>((Hit.GetActor())->GetRootComponent());if (MeshComp){// alternivly you can use  ERadialImpulseFalloff::RIF_Linear for the impulse to get linearly weaker as it gets further from origin.// set the float radius to 500 and the float strength to 2000.MeshComp->AddRadialImpulse(GetActorLocation(), 500.f, 2000.f, ERadialImpulseFalloff::RIF_Constant, true);}}}}// Called every frame
void AAddRadialForce::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}

实际运行起来的效果图如下

这篇关于最简单的 UE 4 C++ 教程 —— 给 Actor 添加径向推力【十八】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/220919

相关文章

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3