calculation of canvas position based on the cursor position and expected rotation
This commit is contained in:
Nikola Petrov 2024-04-01 22:56:57 +02:00
parent a29393bc18
commit 2b2dfe751e
4 changed files with 7 additions and 31 deletions

View File

@ -16,7 +16,6 @@ private:
int pos = 0;
int screenWidth, screenHeight;
std::vector<Canvas> canvases;
Vector2 center;
float rotation = 0.0f;
Rectangle destB;
Rectangle destA;

View File

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

View File

@ -18,9 +18,8 @@ void App::init(int screenWidth, int screenHeight)
canvas.newGen();
}
center = {(float)screenWidth / 2, (float)screenHeight / 2};
float rotation = 0.0f;
destA = calculateRotatedRectangle(center, rotation, screenWidth, screenWidth);
float posY = (screenHeight - screenWidth) / 2;
destA = {0, posY, (float)screenWidth, (float)screenWidth};
destB = destA;
}
@ -29,24 +28,19 @@ 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);
len = Vector2Distance(mouseStart, {destB.x, destB.y});
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
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);
destA.x = newCenter.x + mousePosition.x;
destA.y = newCenter.y + mousePosition.y;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))

View File

@ -19,20 +19,4 @@ Vector2 calculateVector(float rotation, float offSet, float len)
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
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;
}