Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/game/movement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

/* This file has utilities for controlling the player's movement. */

#include <vector>

#include <GLFW/glfw3.h>

#include <game/player.hpp>
Expand Down
37 changes: 35 additions & 2 deletions src/game/player.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "player.hpp"
#include <cmath>

#include <graphics/texture.hpp>

Expand All @@ -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<float, float> 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<float, float> 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
6 changes: 5 additions & 1 deletion src/game/player.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <iostream>

#include <game/gameobject.hpp>
#include <game/renderable.hpp>
Expand All @@ -19,7 +20,10 @@ class Player : public GameObject, Renderable {
// Read and update velocities.
std::pair<float, float> getvel();
void setvel(std::pair<float, float> newvel);
void setangle(float a);

protected:
float angle;
};

} // namespace
} // namespace game