diff options
Diffstat (limited to 'engine/components')
-rw-r--r-- | engine/components/BoundingBox.ts | 57 | ||||
-rw-r--r-- | engine/components/Control.ts | 8 | ||||
-rw-r--r-- | engine/components/Forces.ts | 2 | ||||
-rw-r--r-- | engine/components/Sprite.ts | 6 | ||||
-rw-r--r-- | engine/components/Velocity.ts | 10 |
5 files changed, 41 insertions, 42 deletions
diff --git a/engine/components/BoundingBox.ts b/engine/components/BoundingBox.ts index 2b1d648..5e21b2f 100644 --- a/engine/components/BoundingBox.ts +++ b/engine/components/BoundingBox.ts @@ -1,6 +1,6 @@ import { Component, ComponentNames } from "."; import type { Coord2D, Dimension2D } from "../interfaces"; -import { dotProduct, rotateVector, normalizeVector } from "../utils"; +import { dotProduct, rotateVector } from "../utils"; export class BoundingBox extends Component { public center: Coord2D; @@ -15,10 +15,11 @@ export class BoundingBox extends Component { this.rotation = rotation ?? 0; } + // https://en.wikipedia.org/wiki/Hyperplane_separation_theorem public isCollidingWith(box: BoundingBox): boolean { const boxes = [this.getVertices(), box.getVertices()]; for (const poly of boxes) { - for (let i = 0; i < poly.length; ++i) { + for (let i = 0; i < poly.length; i++) { const [A, B] = [poly[i], poly[(i + 1) % poly.length]]; const normal: Coord2D = { x: B.y - A.y, y: A.x - B.x }; @@ -28,8 +29,8 @@ export class BoundingBox extends Component { const projection = dotProduct(normal, vertex); return [Math.min(min, projection), Math.max(max, projection)]; }, - [Infinity, -Infinity] - ) + [Infinity, -Infinity], + ), ); if (maxThis < minBox || maxBox < minThis) return false; @@ -55,43 +56,29 @@ export class BoundingBox extends Component { }); } - private getAxes() { - const corners: Coord2D[] = this.getVerticesRelativeToCenter(); - const axes: Coord2D[] = []; - - for (let i = 0; i < corners.length; ++i) { - const [cornerA, cornerB] = [ - corners[i], - corners[(i + 1) % corners.length], - ].map((corner) => rotateVector(corner, this.rotation)); - - axes.push( - normalizeVector({ - x: cornerB.y - cornerA.y, - y: -(cornerB.x - cornerA.x), - }) - ); + public getRotationInPiOfUnitCircle() { + let rads = this.rotation * (Math.PI / 180); + if (rads >= Math.PI) { + rads -= Math.PI; } - - return axes; + return rads; } - private project(axis: Coord2D): [number, number] { - const corners = this.getCornersRelativeToCenter(); - let [min, max] = [Infinity, -Infinity]; + public getOutscribedBoxDims(): Dimension2D { + let rads = this.getRotationInPiOfUnitCircle(); + const { width, height } = this.dimension; - for (const corner of corners) { - const rotated = rotateVector(corner, this.rotation); - const translated = { - x: rotated.x + this.center.x, - y: rotated.y + this.center.y, + if (rads <= Math.PI / 2) { + return { + width: Math.abs(height * Math.sin(rads) + width * Math.cos(rads)), + height: Math.abs(width * Math.sin(rads) + height * Math.cos(rads)), }; - const projection = dotProduct(translated, axis); - - min = Math.min(projection, min); - max = Math.max(projection, max); } - return [min, max]; + rads -= Math.PI / 2; + return { + width: Math.abs(height * Math.cos(rads) + width * Math.sin(rads)), + height: Math.abs(width * Math.cos(rads) + height * Math.sin(rads)), + }; } } diff --git a/engine/components/Control.ts b/engine/components/Control.ts index 094ef1c..1e782ee 100644 --- a/engine/components/Control.ts +++ b/engine/components/Control.ts @@ -1,7 +1,11 @@ -import { Component, ComponentNames } from "."; +import { Component, ComponentNames, Velocity } from "."; export class Control extends Component { - constructor() { + public controlVelocity: Velocity; + + constructor(controlVelocity: Velocity = new Velocity()) { super(ComponentNames.Control); + + this.controlVelocity = controlVelocity; } } diff --git a/engine/components/Forces.ts b/engine/components/Forces.ts index bf540a1..91ae1c1 100644 --- a/engine/components/Forces.ts +++ b/engine/components/Forces.ts @@ -1,4 +1,4 @@ -import type { Accel2D, Force2D } from "../interfaces"; +import type { Force2D } from "../interfaces"; import { Component } from "./Component"; import { ComponentNames } from "."; diff --git a/engine/components/Sprite.ts b/engine/components/Sprite.ts index 90e1389..bdb4982 100644 --- a/engine/components/Sprite.ts +++ b/engine/components/Sprite.ts @@ -17,7 +17,7 @@ export class Sprite extends Component { spriteImgPos: Coord2D, spriteImgDimensions: Dimension2D, msPerFrame: number, - numFrames: number + numFrames: number, ) { super(ComponentNames.Sprite); @@ -44,7 +44,7 @@ export class Sprite extends Component { ctx.save(); ctx.translate(center.x, center.y); - if (rotation != 0) { + if (rotation != undefined && rotation != 0) { ctx.rotate(rotation * (Math.PI / 180)); } ctx.translate(-center.x, -center.y); @@ -56,7 +56,7 @@ export class Sprite extends Component { ctx.drawImage( this.sheet, ...this.getSpriteArgs(), - ...this.getDrawArgs(drawArgs) + ...this.getDrawArgs(drawArgs), ); if (tint) { diff --git a/engine/components/Velocity.ts b/engine/components/Velocity.ts index 119427d..068d8cd 100644 --- a/engine/components/Velocity.ts +++ b/engine/components/Velocity.ts @@ -6,10 +6,18 @@ export class Velocity extends Component { public dCartesian: Velocity2D; public dTheta: number; - constructor(dCartesian: Velocity2D, dTheta: number) { + constructor(dCartesian: Velocity2D = { dx: 0, dy: 0 }, dTheta: number = 0) { super(ComponentNames.Velocity); this.dCartesian = dCartesian; this.dTheta = dTheta; } + + public add(velocity?: Velocity) { + if (velocity) { + this.dCartesian.dx += velocity.dCartesian.dx; + this.dCartesian.dy += velocity.dCartesian.dy; + this.dTheta += velocity.dTheta; + } + } } |