Thursday, July 9, 2015

unity - What is the geometrical interpretation of Input.GetAxis("Mouse X")?


According to the documentation, Input.GetAxis("Mouse X") produces a range of values from -1 to 1. However it will produce another range if it is setup to be delta mouse movement.



Returns the value of the virtual axis identified by axisName.


The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1.





I don't understand what the value means geometrically. Is it a displacement between two points each in consecutive frame updates? If so what is its unit? Pixels, points, centimeters, etc?



Answer



This is something you can determine empirically. Drop in a script like this:


public class MouseDeltaCheck : MonoBehaviour
{
public Vector2 screenPos;
public Vector2 positionDelta;
public Vector2 inputDelta;

public Vector2 ratio;

void Update() {
Vector2 newScreenPos = Input.mousePosition;

positionDelta = newScreenPos - screenPos;

inputDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

if(inputDelta.x != 0f)

ratio.x = positionDelta.x / inputDelta.x;
if (inputDelta.y != 0f)
ratio.y = positionDelta.y / inputDelta.y;

screenPos = newScreenPos;
}
}

In my experiments, the mouse delta value is approximately:


{Change in Pixel Position} * {Axis Sensitivity} / ({OS Scaling Factor} * 2)


Where {Axis Sensitivity} is the sensitivity value you've configured for the "Mouse X" and "Mouse Y" axes in the Input Manager, and the {OS Scaling Factor} is any scaling being applied outside the game (for instance, in Windows 10 Display Settings, I usually run my Surface with the "Scale and Layout" setting at its recommended value of 150% so text is a readable size on the high-resolution display).


I say "approximately" because there is some variance - likely due to the Unity deltas being summed from several hardware event deltas that might include fractions of a pixel, while the absolute position value is necessarily rounded to a whole pixel coordinate prior to scaling being applied. This variation is small and bounded - even when accumulating deltas over many frames spanning movement all over the screen, I never found the two measures of travel disagreed by more than 1.5 pixels (1.5x being my OS scaling factor at the moment).


I don't know what accounts for the * 2 multiplier though.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...