Skip to content

Instantly share code, notes, and snippets.

@maxattack
Last active May 7, 2023 04:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxattack/3d69ba29e2f4461d4c1974ea64b444b0 to your computer and use it in GitHub Desktop.
Save maxattack/3d69ba29e2f4461d4c1974ea64b444b0 to your computer and use it in GitHub Desktop.
Sample Code Listing for "Procedural Animation: Locomotion (Part 2)"
/*
Copyright 2023 Max Kaufmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Sample Code for: https://blog.littlepolygon.com/posts/loco2/
#include "LocoPawn.h"
#include "Components/SkeletalMeshComponent.h"
namespace LocoUtil
{
static const FName NAME_MoveX( "MoveX" );
static const FName NAME_MoveY( "MoveY" );
static const FName NAME_CamX( "CamX" );
void DampSpring( float& Value, float& Speed, float Target, float Duration, float DeltaTime )
{
const float Omega = 2.0f / Duration;
const float Exp = FMath::Exp( -Omega * DeltaTime );
const float Change = Value - Target;
const float Temp = ( Speed + Change * Omega ) * DeltaTime;
Speed = ( Speed - Temp * Omega ) * Exp;
Value = Target + ( Change + Temp ) * Exp;
}
float StepArc( float X )
{
float _X = ( 1.0f - X );
return 9.481481481f * _X * _X * _X * X;
}
}
ALocoPawn::ALocoPawn()
{
static const FName NAME_SkelComponent( "SkelComponent" );
Skel = CreateDefaultSubobject< USkeletalMeshComponent >( NAME_SkelComponent );
RootComponent = Skel;
}
/*virtual*/ void ALocoPawn::SetupPlayerInputComponent( UInputComponent* Comp ) /*override*/
{
Super::SetupPlayerInputComponent( Comp );
Comp->BindAxis( LocoUtil::NAME_MoveX );
Comp->BindAxis( LocoUtil::NAME_MoveY );
Comp->BindAxis( LocoUtil::NAME_CamX );
}
/*virtual*/ void ALocoPawn::BeginPlay() /*override*/
{
Super::BeginPlay();
/* Initialize Movement */
Position = GetActorLocation();
Position.Z = 0.0f;
Velocity = FVector( 0.0f );
Yaw = TargetYaw = GetActorRotation().Yaw;
SmoothLean.Reset();
SetActorLocationAndRotation( Position, FRotator( 0, Yaw, 0 ) );
/* Initialize feet */
FootL.Init( *this );
FootR.Init( *this );
}
/*virtual*/ void ALocoPawn::Tick( float DeltaTime ) /*override*/
{
Super::Tick( DeltaTime );
/* Update Camera Rotation */
AddControllerYawInput( DeltaTime * CameraSpeed * GetInputAxisValue( LocoUtil::NAME_CamX ) );
FRotator ControlRotation ( 0, GetControlRotation().Yaw, 0 );
/* Update Root Movement */
FVector Stick( GetInputAxisValue( LocoUtil::NAME_MoveY ), GetInputAxisValue( LocoUtil::NAME_MoveX ), 0.0f );
Stick = ControlRotation.RotateVector( Stick ).GetClampedToMaxSize( 1.0f );
float StickTilt = Stick.Size();
Accel = FVector( 0.0f );
if( StickTilt > 0.35f )
{
float AccelFriction = 4.0f / AccelSeconds;
Accel -= AccelFriction * Velocity;
Accel += AccelFriction * MovementSpeed * Stick;
TargetYaw = FMath::RadiansToDegrees( FMath::Atan2( Stick.Y, Stick.X ) );
}
else
{
float DecelFriction = 4.0f / DecelSeconds;
Accel -= DecelFriction * Velocity;
}
Velocity += Accel * DeltaTime;
Position += Velocity * DeltaTime;
float NormalizedSpeed = FMath::Min( 1.0f, Velocity.Size() / MovementSpeed );
float PrevYaw = Yaw;
if( NormalizedSpeed > 0.05f )
{
float DeltaAngle = FRotator::NormalizeAxis( TargetYaw - Yaw );
LocoUtil::DampSpring( Yaw, YawSpeed, Yaw + DeltaAngle, RotationSeconds, NormalizedSpeed * DeltaTime );
Yaw = FRotator::NormalizeAxis( Yaw );
}
FVector Lean = FVector::UpVector ^ Accel;
float LeanAmount = Lean.Size();
FQuat TargetLean = FQuat::Identity;
if( LeanAmount > 0.1f )
{
FVector LeanAxis = Lean / LeanAmount;
float LeanAngle = LeanMulti * ( LeanAmount / 100.0f );
LeanAngle = FMath::Min( LeanAngle, MaxLeanAngle );
TargetLean = FQuat( LeanAxis, FMath::DegreesToRadians( LeanAngle ) );
}
SmoothLean.Update( TargetLean, LeanSmoothingSeconds, DeltaTime );
FQuat Rotation = SmoothLean.Value * FRotator( 0, Yaw, 0 ).Quaternion();
SetActorLocationAndRotation( Position, Rotation );
/* Update Hip Offset */
float HipPlayRate = FMath::Lerp( 0.25f, 1.0f, NormalizedSpeed );
LocoUtil::DampSpring( HipMulti, HipMultiSpeed, StickTilt, HipAmplitudeDampSeconds, DeltaTime );
if( HipMulti < 0.001f )
HipPhase = 0;
else
HipPhase += HipPhaseSpeed * HipPlayRate * DeltaTime;
HipOffset.Z = HipMulti * ( HipBiasZ + HipOffsetZ * FMath::Sin( HipPhase * 2.0f * PI ) );
HipRotation.Roll = HipMulti * HipRoll * FMath::Sin( 0.5f * HipPhase * 2.0f * PI );
FootL.Update( *this, DeltaTime );
FootR.Update( *this, DeltaTime );
}
void ALocoPawn::FootController::Init( ALocoPawn& Pawn )
{
const FTransform& ToWorld = Pawn.GetActorTransform();
RestPos = RefPos( Pawn );
PinnedPos = ToWorld.TransformPosition( RestPos );
bPinned = true;
}
void ALocoPawn::FootController::Update( ALocoPawn& Pawn, float DeltaTime )
{
const FTransform& ToWorld = Pawn.GetActorTransform();
FVector& Pos = RefPos( Pawn );
float RadiansOffset = IsLeft( Pawn ) ? 0.0f : PI;
float Radians = FMath::Fmod( 0.5f * Pawn.HipPhase * 2.0f * PI + RadiansOffset, 2.0f * PI );
Pos = ToWorld.TransformPosition( RestPos );
Pos += Pawn.StepExtrap * Pawn.Velocity;
Pos.Z = 0.0f;
bool bWantPin = Radians >= PI;
if( bPinned != bWantPin )
{
if( bWantPin )
PinnedPos = Pos;
else
PinnedPos = ToWorld.InverseTransformPosition( PinnedPos );
bPinned = bWantPin;
}
if( bPinned )
{
Pos = ToWorld.InverseTransformPosition( PinnedPos );
}
else
{
float X = FMath::Min( 1.0f, Radians / PI );
Pos.Z += Pawn.StepHeight * Pawn.HipMulti * LocoUtil::StepArc( X );
Pos = FMath::Lerp( PinnedPos, ToWorld.InverseTransformPosition( Pos ), X );
}
}
/*
Copyright 2023 Max Kaufmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Sample Code for: https://blog.littlepolygon.com/posts/loco2/
#pragma once
#include "GameFramework/Pawn.h"
#include "MovementUtil.h"
#include "LocoPawn.generated.h"
UCLASS( Abstract )
class ALocoPawn : public APawn
{
GENERATED_BODY()
public:
/* Components */
UPROPERTY( VisibleAnywhere, BlueprintReadOnly )
class USkeletalMeshComponent* Skel;
/* Movement Configuration */
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float MovementSpeed = 400.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float AccelSeconds = 0.5f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float DecelSeconds = 0.25f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float RotationSeconds = 0.1f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float CameraSpeed = 90.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float LeanMulti = 0.65f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float MaxLeanAngle = 45.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float LeanSmoothingSeconds = 0.25f;
/* Hip Configuration */
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float HipPhaseSpeed = 3.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float HipAmplitudeDampSeconds = 0.5f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float HipOffsetZ = 15.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float HipBiasZ = -17.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float HipRoll = 8.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float StepHeight = 75.0f;
UPROPERTY( EditDefaultsOnly, BlueprintReadWrite )
float StepExtrap = 0.15;
/* Outputs to Animation Instance */
UPROPERTY( BlueprintReadOnly )
FVector FootPosL = FVector( 0, -30, 0 );
UPROPERTY( BlueprintReadOnly )
FVector FootPosR = FVector( 0, 30, 0 );
UPROPERTY( BlueprintReadOnly )
FVector HipOffset = FVector( 0, 0, 0 );
UPROPERTY( BlueprintReadOnly )
FRotator HipRotation = FRotator( 0, 0, 0 );
ALocoPawn();
/* APawn Interface */
virtual void SetupPlayerInputComponent( UInputComponent* Comp ) override;
virtual void BeginPlay() override;
virtual void Tick( float DeltaTime ) override;
private:
/* Movement State */
FVector Position, Velocity = FVector( 0.0f ), Accel = FVector( 0.0f );
float TargetYaw, Yaw, YawSpeed = 0.0f;
/* Leaning State */
FDampedQuat SmoothLean;
/* Hip State */
float HipPhase = 0.0f;
float HipMulti = 0.0f;
float HipMultiSpeed = 0.0f;
/* Foot Controller */
struct FootController
{
FVector RestPos;
FVector PinnedPos;
bool bPinned;
bool IsLeft( const ALocoPawn& Pawn ) const { return this == &Pawn.FootL; }
const FootController& OtherFoot( const ALocoPawn& Pawn ) const { return IsLeft( Pawn ) ? Pawn.FootR : Pawn.FootL; }
FVector& RefPos( ALocoPawn& Pawn ) const { return IsLeft( Pawn ) ? Pawn.FootPosL : Pawn.FootPosR; }
void Init( ALocoPawn& Pawn );
void Update( ALocoPawn& Pawn, float DeltaTime );
};
FootController FootL;
FootController FootR;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment