Last active
October 8, 2019 21:51
-
-
Save braustin20/96ccb93d64ce045aa19aee43319e452b to your computer and use it in GitHub Desktop.
Unreal 4 surveyor wheel which calculates procedural run cycle keyframe based on distance travelled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SurveyorWheelComponent.h" | |
#include "GameFramework/Actor.h" | |
USurveyorWheelComponent::USurveyorWheelComponent() | |
{ | |
PrimaryComponentTick.bCanEverTick = true; | |
WheelRadius = GetWheelRadius(); | |
} | |
void USurveyorWheelComponent::InitializeComponent() | |
{ | |
WheelRadius = GetWheelRadius(); | |
} | |
// Called when the game starts | |
void USurveyorWheelComponent::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
} | |
// Called every frame | |
void USurveyorWheelComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) | |
{ | |
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); | |
int32 lastStepState = StepState; | |
lastPosition = FVector(lastPosition.X, lastPosition.Y, 0.0f); | |
FVector currentPosition = FVector(GetOwner()->GetActorLocation().X, GetOwner()->GetActorLocation().Y, 0.0f); | |
float distance = FVector::Dist(lastPosition, currentPosition); | |
TurnAngle = (distance / (2 * PI * GetWheelRadius())) * 360.0f; | |
angleCounter += TurnAngle; | |
if (angleCounter > StepDistance) | |
{ | |
angleCounter = 0.0f; | |
} | |
if (angleCounter > GetWheelRadius()) | |
{ | |
angleCounter = FMath::Fmod(angleCounter, GetWheelRadius()); | |
} | |
StepState = angleCounter / GetWheelRadius(); | |
if (StepState != lastStepState) | |
{ | |
lastStepState = StepState; | |
} | |
lastPosition = currentPosition; | |
} | |
float USurveyorWheelComponent::GetWheelRadius() | |
{ | |
return (StepDistance * 4) / (PI * 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment