summaryrefslogtreecommitdiff
path: root/frontend/_rigidbody/circle.js
blob: c04459703f3c5f6e052805c336ede92c3968f446 (plain)
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
const BYTES_PER_FLOAT = 4;

function circle(pGl, pRadius, pSegments, pColorList, pInitialPosition) {
    const lBuffer = pGl.createBuffer();

    pGl.bindBuffer(pGl.ARRAY_BUFFER, lBuffer);

    var lBufferedData = [];
    vertexCount = 0;

    const lAngleIncrements = (360.0 / pSegments) * (Math.PI / 180.0);
    for (let lSegIdx = 0; lSegIdx < pSegments; lSegIdx++) {
        const lAngle = lAngleIncrements * lSegIdx,
            lNextAngle = lAngleIncrements * (lSegIdx + 1),
            lColorIndex = Math.floor(pColorList.length * (lSegIdx / pSegments)),
            lColor = pColorList[lColorIndex]; // TODO: Calculate which one to use

        lBufferedData = lBufferedData.concat([
            0,                              0,                              lColor.x, lColor.y, lColor.z, lColor.w,
            pRadius * Math.sin(lAngle),     pRadius * Math.cos(lAngle),     lColor.x, lColor.y, lColor.z, lColor.w,
            pRadius * Math.sin(lNextAngle), pRadius * Math.cos(lNextAngle), lColor.x, lColor.y, lColor.z, lColor.w
        ]);

        vertexCount += 3;
    }

    pGl.bufferData(pGl.ARRAY_BUFFER, new Float32Array(lBufferedData), pGl.STATIC_DRAW)
    pGl.bindBuffer(pGl.ARRAY_BUFFER, undefined);

    return { 
        buffer: lBuffer, 
        vertexCount: vertexCount,
        position: pInitialPosition || vec2(),
        velocity: vec2(),
        force: vec2(),
        mass: 1,
        rotationRadians: 0,
        model: mat4(),
        radius: pRadius
    };
}

function renderCircle(pGl, pProgramInfo, pCircle) {
    pGl.bindBuffer(pGl.ARRAY_BUFFER, pCircle.buffer);
    {
        pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.position);
        pGl.vertexAttribPointer(pProgramInfo.attributeLocations.position, 2, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, 0);

        pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.color);
        pGl.vertexAttribPointer(pProgramInfo.attributeLocations.color, 4, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, BYTES_PER_FLOAT * 2);
    }

    pGl.drawArrays(pGl.TRIANGLE_STRIP, 0, pCircle.vertexCount);
}

function getMomentOfInertia(pCircle) {
    return (Math.PI * Math.pow(pCircle.radius, 4)) / 4;
}