Engine

Create a namespace to store all the physics engine core functionality

Engine

Type: ({} | {})

Static Members
Core
Physics
Level
Player
EndGame

lastRenderTime

Engine Loop

lastRenderTime

collision

Collision detection

collision()

Type: function

resolveCollision

Resolve collision using the Impulse Method

resolveCollision(s1: any, s2: any, collisionInfo: CollisionInfo)
Parameters
s1 (any) one of the colliding objects
s2 (any) one od the colliding objects
collisionInfo (CollisionInfo) collision info calculated while collision detection

p

Collision position

p

Type: Vector

tangent

Tangent to a collision normal

tangent

Type: Vector

positionalCorrection

Reduces overlaps between objects by the predefined constant correctionRate

positionalCorrection(s1: any, s2: any, collisionInfo: CollisionInfo)
Parameters
s1 (any)
s2 (any)
collisionInfo (CollisionInfo)

correctionAmount

correctionAmount

Type: Vector

drag

Add drag to objects if they are in a drag area

drag()

j

Change Rigid Shape velocity due to drag

j

screen

Component to store canvas data

screen

Type: {mHeight: any, mWidth: any, mCanvas: HTMLElement, mContext: any}

x

Canvas boundaries

x

Bullet

Class that represents a bullet

new Bullet()
Instance Members
acceleration

BulletSource

A class that represents a bullet source

new BulletSource()
Instance Members
shoot()
takeDamage()

FuelTank

A class that represent a fuel tank

new FuelTank()
Instance Members
pickUp()

drawLevelSky

Function to draw canvas background

drawLevelSky()

setBunkers

Create bunkers object on level

setBunkers()

setFuel

Create fuel object on level

setFuel()

setDrag

Create drag areas object on level

setDrag()

setScene

Set level initial scene

setScene()

setTerrain

Set terrain on level

setTerrain()

Terrain

Terrain object, stores all terrain elements

new Terrain()

playButton

Button to start game

playButton

Type: {x: number, width: number, y: number, height: number}

drawButton

Draw a button to start game

drawButton()

clickBtn

Check if user clicked a button

clickBtn(e: any)
Parameters
e (any) click event

setUp

Set event listener and draw a button

setUp()

getMousePos

Get user's mouse coordinates

getMousePos(event: any): {x: number, y: number}
Parameters
event (any) mouse event
Returns
{x: number, y: number}: mouse coordinates

isInside

Check if mouse is inside a button

isInside(pos: any, button: any): boolean
Parameters
pos (any) mouse position
button (any) button
Returns
boolean:

startGame

Start game, clear previous data

startGame()

PlayerShip

Class that represents player ship

new PlayerShip()
Instance Members
shoot()
loseLife()

ui

Object to store game stats

ui

Type: {score: number, lives: number, fuel: number}

displayUI

Function to display stats on canvas

displayUI()

updateUI

Function to update stats in each loop run

updateUI(score: any, lives: any, fuel: any)
Parameters
score (any) player score
lives (any) player lives left
fuel (any) player fuel

handleKeyInput

Handle keyboard inputs

handleKeyInput(event: any): boolean
Parameters
event (any) keyboard event
Returns
boolean:

CollisionInfo

Class that represents collision info

new CollisionInfo()
Instance Members
setInfo(d, n, s)
changeDirection()
display()

DragArea

Class that represents a drag area

new DragArea()
Instance Members
update()
drawWater()

SineWave

Class that represents a sine wave used in DragArea animation

new SineWave()

sleep

Function to execute code after a given time

sleep(ms: any): Promise<unknown>
Parameters
ms (any) waiting time
Returns
Promise<unknown>:

Vector

A class that represents a vector using Cartesian coordinates

new Vector()
Instance Members
add(a)
subtract(a)
scale(n)
mag()
normalize()
dot(a)
cross(a)
rotate(angle, center)
draw(color)
copy()

constructor

Constructor of Circle class

constructor(mass: number, center: Vector, radius: number, angle: number, friction: number, restitution: number, gravity: boolean, info: string)
Parameters
mass (number) Circle mass
center (Vector) Circle center location
radius (number) Circle radius
angle (number = 0) Circle angle in radians
friction (number = 0) Circle friction
restitution (number = 0) Circle restitution (bounciness) (how much energy is preserved after collision)
gravity (boolean = true) consider Circle when applying gravity
info (string = "") additional info

rotate

Rotate circle

rotate(angle: number)
Parameters
angle (number) rotation angle in radians

displayBounds

Display hit box

displayBounds()

updateInertia

Set and update inertia

updateInertia()

collisionTest

Check if collision occurred, call specified methods to handle collision

collisionTest(otherShape: any, collisionInfo: any): boolean
Parameters
otherShape (any) other shape in the collision event
collisionInfo (any) collision info
Returns
boolean: did the collision occur

collidedCircCirc

Handle collision between circles

collidedCircCirc(c1: any, c2: any, collisionInfo: any): (boolean | CollisionInfo)
Parameters
c1 (any) first circle
c2 (any) second circle
collisionInfo (any) collision info
Returns
(boolean | CollisionInfo):

from1To2

from1To2

Type: Vector

constructor

Constructor of a Rectangle object

constructor(mass: number, center: Vector, width: number, height: number, angle: number, friction: number, restitution: number, gravity: boolean, info: string)
Parameters
mass (number) Mass of a Rectangle object
center (Vector) Mass center of a Rectangle object
width (number) Width of a Rectangle object
height (number) Height of a Rectangle object
angle (number = 0) Angle in radians of a Rectangle object axis to global (canvas) x-axis
friction (number = 0) Rectangle friction
restitution (number = 0) Rectangle restitution (bounciness) (how much energy is preserved after collision)
gravity (boolean = true) consider the Rectangle when applying gravity
info (string = "") additional info

