From 2519db2cedf854467781fc9ee2d210726ed5a33c Mon Sep 17 00:00:00 2001 From: aicorr Date: Sun, 16 Feb 2025 14:43:13 +0530 Subject: [PATCH] Implement better throttle system --- Content/Blueprints/BP_SpaceshipPawn.uasset | 4 +- Content/Input/IA_MouseControl.uasset | 3 + Content/Input/IA_Throttle.uasset | 3 + Content/Input/IMC_Spaceship.uasset | 4 +- Source/MyProject3/SpaceshipPawn.cpp | 121 +++++++++++++++------ Source/MyProject3/SpaceshipPawn.h | 30 ++++- 6 files changed, 121 insertions(+), 44 deletions(-) create mode 100644 Content/Input/IA_MouseControl.uasset create mode 100644 Content/Input/IA_Throttle.uasset diff --git a/Content/Blueprints/BP_SpaceshipPawn.uasset b/Content/Blueprints/BP_SpaceshipPawn.uasset index 23a4580..5894fea 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:2606d9418f7c4e02eb95a46fde742e7f6eb4601d8e5834b85f7d12680f775bd6 -size 33156 +oid sha256:13d19fbcc67188e59de7270ecb5fe422c57c0816d8a750f12b1877090e6c6355 +size 33194 diff --git a/Content/Input/IA_MouseControl.uasset b/Content/Input/IA_MouseControl.uasset new file mode 100644 index 0000000..3cb5c29 --- /dev/null +++ b/Content/Input/IA_MouseControl.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d054bd77ddf158b2409f13e909a2cf42ca1a27020328dd2e2eb1e28bd3462fad +size 2118 diff --git a/Content/Input/IA_Throttle.uasset b/Content/Input/IA_Throttle.uasset new file mode 100644 index 0000000..c262f5c --- /dev/null +++ b/Content/Input/IA_Throttle.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:832406a13455ff4a9a5bfca92bc63d67064cd534d80b86120bf737af9f56fa76 +size 2249 diff --git a/Content/Input/IMC_Spaceship.uasset b/Content/Input/IMC_Spaceship.uasset index 5b06029..ad723fc 100644 --- a/Content/Input/IMC_Spaceship.uasset +++ b/Content/Input/IMC_Spaceship.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ad83c697fe1b6ec82089077ac643167c9b038aa7f73001e13385e20aabecc8a9 -size 7104 +oid sha256:aaae1086867188388e75ae7dc59d97ac8738922e3de37fa72102cc35683ca122 +size 5640 diff --git a/Source/MyProject3/SpaceshipPawn.cpp b/Source/MyProject3/SpaceshipPawn.cpp index 71e0d72..27ad47a 100644 --- a/Source/MyProject3/SpaceshipPawn.cpp +++ b/Source/MyProject3/SpaceshipPawn.cpp @@ -10,20 +10,32 @@ ASpaceshipPawn::ASpaceshipPawn() { PrimaryActorTick.bCanEverTick = true; - // Create and setup the ship's mesh + // Create ship mesh ShipMesh = CreateDefaultSubobject(TEXT("ShipMesh")); RootComponent = ShipMesh; + ShipMesh->SetSimulatePhysics(false); + ShipMesh->SetEnableGravity(false); - // Create and setup the camera spring arm + // Create camera spring arm CameraSpringArm = CreateDefaultSubobject(TEXT("CameraSpringArm")); CameraSpringArm->SetupAttachment(RootComponent); CameraSpringArm->TargetArmLength = 400.0f; CameraSpringArm->bEnableCameraLag = true; CameraSpringArm->CameraLagSpeed = 3.0f; + CameraSpringArm->bEnableCameraRotationLag = true; + CameraSpringArm->CameraRotationLagSpeed = 10.0f; - // Create and setup the camera + // Create camera Camera = CreateDefaultSubobject(TEXT("Camera")); Camera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName); + + // Initialize movement variables + CurrentThrust = 0.0f; + TargetThrust = 0.0f; + bThrottlePressed = false; + CurrentVelocity = FVector::ZeroVector; + CurrentRotation = GetActorQuat(); + TargetRotation = GetActorRotation(); } void ASpaceshipPawn::BeginPlay() @@ -44,10 +56,42 @@ void ASpaceshipPawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); - if (!CurrentVelocity.IsZero()) + // Handle thrust deceleration when not pressing throttle + if (!bThrottlePressed) { - FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime); - SetActorLocation(NewLocation); + CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration); + } + else + { + CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); + } + + // Calculate movement + 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); + + // Smooth rotation using quaternion interpolation + FQuat TargetQuat = TargetRotation.Quaternion(); + CurrentRotation = FQuat::Slerp(CurrentRotation, TargetQuat, RotationSpeed * DeltaTime); + SetActorRotation(CurrentRotation); + + // Debug info + if (GEngine) + { + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Yellow, + FString::Printf(TEXT("Thrust: %.2f"), CurrentThrust)); + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Green, + FString::Printf(TEXT("Velocity: %.2f"), CurrentVelocity.Size())); } } @@ -57,45 +101,54 @@ void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputCompo if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(PlayerInputComponent)) { - // Binding the Movement action - EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Move); + // Bind both Hold and Released events for throttle + EnhancedInputComponent->BindAction(ThrottleAction, ETriggerEvent::Started, this, &ASpaceshipPawn::HandleThrottleStarted); + EnhancedInputComponent->BindAction(ThrottleAction, ETriggerEvent::Completed, this, &ASpaceshipPawn::HandleThrottleReleased); - // Binding the Look action - EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Look); + // Bind mouse control + EnhancedInputComponent->BindAction(MouseControlAction, ETriggerEvent::Started, this, &ASpaceshipPawn::HandleMouseControl); - // Binding the Fire action - EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::Fire); + // Bind fire action + EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &ASpaceshipPawn::HandleFire); } } -void ASpaceshipPawn::Move(const FInputActionValue& Value) +// Split the throttle handling into two functions +void ASpaceshipPawn::HandleThrottleStarted(const FInputActionValue& Value) { - const FVector2D MovementVector = Value.Get(); - - CurrentVelocity = FVector( - MovementVector.X * MovementSpeed, - MovementVector.Y * MovementSpeed, - 0.0f - ); + const float ThrottleValue = Value.Get(); + bThrottlePressed = true; + TargetThrust = ThrottleValue * MaxThrust; } -void ASpaceshipPawn::Look(const FInputActionValue& Value) +void ASpaceshipPawn::HandleThrottleReleased(const FInputActionValue& Value) { - const FVector2D LookAxisVector = Value.Get(); - - // Yaw rotation (left/right) - AddActorLocalRotation(FRotator(0.0f, LookAxisVector.X * RotationSpeed * GetWorld()->GetDeltaSeconds(), 0.0f)); - - // Pitch rotation (up/down) for camera - float NewPitch = CameraSpringArm->GetRelativeRotation().Pitch + - LookAxisVector.Y * RotationSpeed * GetWorld()->GetDeltaSeconds(); - NewPitch = FMath::Clamp(NewPitch, -80.0f, 80.0f); - CameraSpringArm->SetRelativeRotation(FRotator(NewPitch, 0.0f, 0.0f)); + bThrottlePressed = false; } -void ASpaceshipPawn::Fire(const FInputActionValue& Value) +void ASpaceshipPawn::HandleMouseControl(const FInputActionValue& Value) +{ + const FVector2D MouseValue = Value.Get(); + + // Get current rotation + FRotator CurrentRot = GetActorRotation(); + + // Calculate new rotation values + float NewPitch = CurrentRot.Pitch + (-MouseValue.Y * RotationSpeed * GetWorld()->GetDeltaSeconds()); + float NewYaw = CurrentRot.Yaw + (MouseValue.X * RotationSpeed * GetWorld()->GetDeltaSeconds()); + + // Clamp pitch to prevent flipping + NewPitch = FMath::ClampAngle(NewPitch, -85.0f, 85.0f); + + // Set target rotation + TargetRotation = FRotator(NewPitch, NewYaw, CurrentRot.Roll); + + // Update camera spring arm rotation to match + CameraSpringArm->SetRelativeRotation(FRotator(NewPitch * 0.3f, 0.0f, 0.0f)); +} + +void ASpaceshipPawn::HandleFire(const FInputActionValue& Value) { - // Implementation remains the same as before UWorld* World = GetWorld(); if (World) { @@ -110,7 +163,7 @@ void ASpaceshipPawn::Fire(const FInputActionValue& Value) SpawnParams.Owner = this; SpawnParams.Instigator = GetInstigator(); - ASpaceshipProjectile* Projectile = World->SpawnActor( + World->SpawnActor( ASpaceshipProjectile::StaticClass(), SpawnLocation, SpawnRotation, diff --git a/Source/MyProject3/SpaceshipPawn.h b/Source/MyProject3/SpaceshipPawn.h index b056b48..c66be2d 100644 --- a/Source/MyProject3/SpaceshipPawn.h +++ b/Source/MyProject3/SpaceshipPawn.h @@ -18,6 +18,7 @@ public: protected: virtual void BeginPlay() override; + // Components UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components") class UStaticMeshComponent* ShipMesh; @@ -32,25 +33,42 @@ protected: class UInputMappingContext* DefaultMappingContext; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") - class UInputAction* MovementAction; + class UInputAction* ThrottleAction; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") - class UInputAction* LookAction; + class UInputAction* MouseControlAction; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") class UInputAction* FireAction; + // Movement Parameters UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") - float MovementSpeed = 1000.0f; + float MaxThrust = 2000.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") + float ThrustAcceleration = 500.0f; + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") + float ThrustDeceleration = 200.0f; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") float RotationSpeed = 100.0f; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Movement") + float DragCoefficient = 0.1f; + // Input functions - void Move(const FInputActionValue& Value); - void Look(const FInputActionValue& Value); - void Fire(const FInputActionValue& Value); + void HandleThrottleStarted(const FInputActionValue& Value); + void HandleThrottleReleased(const FInputActionValue& Value); + void HandleMouseControl(const FInputActionValue& Value); + void HandleFire(const FInputActionValue& Value); private: + // Movement state + float CurrentThrust; + float TargetThrust; + bool bThrottlePressed; FVector CurrentVelocity; + FRotator TargetRotation; + FQuat CurrentRotation; }; \ No newline at end of file