top of page
Search
cedarcantab

sopiro poly


polygon


import { RigidBody, Type } from "./rigidbody.js"; import { Vector2 } from "./math.js"; import as Util from "./util.js"; import { Settings } from "./settings.js"; // Children: Box export class Polygon extends RigidBody { public readonly vertices: Vector2[]; public readonly area: number; constructor(vertices: Vector2[], type: Type = Type.Dynamic, resetPosition: boolean = true, density: number = Settings.defaultDensity) { super(type); this.vertices = vertices; let centerOfMass = new Vector2(0, 0); let count = this.count; for (let i = 0; i < count; i++) { centerOfMass.x += this.vertices[i].x; centerOfMass.y += this.vertices[i].y; } centerOfMass.x /= count; centerOfMass.y /= count; let area = 0; this.vertices[0].x -= centerOfMass.x; this.vertices[0].y -= centerOfMass.y; for (let i = 1; i < count; i++) { this.vertices[i].x -= centerOfMass.x; this.vertices[i].y -= centerOfMass.y; area += this.vertices[i - 1].cross(this.vertices[i]); } area += this.vertices[count - 1].cross(this.vertices[0]); this.area = Math.abs(area) / 2.0; if (this.type == Type.Dynamic) { Util.assert(density > 0); this._density = density; this._mass = density this.area; this._invMass = 1.0 / this._mass; this._inertia = Util.calculateConvexPolygonInertia(this.vertices, this._mass, this.area); this._invInertia = 1.0 / this._inertia; } if (!resetPosition) this.translate(centerOfMass); } repositionCenterOfMass(p: Vector2) { for (let i = 0; i < this.vertices.length; i++) { let vertex = this.vertices[i]; vertex.x -= p.x; vertex.y -= p.y; } } get count(): number { return this.vertices.length; } override get mass(): number { return this._mass; } // This will automatically set the inertia override set mass(mass: number) { Util.assert(mass > 0); this._density = mass / this.area; this._mass = mass; this._invMass = 1.0 / this._mass; this._inertia = Util.calculateConvexPolygonInertia(this.vertices, this._mass, this.area); this._invInertia = 1.0 / this._inertia; } override get density(): number { return this._density; } // This will automatically set the mass and inertia override set density(density: number) { Util.assert(density > 0); this._density = density; this._mass = density * this.area; this._invMass = 1.0 / this._mass; this._inertia = Util.calculateConvexPolygonInertia(this.vertices, this._mass, this.area); this._invInertia = 1.0 / this._inertia; } }



box


import { Type } from "./rigidbody.js";
import { Vector2 } from "./math.js";
import { Polygon } from "./polygon.js";
import * as Util from "./util.js";
import { Settings } from "./settings.js";

export class Box extends Polygon
{
    public readonly width;
    public readonly height;

    constructor(width: number, height: number = width, type: Type = Type.Dynamic, density: number = Settings.defaultDensity)
    {
        super([new Vector2(0, 0), new Vector2(0, height), new Vector2(width, height), new Vector2(width, 0)], type, true, density);

        this.width = width;
        this.height = height;
    }

    override get mass(): number
    {
        return this._mass;
    }

    // This will automatically set the inertia
    override set mass(mass: number)
    {
        Util.assert(mass > 0);

        this._density = mass / this.area;
        this._mass = mass;
        this._invMass = 1.0 / this._mass;
        this._inertia = Util.calculateBoxInertia(this.width, this.height, this._mass);
        this._invInertia = 1.0 / this._inertia;
    }

    override get density(): number
    {
        return this._density;
    }

    // This will automatically set the mass and inertia
    override set density(density: number)
    {
        Util.assert(density > 0);

        this._density = density;
        this._mass = density * this.area;
        this._invMass = 1.0 / this._mass;
        this._inertia = Util.calculateBoxInertia(this.width, this.height, this._mass);
        this._invInertia = 1.0 / this._inertia;
    }
}

5 views0 comments

Recent Posts

See All

p2 naive broadphase

var Broadphase = require('../collision/Broadphase'); module.exports = NaiveBroadphase; /** * Naive broadphase implementation. Does N^2...

sopiro motor constranit

import { Matrix2, Vector2 } from "./math.js"; import { RigidBody } from "./rigidbody.js"; import { Settings } from "./settings.js";...

Comments


記事: Blog2_Post
bottom of page