Hi everyone, I’m recently working on a RTS style camera system. I finished all the basic functions of the camera system, but there is a strange problem with the part of moving camera. I can get mouse’s position and compare the FVector with viewport size to calculate the direction camera will move. But when I move the mouse to the left or top edge of viewport, there is a little chance to get a incorrect mouse position which the X and Y of the vector are both ZERO. That problem make my camera usually move to the upper left conner of my map.
Here is my C++ code work with the camera movement :
void ARTSCameraPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// Check mouse position for camera movement
UGameViewportClient* GameViewport = GEngine->GameViewport;
check(GameViewport);
if (GameViewport->IsFocused(GameViewport->Viewport))
{
FVector2D MousePosition;
GameViewport->GetMousePosition(MousePosition);
CheckMousePosition(MousePosition);
}
}
void ARTSCameraPawn::CheckMousePosition(FVector2D MousePosition)
{
int ViewportSize[2];
CurPlayerController->GetViewportSize(ViewportSize[0], ViewportSize[1]);
// Present the moving direction of camera
int DirectionModifier[2] = { 0 };
// Check if mouse's position is in the edge area of screen
// Horizontal check
if (MousePosition.X <= ScreenEdgeWidth)
{
DirectionModifier[1] = -1;
FString MessageString = FString("Move Left. Because of Mouse X value is ");
MessageString.AppendInt(MousePosition.X);
GEngine->AddOnScreenDebugMessage(-1, 30, FColor::White, MessageString);
}
else if (MousePosition.X >= (float)(ViewportSize[0] - ScreenEdgeWidth))
{
DirectionModifier[1] = 1;
FString MessageString = FString("Move Right. Because of Mouse X value is ");
MessageString.AppendInt(MousePosition.X);
GEngine->AddOnScreenDebugMessage(-1, 30, FColor::White, MessageString);
}
// Verticle check
if (MousePosition.Y <= ScreenEdgeWidth)
{
DirectionModifier[0] = 1;
FString MessageString = FString("Move Up. Because of Mouse Y value is ");
MessageString.AppendInt(MousePosition.Y);
GEngine->AddOnScreenDebugMessage(-1, 30, FColor::White, MessageString);
}
else if (MousePosition.Y >= (float)(ViewportSize[1] - ScreenEdgeWidth))
{
DirectionModifier[0] = -1;
FString MessageString = FString("Move Down. Because of Mouse Y value is ");
MessageString.AppendInt(MousePosition.Y);
GEngine->AddOnScreenDebugMessage(-1, 30, FColor::White, MessageString);
}
// Make FVector for controller input
FVector InputVector = FVector(DirectionModifier[0], DirectionModifier[1], 0);
// Get current rotator of actor
FRotator CurrentRotator = GetActorRotation();
// Only Yaw axis is necessary
CurrentRotator.Roll = CurrentRotator.Pitch = 0.f;
// Rotate InputVector by CurrentRotator to get correct movement direction
float InputSize = InputVector.Size();
FRotationMatrix MyRotationMatrix(CurrentRotator);
InputVector = MyRotationMatrix.TransformVector(InputVector.GetSafeNormal());
InputVector *= InputSize;
// Apply the input to controller
AddMovementInput(InputVector);
}
And here is the runtime debug message screenshot:
I took the screenshot once the problem occurred. That’s the evidence of I got a incorrect mouse position.
How can I avoid to get the incorrect mouse position? Thanks a lot.