forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1pc.js
More file actions
93 lines (92 loc) · 3.14 KB
/
1pc.js
File metadata and controls
93 lines (92 loc) · 3.14 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
function FirstPersonControls(_canvas,_camera,_micromove){
var canvas=_canvas;
var camera=_camera;
var micromove=_micromove
var kc={"w":87,"s":83,"a":65,"d":68,"space":32,"shift":16};
var keys={}
function ac(qx,qy,qa,qf){
var m_x=-Math.sin(qa)*qf;
var m_y=-Math.cos(qa)*qf;
var r_x=qx-m_x;
var r_y=qy-m_y;
return {x:r_x,y:r_y};
}
$(document).click(function (){
canvas=document.querySelector("canvas");
canvas.requestPointerLock()
})
function lockChangeAlert() {
if (document.pointerLockElement === canvas || document.mozPointerLockElement === canvas) {
document.addEventListener("mousemove", updatePosition, false);
}else{
document.removeEventListener("mousemove", updatePosition, false);
}
}
function updatePosition(e) {
camera.rotation.x-=degtorad(e.movementY/10)
camera.rotation.y-=degtorad(e.movementX/10)
if(radtodeg(camera.rotation.x)<-90){
camera.rotation.x=degtorad(-90)
}
if(radtodeg(camera.rotation.x)>90){
camera.rotation.x=degtorad(90)
}
}
function degtorad(deg){
return deg*Math.PI/180;
}
function radtodeg(rad){
return rad*180/Math.PI;
}
function camMicroMove(){
if(keys[kc["w"]]){
camera.position.x=ac(camera.position.x,camera.position.z,camera.rotation.y+degtorad(180),micromove).x;
camera.position.z=ac(camera.position.x,camera.position.z,camera.rotation.y+degtorad(180),micromove).y;
}
if(keys[kc["s"]]){
camera.position.x=ac(camera.position.x,camera.position.z,camera.rotation.y,micromove).x;
camera.position.z=ac(camera.position.x,camera.position.z,camera.rotation.y,micromove).y;
}
if(keys[kc["a"]]){
camera.position.x=ac(camera.position.x,camera.position.z,camera.rotation.y-degtorad(90),micromove).x;
camera.position.z=ac(camera.position.x,camera.position.z,camera.rotation.y-degtorad(90),micromove).y;
}
if(keys[kc["d"]]){
camera.position.x=ac(camera.position.x,camera.position.z,camera.rotation.y+degtorad(90),micromove).x;
camera.position.z=ac(camera.position.x,camera.position.z,camera.rotation.y+degtorad(90),micromove).y;
}
if(keys[kc["space"]]){
camera.position.y+=micromove;
}
if(keys[kc["shift"]]){
camera.position.y-=micromove;
}
}
document.addEventListener('pointerlockchange', lockChangeAlert, false);
document.addEventListener('mozpointerlockchange', lockChangeAlert, false);
setInterval(function (){
camMicroMove()
})
$(document).keydown(function (z){
keys[z.keyCode]=true;
})
$(document).keyup(function (z){
delete keys[z.keyCode];
})
$('body').append("<canvas id='xxdd'></canvas>")
var p=document.getElementById("xxdd");
var ctx=p.getContext("2d");
$("#xxdd").attr("style","position:fixed;top:0px;left:0px")
p.width=window.innerWidth
p.height=window.innerHeight
var cw=25;
ctx.lineWidth=3
ctx.beginPath();
ctx.moveTo(window.innerWidth/2-cw,window.innerHeight/2);
ctx.lineTo(window.innerWidth/2+cw,window.innerHeight/2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(window.innerWidth/2,window.innerHeight/2-cw);
ctx.lineTo(window.innerWidth/2,window.innerHeight/2+cw);
ctx.stroke()
}