Orbit the camera around a vector

Hi everyone, I currently have a camera that I want to be able to rotate around a point when I swipe left or right in the touch screen and for that I already have a InputManager with the possible input methods bound.

I have a touchEvent and swipe event bound by the method

BindTouch(IE_Pressed, this, &AMyplayerController::TouchEvent)
BindTouch(IE_REPEAT, this, &AMyplayerController::SwipeEvent)

I want to be able to move the camera right or left depending on the swipe direction while being able to keep looking at FVector(0, 0, 0) which is the point of origin of the world.

The InputManager already has a reference to the camera that is on the screen, so is there anyway to make it rotate or obit around that point and keep looking at it even though it is moving?

Thank you.

Hi WolfAlvein

If you focus on applying the rotation to the Spring Arm rather than the Camera and have the location of the Spring Arm at (0, 0, 0) as well, it should rotate in the way you’re looking for. If it does some odd stuff due to the Camera and/or Spring Arm being at an angle, be sure to do the rotation in World space instead of Local space. This will let it rotate along the usual X, Y, and Z axis rather than the relative one.

Hope this helps!

Hi Matthew thanks for your answer I’ll be sure to test it out, but I was thinking of something without the use of a Spring Arm just to test out how to do it.

Thank you again.

Hi Matthew I decided to implement the movement of the camera to fixed positions, but when I implement the rotation so the camera can keep looking at the same spot, it calculates it wrong, how can I go around doing that?

//Posiciones Actuales
		FVector camCurrentPos = mainCamera->GetActorLocation();
		FRotator camCurrentRot = mainCamera->GetActorRotation();

		//Posiciones Futuras
		FVector camMoveToPos;
		FRotator camRotateToPos;

		if (cameraMove == CameraMovement::Fixed)
			switch (cameraPosition)
			case CameraPosition::Front:
				camMoveToPos = FVector(0.0f, 900.0f, 900.0f);
				camRotateToPos = FRotator(0.0f, -45.0f, -90.0f);
			case CameraPosition::Right:
				camMoveToPos = FVector(900.0f, 0.0f, 900.0f);
				camRotateToPos = FRotator(0.0f, -45.0f, -180.0f);
			case CameraPosition::Back:
				camMoveToPos = FVector(0.0f, -900.0f, 900.0f);
				camRotateToPos = FRotator(0.0f, -45.0f, -270.0f);
			case CameraPosition::Left:
				camMoveToPos = FVector(-900.0f, 0.0f, 900.0f);
				camRotateToPos = FRotator(0.0f, -45.0f, -360.0f);
		else if (cameraMove == CameraMovement::Free)


		mainCamera->SetActorLocation(FMath::VInterpTo(camCurrentPos, camMoveToPos, DeltaSeconds, cameraMovSpeed));
		mainCamera->SetActorRotation(FMath::RInterpTo(camCurrentRot, camRotateToPos, DeltaSeconds, cameraMovSpeed).Quaternion());

this is the code I have for the movement and rotation. thank you.

In what ways are the calculations wrong? I’ll need to know how it is messing up to have an idea of why it is working incorrectly.

Well instead of looking at the position indicated it ends up looking anywhere like Rotation X: 170, Y: 20, Z: 90 or something like that, what can I post so you can get a better idea?

As the values are going to a positive value, this is a factor of quaternions and rotation in the engine. If you watch the value as it goes (It may help to use the “slomo” console command at .5 or a lower value to slow everything down) and it reaches a certain point, it’ll cause some weird stuff to occur.

As an example, if you rotate along the Z axis outside of the bounds of -90 to 90, the value will reset to the opposite side of that range and change the other values to compensate.

This is a limitation of 3D rotation in the engine due to Quaternion math. I would suggest converting all of your values to Radians prior to doing calculations as they won’t do that to you and you can convert them back to degrees afterwards.

Hi Matthew thank you for the info but at the end it wasn’t a problem with degrees or radians, it was in the way I formatted the data, since i used this FRotator(0.0f, -45.0f, -360.0f) instead of this FRotator(-45.0f, -360.0f, 0.0f), so the data was being assigned to the camera wrongly, but I still converted everything into radians and then back to degrees so there won’t be any more problems with that. thank you.