-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFPSCam.h
More file actions
62 lines (47 loc) · 1.93 KB
/
Copy pathFPSCam.h
File metadata and controls
62 lines (47 loc) · 1.93 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
#include <stdio.h>
#include <tgmath.h>
#ifndef HMME_FPSCAM_H
#define HMME_FPSCAM_H
#include "Entity.h"
#include "FollowCam.h"
#include "HandmadeMath.h"
#include "debug.h"
class FPSCam : public Entity {
public:
FollowCam cam = FollowCam(0); // TODO: Why on earth is this necessary?? Remove this and fix the error.
Entity target;
// all angles in radians
float yaw = 0;
float pitch = 0;
float sensitivity = 0.002f;
FPSCam() {
target = Entity();
target.position = HMM_Vec3(0.0f, 0.0f, -1.0f);
cam = FollowCam(&target);
AddChild(&target);
AddChild(&cam);
}
void Tick(float deltaSeconds, Input previousInput, Input input) override {
double deltaX = input.mouseX - previousInput.mouseX;
double deltaY = input.mouseY - previousInput.mouseY;
// HACK: Pitch is being weird for reasons I don't understand. It works fine for
// 360 degrees, then does something silly for 360 degrees, then repeats. I suspect
// I'm just doing something wrong with quaternions because I know they encode twice
// the angle or whatever. In any case, I've hacked around it for now to splice
// together ranges that work.
yaw = yaw + (-deltaX * sensitivity);
pitch = HMM_Clamp(-HMM_PI32 / 2, pitch + (-deltaY * sensitivity), HMM_PI32 / 2);
// HACK: MEGAHACK: why the heck is the apparent rotation twice what it should be?
float hackyPitch = HMM_PI32;
if (pitch > 0) {
hackyPitch = HMM_PI32 + pitch / 2;
} else if (pitch < 0) {
hackyPitch = 2 * HMM_PI32 + pitch / 2;
}
printf("%f\t%f\n", pitch, hackyPitch);
hmm_quaternion rotationYaw = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), yaw);
hmm_quaternion rotationPitch = HMM_QuaternionFromAxisAngle(HMM_Vec3(1.0f, 0.0f, 0.0f), hackyPitch);
rotation = rotationPitch * rotationYaw;
}
};
#endif