Derive gravity and jump velocity from time in air and max height

This commit is contained in:
Kevin Poretti 2022-03-15 22:05:50 -04:00
parent 493cca3fee
commit 057c929e5f
5 changed files with 111 additions and 42 deletions

Binary file not shown.

BIN
SwordNGun/Content/Maps/MovementTest.umap (Stored with Git LFS)

Binary file not shown.

View File

@ -205,11 +205,13 @@ void ASNGCharacterBase::Tick(float DeltaTime)
// Debug
if(CharacterMovementDebug)
{
float LateralSpeed = GetVelocity().Size2D();
FVector StartPos(GetActorLocation().X,
GetActorLocation().Y,
GetActorLocation().Z - GetCapsuleComponent()->GetScaledCapsuleHalfHeight());
FVector ActorForward = UKismetMathLibrary::GetForwardVector(GetActorRotation());
DrawDebugDirectionalArrow(GetWorld(), StartPos, StartPos + (ActorForward * 100.0f),
DrawDebugDirectionalArrow(GetWorld(),
StartPos, StartPos + (ActorForward * ((LateralSpeed / GetCharacterMovement()->MaxWalkSpeed) * 100.0f)),
10.0f, FColor::Blue, false, -1.0f, 0, 2.0f);
FVector CurrInputDirection = GetInputAsWorldDirection();
@ -220,7 +222,7 @@ void ASNGCharacterBase::Tick(float DeltaTime)
FString DebugMsg = FString::Printf(TEXT("MaxWalkSpeed: %f"), GetCharacterMovement()->MaxWalkSpeed);
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Magenta, DebugMsg);
float LateralSpeed = GetVelocity().Size2D();
DebugMsg = FString::Printf(TEXT("LateralSpeed: %f"), LateralSpeed);
GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Magenta, DebugMsg);

View File

