Control Overview
Cub3D uses a simple keyboard-based control scheme inspired by classic first-person games. All controls are responsive and support smooth movement through key press/release detection.Movement
W, A, S, D keys for directional movement
Camera
Left/Right arrow keys for rotation
Exit
ESC key to close the game
Window
Close button to exit
Movement Controls
Cub3D implements smooth, direction-relative movement using the WASD key layout.Key Definitions
Fromcub3.h:38-41, the movement keys are defined as:
Movement Keys
- W - Forward
- S - Backward
- A - Strafe Left
- D - Strafe Right
Move forward in the direction you’re facing.The player moves along their current viewing direction using trigonometric calculations.
Movement Speed
Frommain.c:55:
The default movement speed is 1 pixel per frame. With the game’s continuous rendering loop, this provides smooth, responsive movement.
Camera Rotation
Camera rotation is controlled using the arrow keys and directly modifies the player’s viewing angle.Rotation Keys
Left Arrow
Rotate camera counter-clockwise (left)
Right Arrow
Rotate camera clockwise (right)
Rotation Speed
Fromutils6.c:58-64:
The rotation speed is 0.05 radians per frame, providing smooth camera rotation without being too fast or slow.
Angle Normalization
The camera angle is kept within 0 to 2π radians:Direction and Plane Calculation
After rotation, the direction vector and camera plane are recalculated:The camera plane is perpendicular to the direction vector and scaled by 0.66, which determines the field of view.
Key Event System
Cub3D uses a key press/release system for responsive controls:Key Press Handler
Fromutils5.c:29-46, when a key is pressed:
Each key press sets a flag to 1, enabling continuous movement while the key is held down.
Key Release Handler
Fromutils7.c:62-77, when a key is released:
Key release sets the flag back to 0, stopping movement for that direction.
Event Hook Registration
Frommain.c:92-95, event hooks are registered on window initialization:
Collision Detection
Movement includes wall collision detection to prevent walking through walls.Wall Check Function
Fromutils6.c:15-27:
Collision Application
Fromutils6.c:51-56:
Exit Controls
ESC Key
Pressing ESC immediately closes the window and exits:Window Close Button
Clicking the window’s close button (X) triggers the same cleanup:Cleanup Process
Both exit methods call the same cleanup function fromutils5.c:15-27:
All resources (images, textures, window, display connection, and memory) are properly freed before exit.
Initial Player Direction
The player’s starting direction is determined by the character in the map file:Direction Mapping
Fromutils5.c:75-85:
N - North
Starting angle: 3π/2 radians (270°)Player faces upward (north) in the map
S - South
Starting angle: π/2 radians (90°)Player faces downward (south) in the map
E - East
Starting angle: 2π radians (360° / 0°)Player faces right (east) in the map
W - West
Starting angle: π radians (180°)Player faces left (west) in the map
Control Summary
Here’s a quick reference table for all controls:| Key | Action | Implementation |
|---|---|---|
| W | Move forward | mapa->key_w = 1 |
| S | Move backward | mapa->key_s = 1 |
| A | Strafe left | mapa->key_a = 1 |
| D | Strafe right | mapa->key_d = 1 |
| ← | Rotate left | mapa->key_left = 1 |
| → | Rotate right | mapa->key_right = 1 |
| ESC | Exit game | Immediate closewindow() |
Tips for Smooth Movement
Combine movement keys
You can press multiple keys simultaneously for diagonal movement:
- W + A = Forward-left diagonal
- W + D = Forward-right diagonal
- S + A = Backward-left diagonal
- S + D = Backward-right diagonal
Strafe while rotating
Hold a strafe key (A or D) while rotating with arrow keys for smooth circular movement around objects.
Next Steps
Now that you understand the controls, learn how to create your own custom maps:Creating Maps
Learn the map file format and create custom levels