45 lines
1 KiB
C++
45 lines
1 KiB
C++
//
|
|
// Created by trotfunky on 27/05/19.
|
|
//
|
|
|
|
#include "Player.h"
|
|
|
|
|
|
Player::Player(float x, float y, float alpha) : x(x), y(y), orientation(alpha)
|
|
{}
|
|
|
|
void Player::move(float dx, float dy)
|
|
{
|
|
x += dx;
|
|
y += dy;
|
|
}
|
|
|
|
void Player::rotate(float alpha)
|
|
{
|
|
orientation += fmodf(alpha, 360);
|
|
if(orientation > 360)
|
|
{
|
|
orientation -= 360;
|
|
}
|
|
else if(orientation < 0)
|
|
{
|
|
orientation += 360;
|
|
}
|
|
|
|
/*
|
|
* 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
|
|
- sinf(-alpha * deg_to_rad) * prevSpeedY;
|
|
currentMoveSpeedY = sinf(-alpha * deg_to_rad) * prevSpeedX
|
|
+ cosf(-alpha * deg_to_rad) * prevSpeedY;
|
|
}
|
|
|
|
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;
|
|
}
|