Understanding Transformations in Game Programming
In game programming, transformation refers to the process of changing an object’s position, rotation, or scale within a game world. These changes are typically represented using mathematics, especially vectors and matrices.
Types of Transformations
There are three main types of transformations:
- Translation
Moves an object from one place to another.
Example: Moving a player forward by 5 units.
In 2D/3D games, this is often done by adding a vector to the object’s current position. - Rotation
Rotates an object around a point or axis.
In 2D, rotation is around a point (usually the center).
In 3D, rotation can be around the X, Y, or Z axis.
Often represented using angles, quaternions, or rotation matrices. - Scaling
Changes the size of an object.
Uniform scaling: same in all directions.
Non-uniform scaling: different in each direction.
Example:
If you have a character in a 2D game:
Translation moves them around the screen.
Rotation turns them left or right.
Scaling makes them larger or smaller.
Why Transformations Matter
Transformations are used for:
- Moving characters and objects
- Animations
- Camera movement
- Collision detection
- Rendering graphics in the right place
Would you like a simple code example (in Unity or another engine)?
What is 2D Transformation?
2D transformation refers to changing the position, orientation, or size of objects in a two-dimensional (X and Y) space. It’s a core concept used in rendering graphics, animating sprites, detecting collisions, and much more.
Types of 2D Transformations
- Translation (Moving)
Moves an object from one location to another in 2D space.
Formula:
x’ = x + dx
y’ = y + dy
Where:
(x, y) is the original position
(dx, dy) is the distance to move
(x’, y’) is the new positions
Example: Move a sprite 5 units right and 3 units up.
x’ = x + 5
y’ = y + 3 - Rotation
Rotates an object around the origin (0, 0) by an angle θ (theta).
Formula:
x’ = x * cos(θ) – y * sin(θ)
y’ = x * sin(θ) + y * cos(θ)
Angle is in radians (π radians = 180 degrees).
Clockwise or counterclockwise rotation depends on the sign of θ.
Example: Rotate point (1, 0) by 90° counterclockwise:
x’ = 0
y’ = 1 - Scaling
Changes the size of the object.
Formula:
x’ = x * sx
y’ = y * sy
Where sx and sy are scaling factors.
If sx = sy = 2, the object becomes twice as large.
If sx = 1, sy = 2, the object is stretched vertically. - Shearing (Skewing)
Slants the shape of the object.
Formula (X-shear):
x’ = x + shx * y
y’ = y
Formula (Y-shear):
x’ = x
y’ = y + shy * x
Shearing distorts the object into a parallelogram shape.
What is 3D Transformation?
3D transformation refers to changing the position, rotation, or scale of objects in a three-dimensional space (X, Y, Z). It’s the core of how objects move and appear in 3D games. These transformations are typically performed using vectors and 4×4 matrices in homogeneous coordinates.
Types of 3D Transformations
- Translation (Moving an Object)
Moves an object from one point to another in 3D space.
Formula:
x’ = x + dx
y’ = y + dy
z’ = z + dz
Matrix Form:
[ 1 0 0 dx ]
[ 0 1 0 dy ]
[ 0 0 1 dz ]
[ 0 0 0 1 ]
Where (dx, dy, dz) is the direction and amount of movement. - Scaling (Resizing an Object)
Changes the size of an object.
Formula:
x’ = x * sx
y’ = y * sy
z’ = z * sz
Matrix Form:
[ sx 0 0 0 ]
[ 0 sy 0 0 ]
[ 0 0 sz 0 ]
[ 0 0 0 1 ] - Rotation (Turning an Object)
Rotates an object around one of the three axes: X, Y, or Z.
Rotation around X-axis
Matrix:
[ 1 0 0 0 ]
[ 0 cosθ -sinθ 0 ]
[ 0 sinθ cosθ 0 ]
[ 0 0 0 1 ]
Rotation around Y-axis
Matrix:
[ cosθ 0 sinθ 0 ]
[ 0 1 0 0 ]
[ -sinθ 0 cosθ 0 ]
[ 0 0 0 1 ]
Rotation around Z-axis
Matrix:
[ cosθ -sinθ 0 0 ]
[ sinθ cosθ 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]
θ (theta) is the angle of rotation in radians.
What is the Right-Hand Rule?
The Right-Hand Rule is a simple way to determine the direction of a vector result when doing vector cross products, especially in physics and 3D graphics (like calculating torque, magnetic force, or surface normals).
Used For:
- Cross Product of two vectors:
\vec{A} \times \vec{B} - Finding directions in:
- Magnetic fields (in physics)
- Rotational motion
- 3D graphics (surface normals, lighting)
- Angular velocity and torque
How to Use the Right-Hand Rule:
- Point your index finger in the direction of the first vector (A).
- Point your middle finger in the direction of the second vector (B) — it should be at a 90° angle to your index finger.
- Your thumb will now point in the direction of the resulting vector (A × B).
The result follows the right-hand screw rule: if you rotate A toward B, your thumb shows the direction of the cross product.
Example:
Let:
(x-axis)
(y-axis)
Then:
\vec{A} \times \vec{B} = \langle 0, 0, 1 \rangle \quad (\text{z-axis})
The right-hand rule shows the result points upward (along the z-axis).
Important:
The right-hand rule only works for the cross product, not for dot products. Reversing the order (e.g., ) will reverse the direction of the result.
