2019-05-27 13:50:49 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 27/05/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Player.h"
|
|
|
|
|
|
|
|
|
|
|
|
Player::Player(float x, float y, float alpha) : x(x), y(y), orientation(alpha)
|
|
|
|
{}
|
|
|
|
|
2019-06-01 06:42:29 +02:00
|
|
|
void Player::move(float dx, float dy)
|
|
|
|
{
|
2024-01-28 22:08:35 +00:00
|
|
|
x += dx;
|
|
|
|
y += dy;
|
2019-06-01 06:42:29 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 20:31:51 +00:00
|
|
|
void Player::rotate(float alpha)
|
2019-06-01 06:42:29 +02:00
|
|
|
{
|
2024-01-28 22:08:35 +00:00
|
|
|
orientation += fmodf(alpha, 360);
|
|
|
|
if(orientation > 360)
|
|
|
|
{
|
|
|
|
orientation -= 360;
|
|
|
|
}
|
|
|
|
else if(orientation < 0)
|
|
|
|
{
|
|
|
|
orientation += 360;
|
|
|
|
}
|
2024-01-21 22:11:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Rotate the movement vector along the new angle, assumes that the only
|
|
|
|
* speed influencing the player is its own movement.
|
|
|
|
*/
|
|
|
|
float prevSpeedX = currentMoveSpeedX;
|
|
|
|
float prevSpeedY = currentMoveSpeedY;
|
|
|
|
currentMoveSpeedX = cosf(-alpha * deg_to_rad) * prevSpeedX
|
2024-01-28 22:08:35 +00:00
|
|
|
- sinf(-alpha * deg_to_rad) * prevSpeedY;
|
2024-01-21 22:11:46 +00:00
|
|
|
currentMoveSpeedY = sinf(-alpha * deg_to_rad) * prevSpeedX
|
2024-01-28 22:08:35 +00:00
|
|
|
+ cosf(-alpha * deg_to_rad) * prevSpeedY;
|
2024-01-21 22:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player::updateSpeed(float localX, float localY) {
|
|
|
|
// TODO: Support strafing.
|
|
|
|
currentMoveSpeedX = localX * sinf(orientation*deg_to_rad)*moveSpeed;
|
|
|
|
currentMoveSpeedY = localX * cosf(orientation*deg_to_rad)*moveSpeed;
|
2019-06-01 06:42:29 +02:00
|
|
|
}
|