diff --git a/Content/Blueprints/BP_SpaceshipPawn.uasset b/Content/Blueprints/BP_SpaceshipPawn.uasset index 23da8d2..9c01f45 100644 --- a/Content/Blueprints/BP_SpaceshipPawn.uasset +++ b/Content/Blueprints/BP_SpaceshipPawn.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4dc1493b9ecbddf8cd071544d5146380e62f11bb3053cb2d1e043787875c6bac -size 33448 +oid sha256:383453441cf057084b8f8af9f79580d7a51aaad1dae90912ccccd5ca61a6dfef +size 33608 diff --git a/Source/MyProject3/SpaceshipPawn.cpp b/Source/MyProject3/SpaceshipPawn.cpp index 1117057..55cc655 100644 --- a/Source/MyProject3/SpaceshipPawn.cpp +++ b/Source/MyProject3/SpaceshipPawn.cpp @@ -82,30 +82,6 @@ void ASpaceshipPawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); - // Handle thrust - if (!bThrottlePressed) - { - CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration); - } - else - { - CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); - } - - // Update movement based on ship's forward vector - FVector ThrustDirection = GetActorForwardVector(); - FVector ThrustForce = ThrustDirection * CurrentThrust; - - // Apply drag - FVector DragForce = -CurrentVelocity * DragCoefficient; - - // Update velocity - CurrentVelocity += (ThrustForce + DragForce) * DeltaTime; - - // Update position - FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime); - SetActorLocation(NewLocation, true); - // Debug info if (GEngine) { @@ -140,6 +116,67 @@ void ASpaceshipPawn::Tick(float DeltaTime) FQuat NewQuat = FQuat::Slerp(CurrentQuat, TargetQuat, RotationSpeed * DeltaTime); SetActorRotation(NewQuat); + // Get the current forward vector + FVector CurrentDirection = GetActorForwardVector(); + float CurrentSpeed = CurrentVelocity.Size(); + + // Handle thrust and velocity direction + if (!bThrottlePressed) + { + // Decelerate when not thrusting + CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration); + bWasThrottlePressed = false; + + // Maintain current velocity direction when not thrusting + DesiredVelocityDirection = CurrentVelocity.GetSafeNormal(); + } + else + { + // Accelerate when thrusting + CurrentThrust = FMath::FInterpConstantTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); + + if (!bWasThrottlePressed) + { + // Just started thrusting - blend between current and new direction + if (!CurrentVelocity.IsNearlyZero()) + { + // Blend between current velocity direction and new direction + FVector CurrentDir = CurrentVelocity.GetSafeNormal(); + DesiredVelocityDirection = FMath::Lerp(CurrentDir, CurrentDirection, 1.0f - DirectionalInertia); + DesiredVelocityDirection.Normalize(); + } + else + { + // If nearly stationary, use new direction + DesiredVelocityDirection = CurrentDirection; + } + } + else + { + // Continuously blend towards current facing direction while thrusting + DesiredVelocityDirection = FMath::VInterpNormalRotationTo( + DesiredVelocityDirection, + CurrentDirection, + DeltaTime, + VelocityAlignmentSpeed + ); + } + bWasThrottlePressed = true; + } + + // Calculate thrust force + FVector ThrustForce = CurrentDirection * CurrentThrust; + + // Apply drag + FVector DragForce = -CurrentVelocity * DragCoefficient; + + // Update velocity with forces + CurrentVelocity += (ThrustForce + DragForce) * DeltaTime; + + // Update position + FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime); + SetActorLocation(NewLocation, true); + // Update spring arm rotation to match ship with smooth interpolation if (CameraSpringArm) { diff --git a/Source/MyProject3/SpaceshipPawn.h b/Source/MyProject3/SpaceshipPawn.h index e19f64a..876de86 100644 --- a/Source/MyProject3/SpaceshipPawn.h +++ b/Source/MyProject3/SpaceshipPawn.h @@ -60,6 +60,13 @@ protected: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") float DragCoefficient = 0.05f; + // How quickly velocity aligns with new direction + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "10.0")) + float VelocityAlignmentSpeed = 4.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement", meta = (ClampMin = "0.0", ClampMax = "1.0", ToolTip = "How much of the current velocity is maintained when changing direction (0 = none, 1 = full)")) + float DirectionalInertia = 0.3f; + // Input functions void HandleThrottleStarted(const FInputActionValue& Value); void HandleThrottleReleased(const FInputActionValue& Value); @@ -81,4 +88,7 @@ private: FVector2D MouseDeltaSmoothed; FVector2D LastMouseDelta; float MouseSmoothingSpeed = 10.0f; + + bool bWasThrottlePressed; + FVector DesiredVelocityDirection; }; \ No newline at end of file