UE4自定义MovementComponent组件

来源:互联网 发布:java大数字处理 编辑:程序博客网 时间:2024/05/20 00:37

自定义Movement组件 

目的:实现自定义轨迹如抛物线,线性,定点等运动方式,作为组件控制绑定对象的运动。

基类:UMovementComponent

过程:

1.创建UCustomMovementComponenet继承UMovementComponent类,作为各种具体轨迹的父类,完成主要流程的实现。并提供接口给子类override实现具体计算过程。

2.实现子类轨迹计算过程。这里仅提供线性移动轨迹作为示例。


一、UCustomMovementComponent类


  1. /** 
  2.  * class : UCustomMovementComponent 
  3.  * author : Jia Zhipeng 
  4.  * Base class of custom movement component 
  5.  */  
  6. UCLASS(ClassGroup = Movement, abstract, ShowCategories = (CustomMovement))  
  7. class CLIENT_API UCustomMovementComponent : public UMovementComponent  
  8. {  
  9.     GENERATED_UCLASS_BODY()  
  10.   
  11. public:  
  12.     /*Initialize target position, must be called before TickComponent. 
  13.     **@param bFixedPoint : whether target position is fixed point or target component 
  14.     */  
  15.     UFUNCTION(BlueprintCallable, Category = CustomMovement)  
  16.     virtual void SetTargetPosition(bool bFixedPoint, FVector PointLocation, USceneComponent* MoveTarget=nullptr);  
  17.     //Initialize params which will be used during computation, implementation in derived class.  
  18.     virtual void InitComputeParams() {};  
  19.     //Computation process, must be override in derived class.  
  20.     virtual void ComputeMovement(float DeltaTime, FVector& OutMoveDelta, FQuat& OutNewRotation) {};  
  21.     //Update process.  
  22.     virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;  
  23.     //Check whether should be stopped  
  24.     void CheckIsStop();  
  25.   
  26. protected:  
  27.     FVector GetTargetPosition();      
  28.     FVector GetHostPosition();  
  29.     //if bFixedPoint is true, use this location to update.  
  30.     FVector PointLocation;  
  31.   
  32.     //The current target we are homing towards. Can only be set at runtime (when projectile is spawned or updating).  
  33.     TWeakObjectPtr<USceneComponent> MoveTarget;  
  34.   
  35.     //If true, use fixed point to update location; else, use MoveTarget.  
  36.     uint32 bFixedPoint:1;  
  37.   
  38.     //If true, stop TickComponent  
  39.     uint32 bStop:1;       
  40. };  
  41.   
  42. UCustomMovementComponent::UCustomMovementComponent(const FObjectInitializer& ObjectInitializer)  
  43.     : Super(ObjectInitializer)  
  44. {  
  45.     bStop = false;  
  46. }  
  47. void UCustomMovementComponent::SetTargetPosition(bool bFixedPoint, FVector PointLocation, USceneComponent* MoveTarget)  
  48. {  
  49.     bStop = false;  
  50.     this->MoveTarget = MoveTarget;  
  51.     this->bFixedPoint = bFixedPoint;  
  52.     this->PointLocation = PointLocation;  
  53.     InitComputeParams();  
  54. }  
  55.   
  56. void UCustomMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)  
  57. {  
  58.     //Tick parent method first, in order to know whether UpdatedComponent is null.  
  59.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);  
  60.     CheckIsStop();  
  61.     if (bStop)  
  62.         return;  
  63.     FVector OutMoveDelta;  
  64.     FQuat OutNewRotation;  
  65. //计算Location和Rotation的变化  
  66.     ComputeMovement(DeltaTime, OutMoveDelta, OutNewRotation);  
  67. //更改UpdatedComponent坐标值的调用方法  
  68.     MoveUpdatedComponent(OutMoveDelta, OutNewRotation, true);   //whether change orientation?  
  69. //UMovementComponent中注释说在更改Velocity变量后需要调用该方法改变UpdatedComponent的Velocity。看源代码后发现应该是其他如物理Body等需要使用该值。  
  70.     UpdateComponentVelocity();  
  71. }  
  72.   
  73. void UCustomMovementComponent::CheckIsStop()  
  74. {  
  75.     if (!UpdatedComponent)  
  76.     {  
  77.         bStop = true;  
  78.         return;  
  79.     }  
  80.     //whether target is exist  
  81.     if (!bFixedPoint && MoveTarget == nullptr)  
  82.     {  
  83.         bStop = true;  
  84.         return;  
  85.     }  
  86.     //reach the target location then stop  
  87.     float LocationDifference = (GetTargetPosition() - UpdatedComponent->GetComponentLocation()).Size();  
  88.     if (LocationDifference < SMALL_NUMBER)  
  89.     {  
  90.         bStop = true;  
  91.         return;  
  92.     }  
  93. }  
  94.   
  95. FVector UCustomMovementComponent::GetTargetPosition()  
  96. {  
  97.     if (bFixedPoint)  
  98.         return PointLocation;  
  99.     check(MoveTarget != nullptr);  
  100.     return MoveTarget->GetComponentLocation();  
  101. }  
  102.   
  103. FVector UCustomMovementComponent::GetHostPosition()  
  104. {  
  105.     check(UpdatedComponent);  
  106.     return UpdatedComponent->GetComponentLocation();  
  107. <span <="" td="" style="word-wrap: break-word; margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">
0 0
原创粉丝点击