Working with device orientation

I basically want to take the orientation of my phone and use that as one to one input for my player pawn’s aiming direction in game. I’ve done this before outside unreal by just reading the device orientation matrices on ios and android, but it’s not as obvious in Unreal how to do that.

I see there are inputs for Tilt, Gravity, Rotation Rate, and Acceleration. How would I go about binding those and using them in C++? As far as I know, C++ works with floats for axis bindings. And in blueprint I see events for Tilt but for the other events I only see getters.

You can set the device rotation as an axis under the project settings/input section. At least, I have done this in iOS. If you have an axis already in place, just add rotation in the mobile section when adding a new input. This bound the device’s rotation to that axis, allowing me to rotate a camera via the on device gyro. I’ll try to remember to add a screenshot tomorrow.

Hope this helps…

Cheers,

J^2

Oh nice. I must have not seen device rotation itself. It’ll save me quite a bit of work if I don’t have to manually derive it from gravity, and rotation rate, and whatever tilt means. I’ll have to look for that tonight after my day job.

Found it!

OK, found it.

When adding a new axis mapping, at the very bottom of the keyboard section (which makes sense right), there are several touch inputs. Map rotation rate to get the gyro input. At least, this is working for me on iOS. Hope this helps. Cheers,

J^2

Oh yeah I did see rotation rate. I assumed it was raw and I had to derive it from combining it with gravity and tilt or something like on Android if doing it with raw API’s. I’ll see if I get good results from this.

Got it working! It works really well. When I was doing this myself on android I had really jittery input or really bad lag from trying to filter the input. I’m not sure how Unreal does it, but it feels almost as good as an iPhone’s gyro input. Although when I set my phone flat on the table or hold it in my hands without moving it still moves a bit which is dissapointing. Iphones definitely always had better gyros.

Anyway, in C++ I had to call InputComponent->BindVectorAxis(“RotationRate”, this, &AODPlayerTurret::OnRotationInput);

That was the way to get rotation rate as a bindable input.

Good to hear! Cheers,

J^2

Hi JJ,

Thanks for the screen shot. On an iPad 4 and on 4.7.2, I start with a basic scene. table and chairs. Add those Axis inputs as per your screenshot and nothing once it’s on my device. Is there some BP that’s got to go on with those nodes? I see them but have no idea what to plug them into.

Well…

Well, I’m most likely the worst possible person to be answering this, but here goes. After mapping the input, you then have to do something with it. So, if you have a camera that you want to rotate, then you would make a blueprint that uses the proper InputAxis node (the one that matches the name of the input axis you created in the project settings) to trigger the camera to move. Here’s a grab of the blueprint I am using, which is built from Monokkel’s wonderful TBS toolkit:

The spring arm that is getting the input rotation added to it is the focal point of the camera in my case. I hope this helps get you started. I’d strongly recommend going through the getting input via blueprint tutorials though. It will be a much stronger base to build from then anything I could offer. Good luck! Cheers,

J^2

Here’s how I’m doing it in C++ for now. I think I still need to take gravity into account. There’s some random motion in there even when the device is standing still.


void AODPlayerTurret::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	InputComponent->BindVectorAxis("RotationRate", this, &AODPlayerTurret::OnRotationInput);
}

void AODPlayerTurret::OnRotationInput(FVector Input)
{
	static float DEG_TO_RAD = PI / (180.f);
		
	//TODO: get right and down scale factor from player controller to make the game playable for people who don't want to do full body motions, like while sitting

	FQuat RightRot = FQuat(FVector(0.f, 0.f, -1.f), Input.Y * DEG_TO_RAD);		//Why is right reversed, I don't know...  Damnit MATH!!!!
	FQuat DownRot = FQuat(FVector(0.f, 1.f, 0.f), Input.X * DEG_TO_RAD);
	FQuat RollLeftRot = FQuat(FVector(1.f, 0.f, 0.f), Input.Z * DEG_TO_RAD);
	
	FQuat Result = GetActorQuat() * RightRot * DownRot * RollLeftRot;

	SetActorRotation(Result.Rotator());
}

Seems pretty simple on ios. Don’t know about android, but works quite well.

I had an idea for a project that would involve something like this but am not a programmer myself. If anyone gets this working I would appreciate anything that could help me integrate it. Tutorial? Template?

Also very interested in an answer to this

Any chance that someone has tried this through pixelstreaming? Is it even possible to collect this information through the browser?