<commit_before>"use strict";

/* Physics module
 * The key functionality of this module, is that given a set of control inputs,
 * a movement vector and list of collisions that occurred is output.
 */

const zeroVector = new Vector2d();

const maxSpeed = 10;

function mod(v, m) {
    const r = v % m;
    return r < 0 ? r + m : r;
}

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    distanceTo(p2) {
        const x = p2.x - this.x;
        const y = p2.y - this.y;
        return Math.sqrt(x * x + y * y);
    }

    // Translate by something that has co-ordinates (e.g. another point, or
    // a vector.
    translate(coord) {
        this.x += coord.x;
        this.y += coord.y;
        return this;
    }

    copy() {
        return new Point(this.x, this.y);
    }
}

class Rect {
    constructor(x1, y1, x2, y2) {
        if (x2 < x1) {
            const temp = x2;
            x2 = x1;
            x1 = temp;
        }

        if (y2 < y1) {
            const temp = y2;
            y2 = y1;
            y1 = temp;
        }

        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    get width() {
        return this.x2 - this.x1;
    }

    get height() {
        return this.y2 - this.y1;
    }

    intersects(otherRect) {
        // No overlap if:
        // As right edge is to the left of Bs left edge
        // As left edge is to the right of Bs right edge
        // As bottom edge is above Bs top edge
        // As top edge is below Bs bottom edge
        if (this.x2 < otherRect.x1 || this.x1 > otherRect.x2 || this.y2 < otherRect.y1 || this.y1 > otherRect.y2)
            return false;
        return true;
    }
}

class PhysicsSystem {
    constructor(dimensions) {
        this.models = [];
        this.dimensions =
            new Rect(dimensions.minX, dimensions.minY, dimensions.maxX, dimensions.maxY);
    }

    dimensions(minX, minY, maxX, maxY) {
        this.dimensions = new Rect(minX, minY, maxX, maxY);
    }

    add(model) {
        this.models.push(model);
    }

    update(timeDelta) {
        function transferEnergy(a, b) {
            // momentum: p = mv
            // impulse-momentum change: F * t = mass * Delta v
            const temp = a.moveVector.copy();
            a.moveVector.add(b.moveVector);
            b.moveVector.add(temp);
        }

        function findPointOfCollision(a, b) {
             
        }

        for (let model of this.models) {
            model.update(timeDelta);
        }

        // check for collisions
        for (let model of this.models) {
            const modelPos = model.movePosition();
            for (let other of this.models) {
                if (other === model) continue;
                const otherPos = other.movePosition();
                if (modelPos.distanceTo(otherPos) < model.boundingCircleR + other.boundingCircleR) {
                    // change motion, transfer energy, do some damage, don't get stuck
                    console.log('collision');
                    transferEnergy(model, other); 
                }
            }
        }

        // move the objects
        for (let model of this.models) {
            model.move();
        }
    }
}

class Thruster {
    constructor(power, angle) {
        this._thrustVector = vector2d_from_angle(angle, power);
        this.isFiring = false;
    }

    set firing(isFiring) {
        this.isFiring = isFiring === true;
    }

    get thrustVector() {
        if (this.isFiring) {
            return this._thrustVector;
        }
        return zeroVector;
    }
}

class PhysicsModel {
    constructor(system, boundingCircleR, mass, position) {
        this.system = system;
        this.position = new Point(position.x, position.y);
        this.boundingCircleR = boundingCircleR;
        this.mass = mass;
        this.thrusters = [];
        this.rotateRate = 10;
        this.rotateDirection = 0;
        this.rotateAngle = 0;
        this._moveVector = new Vector2d();
        this.otherForce = new Set();
    }

    createThruster(power, angle_vector) {
        const thruster = new Thruster(power, angle_vector);
        this.thrusters.push(thruster);
        return thruster;
    }

