Advertisement
Guest User

UClockworkAnimations.cpp

a guest
Jun 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.78 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "AandSVR.h"
  4. #include "ClockworkAnimations.h"
  5.  
  6. float UClockworkAnimations::timeRatio = 1.0;
  7.  
  8. // Sets default values for this component's properties
  9. UClockworkAnimations::UClockworkAnimations()
  10. {
  11.     // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
  12.     // off to improve performance if you don't need them.
  13.     PrimaryComponentTick.bCanEverTick = true;
  14. }
  15.  
  16.  
  17. // Called when the game starts
  18. void UClockworkAnimations::BeginPlay()
  19. {
  20.     Super::BeginPlay();
  21.     Init();
  22.     // ...
  23.    
  24. }
  25.  
  26.  
  27. // Called every frame
  28. void UClockworkAnimations::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  29. {
  30.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  31.     // ...
  32. }
  33.  
  34. void UClockworkAnimations::SetFrequency(float frequency) {
  35.     this->frequency = frequency;
  36. }
  37.  
  38. void UClockworkAnimations::Init() {
  39.     const UDataTable* data = this->ClockworkAnimationData.DataTable;
  40.     TArray<TArray<FString>>rows = data->GetTableData();
  41.     for (int32 i = 1; i != rows.Num(); ++i) {
  42.         //-----------------------parse data
  43.         TArray<FString> row = rows[i];
  44.         float period = FCString::Atof(*row[3]);
  45.         float opening = FCString::Atof(*row[4]);
  46.         const TCHAR* Delims[] = { TEXT("="), TEXT(","), TEXT(")") };
  47.         TArray<FString> Parsed;
  48.         row[5].ParseIntoArray(Parsed, Delims, 3);
  49.         FVector axis(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
  50.         row[6].ParseIntoArray(Parsed, Delims, 3);
  51.         FVector deformationAxis(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
  52.         row[7].ParseIntoArray(Parsed, Delims, 3);
  53.         FVector deformFactor(FCString::Atof(*Parsed[1]), FCString::Atof(*Parsed[3]), FCString::Atof(*Parsed[5]));
  54.         //------------------------find reference in scene
  55.         UStaticMeshComponent* staticComponent = nullptr;
  56.         USceneComponent* sceneComponent = nullptr;
  57.         //iterate through all StaticMeshComponents in scene
  58.         for (TObjectIterator<UStaticMeshComponent> Itr; Itr; ++Itr)
  59.         {
  60.             UStaticMeshComponent *Component = *Itr;
  61.             if (row[0].Equals(Itr->GetName())) {
  62.                 UE_LOG(LogTemp, Warning, TEXT("Actor name: %s"), *Itr->GetName());
  63.                 staticComponent = *Itr;
  64.             }
  65.         }
  66.         if (staticComponent == nullptr) {
  67.             //iterate through all SceneComponents in scene
  68.             for (TObjectIterator<USceneComponent> Itr; Itr; ++Itr)
  69.             {
  70.                 USceneComponent *Component = *Itr;
  71.                 if (row[0].Equals(Itr->GetName())) {
  72.                     UE_LOG(LogTemp, Warning, TEXT("Actor name: %s"), *Itr->GetName());
  73.                     sceneComponent = *Itr;
  74.                 }
  75.             }
  76.         }
  77.         else {
  78.             sceneComponent = staticComponent;
  79.         }
  80.         //--------------------------populate local array of mobvable pieces
  81.         MovablePiece piece(row[0], row[1], row[2], period, opening, axis, deformationAxis, deformFactor, sceneComponent);
  82.         piece.initStep(this->frequency);
  83.         this->pieces.Add(piece);
  84.     }
  85.     UE_LOG(LogTemp, Warning, TEXT("Number of MovablePieces: %d"), pieces.Num());
  86. }
  87.  
  88. void UClockworkAnimations::AnimateParts() {
  89.     for (MovablePiece piece : this->pieces) {
  90.         if (piece.type.Equals("OC")) {
  91.             OscillatingRotation(piece.sceneComponent, piece.axis, piece.opening, 0);
  92.         }
  93.         else if (piece.type.Equals("RS")) {
  94.             //UE_LOG(LogTemp, Warning, TEXT("This is RS"));
  95.             JerkedRotation(piece);
  96.         }
  97.         else if (piece.type.Equals("AC")) {
  98.             //UE_LOG(LogTemp, Warning, TEXT("This is AC"));
  99.         }
  100.     }
  101. }
  102.  
  103. void UClockworkAnimations::PubOscillatingRotation(USceneComponent* target, FVector axis, float amplitude, float phase) {
  104.     double time = UClockworkAnimations::simulationTime();
  105.     float angle = amplitude*FMath::Sin(2 * PI*this->frequency*time + phase);
  106.     FQuat outQuat(axis, FMath::DegreesToRadians(angle));
  107.     FRotator outRot(outQuat);
  108.     target->SetRelativeRotation(outRot);
  109.     UE_LOG(LogTemp, Warning, TEXT("This is OC"));
  110. }
  111.  
  112. void UClockworkAnimations::OscillatingRotation(USceneComponent* target, FVector axis, float amplitude, float phase) {
  113.     double time = UClockworkAnimations::simulationTime();
  114.     float angle = amplitude*FMath::Sin(2 * PI*frequency*time + phase);
  115.     FQuat outQuat(axis, FMath::DegreesToRadians(angle));
  116.     FRotator outRot(outQuat);
  117.     target->SetRelativeRotation(outRot);
  118. }
  119.  
  120. void UClockworkAnimations::AnchorMovement(USceneComponent* target, FVector axis, float amplitude, float phase) {
  121.     double time = UClockworkAnimations::simulationTime();
  122.     float angle = FMath::Sin(2 * PI*frequency*time + phase);
  123.     if (FMath::IsNearlyEqual(angle, 1.0f, 0.1f)) {
  124.         FQuat outQuat(axis, FMath::DegreesToRadians(amplitude));
  125.         FRotator outRot(outQuat);
  126.         target->SetRelativeRotation(FMath::Lerp(target->RelativeRotation, outRot, amplitude*0.1));
  127.     }
  128.     else if (FMath::IsNearlyEqual(angle, -1.0f, 0.1f)) {
  129.         FQuat outQuat(axis, FMath::DegreesToRadians(-amplitude));
  130.         FRotator outRot(outQuat);
  131.         target->SetRelativeRotation(FMath::Lerp(target->RelativeRotation, outRot, amplitude*0.1));
  132.     }
  133. }
  134.  
  135. void UClockworkAnimations::JerkedRotation(MovablePiece& piece) {
  136.     double time = UClockworkAnimations::simulationTime();
  137.     float impulse = FMath::Sin(time*PI*frequency);
  138.     if (FMath::IsNearlyEqual(impulse, 1.0f, 0.2f) || FMath::IsNearlyEqual(impulse, -1.0f, 0.2f)) {
  139.         FQuat outQuat(piece.axis, FMath::DegreesToRadians(piece.currentStep));
  140.         piece.incrementStep();
  141.         FRotator outRot(outQuat);
  142.         piece.sceneComponent->SetRelativeRotation(FMath::InterpEaseOut(piece.sceneComponent->RelativeRotation, outRot, (FApp::GetDeltaTime() / (1 / frequency)) * UClockworkAnimations::timeRatio, 3.0));
  143.     }
  144. }
  145.  
  146. double UClockworkAnimations::simulationTime()
  147. {
  148.     FDateTime dtTime = FDateTime::Now();
  149.     double dtCurrent = dtTime.GetHour() * 3600 + dtTime.GetMinute() * 60 + dtTime.GetSecond() + dtTime.GetMillisecond() * 0.001;
  150.     return dtCurrent *UClockworkAnimations::timeRatio;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement