treender/random/rotation_in_just_math.cpp

69 lines
2.1 KiB
C++

#include <raymath.h>
#include <cstdio>
int main(int argc, char const *argv[])
{
std::printf("\n\n\n");
Vector2 mousePosition = {.x = 15, .y = 10};
Vector2 mouseStart;
Vector2 orgPosition = {.x = 0, .y = 10};
float screenWidth = 30;
mouseStart = mousePosition;
std::printf("MouseStart x:%f, y:%f\n", mouseStart.x, mouseStart.y);
std::printf("orgPosition x:%f, y:%f\n", orgPosition.x, orgPosition.y);
// distance betwen corner of image and mouse startPoint
float len = Vector2Distance(mouseStart, orgPosition);
std::printf("len %f\n", len);
// angle in rad betwen mouse and image corner
float atan2x = orgPosition.x - mouseStart.x;
float atan2y = orgPosition.y - mouseStart.y;
std::printf("atan2 x:%f, y:%f\n", atan2x, atan2y);
float angleOfset = std::atan2( orgPosition.y - mouseStart.y, orgPosition.x - mouseStart.x);
std::printf("angleOffset %f\n", angleOfset);
mousePosition = {.x = 20, .y = 10};
// get distance that mouse has moved from original press location
// can be positive or negative if mouse start on right side and
// end on left it will be 0 - 100 so -100 dist
float dist = mousePosition.x - mouseStart.x;
printf("dist: %f\n", dist);
// this value can get from -1 to 1 based on if we swipe left or right
float l = dist / screenWidth;
printf("l: %f\n", l);
// first convert range of -1 to 1 to 0 to 1
// then change to degres from 45 to -45
float rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
printf("rotation: %f\n", rotation);
// convert to radians
float angle = ((rotation)*PI) / 180.0f;
printf("angle: %f\n", angle);
angle += angleOfset;
printf("angle + angleOfser: %f\n", angle);
// vector betwen image corrner and mouse
Vector2 newCornerVec = {.x = len * std::cos(angle), .y = len * std::sin(angle)};
printf("newCornerVec x: %f, y:%f \n", newCornerVec.x, newCornerVec.y);
// get global vector where to draw image
Vector2 newPosition;
newPosition.x = newCornerVec.x + mousePosition.x;
newPosition.y = newCornerVec.y + mousePosition.y;
printf("New position x: %f, y:%f \n", newPosition.x, newPosition.y);
printf("Draw image at this angle %f\n", rotation + 90);
return 0;
}