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;
Rectangle destB;
Rectangle destA;
Vector2 mouseStart;
float len;
float ofset;
};

View File

@ -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);
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};
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;
}
}

View File

@ -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;
}