diff --git a/src/game/movement.hpp b/src/game/movement.hpp index bd29fbb..5c3e2d6 100644 --- a/src/game/movement.hpp +++ b/src/game/movement.hpp @@ -2,8 +2,6 @@ /* This file has utilities for controlling the player's movement. */ -#include - #include #include diff --git a/src/game/player.cpp b/src/game/player.cpp index 2163718..f7651d4 100644 --- a/src/game/player.cpp +++ b/src/game/player.cpp @@ -1,4 +1,5 @@ #include "player.hpp" +#include #include @@ -11,19 +12,51 @@ Player::Player(int xloc, int yloc) { this->y = yloc; this->vx = 0; this->vy = 0; + this->angle; } void Player::render() { - tex::PLAYER_TEX->render(this->x, this->y, PLAYER_SIZE, PLAYER_SIZE, tex::RenderBasis::MID, tex::RenderBasis::MID); + tex::PLAYER_TEX->render(this->x, this->y, PLAYER_SIZE, PLAYER_SIZE, + tex::RenderBasis::MID, tex::RenderBasis::MID); } std::pair Player::getvel() { return std::pair(this->vx, this->vy); } +float vec_to_rotation(float vx, float vy) { + const float PI = 3.14159265; + const float DEG_CONV = 180.0 / PI; + + float x, y, m; + x = fabs(vx); + y = fabs(vy); + + // Normalize the vector (x, y) + float r = sqrt(y * y + x * x); + x /= r; + y /= r; + + // arccos approximation [-1, 1] + // m = -1.4 * x + 1.55; + + // arccos approximation [0, 1] + // This one will be used because x is in the domain [0, 1] + // err = integral 0->1 arccos(x) - f(x) dx = -0.01 + m = -1.12 * x + 1.57; + m *= DEG_CONV; + + return m; +} + void Player::setvel(std::pair newvel) { this->vx = newvel.first; this->vy = newvel.second; -} + float m = vec_to_rotation(this->vx, this->vy); + setangle(m); } + +void Player::setangle(float a) { this->angle = a; } + +} // namespace game diff --git a/src/game/player.hpp b/src/game/player.hpp index 3284fcb..444753a 100644 --- a/src/game/player.hpp +++ b/src/game/player.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -19,7 +20,10 @@ class Player : public GameObject, Renderable { // Read and update velocities. std::pair getvel(); void setvel(std::pair newvel); + void setangle(float a); + protected: + float angle; }; -} // namespace +} // namespace game