Change so it spins around pointer

This commit is contained in:
Nikola Petrov 2024-04-01 21:10:25 +02:00
parent e62b0eb6bf
commit b87dd4203b
4 changed files with 45 additions and 20 deletions

View File

@ -20,4 +20,8 @@ private:
float rotation = 0.0f; float rotation = 0.0f;
Rectangle destB; Rectangle destB;
Rectangle destA; Rectangle destA;
Vector2 mouseStart;
float len;
float ofset;
}; };

View File

@ -1,4 +1,5 @@
#include "raylib.h" #include "raylib.h"
Color ColorLerp(Color c1, Color c2, float amount); Color ColorLerp(Color c1, Color c2, float amount);
Rectangle CalculateRect(Vector2 center, float rotation, float width, float height); Vector2 calculateVector(float rotation, float offSet, float len);
Rectangle calculateRotatedRectangle(Vector2 center, float rotation, float width, float height);

View File

@ -20,20 +20,33 @@ void App::init(int screenWidth, int screenHeight)
center = {(float)screenWidth / 2, (float)screenHeight / 2}; center = {(float)screenWidth / 2, (float)screenHeight / 2};
float rotation = 0.0f; float rotation = 0.0f;
destA = CalculateRect(center, rotation, screenWidth, screenWidth); destA = calculateRotatedRectangle(center, rotation, screenWidth, screenWidth);
destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; destB = destA;
} }
void App::update() 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)) if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{ {
int mouseX = GetMouseX(); Vector2 mousePosition = GetMousePosition();
center.x = mouseX;
center.y = GetMouseY(); float dist = mousePosition.x - mouseStart.x;
float l = (float)mouseX / screenWidth;
rotation = Lerp(45.0f, -45.0f, l); float l = dist / screenWidth;
destA = CalculateRect(center, rotation, screenWidth, 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)) if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
@ -41,9 +54,7 @@ void App::update()
canvases[1 - pos].newGen(); canvases[1 - pos].newGen();
pos = 1 - pos; pos = 1 - pos;
rotation = 0.0f; rotation = 0.0f;
center.x = screenWidth / 2; destA = destB;
center.y = screenHeight / 2;
destA = CalculateRect(center, rotation, screenWidth, screenWidth);
} }
} }

View File

@ -12,18 +12,27 @@ Color ColorLerp(Color c1, Color c2, float amount)
return ret; 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}; Rectangle ret = {0, 0, width, height};
width /= 2; // a width /= 2; // a
height /= 2; // b height /= 2; // b
float len = std::sqrt(std::pow(width, 2) + std::pow(height, 2)); // c float len = std::sqrt(std::pow(width, 2) + std::pow(height, 2)); // c
rotation += 180.0f; // add 180 to point 0 up float offSet = std::atan2(width, height); // add angle alfa
float angle = (rotation * PI) / 180.0f; // from degrees to radians
angle += std::atan(width / height); // add angle alfa Vector2 vec = calculateVector(rotation + 180.0f, offSet, len);
float nx = len * std::sin(angle);
float ny = len * std::cos(angle); ret.x = vec.x + center.x;
ret.x = center.x + nx; ret.y = vec.y + center.y;
ret.y = center.y + ny;
return ret; return ret;
} }