/
index.html
283 lines (217 loc) · 11.3 KB
/
index.html
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>
CubicVR.js: Bowling game - initial testing, SPACE = reset ball, R = reset pins
</title>
<script src="libs/ammo.js" type="text/javascript">
</script>
<script src="libs/CubicVR.js" type="text/javascript">
</script>
<script src="game/SceneLoader.js" type="text/javascript">
</script>
<script type='text/javascript'>
function BowlingGame(canvas) {
this.canvas = canvas;
this.loader = null;
this.scene = null;
this.physics = null;
this.layout = null;
this.mainloop = null;
this.levelModel = "models/bowling.dae";
this.camStartPos = null;
this.init();
this.run();
}
BowlingGame.prototype = {
init: function() {
this.initializeScene();
},
initializeScene: function() {
var canvas = this.canvas;
var loader = new SceneLoader(this.levelModel);
scene = loader.getScene();
scene.lights = [];
this.camStartPos = scene.camera.position.slice(0);
scene.camera.resize(canvas.width,canvas.height);
scene.camera.setTargeted(true);
// Create a shadowed area light, map resolution 2048
scene.bindLight(new CubicVR.Light({
type:"area",
intensity:1.1,
specular:[1,1,1],
mapRes:2048, // 4096 ? 8192 ? ;)
areaCeiling:10,
areaFloor:-10,
areaAxis: [15,-4], // specified in degrees east/west north/south
distance: 30
}));
// init physics manager
var physics = new CubicVR.ScenePhysics();
loader.setupRigidBody(physics);
// Add our scene to the window resize list
CubicVR.addResizeable(scene);
this.scene = scene;
this.physics = physics;
this.loader = loader;
},
run: function() {
var scene = this.scene;
var physics = this.physics;
var canvas = this.canvas;
var camStartPos = this.camStartPos;
// initialize a mouse view controller
var mvc = new CubicVR.MouseViewController(canvas, scene.camera);
var pickConstraint = null;
var ballConstraint = null;
var pickDist = 0;
var rigidBall = this.loader.getRigidBall();
var throwVec = [0,0,0];
var spinVal = 0;
mvc.setEvents({
mouseMove: function (ctx, mpos, mdelta, keyState) {
if (!ctx.mdown) return;
if (pickConstraint) {
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
} else if (ballConstraint) {
var pos = ballConstraint.getPosition();
var mvx = mdelta[0]/100.0;
if (mvx>1) mvx = 1;
if (mvx<-1) mvx = -1;
var mvy = mdelta[1]/100.0;
if (mvy>1) mvy = 1;
if (mvy<-1) mvy = -1;
pos = CubicVR.vec3.add(pos,CubicVR.vec3.multiply(CubicVR.vec3.normalize([-throwVec[0],0,-throwVec[2]]),mvy));
//pos[2] += mvy;
if (pos[2] > 12) {
pos[0] += mvx;
} else {
rigidBall.applyImpulse([mvx*15,0,0], [0,rigidBall.sceneObject.scale[0]/2.0,0]);
}
var raise = (pos[2]>11)?((pos[2]-11)/2.0):0;
raise = (raise*raise)/2.0;
pos[1] = raise+rigidBall.sceneObject.scale[0]/2+0.2;
if (pos[2]<10 || pos[2]>14 || pos[0] < -3 || pos[0] > 3) {
} else {
ballConstraint.setPosition(pos);
}
} else {
ctx.orbitView(mdelta);
}
// ctx.panView(mdelta);
},
mouseWheel: function (ctx, mpos, wdelta, keyState) {
ctx.zoomView(wdelta);
},
mouseDown: function (ctx, mpos, keyState) {
var rayTo = scene.camera.unProject(mpos[0],mpos[1]);
var result = physics.getRayHit(scene.camera.position,rayTo);
if (result && !pickConstraint && !ballConstraint) {
var resultName = result.rigidBody.getSceneObject().name;
if (resultName == "BowlingBall") {
/* result.rigidBody.activate();
var camVec = CubicVR.vec3.subtract(result.rigidBody.getSceneObject().position,scene.camera.position);
camVec = CubicVR.vec3.multiply(CubicVR.vec3.normalize(camVec),50);
result.rigidBody.applyImpulse(camVec,result.localPosition);*/
var ballPos = rigidBall.getSceneObject().position;
if (ballPos[2]<10) {
return false;
}
ballConstraint = new CubicVR.Constraint({
type: CubicVR.enums.physics.constraint.P2P,
rigidBody: result.rigidBody,
positionA: [0,0,0]
});
physics.addConstraint(ballConstraint);
ballConstraint.setPosition([0,rigidBall.sceneObject.scale[0]/2+0.2,12]);
document.body.style.cursor='move';
throwVec = CubicVR.vec3.subtract(result.rigidBody.getSceneObject().position,scene.camera.position);
} else {
pickConstraint = new CubicVR.Constraint({
type: CubicVR.enums.physics.constraint.P2P,
rigidBody: result.rigidBody,
positionA: result.localPosition
});
physics.addConstraint(pickConstraint);
pickDist = CubicVR.vec3.length(CubicVR.vec3.subtract(scene.camera.position,result.position));
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
}
}
},
mouseUp: function(ctx, mpos, keyState) {
if (pickConstraint) {
physics.removeConstraint(pickConstraint);
pickConstraint = null;
}
if (ballConstraint) {
physics.removeConstraint(ballConstraint);
ballConstraint = null;
}
document.body.style.cursor='auto';
},
keyDown: function(ctx, mpos, key, keyState) {
if (key == CubicVR.keyboard.SPACE && rigidBall) {
scene.camera.position = camStartPos.slice(0);
rigidBall.reset();
} else if (key == CubicVR.keyboard.KEY_R && rigidBall) {
doReset = true;
}
},
keyUp: null
});
var doReset = false;
var bowlingBall = this.loader.getBall();
var ballMats = bowlingBall.getMesh().materials;
// ballMats[0].setTexture(new CubicVR.Texture("images/bowlingball-spec.jpg"),"specular");
ballMats[0].specular = [0.5,0.5,1];
ballMats[0].shininess = 0.4;
// Start our main drawing loop, it provides a timer and the gl context as parameters
this.mainloop = new CubicVR.MainLoop(function(timer, gl) {
var seconds = timer.getSeconds();
var lus = timer.getLastUpdateSeconds();
if (lus > 0.5) {
return;
}
if (doReset) {
physics.reset();
scene.camera.position = camStartPos.slice(0);
doReset = false;
}
physics.stepSimulation(lus);
if (bowlingBall.position[2] > -12) {
scene.camera.trackTarget(bowlingBall.position,lus*3.0,3.5);
scene.camera.target = bowlingBall.position.slice(0);
}
if (scene.camera.position[1] < 0.5) {
scene.camera.position[1] = 0.5;
}
if (scene.camera.position[1] < bowlingBall.position[1]+0.4) {
scene.camera.position[1] = bowlingBall.position[1]+0.4;
}
if (ballConstraint) {
scene.camera.position[0] = bowlingBall.position[0]-throwVec[0];
}
if (rigidBall && rigidBall.sceneObject.position[2]>18) {
scene.camera.position = camStartPos.slice(0);
rigidBall.reset();
}
scene.render();
});
}
}
function gameStart() {
// by default generate a full screen canvas with automatic resize
var gl = CubicVR.init();
var canvas = CubicVR.getCanvas();
if (!gl) {
alert("Sorry, no WebGL support.");
return;
};
CubicVR.setSoftShadows(true);
var game = new BowlingGame(canvas);
}
</script>
</head>
<body onLoad="gameStart();">
</body>
</html>