-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
67 lines (56 loc) · 1.55 KB
/
Player.cpp
File metadata and controls
67 lines (56 loc) · 1.55 KB
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
#include "Player.h"
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
using namespace glm;
Player::Player(const string& objPath, const string& texPath,
float scaleVal, float groundY)
: Model(objPath, texPath), groundY(groundY)
{
scale = vec3(scaleVal);
position = vec3(0.f, groundY, 0.f);
rotation = vec3(0.f);
}
// ---- Per-frame update (jump physics) ----
void Player::update(float dt) {
if (isJumping) {
position.y += jumpVelocity * dt;
jumpVelocity += gravity * dt;
if (position.y <= groundY) {
position.y = groundY;
isJumping = false;
jumpVelocity = 0.f;
}
}
}
// ---- Movement ----
void Player::moveForward(float dt) {
vec3 fwd = getForwardVector();
position += fwd * moveSpeed * dt;
}
void Player::moveBackward(float dt) {
vec3 fwd = getForwardVector();
position -= fwd * moveSpeed * dt;
}
void Player::turnLeft(float dt) {
yaw += turnSpeed * dt;
rotation.y = yaw;
}
void Player::turnRight(float dt) {
yaw -= turnSpeed * dt;
rotation.y = yaw;
}
void Player::jump() {
if (!isJumping) {
isJumping = true;
jumpVelocity = 10.f; // initial upward velocity
}
}
// ---- Helpers ----
vec3 Player::getForwardVector() const {
float rad = radians(yaw);
return normalize(vec3(sin(rad), 0.f, cos(rad)));
}
vec3 Player::getFrontPosition() const {
// One unit in front of the tank at eye height
return position + getForwardVector() * 1.5f + vec3(0.f, 0.8f, 0.f);
}