-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.cpp
156 lines (118 loc) · 2.74 KB
/
player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cmath>
#include <SDL.h>
#include "player.h"
#define PI 3.14159265358
Player::Player(Screen * inputScreen) {
//Pointer to screen
screen = inputScreen;
//Player Values
x = y = z = xSpeed = ySpeed = zSpeed = 0;
hFov = PI/4;
vFov = hFov*(((float) screen->SCREEN_HEIGHT)/((float) screen->SCREEN_WIDTH));
deAccFactor = 0.925;
speed = 0.01f;
maxVel = 1;
thetaAngle = 0;
phiAngle = 0;
//Init mouse tracking
rotX = 0;
rotY = 0;
}
void Player::addForwardSpeed(float speed) {
if (getVelocity() < maxVel) {
xSpeed += speed*cos(phiAngle);
ySpeed += speed*sin(phiAngle);
}
}
void Player::addBackwardSpeed(float speed) {
if (getVelocity() < maxVel) {
xSpeed -= speed*cos(phiAngle);
ySpeed -= speed*sin(phiAngle);
}
}
void Player::addLeftSpeed(float speed) {
if (getVelocity() < maxVel) {
xSpeed -= speed*cos(phiAngle + PI/2);
ySpeed -= speed*sin(phiAngle + PI/2);
}
}
void Player::Player::addRightSpeed(float speed) {
if (getVelocity() < maxVel) {
xSpeed += speed*cos(phiAngle + PI/2);
ySpeed += speed*sin(phiAngle + PI/2);
}
}
void Player::addUpSpeed(float speed) {
if (getVelocity() < maxVel) {
zSpeed -= speed;
}
}
void Player::addDownSpeed(float speed) {
if (getVelocity() < maxVel) {
zSpeed += speed;
}
}
void Player::addRotationLeft(float amount) {
phiAngle += amount;
}
void Player::addRotationRight(float amount) {
phiAngle -= amount;
}
float Player::getVelocity() {
return sqrt(pow(xSpeed, 2) + pow(ySpeed, 2) + pow(zSpeed, 2));
}
void Player::handleInput(const Uint8* keystates) {
if(keystates[SDL_SCANCODE_W]) {
addForwardSpeed(speed);
}
if(keystates[SDL_SCANCODE_LSHIFT]) {
addForwardSpeed(speed);
}
if(keystates[SDL_SCANCODE_S]) {
addBackwardSpeed(speed);
}
if(keystates[SDL_SCANCODE_A]) {
addLeftSpeed(speed);
}
if(keystates[SDL_SCANCODE_D]) {
addRightSpeed(speed);
}
if(keystates[SDL_SCANCODE_SPACE]) {
addUpSpeed(speed);
}
if(keystates[SDL_SCANCODE_LCTRL]) {
addDownSpeed(speed);
}
handleMouse();
}
void Player::handleMouse() {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
rotX = (mouseX - screen->SCREEN_WIDTH/2)/1000.0f;
rotY = (mouseY - screen->SCREEN_HEIGHT/2)/1000.0f;
phiAngle += rotX;
thetaAngle += rotY;
//Restrict mouse angle
if (thetaAngle > PI/2) {
thetaAngle = PI/2;
} else if (thetaAngle < -PI/2) {
thetaAngle = -PI/2;
}
//Wrap around phi value
if (phiAngle < 0) {
phiAngle = 2*PI;
} else if (phiAngle > 2*PI) {
phiAngle = 0;
}
//Reset mouse position
SDL_WarpMouseInWindow(screen->gWindow, screen->SCREEN_WIDTH/2, screen->SCREEN_HEIGHT/2);
}
void Player::update() {
handleInput(SDL_GetKeyboardState(NULL));
x += xSpeed;
y += ySpeed;
z += zSpeed;
xSpeed *= deAccFactor;
ySpeed *= deAccFactor;
zSpeed *= deAccFactor;
}