FINALLY figured out how to rotate to face a square, thanks to all the help from Twitch chat. I need to study Trig.

This commit is contained in:
Zed A. Shaw 2025-09-02 02:26:08 -04:00
parent d822cb3438
commit 9faad5f263
5 changed files with 85 additions and 11 deletions

View file

@ -650,13 +650,20 @@ bool System::use_item(const string& slot_name) {
}
}
gui::Event System::shortest_rotate(Point player_at, Point aiming_at, Point turn_to) {
dbc::check(aiming_at != turn_to, "you're already pointing there.");
dbc::check(player_at != turn_to, "you can't turn on yourself");
gui::Event System::shortest_rotate(Point player_at, Point aiming_at, Point target) {
dbc::check(aiming_at != target, "you're already pointing there.");
dbc::check(player_at != target, "you can't turn on yourself");
Point facing{player_at.x - aiming_at.x, player_at.y - aiming_at.y};
Point target{player_at.x - turn_to.x, player_at.y - turn_to.y};
Point mag{size_t(abs(int(facing.x - target.x))), size_t(abs(int(facing.y - target.y)))};
float target_dx = float(player_at.x) - float(target.x);
float target_dy = float(player_at.y) - float(target.y);
float aiming_dx = float(player_at.x) - float(aiming_at.x);
float aiming_dy = float(player_at.y) - float(aiming_at.y);
return mag.x <= mag.y ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
float target_angle = atan2(-target_dy, target_dx) * (180.0 / std::numbers::pi);
float aiming_angle = atan2(-aiming_dy, aiming_dx) * (180.0 / std::numbers::pi);
float diff = target_angle - aiming_angle;
double normalized = fmod(diff + 360.0, 360.0);
return normalized < 180.0 ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
}