最简单的 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

相关文章

电脑没有仿宋GB2312字体怎么办? 仿宋GB2312字体下载安装及调出来的教程

《电脑没有仿宋GB2312字体怎么办?仿宋GB2312字体下载安装及调出来的教程》仿宋字体gb2312作为一种经典且常用的字体,广泛应用于各种场合,如何在计算机中调出仿宋字体gb2312?本文将为您... 仿宋_GB2312是公文标准字体之一,仿China编程宋是字体名称,GB2312是字php符编码标准名称(简

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Window Server创建2台服务器的故障转移群集的图文教程

《WindowServer创建2台服务器的故障转移群集的图文教程》本文主要介绍了在WindowsServer系统上创建一个包含两台成员服务器的故障转移群集,文中通过图文示例介绍的非常详细,对大家的... 目录一、 准备条件二、在ServerB安装故障转移群集三、在ServerC安装故障转移群集,操作与Ser

windos server2022的配置故障转移服务的图文教程

《windosserver2022的配置故障转移服务的图文教程》本文主要介绍了windosserver2022的配置故障转移服务的图文教程,以确保服务和应用程序的连续性和可用性,文中通过图文介绍的非... 目录准备环境:步骤故障转移群集是 Windows Server 2022 中提供的一种功能,用于在多个

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

龙蜥操作系统Anolis OS-23.x安装配置图解教程(保姆级)

《龙蜥操作系统AnolisOS-23.x安装配置图解教程(保姆级)》:本文主要介绍了安装和配置AnolisOS23.2系统,包括分区、软件选择、设置root密码、网络配置、主机名设置和禁用SELinux的步骤,详细内容请阅读本文,希望能对你有所帮助... ‌AnolisOS‌是由阿里云推出的开源操作系统,旨

PyTorch使用教程之Tensor包详解

《PyTorch使用教程之Tensor包详解》这篇文章介绍了PyTorch中的张量(Tensor)数据结构,包括张量的数据类型、初始化、常用操作、属性等,张量是PyTorch框架中的核心数据结构,支持... 目录1、张量Tensor2、数据类型3、初始化(构造张量)4、常用操作5、常用属性5.1 存储(st

C++中实现调试日志输出

《C++中实现调试日志输出》在C++编程中,调试日志对于定位问题和优化代码至关重要,本文将介绍几种常用的调试日志输出方法,并教你如何在日志中添加时间戳,希望对大家有所帮助... 目录1. 使用 #ifdef _DEBUG 宏2. 加入时间戳:精确到毫秒3.Windows 和 MFC 中的调试日志方法MFC