/
BoundingCapsule.js
87 lines (72 loc) · 2.6 KB
/
BoundingCapsule.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
//import Renderable from "./Renderable.js";
//import { Vec3 } from "../Maths.js";
//import gl, {VAO, ATTR_POSITION_LOC} from "../gl.js";
import gl from "../../fungi/gl.js";
import Vao from "../../fungi/Vao.js";
import { Vec3 } from "../../fungi/Maths.js";
import Renderable from "../../fungi/rendering/Renderable.js";
class BoundingCapsule extends Renderable{
constructor(name, matName){
super(name, null, matName);
this.drawMode = gl.ctx.LINE_STRIP;
this.radius = 0.5;
this.radiusSqr = this.radius * this.radius;
this.vecStart = new Vec3(0,0.5,0);
this.vecEnd = new Vec3(0,-0.5,0);
this.vao = this.gen();
/*Precalc values for reuse
RadiusSqr = raduis * radius;
VecLen = vecEnd - vecStart;
vecNorm = Normalize(veclen);
*/
}
gen(){
/*.......................................
Rotate By Y
ry = y;
rx = z*sin + x*cos;
rz = z*cos - x*sin; */
var vSize = 9, //How many points from bottom to top.
hSize = 3, //How many times to rotate the circle
vHalf = (vSize-1) * this.vecStart.y, //Halfway point to extend the sphere to make a capsule
vInc = Math.PI / (vSize-1), //Increment bottom to top
hInc = Math.PI / (hSize), //Increment rotation for each circle created
half = Math.PI * 0.5, //Half of PI, will be origin when creating the circle
x,
y,
r, //rads for circle rotation
i, //loop var for circle making
rad, //rads for making the cicle
sin, //sin of the angle to rotate circle
cos, //cos of the angle to rotate circle
v = [];
//.......................................
//Draw half of a 2D capsule by creating half a circle that extends the center
for(i=0; i < vSize; i++){
rad = vInc * i - half;
x = this.radius * Math.cos(rad);
y = this.radius * Math.sin(rad);
if(i < vHalf) y -= 0.5;
else if(i > vHalf) y += 0.5;
else if(i == vHalf){ v.push(x,y-0.5,0, x,y+0.5,0); continue; }
v.push(x,y,0); //Since Z is 0, exclude it from Y rotation equation.
}
//.......................................
//Repeat the capsule half in reverse to build one complete 2D capsule.
for(i=v.length-6; i > 0; i-=3) v.push(-v[i],v[i+1],v[i+2]);
//.......................................
//Repeat the 2D Capsule a few times to make it 3D-ish
var vLen = v.length;
for(var j=1; j < hSize; j++){
rad = hInc * j;
cos = Math.cos(rad);
sin = Math.sin(rad);
for(i=0; i < vLen; i+=3) v.push(v[i]*cos,v[i+1],-v[i]*sin);
}
//.......................................
//Final Point to finish up capsule
v.push(v[0],v[1],v[2]);
return Vao.standardRenderable("FungiBCapsule", 3, v);
}
}
export default BoundingCapsule;