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

View File

@ -4,21 +4,21 @@
#include "Components/SNGCharacterMovementComponent.h" #include "Components/SNGCharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h" #include "Kismet/KismetMathLibrary.h"
#include "PhysicsEngine/PhysicsSettings.h"
USNGCharacterMovementComponent::USNGCharacterMovementComponent() USNGCharacterMovementComponent::USNGCharacterMovementComponent()
{ {
// Defaults // Defaults
DefaultGroundFriction = 8.0f; DefaultGroundFriction = 8.0f;
DefaultGravityScale = 1.0f;
DefaultMaxWalkSpeed = 600.0f; DefaultMaxWalkSpeed = 600.0f;
DefaultRotationRate = FRotator(0.0f, 360.0f, 0.0f); DefaultRotationRate = FRotator(0.0f, 360.0f, 0.0f);
// sprint // sprint
SprintingMaxWalkSpeed = 800.0f; SprintingMaxWalkSpeed = 1000.0f;
SprintLateralSpeedThreshold = 300.0f; SprintLateralSpeedThreshold = 500.0f;
SprintingRotationRate = FRotator(0.0f, 180.0f, 0.0f); SprintingRotationRate = FRotator(0.0f, 180.0f, 0.0f);
TimeToInitiateSprint = 4.0f; TimeToInitiateSprint = 2.0f;
WalkToSprintInterpSpeed = 1.0f; WalkToSprintInterpSpeed = 4.0f;
// firing // firing
FiringMaxWalkSpeed = 300.0f; FiringMaxWalkSpeed = 300.0f;
@ -26,11 +26,16 @@ USNGCharacterMovementComponent::USNGCharacterMovementComponent()
// reloading // reloading
ReloadingMaxWalkSpeed = 300.0f; ReloadingMaxWalkSpeed = 300.0f;
// Set defaults dealing with jumping and being airborne // airborne defaults
DefaultJumpForce = 1000.0f; DefaultAirControl = 0.3f;
DefaultAirFriction = 150.0f;
DefaultAirControl = 0.35;
// jump
MaxJumpHeight = 250.0f;
DesiredJumpDuration = 0.75f;
ComputerGravityScaleAndJumpZVelocity();
/*
// not used...yet
MinJumpHeight = 200.0f; MinJumpHeight = 200.0f;
MaxJumpTime = 0.2f; MaxJumpTime = 0.2f;
@ -38,6 +43,13 @@ USNGCharacterMovementComponent::USNGCharacterMovementComponent()
MinAllowedDashHeight = 175.0f; MinAllowedDashHeight = 175.0f;
MinAllowedAttackHeight = 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; GravityScale = DefaultGravityScale;
RotationRate = DefaultRotationRate; RotationRate = DefaultRotationRate;
MaxWalkSpeed = DefaultMaxWalkSpeed; MaxWalkSpeed = DefaultMaxWalkSpeed;
BrakingDecelerationFalling = DefaultAirFriction;
AirControl = DefaultAirControl; 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() void USNGCharacterMovementComponent::OnSprintTimer()
{ {
bIsSprinting = true; 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() void USNGCharacterMovementComponent::StartJump()
{ {
/* /*

View File

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