summaryrefslogtreecommitdiff
path: root/_shared/math/rectangle.js
blob: 012c46063b8faf69a6ef87384a1ae8de76216b06 (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
/// <reference path="rigidbody2.js" />

function rectangle(pGl, pData) {
    var lBuffer = pGl.createBuffer(),
        lColor = pData.color || { x: 1, y: 0, z: 0, w: 1 };

    pGl.bindBuffer(pGl.ARRAY_BUFFER, lBuffer);

    var lBufferedData = [
        -pData.width / 2.0, -pData.height / 2.0,    lColor.x, lColor.y, lColor.z, lColor.w,
        -pData.width / 2.0, pData.height / 2.0,     lColor.x, lColor.y, lColor.z, lColor.w,
        pData.width / 2.0,  pData.height / 2.0,     lColor.x, lColor.y, lColor.z, lColor.w,
        pData.width / 2.0,  pData.height /2.0,      lColor.x, lColor.y, lColor.z, lColor.w,
        pData.width / 2.0,  -pData.height / 2.0,    lColor.x, lColor.y, lColor.z, lColor.w,
        -pData.width / 2.0, -pData.height / 2.0,    lColor.x, lColor.y, lColor.z, lColor.w
    ];

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

    pData.getMomentOfInertia = function() {
        return (1.0 / 12.0) * pData.mass * (pData.height * pData.height + pData.width * pData.width);
    };
        
    return makeRigidBody2({
        vertexCount:    6,
        buffer:         lBuffer, 
        width:          pData.width,
        height:         pData.height,
        model:          translateMatrix(mat4(), pData.position ? pData.position.x : 0, pData.position ? pData.position.y : 0, 0),
    }, pData);
}