-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.js
More file actions
115 lines (104 loc) · 2.05 KB
/
Copy pathCamera.js
File metadata and controls
115 lines (104 loc) · 2.05 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
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
function Camera() {
var position = {
x: 0,
z: 0
};
var angle = {
x: 0,
y: 0
};
var keys = {
left: false,
right: false,
up: false,
down: false,
leftStrafe: false,
rightStrafe: false
};
this.startLeft = function () {
keys.left = true;
};
this.stopLeft = function () {
keys.left = false;
};
this.startRight= function () {
keys.right = true;
};
this.stopRight= function () {
keys.right = false;
};
this.startUp = function () {
keys.up = true;
};
this.stopUp = function () {
keys.up = false;
};
this.startDown = function () {
keys.down = true;
};
this.stopDown = function () {
keys.down = false;
};
this.startStrafeLeft = function () {
keys.leftStrafe = true;
};
this.stopStrafeLeft = function () {
keys.leftStrafe = false;
};
this.startStrafeRight = function () {
keys.rightStrafe = true;
};
this.stopStrafeRight = function () {
keys.rightStrafe = false;
};
var walkVelocity = 512;
var turnVelocity = Math.PI;
this.rotate = function (dx, dy) {
angle.x += dx * turnVelocity;
angle.y += dy * turnVelocity;
};
this.resetAngle = function () {
angle.x = 0;
};
this.tick = function (dt) {
dt /= 1000;
var ds = walkVelocity * dt;
var da = turnVelocity * dt;
var dx = 0;
var dz = 0;
if (keys.up) {
dx += ds * -Math.sin(angle.y);
dz += ds * Math.cos(angle.y);
}
if (keys.down) {
dx -= ds * -Math.sin(angle.y);
dz -= ds * Math.cos(angle.y);
}
if (keys.left) {
angle.y += da;
}
if (keys.right) {
angle.y -= da;
}
if (keys.leftStrafe) {
dx += ds * -Math.sin(angle.y + Math.PI / 2);
dz += ds * Math.cos(angle.y + Math.PI / 2);
}
if (keys.rightStrafe) {
dx += ds * -Math.sin(angle.y - Math.PI / 2);
dz += ds * Math.cos(angle.y - Math.PI / 2);
}
position.x += dx;
position.z += dz;
};
this.getX = function () {
return position.x;
};
this.getZ = function () {
return position.z;
};
this.uniform = function (program) {
program.uniform2f('camera.position', position.x, position.z);
program.uniform2f('camera.angle', angle.x, angle.y);
};
}