Skip to content

Instantly share code, notes, and snippets.

@braustin20
Last active October 8, 2019 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braustin20/96ccb93d64ce045aa19aee43319e452b to your computer and use it in GitHub Desktop.
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
#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