I need a simple way to convert my objects coordinate into world coordinates, so that a can locate them in that coordinate system and do collision detection?
gCoord = local2globalcoord( lclCoord ) converts the local rectangular coordinates lclCoord to the global rectangular coordinates gCoord . gCoord = local2globalcoord( lclCoord , option ) converts local coordinates to global coordinates using the coordinate transformation type option .
We also use 4x4 matrices to transform cameras. Therefore, we can also convert points from world space to camera space. Computing the coordinates of a point from camera space onto the canvas, can be done using perspective projection.
In particular, Unity offers two ways to see (and set) coordinates: World coordinates: These are absolute coordinates of where the game object is located (by absolute, I mean with respect to the world frame, which is considered to be absolute in the game)
First, some background. In 3D graphics, you need to worry about several vector spaces:
[-1, +1]
in each dimension.Coordinates are specified homogeneously, so each vector has components (x, y, z, w)
, where w
is a scaling factor. You can obtain coordinates in 3-space as (x/w, y/w, z/w)
. The scaling factor is needed for certain transformations like perspective projection that wouldn't be possible with non-homogenous coordinates.
4x4 matrices are needed to transform coordinates from one vector space to another. You could have three different matrices:
You would project a coordinate C
onto the screen using the formula:
C' = P * V * M * C
OpenGL internally maintains two matrices: modelview (model and view multiplied together), and projection. There is also a texture matrix we won't worry about. When you call glMatrixMode
, you are switching between these matrices. You replace the current matrix with the identity matrix using glLoadIdentity
. You apply transformations to the current matrix with functions like glTranslatef
, glRotatef
, or gluProjection
. Each of these functions just creates a 4x4 matrix the implements that specific transformation, then multiplies the current matrix by it. You can see what the transformation matrices are in the OpenGL 2.1 reference pages.
Now for the actual answer. You need to maintain a 4x4 model matrix for each object in your scene. The model matrix will contain all the transformations needed to change model coordinates into world coordinates. For instance, every time you call glTranslate
, you would update your model matrix:
T = [ 1 0 0 x ]
[ 0 1 0 y ]
[ 0 0 1 z ]
[ 0 0 0 1 ]
M' = M * T
You can then use your model matrix to get coordinates into world space (make sure they are homogenous coordinates first; just set w = 1
, if they aren't):
V' = V * M
Since you are maintaining these transformations in parallel, you don't actually need to maintain the OpenGL modelview matrix anymore. You can pass in your model matrix as a uniform to your shaders. You can maintain your own view and projection matrices in similar ways. This is required in recent versions of OpenGL; all of the matrix handling functions are deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With