x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Get incorrect mouse position

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: alt text

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.

Product Version: UE 4.14
Tags:
more ▼

asked Feb 15 '17 at 07:22 AM in C++ Programming

avatar image

Last_Soldier
6 1 4

avatar image MihailYa Feb 25 '18 at 04:14 PM

Faced the same problem.

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

Hello,

Why not try moving try storing the camera's last known vector position as a variable and have the camera move based on where the camera's current position is in relevance to its previous position? Hope this helps.

Thanks, Daniel

more ▼

answered Feb 27 '18 at 04:02 PM

avatar image

Daniel U ♦♦ STAFF
225 2 5 3

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question