Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unreal 4 surveyor wheel which calculates procedural run cycle keyframe based on distance travelled
#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