From 49895ec6a68a91152eed15dd9c9b3719e3b86cda Mon Sep 17 00:00:00 2001 From: aicorr Date: Sun, 16 Feb 2025 15:16:22 +0530 Subject: [PATCH] Begin mouse control implementation --- Content/Blueprints/BP_SpaceshipPawn.uasset | 4 +- Content/Input/IA_MouseControl.uasset | 4 +- Source/MyProject3/SpaceshipPawn.cpp | 55 +++++++++++++--------- Source/MyProject3/SpaceshipPawn.h | 5 +- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Content/Blueprints/BP_SpaceshipPawn.uasset b/Content/Blueprints/BP_SpaceshipPawn.uasset index 5894fea..24f4f92 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:13d19fbcc67188e59de7270ecb5fe422c57c0816d8a750f12b1877090e6c6355 -size 33194 +oid sha256:eee80d735172d622902083842d0708c03911b8deddc22831cf9a72aefb0d18a3 +size 33172 diff --git a/Content/Input/IA_MouseControl.uasset b/Content/Input/IA_MouseControl.uasset index 3cb5c29..6704ae7 100644 --- a/Content/Input/IA_MouseControl.uasset +++ b/Content/Input/IA_MouseControl.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d054bd77ddf158b2409f13e909a2cf42ca1a27020328dd2e2eb1e28bd3462fad -size 2118 +oid sha256:622501565ea35f90e1771712aa99273c84b3db2b83d0f2c081845cf2c952174b +size 2121 diff --git a/Source/MyProject3/SpaceshipPawn.cpp b/Source/MyProject3/SpaceshipPawn.cpp index 27ad47a..c8d7f78 100644 --- a/Source/MyProject3/SpaceshipPawn.cpp +++ b/Source/MyProject3/SpaceshipPawn.cpp @@ -24,6 +24,10 @@ ASpaceshipPawn::ASpaceshipPawn() CameraSpringArm->CameraLagSpeed = 3.0f; CameraSpringArm->bEnableCameraRotationLag = true; CameraSpringArm->CameraRotationLagSpeed = 10.0f; + CameraSpringArm->bUsePawnControlRotation = false; // Add this line + CameraSpringArm->bInheritPitch = true; // Add this line + CameraSpringArm->bInheritYaw = true; // Add this line + CameraSpringArm->bInheritRoll = false; // Add this line // Create camera Camera = CreateDefaultSubobject(TEXT("Camera")); @@ -34,8 +38,9 @@ ASpaceshipPawn::ASpaceshipPawn() TargetThrust = 0.0f; bThrottlePressed = false; CurrentVelocity = FVector::ZeroVector; - CurrentRotation = GetActorQuat(); - TargetRotation = GetActorRotation(); + CurrentPitch = 0.0f; + CurrentYaw = 0.0f; + ShipRotation = FRotator::ZeroRotator; } void ASpaceshipPawn::BeginPlay() @@ -56,7 +61,7 @@ void ASpaceshipPawn::Tick(float DeltaTime) { Super::Tick(DeltaTime); - // Handle thrust deceleration when not pressing throttle + // Handle thrust if (!bThrottlePressed) { CurrentThrust = FMath::FInterpTo(CurrentThrust, 0.0f, DeltaTime, ThrustDeceleration); @@ -66,7 +71,17 @@ void ASpaceshipPawn::Tick(float DeltaTime) CurrentThrust = FMath::FInterpTo(CurrentThrust, TargetThrust, DeltaTime, ThrustAcceleration); } - // Calculate movement + // Smoothly interpolate to target rotation + FRotator CurrentRotation = GetActorRotation(); + FRotator NewRotation = FMath::RInterpTo( + CurrentRotation, + ShipRotation, + DeltaTime, + RotationSpeed + ); + SetActorRotation(NewRotation); + + // Update movement based on ship's forward vector FVector ThrustDirection = GetActorForwardVector(); FVector ThrustForce = ThrustDirection * CurrentThrust; @@ -80,11 +95,6 @@ void ASpaceshipPawn::Tick(float DeltaTime) 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) { @@ -130,21 +140,24 @@ void ASpaceshipPawn::HandleMouseControl(const FInputActionValue& Value) { const FVector2D MouseValue = Value.Get(); - // Get current rotation - FRotator CurrentRot = GetActorRotation(); + // Mouse sensitivity factors + const float MouseSensitivityX = 1.0f; + const float MouseSensitivityY = 1.0f; - // Calculate new rotation values - float NewPitch = CurrentRot.Pitch + (-MouseValue.Y * RotationSpeed * GetWorld()->GetDeltaSeconds()); - float NewYaw = CurrentRot.Yaw + (MouseValue.X * RotationSpeed * GetWorld()->GetDeltaSeconds()); + // Update current rotation based on mouse input + CurrentYaw += MouseValue.X * MouseSensitivityX; + CurrentPitch = FMath::ClampAngle(CurrentPitch + (-MouseValue.Y * MouseSensitivityY), -85.0f, 85.0f); - // Clamp pitch to prevent flipping - NewPitch = FMath::ClampAngle(NewPitch, -85.0f, 85.0f); + // Create target rotation + ShipRotation = FRotator(CurrentPitch, CurrentYaw, 0.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)); + if (GEngine) + { + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Blue, + FString::Printf(TEXT("Mouse Input - X: %.2f, Y: %.2f"), MouseValue.X, MouseValue.Y)); + GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Purple, + FString::Printf(TEXT("Ship Rotation - Pitch: %.2f, Yaw: %.2f"), CurrentPitch, CurrentYaw)); + } } void ASpaceshipPawn::HandleFire(const FInputActionValue& Value) diff --git a/Source/MyProject3/SpaceshipPawn.h b/Source/MyProject3/SpaceshipPawn.h index c66be2d..fe59130 100644 --- a/Source/MyProject3/SpaceshipPawn.h +++ b/Source/MyProject3/SpaceshipPawn.h @@ -69,6 +69,7 @@ private: float TargetThrust; bool bThrottlePressed; FVector CurrentVelocity; - FRotator TargetRotation; - FQuat CurrentRotation; + FRotator ShipRotation; + float CurrentPitch; + float CurrentYaw; }; \ No newline at end of file