Change so it spins around pointer
This commit is contained in:
parent
e62b0eb6bf
commit
b87dd4203b
@ -20,4 +20,8 @@ private:
|
||||
float rotation = 0.0f;
|
||||
Rectangle destB;
|
||||
Rectangle destA;
|
||||
|
||||
Vector2 mouseStart;
|
||||
float len;
|
||||
float ofset;
|
||||
};
|
||||
|
@ -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);
|
33
src/App.cpp
33
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
25
src/Math.cpp
25
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user