RightVector to degree turn

Does anyone have a good formula to convert a RightVector or some CFrame vector to degrees? I need this because I plan to allow for device rotation to control steering on a car. The way I plan to implement this is to:

  • get device rotation
  • convert the rightVector of the device CFrame to a percentage of 180 degrees
    ( 1 - ( RightVector - Vector3.new(1,0,0) ).magnitude )
  • Divide this value by two so it’s in terms of 90 degrees (90 is the max. you can turn your phone)
  • multiply the max. turn radius by the new value of turn we’ve calculated

Before I go and make this happen, I wanted to make sure nobody else had a better method of figuring this out. Thanks!
-big

1 Like

This isn’t quite a correct translation from vector to degrees. The dot product of two vectors is defined as the cosine of the angle between them scaled by each vectors’ magnitude. We could get the angle like so:

local right = Vector3.new(1, 0, 0)
local function getAngle(v3)
    local theta = math.acos(right:Dot(v3.Unit))
    local sign = v3.Y >= 0 and 1 or -1
    return sign * theta
end

This angle will always be between -pi and pi. Anything greater than pi/2 means that the phone is upside down. I’d recommend instead of dividing this by two, truncating anything with a magnitude greater than pi/2 so users don’t have to completely flip their phone upside down for the maximum turn speed, and they don’t quickly jump between a hard right/left turn when the phone turns too much while upside down.

1 Like

This seems to work fine in studio with a fake phone, using a brick as a “control” for studio instead of an actual phone, but when I start using the CFrame returned from UserInputService:GetDeviceRotation() there is a small gap in where the turn is detected. Here’s a video:

The number in the top left box is the value returned by the getAngle function with the right vector of the GetDeviceRotation() inputted.

It works fine with a regular part though, check this out:
https://gyazo.com/366fee34d0608e966addb403d9ca510b.gif

Any ideas on why that might happen?

Is the PlayerGui.CurrentScreenOrientation changing? What is it? (https://developer.roblox.com/articles/Device-Orientation-for-Mobile-Roblox-Games)