@ -4,21 +4,21 @@
#include "Components/SNGCharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "PhysicsEngine/PhysicsSettings.h"
USNGCharacterMovementComponent::USNGCharacterMovementComponent()
{
// Defaults
DefaultGroundFriction = 8.0f;
DefaultGravityScale = 1.0f;
DefaultMaxWalkSpeed = 600.0f;
DefaultRotationRate = FRotator(0.0f, 360.0f, 0.0f);
// sprint
SprintingMaxWalkSpeed = 800.0f;
SprintLateralSpeedThreshold = 300.0f;
SprintingMaxWalkSpeed = 1000.0f;
SprintLateralSpeedThreshold = 500.0f;
SprintingRotationRate = FRotator(0.0f, 180.0f, 0.0f);
TimeToInitiateSprint = 4.0f;
WalkToSprintInterpSpeed = 1.0f;
TimeToInitiateSprint = 2.0f;
WalkToSprintInterpSpeed = 4.0f;
// firing
FiringMaxWalkSpeed = 300.0f;
@ -26,11 +26,16 @@ USNGCharacterMovementComponent::USNGCharacterMovementComponent()
// reloading
ReloadingMaxWalkSpeed = 300.0f;
// Set defaults dealing with jumping and being airborne
DefaultJumpForce = 1000.0f;
DefaultAirFriction = 150.0f;
DefaultAirControl = 0.35;
// airborne defaults
DefaultAirControl = 0.3f;
// jump
MaxJumpHeight = 250.0f;
DesiredJumpDuration = 0.75f;
ComputerGravityScaleAndJumpZVelocity();
/*
// not used...yet
MinJumpHeight = 200.0f;
MaxJumpTime = 0.2f;
@ -38,6 +43,13 @@ USNGCharacterMovementComponent::USNGCharacterMovementComponent()
MinAllowedDashHeight = 175.0f;
MinAllowedAttackHeight = 175.0f;
*/
// Character Movement component defaults
SetWalkableFloorAngle(45.622231f);
AirControlBoostMultiplier = 0.0f;
AirControlBoostVelocityThreshold = 0.0f;
}
/**
@ -50,16 +62,48 @@ void USNGCharacterMovementComponent::RestoreMovementDefaults()
GravityScale = DefaultGravityScale;
RotationRate = DefaultRotationRate;
MaxWalkSpeed = DefaultMaxWalkSpeed;
BrakingDecelerationFalling = DefaultAirFriction;
AirControl = DefaultAirControl;
JumpZVelocity = DefaultJumpForce;
JumpZVelocity = DefaultJumpZVelocity;
}
#if WITH_EDITOR
void USNGCharacterMovementComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FProperty* PropertyThatChanged = PropertyChangedEvent.MemberProperty;
if (PropertyThatChanged && PropertyThatChanged->GetFName() == GET_MEMBER_NAME_CHECKED(USNGCharacterMovementComponent, MaxJumpHeight) ||
PropertyThatChanged && PropertyThatChanged->GetFName() == GET_MEMBER_NAME_CHECKED(USNGCharacterMovementComponent, DesiredJumpDuration))
{
// Compute WalkableFloorZ from the Angle.
ComputerGravityScaleAndJumpZVelocity();
}
}
void USNGCharacterMovementComponent::PostLoad()
{
Super::PostLoad();
// Compute gravity scale and jump z velocity for first time
ComputerGravityScaleAndJumpZVelocity();
}
#endif // WITH_EDITOR
void USNGCharacterMovementComponent::OnSprintTimer()
{
bIsSprinting = true;
}
void USNGCharacterMovementComponent::ComputerGravityScaleAndJumpZVelocity()
{
float HalfJumpDuration = DesiredJumpDuration / 2.0f;
DefaultJumpZVelocity = (2 * MaxJumpHeight) / HalfJumpDuration;
float GameGravity = UPhysicsSettings::Get()->DefaultGravityZ;
float TargetGravity = (-2 * MaxJumpHeight) / (HalfJumpDuration * HalfJumpDuration);
DefaultGravityScale = TargetGravity / GameGravity;
}
void USNGCharacterMovementComponent::StartJump()
{
/*

View File

@ -20,6 +20,8 @@ class SWORDNGUN_API USNGCharacterMovementComponent : public UCharacterMovementCo
public:
USNGCharacterMovementComponent();
virtual void PostLoad() override;
/**
* Instantly sets all movement parameters to their defaults values (i.e. ground friction to DefaultGroundFriction,
* MaxWalkSpeed to DefaultMaxWalkSpeed, etc.)
@ -33,39 +35,45 @@ public:
UFUNCTION(BlueprintCallable)
FORCEINLINE bool GetIsSprinting() { return bIsSprinting; }
#if WITH_EDITOR
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif // WITH_EDITOR
protected:
// default ground
/** Default friction when not performing any special actions and on the ground */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultGroundFriction;
/** Default ground walk speed when not performing any special actions */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultMaxWalkSpeed;
/** Default ground walk speed when not performing any special actions */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Defaults", meta=(ClampMin="0", UIMin="0"))
FRotator DefaultRotationRate;
// sprinting
/** Ground walk speed when sprinting */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Sprint", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Sprint", meta=(ClampMin="0", UIMin="0"))
float SprintingMaxWalkSpeed;
/** Lateral speed the player needs to maintain to stay in the sprinting state */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Sprint", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Sprint", meta=(ClampMin="0", UIMin="0"))
float SprintLateralSpeedThreshold;
/** Default ground walk speed when not performing any special actions */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Sprint", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Sprint", meta=(ClampMin="0", UIMin="0"))
FRotator SprintingRotationRate;
/** How long after applying movement input should the character start sprinting? */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Sprint", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Sprint", meta=(ClampMin="0", UIMin="0"))
float TimeToInitiateSprint;
/** The speed in which the max walk speed to interpolated to the max sprint speed when a sprint has been initiated */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Ground.Sprint", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Ground|Sprint", meta=(ClampMin="0", UIMin="0"))
float WalkToSprintInterpSpeed;
FTimerHandle TimerHandle_SprintTimer;
@ -77,34 +85,53 @@ protected:
// firing
/** Ground walk speed when firing a ranged weapon */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Firing", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Firing", meta=(ClampMin="0", UIMin="0"))
float FiringMaxWalkSpeed;
// reloading
/** Ground walk speed when reloading a ranged weapon */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Reloading", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Reloading", meta=(ClampMin="0", UIMin="0"))
float ReloadingMaxWalkSpeed;
// airborne
/** Gravity scale when not performing any special actions */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Air.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="SNG Character Movement|Air|Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultGravityScale;
/** Force to continuously apply to character when they are holding the jump button */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Air.Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultJumpForce;
/** How much to dampen lateral velocities at the start of a jump */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Defaults", meta=(ClampMin="0", UIMin="0"))
float LateralVelocityDampening;
/**
* How high (Z) is the character able to reach when holding the jump button for the maximum allowed time.
*
* This is used in conjunction with DesiredJumpDuration to derive DefaultGravityScale and JumpZVelocity.
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Jump", meta=(ClampMin="0", UIMin="0"))
float MaxJumpHeight;
/**
* How long the character should be in the air for when performing a single jump and holding the jump button to reach
* the maximum jump height.
*
* This is used in conjunction with MaxJumpHeight to derive DefaultGravityScale and JumpZVelocity.
*/
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Jump", meta=(ClampMin="0", UIMin="0"))
float DesiredJumpDuration;
/** Force to continuously apply to character when they are holding the jump button */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="SNG Character Movement|Air|Jump", meta=(ClampMin="0", UIMin="0"))
float DefaultJumpZVelocity;
/** Minimum height a character must reach before jump force is no longer applied even if the jump input is not being held */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Air.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Jump", meta=(ClampMin="0", UIMin="0"))
float MinJumpHeight;
/** Max time the jump button can be held to give character upward velocity */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Air.Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Jump", meta=(ClampMin="0", UIMin="0"))
float MaxJumpTime;
/** How much to dampen lateral velocities at the start of a jump */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement.Air.Defaults", meta=(ClampMin="0", UIMin="0"))
float LateralVelocityDampening;
void ComputerGravityScaleAndJumpZVelocity();
/**
* Sets up character velocities and jump parameters like the jump timer
@ -117,21 +144,17 @@ protected:
*/
UFUNCTION(BlueprintCallable)
void AddJumpForce();
/** Default friction when not performing any special actions and in the air */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Character Movement: SNG Air Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultAirFriction;
/** Default friction when not performing any special actions and in the air */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Character Movement: SNG Air Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Defaults", meta=(ClampMin="0", UIMin="0"))
float DefaultAirControl;
/** Minimum distance from the ground a character has to be to be able to perform an air dash */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Character Movement: SNG Air Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Defaults", meta=(ClampMin="0", UIMin="0"))
float MinAllowedDashHeight;
/** Minimum distance from the ground a character has to be to be able to perform an air attack */
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Character Movement: SNG Air Defaults", meta=(ClampMin="0", UIMin="0"))
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="SNG Character Movement|Air|Defaults", meta=(ClampMin="0", UIMin="0"))
float MinAllowedAttackHeight;
virtual void BeginPlay() override;