diff --git a/src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts b/src/lib/shimeji/physics2d/rigidbody/intersection_detector2d.ts similarity index 100% rename from src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts rename to src/lib/shimeji/physics2d/rigidbody/intersection_detector2d.ts diff --git a/src/lib/shimeji/renderer/renderer.ts b/src/lib/shimeji/renderer/renderer.ts index 2929726..8fd245d 100644 --- a/src/lib/shimeji/renderer/renderer.ts +++ b/src/lib/shimeji/renderer/renderer.ts @@ -4,6 +4,7 @@ class Renderer { private gl: WebGL2RenderingContext; private posBuffer!: WebGLBuffer; private program!: WebGLProgram; + private drawMode: DrawMode = DrawMode.TEX; constructor(canvas: HTMLCanvasElement) { let context = canvas.getContext('webgl2'); @@ -45,7 +46,6 @@ class Renderer { this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height); let resUniLoc = this.gl.getUniformLocation(program, 'u_resolution'); - this.gl.uniform2f(resUniLoc, this.gl.canvas.width, this.gl.canvas.height); this.gl.clearColor(0, 0, 0, 0); this.gl.clear(this.gl.COLOR_BUFFER_BIT); @@ -56,6 +56,7 @@ class Renderer { this.gl.bindBuffer(this.gl.ARRAY_BUFFER, pos); this.gl.vertexAttribPointer(0, 2, this.gl.FLOAT, false, 0, 0); + this.gl.uniform2f(resUniLoc, this.gl.canvas.width, this.gl.canvas.height); } catch (error) { console.log(error); this.gl.VERTEX_SHADER; @@ -91,7 +92,17 @@ class Renderer { this.gl.viewport(0, 0, width, height); } + /** + * Set the draw mode to one of the enum values + * @param {DrawMode} mode + * @returns {void} + */ + public set SetDrawMode(mode: DrawMode) {} + public Draw(pos: Vector2, size: number): void { + //this.gl.clearColor(0, 0, 0, 0); + // this.gl.clear(this.gl.COLOR_BUFFER_BIT); + let x1 = pos.x - size; let x2 = pos.x + size; let y1 = pos.y + size; @@ -101,7 +112,7 @@ class Renderer { this.gl.bufferData( this.gl.ARRAY_BUFFER, new Float32Array([x1, y1, x2, y1, x2, y2, x1, y1, x1, y2, x2, y2]), - this.gl.STATIC_DRAW + this.gl.DYNAMIC_DRAW ); var primitiveType = this.gl.TRIANGLES; @@ -111,4 +122,10 @@ class Renderer { } } +enum DrawMode { + TEX = 0, + WIRE = 1, + VERT = 2 +} + export default Renderer; diff --git a/src/lib/shimeji/renderer/shaders/vertex_shader.vert b/src/lib/shimeji/renderer/shaders/vertex_shader.vert index a6e9ec3..f6d7484 100644 --- a/src/lib/shimeji/renderer/shaders/vertex_shader.vert +++ b/src/lib/shimeji/renderer/shaders/vertex_shader.vert @@ -11,5 +11,5 @@ void main() { gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); //gl_Position = vec4(a_position, 0, 1); - v_color = vec4(a_position.xyx * 0.5 + 0.5, 1.0); + v_color = vec4(a_position.xy, 0.0, 1.0); } \ No newline at end of file diff --git a/src/lib/shimeji/renderer/web_g_engine.ts b/src/lib/shimeji/renderer/web_g_engine.ts new file mode 100644 index 0000000..a3cf1c9 --- /dev/null +++ b/src/lib/shimeji/renderer/web_g_engine.ts @@ -0,0 +1,37 @@ +namespace WebGEngine { + class Engine { + private _GL: WebGL2RenderingContext; + private _DrawMode: EngineDrawMode = EngineDrawMode.TEXTURE; + + constructor(canvas: HTMLCanvasElement) { + let context = canvas.getContext('webgl2'); + if (!context) throw new Error(`Failed to get webgl2 context from ${canvas}`); + + this._GL = context; + this.init(); + } + + private init(): void {} + } + + enum EngineDrawMode { + TEXTURE = 0, + WIREFRAME = 1, + VERTEX = 2 + } + + interface ShaderSrc { + type: number, + + } + + interface EngineConfig { + canvas: HTMLCanvasElement, + vertexShader: string, + fragmentShader: string, + additionalShaders?: + + } +} + +export default WebGEngine; diff --git a/src/lib/stores/shimeji.ts b/src/lib/stores/shimeji.ts index 20273ea..2652b15 100644 --- a/src/lib/stores/shimeji.ts +++ b/src/lib/stores/shimeji.ts @@ -2,10 +2,9 @@ import Renderer from '$lib/shimeji/renderer/renderer'; import Vector2 from '../vector'; export class Shimeji { - private element: HTMLElement; private canvas: HTMLCanvasElement; private renderer: Renderer; - private rect: DOMRect; + private size: number; private pos: Vector2 = Vector2.zero; private vel: Vector2 = Vector2.zero; @@ -29,11 +28,10 @@ export class Shimeji { private bounds: { minX: number; minY: number; maxX: number; maxY: number }; - constructor(element: HTMLElement, canvas: HTMLCanvasElement, renderer: Renderer) { - this.element = element; + constructor(size: number = 40, canvas: HTMLCanvasElement, renderer: Renderer) { + this.size = size; this.canvas = canvas; this.renderer = renderer; - this.rect = element.getBoundingClientRect(); this.bounds = { minX: 0, @@ -43,13 +41,10 @@ export class Shimeji { }; this.pos.set(window.innerWidth / 2, window.innerHeight / 2); + this.vel.set(Math.floor(Math.random() * 5000) - 2500, Math.floor(Math.random() * 5000) - 2500); this.updateBounds(); window.addEventListener('resize', () => this.updateBounds()); - - console.log(this.element.getBoundingClientRect()); - - this.setupDragEvents(); } private updateBounds(): void { @@ -94,8 +89,8 @@ export class Shimeji { } } - private setupDragEvents(): void { - this.element.addEventListener('mousedown', (e) => { + /* private setupDragEvents(): void { + this.element.addEventListener('mousedown', (e) => { e.preventDefault(); this.isBeingDragged = true; this.element.style.cursor = 'grabbing'; @@ -129,9 +124,10 @@ export class Shimeji { this.element.addEventListener('dragstart', (e) => { e.preventDefault(); }); - } + } */ private updatePhysics(dt: number): void { + console.log(this.vel.x, this.vel.y); if (this.isBeingDragged) { const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos); @@ -148,17 +144,21 @@ export class Shimeji { this.vel.y += this.GRAVITY; this.vel.mulEq(this.DAMPING); - if (this.pos.y + this.rect.width >= this.bounds.maxY) { + if (this.pos.y + this.size >= this.bounds.maxY) { this.vel.x *= this.FRICTION; } + this.vel.deadZoneEq(0.00001); + //if (Math.abs(this.vel.x) <= 0.00001) this.vel.x = 0; + //if (Math.abs(this.vel.y) <= 0.00001) this.vel.y = 0; + const outVel = this.vel.mul(dt); this.pos.addEq(outVel); } private applyBoundaries(): void { - const leftBound = this.rect.width / 2; - const rightBound = this.bounds.maxX - this.rect.width / 2; + const leftBound = this.size; + const rightBound = this.bounds.maxX - this.size; if (this.pos.x < leftBound) { this.pos.x = leftBound; @@ -168,8 +168,8 @@ export class Shimeji { this.vel.x = -this.RESTITUTION * this.vel.x; } - const topBound = this.rect.height / 2; - const bottomBound = this.bounds.maxY - this.rect.height / 2; + const topBound = this.size; + const bottomBound = this.bounds.maxY - this.size; if (this.pos.y < topBound) { this.pos.y = topBound; @@ -192,8 +192,7 @@ export class Shimeji { } private render(): void { - this.element.style.transform = `translate(${this.pos.x}px, ${this.pos.y}px)`; - this.renderer.Draw(this.pos, this.rect.width / 2); + this.renderer.Draw(this.pos, this.size); /* if(this.isBeingDragged) { this.drawRay( diff --git a/src/lib/vector.ts b/src/lib/vector.ts index effffd6..391d372 100644 --- a/src/lib/vector.ts +++ b/src/lib/vector.ts @@ -1,276 +1,281 @@ class Vector2 { - public x: number; - public y: number; + public x: number; + public y: number; - constructor(x?: number, y?: number); - constructor(v?: Vector2); - constructor(x: Vector2 | number = 0, y: number = 0) { - if (typeof x === 'object' && x !== null) { - this.x = x.x; - this.y = x.y; - } - else { - this.x = x; - this.y = y; - } - } + constructor(x?: number, y?: number); + constructor(v?: Vector2); + constructor(x: Vector2 | number = 0, y: number = 0) { + if (typeof x === 'object' && x !== null) { + this.x = x.x; + this.y = x.y; + } else { + this.x = x; + this.y = y; + } + } - add(s: number): Vector2; - add(v: Vector2): Vector2; - add(arg: number | Vector2): Vector2 { - if (typeof arg === 'number') { - return new Vector2(this.x + arg, this.y + arg); - } - else { - return new Vector2(this.x + arg.x, this.y + arg.y); - } - } + add(s: number): Vector2; + add(v: Vector2): Vector2; + add(arg: number | Vector2): Vector2 { + if (typeof arg === 'number') { + return new Vector2(this.x + arg, this.y + arg); + } else { + return new Vector2(this.x + arg.x, this.y + arg.y); + } + } - sub(s: number): Vector2; - sub(v: Vector2): Vector2; - sub(arg: number | Vector2): Vector2 { - if (typeof arg === 'number') { - return new Vector2(this.x - arg, this.y - arg); - } - else { - return new Vector2(this.x - arg.x, this.y - arg.y); - } - } + sub(s: number): Vector2; + sub(v: Vector2): Vector2; + sub(arg: number | Vector2): Vector2 { + if (typeof arg === 'number') { + return new Vector2(this.x - arg, this.y - arg); + } else { + return new Vector2(this.x - arg.x, this.y - arg.y); + } + } - mul(s: number): Vector2; - mul(v: Vector2): Vector2; - mul(arg: number | Vector2): Vector2 { - if (typeof arg === 'number') { - return new Vector2(this.x * arg, this.y * arg); - } - else { - return new Vector2(this.x * arg.x, this.y * arg.y); - } - } + mul(s: number): Vector2; + mul(v: Vector2): Vector2; + mul(arg: number | Vector2): Vector2 { + if (typeof arg === 'number') { + return new Vector2(this.x * arg, this.y * arg); + } else { + return new Vector2(this.x * arg.x, this.y * arg.y); + } + } - div(s: number): Vector2; - div(v: Vector2): Vector2; - div(arg: number | Vector2): Vector2 { - if (typeof arg === 'number') { - return new Vector2(this.x / arg, this.y / arg); - } - else { - return new Vector2(this.x / arg.x, this.y / arg.y); - } - } + div(s: number): Vector2; + div(v: Vector2): Vector2; + div(arg: number | Vector2): Vector2 { + if (typeof arg === 'number') { + return new Vector2(this.x / arg, this.y / arg); + } else { + return new Vector2(this.x / arg.x, this.y / arg.y); + } + } - equals(v: Vector2, epsilon: number = 1e-10): boolean { - return Math.abs(this.x - v.x) < epsilon && Math.abs(this.y - v.y) < epsilon; - } + equals(v: Vector2, epsilon: number = 1e-10): boolean { + return Math.abs(this.x - v.x) < epsilon && Math.abs(this.y - v.y) < epsilon; + } - get sqrLength(): number { - return this.x * this.x + this.y * this.y; - } + get sqrLength(): number { + return this.x * this.x + this.y * this.y; + } - get length(): number { - return Math.sqrt(this.sqrLength); - } + get length(): number { + return Math.sqrt(this.sqrLength); + } - get normalized(): Vector2 { - const len = this.length; - return len === 0 ? new Vector2(0, 0) : new Vector2(this.x / len, this.y / len); - } + get normalized(): Vector2 { + const len = this.length; + return len === 0 ? new Vector2(0, 0) : new Vector2(this.x / len, this.y / len); + } - get angle(): number { - return Math.atan2(this.y, this.x); - } + get angle(): number { + return Math.atan2(this.y, this.x); + } - addEq(s: number): this; - addEq(v: Vector2): this; - addEq(arg: number | Vector2): this { - if (typeof arg === 'number') { - this.x += arg; - this.y += arg; - } - else { - this.x += arg.x; - this.y += arg.y; - } - return this; - } + addEq(s: number): this; + addEq(v: Vector2): this; + addEq(arg: number | Vector2): this { + if (typeof arg === 'number') { + this.x += arg; + this.y += arg; + } else { + this.x += arg.x; + this.y += arg.y; + } + return this; + } - subEq(s: number): this; - subEq(v: Vector2): this; - subEq(arg: number | Vector2): this { - if (typeof arg === 'number') { - this.x -= arg; - this.y -= arg; - } - else { - this.x -= arg.x; - this.y -= arg.y; - } - return this; - } + subEq(s: number): this; + subEq(v: Vector2): this; + subEq(arg: number | Vector2): this { + if (typeof arg === 'number') { + this.x -= arg; + this.y -= arg; + } else { + this.x -= arg.x; + this.y -= arg.y; + } + return this; + } - mulEq(s: number): this; - mulEq(v: Vector2): this; - mulEq(arg: number | Vector2): this { - if (typeof arg === 'number') { - this.x *= arg; - this.y *= arg; - } - else { - this.x *= arg.x; - this.y *= arg.y; - } - return this; - } + mulEq(s: number): this; + mulEq(v: Vector2): this; + mulEq(arg: number | Vector2): this { + if (typeof arg === 'number') { + this.x *= arg; + this.y *= arg; + } else { + this.x *= arg.x; + this.y *= arg.y; + } + return this; + } - divEq(s: number): this; - divEq(v: Vector2): this; - divEq(arg: number | Vector2): this { - if (typeof arg === 'number') { - this.x /= arg; - this.y /= arg; - } - else { - this.x /= arg.x; - this.y /= arg.y; - } - return this; - } + divEq(s: number): this; + divEq(v: Vector2): this; + divEq(arg: number | Vector2): this { + if (typeof arg === 'number') { + this.x /= arg; + this.y /= arg; + } else { + this.x /= arg.x; + this.y /= arg.y; + } + return this; + } - cross(v: Vector2): number { - return this.x * v.y - this.y * v.x; - } + cross(v: Vector2): number { + return this.x * v.y - this.y * v.x; + } - dot(v: Vector2): number { - return this.x * v.x + this.y * v.y; - } + dot(v: Vector2): number { + return this.x * v.x + this.y * v.y; + } - set(x: number, y: number): this { - this.x = x; - this.y = y; - return this; - } + set(x: number, y: number): this { + this.x = x; + this.y = y; + return this; + } - normalize(): this { - const len = this.length; - if (len !== 0) { - this.x /= len; - this.y /= len; - } - return this; - } + normalize(): this { + const len = this.length; + if (len !== 0) { + this.x /= len; + this.y /= len; + } + return this; + } - get perpendicular(): Vector2 { - return new Vector2(-this.y, this.x); - } + get perpendicular(): Vector2 { + return new Vector2(-this.y, this.x); + } - clone(): Vector2 { - return new Vector2(this.x, this.y); - } + clone(): Vector2 { + return new Vector2(this.x, this.y); + } - toString(): string { - return `Vector2(${this.x}, ${this.y})`; - } + toString(): string { + return `Vector2(${this.x}, ${this.y})`; + } - toArray(): [number, number] { - return [this.x, this.y]; - } + toArray(): [number, number] { + return [this.x, this.y]; + } - [Symbol.iterator](): Iterator { - let index = 0; - const components = [this.x, this.y]; - return { - next: (): IteratorResult => { - if (index < components.length) { - return { value: components[index++], done: false }; - } - else { - return { value: undefined as any, done: true }; - } - }, - }; - } + [Symbol.iterator](): Iterator { + let index = 0; + const components = [this.x, this.y]; + return { + next: (): IteratorResult => { + if (index < components.length) { + return { value: components[index++], done: false }; + } else { + return { value: undefined as any, done: true }; + } + } + }; + } - deadZone(threshold: number = 1e-4): Vector2 { - return new Vector2( - Math.abs(this.x) < threshold ? 0 : this.x, - Math.abs(this.y) < threshold ? 0 : this.y - ); - } + /** + * Checks if this.xy below threshold, if so - set to 0 (each param individually) + * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1) + * @returns {this} new Vector2 with values of this or 0 + * @example + * // sets this.x or this.y to 0 if less then 0.0000001 + * vecFoo.deadZone(1e-6); + */ + deadZone(threshold: number = 1e-4): Vector2 { + return new Vector2( + Math.abs(this.x) < threshold ? 0 : this.x, + Math.abs(this.y) < threshold ? 0 : this.y + ); + } - deadZoneEq(threshold: number = 1e-4): this { - if (Math.abs(this.x) < threshold) this.x = 0; - if (Math.abs(this.y) < threshold) this.y = 0; - return this; - } + /** + * Checks if *this*xy below threshold, if so - set to 0 (each param individually) + * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1) + * @returns {Vector2} this - i.e. the vector that calls the method. + * @example + * // sets this.x or this.y to 0 if less then 0.0000001 + * vecFoo.deadZone(1e-6); + */ + deadZoneEq(threshold: number = 1e-4): this { + if (Math.abs(this.x) < threshold) this.x = 0; + if (Math.abs(this.y) < threshold) this.y = 0; + return this; + } - static fromAngle(angle: number, magnitude: number = 1): Vector2 { - return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude); - } + static fromAngle(angle: number, magnitude: number = 1): Vector2 { + return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude); + } - static dot(a: Vector2, b: Vector2): number { - return a.x * b.x + a.y * b.y; - } + static dot(a: Vector2, b: Vector2): number { + return a.x * b.x + a.y * b.y; + } - static cross(a: Vector2, b: Vector2): number { - return a.x * b.y - a.y * b.x; - } + static cross(a: Vector2, b: Vector2): number { + return a.x * b.y - a.y * b.x; + } - static distance(a: Vector2, b: Vector2): number { - const dx = a.x - b.x; - const dy = a.y - b.y; - return Math.sqrt(dx * dx + dy * dy); - } + static distance(a: Vector2, b: Vector2): number { + const dx = a.x - b.x; + const dy = a.y - b.y; + return Math.sqrt(dx * dx + dy * dy); + } - static distanceSqr(a: Vector2, b: Vector2): number { - const dx = a.x - b.x; - const dy = a.y - b.y; - return dx * dx + dy * dy; - } + static distanceSqr(a: Vector2, b: Vector2): number { + const dx = a.x - b.x; + const dy = a.y - b.y; + return dx * dx + dy * dy; + } - static lerp(a: Vector2, b: Vector2, t: number): Vector2 { - return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); - } + static lerp(a: Vector2, b: Vector2, t: number): Vector2 { + return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); + } - static clamp(v: Vector2, min: Vector2, max: Vector2): Vector2 { - return new Vector2( - Math.min(Math.max(v.x, min.x), max.x), - Math.min(Math.max(v.y, min.y), max.y) - ); - } + static clamp(v: Vector2, min: Vector2, max: Vector2): Vector2 { + return new Vector2( + Math.min(Math.max(v.x, min.x), max.x), + Math.min(Math.max(v.y, min.y), max.y) + ); + } - static angleBetween(a: Vector2, b: Vector2): number { - const dot = Vector2.dot(a, b); - const mag = a.length * b.length; - if (mag === 0) - return 0; - - const cos = dot / mag; - return Math.acos(Math.min(1, Math.max(-1, cos))); - } + static angleBetween(a: Vector2, b: Vector2): number { + const dot = Vector2.dot(a, b); + const mag = a.length * b.length; + if (mag === 0) return 0; - static get zero(): Vector2 { - return new Vector2(0, 0); - } + const cos = dot / mag; + return Math.acos(Math.min(1, Math.max(-1, cos))); + } - static get one(): Vector2 { - return new Vector2(1, 1); - } + static get zero(): Vector2 { + return new Vector2(0, 0); + } - static get up(): Vector2 { - return new Vector2(0, 1); - } + static get one(): Vector2 { + return new Vector2(1, 1); + } - static get down(): Vector2 { - return new Vector2(0, -1); - } + static get up(): Vector2 { + return new Vector2(0, 1); + } - static get left(): Vector2 { - return new Vector2(-1, 0); - } + static get down(): Vector2 { + return new Vector2(0, -1); + } - static get right(): Vector2 { - return new Vector2(1, 0); - } + static get left(): Vector2 { + return new Vector2(-1, 0); + } + + static get right(): Vector2 { + return new Vector2(1, 0); + } } export default Vector2; diff --git a/src/modules/Shimeji.svelte b/src/modules/Shimeji.svelte index 5377662..169bfe5 100644 --- a/src/modules/Shimeji.svelte +++ b/src/modules/Shimeji.svelte @@ -1,7 +1,6 @@