Simplify
calculation of canvas position based on the cursor position and expected rotation
This commit is contained in:
parent
a29393bc18
commit
2b2dfe751e
@ -16,7 +16,6 @@ private:
|
|||||||
int pos = 0;
|
int pos = 0;
|
||||||
int screenWidth, screenHeight;
|
int screenWidth, screenHeight;
|
||||||
std::vector<Canvas> canvases;
|
std::vector<Canvas> canvases;
|
||||||
Vector2 center;
|
|
||||||
float rotation = 0.0f;
|
float rotation = 0.0f;
|
||||||
Rectangle destB;
|
Rectangle destB;
|
||||||
Rectangle destA;
|
Rectangle destA;
|
||||||
|
@ -2,4 +2,3 @@
|
|||||||
|
|
||||||
Color ColorLerp(Color c1, Color c2, float amount);
|
Color ColorLerp(Color c1, Color c2, float amount);
|
||||||
Vector2 calculateVector(float rotation, float offSet, float len);
|
Vector2 calculateVector(float rotation, float offSet, float len);
|
||||||
Rectangle calculateRotatedRectangle(Vector2 center, float rotation, float width, float height);
|
|
18
src/App.cpp
18
src/App.cpp
@ -18,9 +18,8 @@ void App::init(int screenWidth, int screenHeight)
|
|||||||
canvas.newGen();
|
canvas.newGen();
|
||||||
}
|
}
|
||||||
|
|
||||||
center = {(float)screenWidth / 2, (float)screenHeight / 2};
|
float posY = (screenHeight - screenWidth) / 2;
|
||||||
float rotation = 0.0f;
|
destA = {0, posY, (float)screenWidth, (float)screenWidth};
|
||||||
destA = calculateRotatedRectangle(center, rotation, screenWidth, screenWidth);
|
|
||||||
destB = destA;
|
destB = destA;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,24 +28,19 @@ void App::update()
|
|||||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||||
{
|
{
|
||||||
mouseStart = GetMousePosition();
|
mouseStart = GetMousePosition();
|
||||||
len = Vector2Distance(mouseStart, center);
|
len = Vector2Distance(mouseStart, {destB.x, destB.y});
|
||||||
ofset = std::atan2(center.x - mouseStart.x, center.y - mouseStart.y);
|
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
|
||||||
{
|
{
|
||||||
Vector2 mousePosition = GetMousePosition();
|
Vector2 mousePosition = GetMousePosition();
|
||||||
|
|
||||||
float dist = mousePosition.x - mouseStart.x;
|
float dist = mousePosition.x - mouseStart.x;
|
||||||
|
|
||||||
float l = dist / screenWidth;
|
float l = dist / screenWidth;
|
||||||
rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
|
rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
|
||||||
|
|
||||||
Vector2 newCenter = calculateVector(rotation, ofset, len);
|
Vector2 newCenter = calculateVector(rotation, ofset, len);
|
||||||
newCenter.x += mousePosition.x;
|
destA.x = newCenter.x + mousePosition.x;
|
||||||
newCenter.y += mousePosition.y;
|
destA.y = newCenter.y + mousePosition.y;
|
||||||
|
|
||||||
destA = calculateRotatedRectangle(newCenter, rotation, screenWidth, screenWidth);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
||||||
|
16
src/Math.cpp
16
src/Math.cpp
@ -20,19 +20,3 @@ Vector2 calculateVector(float rotation, float offSet, float len)
|
|||||||
.x = len * std::sin(angle),
|
.x = len * std::sin(angle),
|
||||||
.y = len * std::cos(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;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user