Definition
- A matrix that, when multiplied with the original matrix, results in the identity matrix.
- Notation: .
Calculating the Inverse of a Matrix
- Conditions for Inversibility:
- Matrix must be square ().
- Determinant .
- Method:
- Example:
For larger matrices, use Gaussian Elimination
- todo: example here
Practical Use of Inverse Transformations
- In Unity, objects exist in world space, but the camera views them in camera space. Converting coordinates between these spaces is crucial for accurate interaction, like selecting objects with the mouse.
Example: Object Selection with Mouse Picking
- High-Level Unity Abstraction:
- Unity provides functions to easily convert screen space (mouse position) to world space.
- Code Example (High-Level Unity Function):
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Object hit by raycast
}
Under-the-Hood Process:
- Internally, Unity uses inverse transformations to convert the screen space position to world space.
- Code Example (Manual Inverse Transformation):
// Getting the inverse of the camera's projection matrix
Matrix4x4 invProjMat = Camera.main.projectionMatrix.inverse;
// Converting mouse position to viewport space
Vector3 mousePosition = Input.mousePosition;
Vector3 viewportPoint = Camera.main.ScreenToViewportPoint(mousePosition);
// Transforming to world space
Vector3 worldSpaceRay = invProjMat.MultiplyPoint(viewportPoint);
// Creating a ray from camera position in the direction of worldSpaceRay
Ray ray = new Ray(Camera.main.transform.position, worldSpaceRay);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// Object hit by raycast
}