vertex

Array to store vertex positions of the rectangle

vertex

Type: Array<Vector>

faceNormal

Array to store the face normal vectors

faceNormal

Type: Array<Vector>

computeVertex

Compute Rectangle vertices

computeVertex()

computeFaceNormal

Compute face normal vectors

computeFaceNormal()

rotate

Rotate Rectangle

rotate(angle: number)
Parameters
angle (number) Angle in radians

updateInertia

Set and update inertia

updateInertia()

collisionTest

collisionTest(otherShape: any, collisionInfo: any): boolean
Parameters
otherShape (any)
collisionInfo (any)
Returns
boolean:

findSupportPoint

Compute a support point based on dir (find a support point for any face normal)

findSupportPoint(dir: Vector, ptOnEdge: Vector): {supportPoint: Vector, supportPointDist: number}
Parameters
dir (Vector) Negated face normal direction
ptOnEdge (Vector) A position on the given edge (e.g. a vertex)
Returns
{supportPoint: Vector, supportPointDist: number}:

findAxisLeastPenetration

Find the axis of the least penetration based on the support point with the least support point distant

findAxisLeastPenetration(otherRect: Rectangle, collisionInfo: any): boolean
Parameters
otherRect (Rectangle) other Rectangle with which collision occurs
collisionInfo (any) collision info
Returns
boolean: hasSupport

collidedRectRect

Compute the axis of lest penetration and choose smaller of the two results

collidedRectRect(r1: Rectangle, r2: Rectangle, collisionInfo: CollisionInfo): boolean
Parameters
r1 (Rectangle) Rectangle that called the method
r2 (Rectangle) Rectangle with which collision occurs
collisionInfo (CollisionInfo) collision info
Returns
boolean:

collidedRectCirc

Detect collision according to the relative position of the circle's center with respect to the rectangle

collidedRectCirc(otherCir: Circle, collisionInfo: CollisionInfo)
Parameters
otherCir (Circle) collided Circle
collisionInfo (CollisionInfo) collision info

supportPoint

supportPoint

Type: Vector

supportPoint

supportPoint

Type: Vector

collisionInfoR1

collisionInfoR1

Type: CollisionInfo

collisionInfoR1

collisionInfoR1

Type: CollisionInfo

collisionInfoR2

collisionInfoR2

Type: CollisionInfo

collisionInfoR2

collisionInfoR2

Type: CollisionInfo

radiusVec

radiusVec

Type: Vector

RigidShape

Abstract class that represents a rigid body

new RigidShape()
Instance Members
angularVelocity
update()
move(s)
updateMass(delta)
collisionTest(otherShape, collisionInfo)
boundTest(otherShape)

isGravity

Symplectic Euler Integration

isGravity

constructor

Constructor of a Triangle object

constructor(mass: number, center: Vector, width: number, height: number, angle: number, friction: number, restitution: number, gravity: boolean, info: string)
Parameters
mass (number) Mass of a Triangle object
center (Vector) Mass center of a Triangle object
width (number) Width of a Triangle object
height (number) Height of a Triangle object
angle (number = 0) Angle in radians of a Triangle object axis to global (canvas) x-axis
friction (number = 0) Triangle friction
restitution (number = 0) Triangle restitution (bounciness) (how much energy is preserved after collision)
gravity (boolean = true) consider the Triangle when applying gravity
info (string = "") additional info

vertex

Array to store vertex positions of the triangle

vertex

Type: Array<Vector>

faceNormal

Array to store the face normal vectors

faceNormal

Type: Array<Vector>

computeVertex

Compute Rectangle vertices

computeVertex()

computeFaceNormal

Compute face normal vectors

computeFaceNormal()

rotate

Rotate Rectangle

rotate(angle: number)
Parameters
angle (number) Angle in radians

updateInertia

Set and update inertia

updateInertia()

collisionTest

Check if collision occurred, call specified methods to handle collision

collisionTest(otherShape: any, collisionInfo: any): boolean
Parameters
otherShape (any) other shape in the collision event
collisionInfo (any) collision info
Returns
boolean: did the collision occur

findSupportPoint

Compute a support point based on dir (find a support point for any face normal)

findSupportPoint(dir: Vector, ptOnEdge: Vector): {supportPoint: Vector, supportPointDist: number}
Parameters
dir (Vector) Negated face normal direction
ptOnEdge (Vector) A position on the given edge (e.g. a vertex)
Returns
{supportPoint: Vector, supportPointDist: number}:

findAxisLeastPenetration

Find the axis of the least penetration based on the support point with the least support point distance

findAxisLeastPenetration(otherShape: (Rectangle | Triangle), collisionInfo: any): boolean
Parameters
otherShape ((Rectangle | Triangle)) other Rectangle or Triangle with which collision occurs
collisionInfo (any) collision info
Returns
boolean: hasSupport

collidedTrianTrianRect

Compute the axis of lest penetration and choose smaller of the two results

collidedTrianTrianRect(r1: Triangle, r2: (Triangle | Rectangle), collisionInfo: CollisionInfo): boolean
Parameters
r1 (Triangle) Triangle that called the method
r2 ((Triangle | Rectangle)) Triangle or Rectangle with which collision occurs
collisionInfo (CollisionInfo) collision info
Returns
boolean:

collidedTrianCirc

Detect collision according to the relative position of the circle's center with respect to the triangle

collidedTrianCirc(otherCirc: any, collisionInfo: any): boolean
Parameters
otherCirc (any) collided Circle
collisionInfo (any) collision info
Returns
boolean:

normal

normal

Type: Vector