Add basic rotation

Rotate texuture based on mouse position
This commit is contained in:
Nikola Petrov
2024-03-05 01:51:26 +01:00
parent acb478f970
commit e5f9e8d7f3
6 changed files with 54 additions and 16 deletions

27
src/Math.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include "Math.hpp"
Color ColorLerp(Color c1, Color c2, float amount)
{
Color ret{0};
ret.r = Clamp(Lerp(c1.r, c2.r, amount), 0, 255);
ret.g = Clamp(Lerp(c1.g, c2.g, amount), 0, 255);
ret.b = Clamp(Lerp(c1.b, c2.b, amount), 0, 255);
ret.a = Clamp(Lerp(c1.a, c2.a, amount), 0, 255);
return ret;
}
Rectangle CalculateRect(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;
return ret;
}

View File

@@ -24,6 +24,7 @@ void Tree::draw(Rectangle dest, float rotation)
{
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f};
rotation = 360 - rotation;
DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE);
}