From 6012c6776472acf53badad0a9235fde2a3b0f863 Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Sun, 22 Feb 2026 14:15:27 +0200 Subject: [PATCH] rolling cube --- src/main.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b05707e..d3566ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -178,7 +178,7 @@ int main() { Shape plane { shader, shapes.at("square") }; plane.init(); - plane.translate(glm::vec3 { 0.0f, -1.0f, 0.0f }); + plane.translate(glm::vec3 { 0.0f, -0.75f, 0.0f }); plane.rotate(glm::radians(90.0f), glm::vec3 { 1.0f, 0.0f, 0.0f }); plane.scale(glm::vec3 { 1.0f } * 100.0f); @@ -188,6 +188,11 @@ int main() { long unsigned int frame { 0 }; double last_frame_time { 0 }; + struct { + float x; + float z; + } cube_last_pos { .x = 0.0f, .z = 0.0f }; + while (!glfwWindowShouldClose(window)) { const double current_time { glfwGetTime() }; delta = current_time - last_frame_time; @@ -201,6 +206,7 @@ int main() { process_keyboard_input(window); glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + // glClearColor(0.0f, 0.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glm::mat4 view { camera.get_view_matrix() }; @@ -222,7 +228,15 @@ int main() { plane.draw(); shader->set_vec4("color", glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }); - cube.rotate(glm::radians(1.0f), glm::vec3 { 0.0f, 1.0f, 0.0f }); + cube.reset_transform(); + const float x { static_cast(std::cos(current_time)) }; + const float z { static_cast(std::sin(current_time)) }; + cube.translate(glm::vec3 { x, 0.0f, z } ); + const float dx { x - cube_last_pos.x }; + const float dz { z - cube_last_pos.z }; + cube.rotate(glm::radians(-90.0f), glm::vec3 { dx, 0.0f, dz }); + cube_last_pos.x = x; + cube_last_pos.z = z; cube.draw(); const double time_since_last_frame { glfwGetTime() - last_frame_time };