    update(timeDelta) {
        const forceVector = new Vector2d();

        for (let other of this.otherForce) {
            if (other.duration > 0) {
                forceVector.add(other.vector);
                other.duration -= timeDelta;
            }
            // FIXME: Remove from the set
        }

        for (let thruster of this.thrusters) {
            forceVector.add(thruster.thrustVector)
        }

        forceVector.divide(this.mass);
        forceVector.divide(1000 / timeDelta);

        this.rotateAngle += (this.rotateDirection/ (this.rotateRate / timeDelta));
<commit_msg>Remove external force once it expires.<commit_after>"use strict";

/* Physics module
 * The key functionality of this module, is that given a set of control inputs,
 * a movement vector and list of collisions that occurred is output.
 */

const zeroVector = new Vector2d();

const maxSpeed = 10;

function mod(v, m) {
    const r = v % m;
    return r < 0 ? r + m : r;
}

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    distanceTo(p2) {
        const x = p2.x - this.x;
        const y = p2.y - this.y;
        return Math.sqrt(x * x + y * y);
    }

    // Translate by something that has co-ordinates (e.g. another point, or
    // a vector.
    translate(coord) {
        this.x += coord.x;
        this.y += coord.y;
        return this;
    }

    copy() {
        return new Point(this.x, this.y);
    }
}

class Rect {
    constructor(x1, y1, x2, y2) {
        if (x2 < x1) {
            const temp = x2;
            x2 = x1;
            x1 = temp;
        }

        if (y2 < y1) {
            const temp = y2;
            y2 = y1;
            y1 = temp;
        }

        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    get width() {
        return this.x2 - this.x1;
    }

    get height() {
        return this.y2 - this.y1;
    }

    intersects(otherRect) {
        // No overlap if:
        // As right edge is to the left of Bs left edge
        // As left edge is to the right of Bs right edge
        // As bottom edge is above Bs top edge
        // As top edge is below Bs bottom edge
        if (this.x2 < otherRect.x1 || this.x1 > otherRect.x2 || this.y2 < otherRect.y1 || this.y1 > otherRect.y2)
            return false;
        return true;
    }
}

class PhysicsSystem {
    constructor(dimensions) {
        this.models = [];
        this.dimensions =
            new Rect(dimensions.minX, dimensions.minY, dimensions.maxX, dimensions.maxY);
    }

    dimensions(minX, minY, maxX, maxY) {
        this.dimensions = new Rect(minX, minY, maxX, maxY);
    }

    add(model) {
        this.models.push(model);
    }

    update(timeDelta) {
        function transferEnergy(a, b) {
            // momentum: p = mv
            // impulse-momentum change: F * t = mass * Delta v
            const temp = a.moveVector.copy();
            a.moveVector.add(b.moveVector);
            b.moveVector.add(temp);
        }

        function findPointOfCollision(a, b) {
             
        }

        for (let model of this.models) {
            model.update(timeDelta);
        }

        // check for collisions
        for (let model of this.models) {
            const modelPos = model.movePosition();
            for (let other of this.models) {
                if (other === model) continue;
                const otherPos = other.movePosition();
                if (modelPos.distanceTo(otherPos) < model.boundingCircleR + other.boundingCircleR) {
                    // change motion, transfer energy, do some damage, don't get stuck
                    console.log('collision');
                    transferEnergy(model, other); 
                }
            }
        }

        // move the objects
        for (let model of this.models) {
            model.move();
        }
    }
}

class Thruster {
    constructor(power, angle) {
        this._thrustVector = vector2d_from_angle(angle, power);
        this.isFiring = false;
    }

    set firing(isFiring) {
        this.isFiring = isFiring === true;
    }

    get thrustVector() {
        if (this.isFiring) {
            return this._thrustVector;
        }
        return zeroVector;
    }
}

class PhysicsModel {
    constructor(system, boundingCircleR, mass, position) {
        this.system = system;
        this.position = new Point(position.x, position.y);
        this.boundingCircleR = boundingCircleR;
        this.mass = mass;
        this.thrusters = [];
        this.rotateRate = 10;
        this.rotateDirection = 0;
        this.rotateAngle = 0;
        this._moveVector = new Vector2d();
        this.otherForce = new Set();
    }

    createThruster(power, angle_vector) {
        const thruster = new Thruster(power, angle_vector);
        this.thrusters.push(thruster);
        return thruster;
    }

    update(timeDelta) {
        const forceVector = new Vector2d();

        this.otherForce.forEach(e => {
            forceVector.add(e.vector);
            e.duration -= timeDelta;
        });
        this.otherForce.filter = this.otherForce.filter(e => e.duration > 0);

        for (let thruster of this.thrusters) {
            forceVector.add(thruster.thrustVector)
        }

        forceVector.divide(this.mass);
        forceVector.divide(1000 / timeDelta);

        this.rotateAngle += (this.rotateDirection/ (this.rotateRate / timeDelta));

{'fraction_non_alphanumeric': 0.1095636025998143, 'fraction_numerical': 0.011348395749509955, 'mean_word_length': 1.608719052744887, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '56170', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2944, 'n_words': 773}