From b87dd4203b93d66ad19830b6d166985d0695c7a6 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Mon, 1 Apr 2024 21:10:25 +0200 Subject: [PATCH] Change so it spins around pointer --- inc/App.hpp | 4 ++++ inc/Math.hpp | 3 ++- src/App.cpp | 33 ++++++++++++++++++++++----------- src/Math.cpp | 25 +++++++++++++++++-------- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/inc/App.hpp b/inc/App.hpp index 2bd2564..1812245 100644 --- a/inc/App.hpp +++ b/inc/App.hpp @@ -20,4 +20,8 @@ private: float rotation = 0.0f; Rectangle destB; Rectangle destA; + + Vector2 mouseStart; + float len; + float ofset; }; diff --git a/inc/Math.hpp b/inc/Math.hpp index 6a93abb..ce0bea4 100644 --- a/inc/Math.hpp +++ b/inc/Math.hpp @@ -1,4 +1,5 @@ #include "raylib.h" Color ColorLerp(Color c1, Color c2, float amount); -Rectangle CalculateRect(Vector2 center, float rotation, float width, float height); \ No newline at end of file +Vector2 calculateVector(float rotation, float offSet, float len); +Rectangle calculateRotatedRectangle(Vector2 center, float rotation, float width, float height); \ No newline at end of file diff --git a/src/App.cpp b/src/App.cpp index 41a8f2b..db6b35a 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -20,20 +20,33 @@ void App::init(int screenWidth, int screenHeight) center = {(float)screenWidth / 2, (float)screenHeight / 2}; float rotation = 0.0f; - destA = CalculateRect(center, rotation, screenWidth, screenWidth); - destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; + destA = calculateRotatedRectangle(center, rotation, screenWidth, screenWidth); + destB = destA; } void App::update() { + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + { + mouseStart = GetMousePosition(); + len = Vector2Distance(mouseStart, center); + ofset = std::atan2(center.x - mouseStart.x, center.y - mouseStart.y); + } + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { - int mouseX = GetMouseX(); - center.x = mouseX; - center.y = GetMouseY(); - float l = (float)mouseX / screenWidth; - rotation = Lerp(45.0f, -45.0f, l); - destA = CalculateRect(center, rotation, screenWidth, screenWidth); + Vector2 mousePosition = GetMousePosition(); + + float dist = mousePosition.x - mouseStart.x; + + float l = dist / screenWidth; + rotation = Lerp(45.0f, -45.0f, (l + 1) / 2); + + Vector2 newCenter = calculateVector(rotation, ofset, len); + newCenter.x += mousePosition.x; + newCenter.y += mousePosition.y; + + destA = calculateRotatedRectangle(newCenter, rotation, screenWidth, screenWidth); } if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) @@ -41,9 +54,7 @@ void App::update() canvases[1 - pos].newGen(); pos = 1 - pos; rotation = 0.0f; - center.x = screenWidth / 2; - center.y = screenHeight / 2; - destA = CalculateRect(center, rotation, screenWidth, screenWidth); + destA = destB; } } diff --git a/src/Math.cpp b/src/Math.cpp index 2ea10f0..4d3355c 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -12,18 +12,27 @@ Color ColorLerp(Color c1, Color c2, float amount) return ret; } -Rectangle CalculateRect(Vector2 center, float rotation, float width, float height) +Vector2 calculateVector(float rotation, float offSet, float len) +{ + float angle = ((rotation)*PI) / 180.0f; + angle += offSet; + return { + .x = len * std::sin(angle), + .y = len * std::cos(angle)}; +} + +Rectangle calculateRotatedRectangle(Vector2 center, float rotation, float width, float height) { Rectangle ret = {0, 0, width, height}; + width /= 2; // a height /= 2; // b float len = std::sqrt(std::pow(width, 2) + std::pow(height, 2)); // c - rotation += 180.0f; // add 180 to point 0 up - float angle = (rotation * PI) / 180.0f; // from degrees to radians - angle += std::atan(width / height); // add angle alfa - float nx = len * std::sin(angle); - float ny = len * std::cos(angle); - ret.x = center.x + nx; - ret.y = center.y + ny; + float offSet = std::atan2(width, height); // add angle alfa + + Vector2 vec = calculateVector(rotation + 180.0f, offSet, len); + + ret.x = vec.x + center.x; + ret.y = vec.y + center.y; return ret; } \ No newline at end of file