基于C++代码的UE4学习(一)—— ActorComponent与SceneComponent

 

Actor是UE引擎中的一个类。类中可以包含很多类型的东西,这些东西可以是类的对象,也可以是组件的对象。

ActorComponent就是为Actor类添加更多自定义功能的组件。它仅仅针对的是Actor类及其派生类的对象。你可以为这个组件设置很多你想要的属性和功能,当Actor类的对象挂载了这个组件的时候,这个Actor类就拥有了这个组件的功能,好比擎天柱拥有了天火的翅膀,从而实现了飞翔的功能一样。

 

SceneComponent是ActorComponent组件的派生类型,它比ActorComponent组件多拥有了Transform的功能,而且它并不会被渲染出来,意思就是好比在你家里搭建了一个舞台,一个看不见的舞台,所有的活动必须在这个舞台里。家就是Actor,舞台就是SceneComponent,而所有参与演出的乐器和演职人员都必须在SceneComponent里,SceneComponent就如同一个玻璃盒子,把它们都装了起来。

 

现在,利用SceneComponent完成旋转组件的编写

 

效果图:

 

 

头文件:

 

1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "Components/SceneComponent.h"
 7 #include "MyRotationComponent.generated.h"
 8 
 9 
10 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
11 class MYPROJECT5_API UMyRotationComponent : public USceneComponent
12 {
13     GENERATED_BODY()
14 
15 public:    
16     // Sets default values for this component's properties
17     UMyRotationComponent();
18 
19 protected:
20     // Called when the game starts
21     virtual void BeginPlay() override;
22 
23 public:    
24     // Called every frame
25     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
26 
27 public:
28     UPROPERTY()
29     float orbitDistance;
30     UPROPERTY()
31     float currentValue;
32     UPROPERTY()
33     float offsetSpeed;
34 };

 

 

源文件:

 

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 
 4 #include "MyRotationComponent.h"
 5 
 6 // Sets default values for this component's properties
 7 UMyRotationComponent::UMyRotationComponent()
 8 {
 9     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
10     // off to improve performance if you don't need them.
11     PrimaryComponentTick.bCanEverTick = true;
12 
13     orbitDistance = 200.0f;
14     currentValue = 0.0f;
15     offsetSpeed = 50.0f;
16 }
17 
18 
19 // Called when the game starts
20 void UMyRotationComponent::BeginPlay()
21 {
22     Super::BeginPlay();
23     
24 }
25 
26 
27 // Called every frame
28 void UMyRotationComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
29 {
30     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
31 
32     float radians = FMath::DegreesToRadians<float>(currentValue);
33 
34     SetRelativeLocation(FVector(orbitDistance * FMath::Cos(radians), orbitDistance * FMath::Sin(radians),RelativeLocation.Z));
35 
36     FVector dir_normal = RelativeLocation.GetSafeNormal();
37     FRotator rot = dir_normal.Rotation();
38 
39     SetRelativeRotation(rot);
40 
41     currentValue = FMath::Fmod(currentValue + (DeltaTime * offsetSpeed),360);
42 }

 

这里需要说明的是,你运行所看到的旋转,是SceneComponent组件在相对Actor对象的旋转,两个物体并没有发生自转。

 

1     FVector dir_normal = RelativeLocation.GetSafeNormal();
2     FRotator rot = dir_normal.Rotation();

 

第一行代码中的GetSafeNormal()方法是用来将相对位置向量进行单位化。

第一行代码中的Rotation()方法可以理解为将向量的数据作为旋转的数据,且将Roll方向的旋转设置为0。

 

1 currentValue = FMath::Fmod(currentValue + (DeltaTime * offsetSpeed),360);

 

这一句话的意思是将不断变化的角度数值与360度进行取模操作,可有让它们顺利的、圆滑的不停旋转。

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值