Skip to content

Instantly share code, notes, and snippets.

@thacuber2a03
Created June 1, 2022 15:38
Show Gist options
  • Select an option

  • Save thacuber2a03/47d79765210414ab9ce120379bb3bbc8 to your computer and use it in GitHub Desktop.

Select an option

Save thacuber2a03/47d79765210414ab9ce120379bb3bbc8 to your computer and use it in GitHub Desktop.
SCRIPT-8
init = state => {
}
update = (state, input, elapsed) => {
}
draw = state => {
}
class Vec2 {
constructor(x=0, y=0) {
this.x = x
this.y = y
}
set(x, y) {
this.x = x
this.y = y
}
setTo(other) {
this.x = other.x
this.y = other.y
}
length() { return Math.sqrt(this.x * this.x + this.y * this.y) }
distance(other) {
const x = this.x - other.x
const y = this.y - other.y
return Math.sqrt(x * x + y * y)
}
add(other) {
this.x += other.x
this.y += other.y
}
subtract(other) {
this.x -= other.x
this.y -= other.y
}
setSubtract(a, b) {
this.x = a.x - b.x
this.y = a.y - b.y
}
dot(other) { return this.x * other.x + this.y * other.y }
multiplyScalar(a) {
this.x *= a
this.y *= a
}
setMultiplyScalar(other, a) {
this.x = other.x * a
this.y = other.y * a
}
setNormal(a, b) {
// perpendicular
const x = a.y - b.y
const y = b.x - a.x
// normalize
const length = Math.sqrt(x * x + y * y)
if (length < Number.MIN_VALUE) {
this.x = x
this.y = y
return
}
const inverseLength = 1 / length
this.x = x * inverseLength
this.y = y * inverseLength
}
}
class Point {
constructor(parent, x, y) {
this.parent = parent
this.position = new Vec2(x, y)
this.oldPosition = new Vec2(x, y)
parent.vertices.push(this)
parent.positions.push(this.position)
vertices.push(this)
}
integrate() {
const p = this.position
const o = this.oldPosition
const x = p.x
const y = p.y
p.x += p.x - o.x
p.y += p.y - o.y + kGravity
o.set(x, y)
// screen limits
if (p.y < -100) p.y = -100
else if (p.y >= canvas.height + 250) {
p.x -= (p.x - o.x) * kFrictionGround
p.y = canvas.height - 1
}
if (p.x < 0) p.x = 0
else if (p.x >= canvas.width) p.x = canvas.width - 1
}
}
{
"iframeVersion": "0.1.280",
"lines": [
8,
69,
35,
0,
0,
0,
0,
0
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment