-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkata.js
609 lines (566 loc) · 19.9 KB
/
kata.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* Kata Javascript Graphics Layer
* kata.js
*
* Copyright (c) 2010, katalabs, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Sirikata nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
KJS = {}
KJS.addProtoSafely = function (cls, proto, func){
if (cls.prototype[proto] != null) {
alert("oh no! This prototype already exists: " + proto)
}
else {
cls.prototype[proto] = func
}
}
// add getObjectById method to Scene
KJS.addProtoSafely(GLGE.Scene, "getObjectById", function(id) {
for(var i=0; i<this.objects.length; i++) {
if (this.objects[i].getRef()==id) return this.objects[i]
}
return null
})
KJS.addProtoSafely(GLGE.Scene, "getObjectRootById", function(id) {
for(var i=0; i<this.objects.length; i++) {
if (this.objects[i].getRef()==id) return this.objects[i].getRoot()
}
return null
})
KJS.addProtoSafely(GLGE.Scene, "getObjectsById", function(id) {
var o = []
for(var i=0; i<this.objects.length; i++) {
if (this.objects[i].getRef()==id) o.push(this.objects[i])
}
return o
})
KJS.addProtoSafely(GLGE.Scene, "addPickable", function(id) {
if (this.pickable.indexOf(id)<0) this.pickable.push(id)
})
// add makeDragable to Object.
KJS.addProtoSafely(GLGE.Object, "makeDragable", function(cbStart, cbUpdate) {
this.dragable = true
this.dragStartCb = cbStart
this.dragUpdateCb = cbUpdate
this.dragStart = function (mouse_x, mouse_y) {
this.dragStartMouseX = mouse_x
this.dragStartMouseY = mouse_y
this.dragStartCb(mouse_x, mouse_y)
}
this.dragUpdate = function (mouse_x, mouse_y) {
this.dragUpdateCb(mouse_x-this.dragStartMouseX, mouse_y-this.dragStartMouseY)
}
KJS.gameScene.addPickable(this.getRef())
})
// add makeHoverable to Object.
KJS.addProtoSafely(GLGE.Object, "makeHoverable", function(cbStart, cbEnd) {
this.hoverable = true
this.hoverStart = cbStart
this.hoverStop = cbEnd
KJS.gameScene.addPickable(this.getRef())
})
// add makeSelectable to Object.
KJS.addProtoSafely(GLGE.Object, "makeSelectable", function(cbSelect, cbDeselect) {
this.selectable = true
this.selectStart = cbSelect
this.selectStop = cbDeselect
KJS.gameScene.addPickable(this.getRef())
})
KJS.addProtoSafely(GLGE.Object, "makeClickableCallback", function(cbDown, cbUp) {
this.clickable = true
this.clickCallbackDown = cbDown
this.clickCallbackUp = cbUp
this.clickDown = function (mouse_x, mouse_y) {
if (this.clickCallbackDown) this.clickCallbackDown(mouse_x, mouse_y)
}
this.clickUp = function (mouse_x, mouse_y) {
if (this.clickCallbackUp) this.clickCallbackUp(mouse_x, mouse_y)
}
KJS.gameScene.addPickable(this.getRef())
})
// get position buffer
bugg=0
KJS.addProtoSafely(GLGE.Object, "getPositions", function() {
var posbuf
if (this.mesh) {
for (i in this.mesh.buffers) {
if (this.mesh.buffers[i].name == "position")
posbuf = this.mesh.buffers[i].data
}
}
else {
// pdebug("meshless object: " + this.getRef() + " " + bugg++,8)
}
return posbuf
})
// does a ray intersect this object? return null or point
KJS.addProtoSafely(GLGE.Object, "rayIntersect", function() {
})
KJS.initComplete = false
KJS.onInitComplete = function () {}
KJS.init = function (map, objectcount, cb){
if (cb) KJS.onInitComplete = cb
//create the GLGE renderer.
KJS.gameRenderer = new GLGE.Renderer(document.getElementById('canvas'));
KJS.gameScene = doc.getElement("mainscene");
KJS.gameScene.objectsToLoad=objectcount
KJS.gameScene.pickable = []
KJS.gameRenderer.setScene(KJS.gameScene);
if (KJS.gameScene.mouse) alert("uh oh, method name conflict KJS.gameScene.mouse!")
KJS.gameScene.mouse = new GLGE.MouseInput(document.getElementById('canvas'));
KJS.keys = new GLGE.KeyInput();
KJS.mouseovercanvas=false
document.getElementById("canvas").onmouseover=function(e){KJS.mouseovercanvas=true;}
document.getElementById("canvas").onmousemove=function(e){KJS.mouseovercanvas=true;}
document.getElementById("canvas").onmouseout=function(e){KJS.mouseovercanvas=false;}
KJS.selectedObj = {} // to avoid some null refs
KJS.hoverObj = null
KJS.oldLeftBtn = false
if (map) {
KJS.levelmap = new GLGE.HeightMap(map.image, map.width, map.height, map.lowX, map.upX, map.lowY, map.upY, map.lowZ, map.upZ);
}
KJS.lasttime=0;
KJS.lasttimeFps=0;
KJS.frameRateBuffer=10;
KJS.cnt=0;
KJS.inc=-0.025;
setInterval(KJS.render,15);
return KJS.gameScene
}
g_noselection = {}
KJS.mouselook = function(){
// KJS.gameScene.picker = KJS.gameScene.pick
KJS.gameScene.picker = KJS.gameScene.pickSoft
if (KJS.mouseovercanvas) {
var mousepos = KJS.gameScene.mouse.getMousePosition();
var leftbutton = KJS.gameScene.mouse.isButtonDown(0)
mousepos.x = mousepos.x - document.getElementById("container").offsetLeft;
mousepos.y = mousepos.y - document.getElementById("container").offsetTop;
// pdebug("KJS.gameScene.mouse x: " + mousepos.x + " y: " + mousepos.y + " left button: " + leftbutton, 0)
if (mousepos.x && mousepos.y) {
var obj = KJS.gameScene.picker(mousepos.x, mousepos.y, KJS.gameScene.hoverable);
if (obj == null && KJS.hoverObj && KJS.hoverObj.hoverable) {
KJS.hoverObj.hoverStop(mousepos.x, mousepos.y)
KJS.hoverObj=null
}
}
if (leftbutton) {
if (KJS.oldLeftBtn == false) {
var obj = KJS.gameScene.picker(mousepos.x, mousepos.y, KJS.gameScene.pickable);
if (obj==null) KJS.selectedObj = g_noselection
if (obj && obj.clickable) obj.clickDown(mousepos.x, mousepos.y)
if (obj && obj != KJS.selectedObj) {
if (KJS.selectedObj && KJS.selectedObj.selectable) KJS.selectedObj.selectStop(mousepos.x, mousepos.y)
if (obj.selectable) obj.selectStart(mousepos.x, mousepos.y)
KJS.selectedObj = obj;
// pdebug("selected: " + KJS.selectedObj.id, 2)
}
if (KJS.selectedObj.dragable) KJS.selectedObj.dragStart(mousepos.x, mousepos.y)
}
if (KJS.selectedObj.dragable) KJS.selectedObj.dragUpdate(mousepos.x, mousepos.y)
}
else {
if (obj && obj != KJS.hoverObj) {
if (KJS.hoverObj && KJS.hoverObj.hoverable) KJS.hoverObj.hoverStop(mousepos.x, mousepos.y)
KJS.hoverObj = obj;
if (obj.hoverable) obj.hoverStart(mousepos.x, mousepos.y)
// pdebug("hovering over: " + KJS.hoverObj.id, 3)
}
}
KJS.oldLeftBtn = leftbutton
}
}
KJS.checkkeys = function (){
if (KJS.mouseovercanvas || KJS.forceMovement) {
var camera = KJS.gameScene.camera;
camerapos = camera.getPosition();
camerarot = camera.getRotation();
var mat = camera.getRotMatrix();
var trans = mat.x([0, 0, -1]);
var mag = Math.pow(Math.pow(trans.e(1), 2) + Math.pow(trans.e(2), 2), 0.5);
var elapsed
var incAmt = KJS.elapsedTime / 1500.0
if(incAmt > 0.1) incAmt=0.1
var trans0 = trans.e(1)
var trans1 = trans.e(2)
trans[0] = trans.e(1)*incAmt*40 / mag;
trans[1] = trans.e(2)*incAmt*40 / mag;
var yinc = 0;
var xinc = 0;
if (KJS.keys.isKeyPressed(GLGE.KI_W)) {
yinc = yinc + parseFloat(trans[1]);
xinc = xinc + parseFloat(trans[0]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_UP_ARROW) || KJS.forceMovement=="fwd") {
yinc = yinc + parseFloat(trans[1]);
xinc = xinc + parseFloat(trans[0]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_S)) {
yinc = yinc - parseFloat(trans[1]);
xinc = xinc - parseFloat(trans[0]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_DOWN_ARROW) || KJS.forceMovement=="back") {
yinc = yinc - parseFloat(trans[1]);
xinc = xinc - parseFloat(trans[0]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_A)) {
yinc = yinc + parseFloat(trans[0]);
xinc = xinc - parseFloat(trans[1]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_D)) {
yinc = yinc - parseFloat(trans[0]);
xinc = xinc + parseFloat(trans[1]);
}
if (KJS.keys.isKeyPressed(GLGE.KI_U)) {
KJS.inc -= incAmt
}
if (KJS.keys.isKeyPressed(GLGE.KI_J)) {
KJS.inc += incAmt
}
if (KJS.keys.isKeyPressed(GLGE.KI_LEFT_ARROW) || KJS.forceMovement=="left") {
camera.setRotY(camerarot.y + incAmt);
}
if (KJS.keys.isKeyPressed(GLGE.KI_RIGHT_ARROW) || KJS.forceMovement=="right") {
camera.setRotY(camerarot.y - incAmt);
}
// pdebug("KJS.levelmap: " + KJS.levelmap.getHeightAt(camerapos.x + xinc, camerapos.y + yinc), 5)
if (KJS.levelmap) {
// pdebug("camera: " + camerapos.x.toFixed(2) + ", " + camerapos.y.toFixed(2) + " height: " +
// KJS.levelmap.getHeightAt(camerapos.x, camerapos.y).toFixed(2) ,1)
if (KJS.levelmap.getHeightAt(camerapos.x + xinc, camerapos.y) > 30)
xinc = 0;
if (KJS.levelmap.getHeightAt(camerapos.x, camerapos.y + yinc) > 30)
yinc = 0;
if (KJS.levelmap.getHeightAt(camerapos.x + xinc, camerapos.y + yinc) > 30) {
yinc = 0;
xinc = 0;
}
else {
camera.setLocZ(KJS.levelmap.getHeightAt(camerapos.x + xinc, camerapos.y + yinc) + 8);
}
}
if (xinc != 0 || yinc != 0) {
camera.setLocY(camerapos.y + yinc);
camera.setLocX(camerapos.x + xinc);
}
camera.setRotX(1.56 - trans1 * KJS.inc);
camera.setRotZ(-trans0 * KJS.inc);
}
}
KJS.render = function (){
//pdebug("# of objects: " + KJS.gameScene.objects.length,4)
if (!KJS.initComplete) {
var c = KJS.gameScene.incompleteObjects()
if (c == 0) {
KJS.initComplete = true
KJS.gameScene.computeBoundingSpheres()
KJS.onInitComplete()
}
else {
KJS.gameRenderer.render(); // somehow this pumps a process that needs to occur
return
}
}
var now=parseInt(new Date().getTime());
KJS.elapsedTime = now-KJS.lasttime
KJS.cnt=(KJS.cnt+1)%10;
if(KJS.cnt==0){
KJS.frameRateBuffer=(10000/(now-KJS.lasttimeFps)).toFixed(1);
KJS.lasttimeFps=now
document.getElementById("debug").innerHTML="Frame Rate:"+KJS.frameRateBuffer;
}
KJS.lasttime=now;
KJS.mouselook();
KJS.checkkeys();
KJS.gameRenderer.render();
}
// return point at which mouse picks object + distance from camera, or null
// also returns normal
KJS.addProtoSafely(GLGE.Object, "getPickPoint", function(mx, my, coordType){
// create raycast from camera to mouse xy
if (mx == null || coordType=="pixels") {
if (mx == null) {
var mousepos = KJS.gameScene.mouse.getMousePosition();
mx = mousepos.x - document.getElementById("container").offsetLeft;
my = mousepos.y - document.getElementById("container").offsetTop;
}
var can = document.getElementById("container").getElementsByTagName("canvas")[0]
var w = parseFloat(can.width)
var h = parseFloat(can.height)
var focal = 1.03 // ad-hack; get from camera perspective matrix & things of that nature
mx = focal * (mx / w - 0.5)
my = -focal * (h / w) * (my / h - 0.5)
}
var cam = KJS.gameScene.camera
var mro = GLGE.rotateMatrix(cam.getRotX(), cam.getRotY(), cam.getRotZ(), GLGE.ROT_XZY)
var mlo = GLGE.translateMatrix(cam.getLocX(), cam.getLocY(), cam.getLocZ())
var mat = mlo.x(mro) // camera matrix
// pdebug("mx: " + mx + " my: " + my)
var v = new GLGE.Vec([mx, my, -1, 1]) // camera points along -Z axis
v = mat.x(v) // transform to camera matrix
var P0 = [parseFloat(cam.getLocX()), parseFloat(cam.getLocY()), parseFloat(cam.getLocZ())]
var vP0 = new GLGE.Vec(P0)
if (!this.rayVsBoundingSphere(vP0, new GLGE.Vec([v.e(1), v.e(2), v.e(3)]) ) ) {
return null
}
var P1 = [v.e(1), v.e(2), v.e(3)]
var R = [P0, P1]
// pdebug("R = [[" + R[0] + "],[" + R[1] + "]]")
// get wall matrix
var root = this.getRoot()
mro = GLGE.rotateMatrix(root.getRotX(), root.getRotY(), root.getRotZ(), GLGE.ROT_XZY) // our rotation matrix
mlo = GLGE.translateMatrix(root.getLocX(), root.getLocY(), root.getLocZ()) // our location matrix
mat = mlo.x(mro) // our full matrix
// scale wall vertices
var sx = parseFloat(root.getScaleX())
var sy = parseFloat(root.getScaleY())
var sz = parseFloat(root.getScaleZ())
var minI = null
var mindist = 999999.9
var minN = null
// get vertex coords
var p = this.getPositions()
var ff = this.mesh.faces.data
var f = []
for (i in ff) f[i] = parseInt(ff[i]); /// FIXME: this is stupid -- shouldn't be strings!
// console.log(p,f)
for (var i = 0; i < f.length; i += 3) { // for each tri
var A = new GLGE.Vec([sx * p[f[i]*3], sy * p[f[i]*3 + 1], sz * p[f[i]*3 + 2], 1]) // get vertex points for triangle ABC
var B = new GLGE.Vec([sx * p[f[i+1]*3], sy * p[f[i+1]*3 + 1], sz * p[f[i+1]*3 + 2], 1])
var C = new GLGE.Vec([sx * p[f[i+2]*3], sy * p[f[i+2]*3 + 1], sz * p[f[i+2]*3 + 2], 1])
A = mat.x(A) // convert them to global coords
B = mat.x(B)
C = mat.x(C)
var T = [A.data.slice(0, 3), B.data.slice(0, 3), C.data.slice(0, 3)]
// pdebug("T: " + T)
var N = []
var I = ray_tri_intersect(R, T, N)
if (I) {
dist = vP0.distanceFrom(I)
if (dist < mindist) {
mindist = dist
minI = I
minN = N[0]
}
}
}
if (minI == null) {
return null
}
else {
return [minI.data, mindist, minN]
}
})
// return pick point of nearest object in olist under cursor, or null if none
// list can include actual objects or string id's
KJS.getNearestObject = function (olist, x, y, coordType) {
var dist = 9999999.9
var place = null
var hit = null
var norm = null
for (var i in olist) {
var obj = olist[i]
if (typeof(obj)=="string") obj = KJS.gameScene.getObjectById(obj)
if (!obj) continue
var pdn = obj.getPickPoint(x,y,coordType)
if (pdn) {
if (pdn[1] < dist) {
place = pdn[0]
dist = pdn[1]
hit = i
norm = pdn[2]
}
}
}
if (place)
return [place, hit, norm]
return null
}
// create a standard texture-mapped object with new material & texture; add to the scene
// if no texture, do not create material
KJS.createObjectAndAddToScene = function (id, mesh, texurl, cb) {
var obj = new GLGE.Object()
obj.setId(id)
obj.setMesh(mesh)
if (texurl) {
var ml = new GLGE.MaterialLayer(0,GLGE.M_COLOR,GLGE.UV1,null,null)
var mat = new GLGE.Material()
mat.id = id + "_mat"
var tx = new GLGE.Texture(texurl, cb)
ml.setTexture(tx)
mat.addTexture(tx)
mat.addMaterialLayer(ml)
obj.setMaterial(mat)
}
scene.addObject(obj)
return obj
}
// rewrite the Texture class (this may have to change -- should be incorporated into GLGE)
GLGE.Texture=function(url, cb){
this.image=new Image();
this.image.texture=this;
this.cbOnLoad=cb
this.image.onload = function(){
this.texture.state=1;
if (this.texture.cbOnLoad) {
this.texture.cbOnLoad(this)
}
}
this.image.src=url;
this.state=0;
this.glTexture=null;
}
// line segment of balls, used for editing
// beg, end are GLGE.Vec
KJS.lineSegOfBalls = function(beg, end, num, size, color){
// console.log("lineSegOfBalls:", beg, end, num, size, color)
num -= 1
var div = 1.0 / num
var delta = [(end.e(1) - beg.e(1)) * div, (end.e(2) - beg.e(2)) * div, (end.e(3) - beg.e(3)) * div]
// console.log("delta:", delta)
}
/// software-only pick function
KJS.addProtoSafely(GLGE.Scene, "pickSoft", function(x, y, objlist) {
// objlist=null
if (objlist==null) objlist=this.pickable
var s= ""; for (i in objlist) s+=objlist[i]+" "
var place_hit_norm = KJS.getNearestObject(objlist, x, y, "pixels")
if (place_hit_norm) {
return this.getObjectById(objlist[place_hit_norm[1]])
}
return null
})
/// compute bounding sphere, add boundingRadius, boundingCenter
KJS.addProtoSafely(GLGE.Object, "computeBoundingSphere", function(){
root = this.getRoot() /// give all objects the bounding sphere of the group
var sx = parseFloat(root.getScaleX())
var sy = parseFloat(root.getScaleY())
var sz = parseFloat(root.getScaleZ())
var p = this.getPositions()
if (!p) return
// first compute center
var O = new GLGE.Vec([0, 0, 0])
for (var i = 0; i < p.length; i+=3) { // for each vertex
var V = new GLGE.Vec([sx * p[i], sy * p[i+1], sz * p[i+2]])
O = O.add(V)
}
O = O.mul(3.0 / p.length)
this.boundingCenter = O
var r = 0
for (var i = 0; i < p.length; i+=3) { // for each vertex
var V = new GLGE.Vec([sx * p[i], sy * p[i+1], sz * p[i+2]])
var d = V.distanceFrom(O)
if (d > r) r = d
}
this.boundingRadius = r
})
KJS.addProtoSafely(GLGE.Scene, "computeBoundingSpheres", function() {
for (var i in this.objects) {
this.objects[i].computeBoundingSphere()
// pdebug_log("object:", this.objects[i], this.objects[i].getRef(), "radius:", this.objects[i].boundingRadius)
}
})
// return count of objects not fully loaded
KJS.addProtoSafely(GLGE.Scene, "incompleteObjects", function() {
if (this.objects.length < this.objectsToLoad) return this.objectsToLoad-this.objects.length
var count = 0
for (var i in this.objects) {
if ( (!this.objects[i].mesh) || (!this.objects[i].getRef()) ){
count ++
}
}
return count
})
// remove object from scene
KJS.addProtoSafely(GLGE.Scene, "removeObjectById", function(id) {
var j = null
var temp
for (var i in this.objects) {
if (this.objects[i].getRef() == id) {
j = i
break
}
}
if (j != null) {
this.objects[i] = this.objects[this.objects.length-1]
this.objects.pop()
}
j = null
for (var i in this.pickable) {
if (this.pickable[i] == id) {
j = i
break
}
}
if (j != null) {
this.pickable[i] = this.pickable[this.pickable.length-1]
this.pickable.pop()
}
})
// test ray against our bounding sphere
KJS.addProtoSafely(GLGE.Object, "rayVsBoundingSphere", function(RayBeg, RayEnd){
if (this.boundingRadius == null) {
if (this.mesh) {
this.computeBoundingSphere() // ech, gotta do it here due to async mesh load
}
else {
return true // better safe than sorry!
}
}
var ret;
var c;
// gotta rotate boundingCenter by our matrix (FIXME: this is all crap! Objects should be normed with proper centers)
var root = this.getRoot()
if (root.getRotX() || root.getRotY() || root.getRotZ()) {
var mat = GLGE.rotateMatrix([root.getRotX(), root.getRotY(), root.getRotZ()], GLGE.ROT_XZY)
c = mat.cross(this.boundingCenter)
c = new GLGE.Vec([c.e(1), c.e(2), c.e(3)]) // make it length 3
}
else {
c = this.boundingCenter
}
var S = c.add([parseFloat(root.getLocX()),parseFloat(root.getLocY()),parseFloat(root.getLocZ())])
var r = this.boundingRadius
var d = S.distanceFrom(RayBeg) // distance from ray origin to sphere center
if (d < r)
ret = true // origin is inside sphere
else {
var a = S.sub(RayBeg).angle(RayEnd.sub(RayBeg)) // angle between ray and center of sphere
if (a > 1.5708) {
ret = false // sphere is behind us
}
else {
var d2 = Math.sin(a) * d // distance from sphere center to nearest point on ray
if (d2 < r) {
ret = true
}
else {
ret = false
}
}
}
return ret
})