yaw pitch roll
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [models] example - yaw pitch roll
//!
//! Zig-API translation of examples/models/models_yaw_pitch_roll.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2017-2025 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
const rl = @import("raylib");
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
//rl.setConfigFlags(.{ .msaa_4x_hint = true, .window_highdpi = true });
rl.initWindow(screen_width, screen_height, "raylib [models] example - yaw pitch roll");
defer rl.closeWindow(); // Close window and OpenGL context
const camera: rl.Camera = .{
.position = .{ .x = 0.0, .y = 50.0, .z = -120.0 }, // Camera position perspective
.target = .{ .x = 0.0, .y = 0.0, .z = 0.0 }, // Camera looking at point
.up = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target)
.fovy = 30.0, // Camera field-of-view Y
.projection = .perspective, // Camera type
};
var model = try rl.loadModel("resources/models/obj/plane.obj"); // Load model
defer rl.unloadModel(model);
const texture = try rl.loadTexture("resources/models/obj/plane_diffuse.png"); // Load model texture
defer rl.unloadTexture(texture);
rl.setTextureWrap(texture, .repeat); // Force Repeat to avoid issue on Web version
model.materials.?[0].map(.diffuse).texture = texture; // Set map diffuse texture
var pitch: f32 = 0.0;
var roll: f32 = 0.0;
var yaw: f32 = 0.0;
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
// Plane pitch (x-axis) controls
if (rl.isKeyDown(.down)) {
pitch += 0.6;
} else if (rl.isKeyDown(.up)) {
pitch -= 0.6;
} else {
if (pitch > 0.3) {
pitch -= 0.3;
} else if (pitch < -0.3) {
pitch += 0.3;
}
}
// Plane yaw (y-axis) controls
if (rl.isKeyDown(.s)) {
yaw -= 1.0;
} else if (rl.isKeyDown(.a)) {
yaw += 1.0;
} else {
if (yaw > 0.0) {
yaw -= 0.5;
} else if (yaw < 0.0) {
yaw += 0.5;
}
}
// Plane roll (z-axis) controls
if (rl.isKeyDown(.left)) {
roll -= 1.0;
} else if (rl.isKeyDown(.right)) {
roll += 1.0;
} else {
if (roll > 0.0) {
roll -= 0.5;
} else if (roll < 0.0) {
roll += 0.5;
}
}
// Tranformation matrix for rotations
model.transform = rl.math.matrixRotateXYZ(.{ .x = rl.deg2rad * pitch, .y = rl.deg2rad * yaw, .z = rl.deg2rad * roll });
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
// Draw 3D model (recomended to draw 3D always before 2D)
{
rl.beginMode3D(camera);
defer rl.endMode3D();
rl.drawModel(model, .{ .x = 0.0, .y = -8.0, .z = 0.0 }, 1.0, .white); // Draw 3d model with texture
rl.drawGrid(10, 10.0);
}
// Draw controls info
rl.drawRectangle(30, 370, 260, 70, rl.fade(.green, 0.5));
rl.drawRectangleLines(30, 370, 260, 70, rl.fade(.dark_green, 0.5));
rl.drawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, .dark_gray);
rl.drawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, .dark_gray);
rl.drawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, .dark_gray);
rl.drawText("(c) WWI Plane Model created by GiaHanLam", screen_width - 240, screen_height - 20, 10, .dark_gray);
}
}/*******************************************************************************************
*
* raylib [models] example - yaw pitch roll
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 1.8, last time updated with raylib 4.0
*
* Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2017-2025 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h" // Required for: MatrixRotateXYZ()
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
//SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI);
InitWindow(screenWidth, screenHeight, "raylib [models] example - yaw pitch roll");
Camera camera = { 0 };
camera.position = (Vector3){ 0.0f, 50.0f, -120.0f };// Camera position perspective
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 30.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera type
Model model = LoadModel("resources/models/obj/plane.obj"); // Load model
Texture2D texture = LoadTexture("resources/models/obj/plane_diffuse.png"); // Load model texture
SetTextureWrap(texture, TEXTURE_WRAP_REPEAT); // Force Repeat to avoid issue on Web version
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
float pitch = 0.0f;
float roll = 0.0f;
float yaw = 0.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Plane pitch (x-axis) controls
if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
else
{
if (pitch > 0.3f) pitch -= 0.3f;
else if (pitch < -0.3f) pitch += 0.3f;
}
// Plane yaw (y-axis) controls
if (IsKeyDown(KEY_S)) yaw -= 1.0f;
else if (IsKeyDown(KEY_A)) yaw += 1.0f;
else
{
if (yaw > 0.0f) yaw -= 0.5f;
else if (yaw < 0.0f) yaw += 0.5f;
}
// Plane roll (z-axis) controls
if (IsKeyDown(KEY_LEFT)) roll -= 1.0f;
else if (IsKeyDown(KEY_RIGHT)) roll += 1.0f;
else
{
if (roll > 0.0f) roll -= 0.5f;
else if (roll < 0.0f) roll += 0.5f;
}
// Tranformation matrix for rotations
model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll });
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw 3D model (recomended to draw 3D always before 2D)
BeginMode3D(camera);
DrawModel(model, (Vector3){ 0.0f, -8.0f, 0.0f }, 1.0f, WHITE); // Draw 3d model with texture
DrawGrid(10, 10.0f);
EndMode3D();
// Draw controls info
DrawRectangle(30, 370, 260, 70, Fade(GREEN, 0.5f));
DrawRectangleLines(30, 370, 260, 70, Fade(DARKGREEN, 0.5f));
DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, DARKGRAY);
DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, DARKGRAY);
DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, DARKGRAY);
DrawText("(c) WWI Plane Model created by GiaHanLam", screenWidth - 240, screenHeight - 20, 10, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(model); // Unload model data
UnloadTexture(texture); // Unload texture data
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.