-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube3D.js
More file actions
149 lines (114 loc) · 2.95 KB
/
Copy pathCube3D.js
File metadata and controls
149 lines (114 loc) · 2.95 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
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
/*
3D cube. Just a proof of concept, might be useful later on
by gib
*/
const display = require("display");
const SCREEN_WIDTH = display.width();
const SCREEN_HEIGHT = display.height();
var bDoRun = true;
var lastTime = now();
function Mesh(vertices, edges){
this.vertices = vertices || [];
this.edges = edges || [];
this.x = 0;
this.y = 0;
this.z = 0;
this.rotX = 0;
this.rotY = 0;
this.rotZ = 0;
this.color = BRUCE_PRICOLOR;
this.rotationSpeedX = 0;
this.rotationSpeedY = 0;
this.rotationSpeedZ = 0;
}
Mesh.prototype.rotateX = function(x, y, z, angle){
var cos = Math.cos(angle);
var sin = Math.sin(angle);
return {x: x, y: y * cos - z * sin, z: y * sin + z * cos};
};
Mesh.prototype.rotateY = function(x, y, z, angle){
var cos = Math.cos(angle);
var sin = Math.sin(angle);
return {x: x * cos + z * sin, y: y, z: -x * sin + z * cos};
};
Mesh.prototype.rotateZ = function(x, y, z, angle){
var cos = Math.cos(angle);
var sin = Math.sin(angle);
return {x: x * cos - y * sin, y: x * sin + y * cos, z: z };
};
Mesh.prototype.project = function(point){
var distance = 200;
var scale = distance / (distance + point.z);
return {x: SCREEN_WIDTH / 2 + point.x * scale, y: SCREEN_HEIGHT / 2 + point.y * scale};
};
Mesh.prototype.update = function(dT){
this.rotX += this.rotationSpeedX * dT;
this.rotY += this.rotationSpeedY * dT;
this.rotZ += this.rotationSpeedZ * dT;
};
Mesh.prototype.draw = function(){
var projected = [];
for(var i = 0; i < this.vertices.length; i++){
var v = this.vertices[i];
var p = {x: v.x + this.x, y: v.y + this.y, z: v.z + this.z};
p = this.rotateX(p.x, p.y, p.z, this.rotX);
p = this.rotateY(p.x, p.y, p.z, this.rotY);
p = this.rotateZ(p.x, p.y, p.z, this.rotZ);
projected.push(this.project(p));
}
for(var i = 0; i < this.edges.length; i++){
var edge = this.edges[i];
var a = projected[edge[0]];
var b = projected[edge[1]];
display.drawLine(a.x, a.y, b.x, b.y, this.color);
}
};
function createCube(size){
var s = size * 0.5;
var vertices = [{ x: -s, y: -s, z: -s },
{ x: s, y: -s, z: -s },
{ x: s, y: s, z: -s },
{ x: -s, y: s, z: -s },
{ x: -s, y: -s, z: s },
{ x: s, y: -s, z: s },
{ x: s, y: s, z: s },
{ x: -s, y: s, z: s }
];
var edges = [[0,1],
[1,2],
[2,3],
[3,0],
[4,5],
[5,6],
[6,7],
[7,4],
[0,4],
[1,5],
[2,6],
[3,7]
];
return new Mesh(vertices, edges);
}
function handleInput(){
if(keyboard.getAnyPress()){
bDoRun = false;
}
}
function main(){
var cube = createCube(80);
cube.rotationSpeedX = 0.01;
cube.rotationSpeedY = 0.02;
cube.z = 100;
while(bDoRun){
var currentTime = now();
var deltaTime = (currentTime - lastTime) / 1000;
lastTime = currentTime;
handleInput();
display.fill(BRUCE_BGCOLOR);
cube.update(deltaTime);
cube.draw();
// ms - FPS: 6 ~ 165, 7 ~ 144, 8 ~ 120, 11 ~ 90, 16 ~ 60, 20 = 50, 21 ~ 48, 33 ~ 30, 40 = 25, 42 ~ 23.976, 66 ~ 15
delay(16);
}
}
main();