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

相关文章

Tomcat的下载安装与使用教程

《Tomcat的下载安装与使用教程》本文介绍了Tomcat的下载、安装和使用方法,包括在本机和云服务器上部署Tomcat的过程,以及解决启动失败问题的方法... 目录Tomcat的下载安装与使用Tomcat的下载与安装Tomcat在本机运行使用Tomcat在php云服务器上的使用总结Tomcat的下载安装与

SpringBoot基于沙箱环境实现支付宝支付教程

《SpringBoot基于沙箱环境实现支付宝支付教程》本文介绍了如何使用支付宝沙箱环境进行开发测试,包括沙箱环境的介绍、准备步骤、在SpringBoot项目中结合支付宝沙箱进行支付接口的实现与测试... 目录一、支付宝沙箱环境介绍二、沙箱环境准备2.1 注册入驻支付宝开放平台2.2 配置沙箱环境2.3 沙箱

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

IDEA接入Deepseek的图文教程

《IDEA接入Deepseek的图文教程》在本篇文章中,我们将详细介绍如何在JetBrainsIDEA中使用Continue插件接入DeepSeek,让你的AI编程助手更智能,提高开发效率,感兴趣的小... 目录一、前置准备二、安装 Continue 插件三、配置 Continue 连接 DeepSeek四

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx