19 - A Star Is Born/script.js
//Game loop (60 frames per second)

Game loop (60 frames per second)
Game loop ( 60 frames per second )
function mainLoop(timestamp) {
    ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight);
    userInput();
    COLLISIONS.length = 0;
    BODIES.forEach((b) => {
        b.draw();
        b.display();
        if(b.player){
            b.keyControl();
        };
        b.reposition();
    })
    BODIES.forEach((b, index) => {
        for(let bodyPair = index+1; bodyPair < BODIES.length; bodyPair++){
            let bestSat = {
                pen: null,
                axis: null,
                vertex: null
            }
            for(let o1comp=0; o1comp<BODIES[index].comp.length; o1comp++){
                for(let o2comp=0; o2comp<BODIES[bodyPair].comp.length; o2comp++){
                    if(sat(BODIES[index].comp[o1comp], BODIES[bodyPair].comp[o2comp]).pen > bestSat.pen){
                        bestSat = sat(BODIES[index].comp[o1comp], BODIES[bodyPair].comp[o2comp]);
                        ctx.fillText("COLLISION", 500, 400);
                    }
                }
            }
            if(bestSat.pen !== null){
                COLLISIONS.push(new CollData(BODIES[index], BODIES[bodyPair], bestSat.axis, bestSat.pen, bestSat.vertex));
           }
        }
    });
    COLLISIONS.forEach((c) => {
        c.penRes();
        c.collRes();
    });
    requestAnimationFrame(mainLoop);
}
function mainLoop ( timestamp ) { ctx . clearRect ( 0 , 0 , canvas . clientWidth , canvas . clientHeight ) ; userInput ( ) ; COLLISIONS . length = 0 ; BODIES . forEach ( ( b ) => { b . draw ( ) ; b . display ( ) ; if ( b . player ) { b . keyControl ( ) ; } ; b . reposition ( ) ; } ) BODIES . forEach ( ( b , index ) => { for ( let bodyPair = index + 1 ; bodyPair < BODIES . length ; bodyPair ++ ) { let bestSat = { pen : null , axis : null , vertex : null } for ( let o1comp = 0 ; o1comp < BODIES [ index ] . comp . length ; o1comp ++ ) { for ( let o2comp = 0 ; o2comp < BODIES [ bodyPair ] . comp . length ; o2comp ++ ) { if ( sat ( BODIES [ index ] . comp [ o1comp ] , BODIES [ bodyPair ] . comp [ o2comp ] ) . pen > bestSat . pen ) { bestSat = sat ( BODIES [ index ] . comp [ o1comp ] , BODIES [ bodyPair ] . comp [ o2comp ] ) ; ctx . fillText ( "COLLISION" , 500 , 400 ) ; } } } if ( bestSat . pen !== null ) { COLLISIONS . push ( new CollData ( BODIES [ index ] , BODIES [ bodyPair ] , bestSat . axis , bestSat . pen , bestSat . vertex ) ) ; } } } ) ; COLLISIONS . forEach ( ( c ) => { c . penRes ( ) ; c . collRes ( ) ; } ) ; requestAnimationFrame ( mainLoop ) ; }
Game loop (60 frames per second)
Game loop ( 60 frames per second )