Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active June 23, 2018 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielflorit/c82ded0b9399b00bad529ec05c9680ff to your computer and use it in GitHub Desktop.
Save gabrielflorit/c82ded0b9399b00bad529ec05c9680ff to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Projectiles
const sin = x => Math.sin(x)
const cos = x => Math.cos(x)
const exp = x => Math.exp(x)
const x0 = 0
const y0 = 0
const g = 9.8 // gravity (m/s2)
const v = 48 // exit velocity (m/s)
const a = 0.6
const deg = a * 180 / Math.PI // launch angle (degrees)
//const a = deg * Math.PI / 180 // launch angle (radians)
const R = 121 // desired final distance
const k = 0.2
// calculate vt given R
const vt = R * g / (v * cos(a))
// 0.266
initialState = { x: 0, y: 0 }
update = state => {}
const project = p => ([p[0], 127 - p[1]])
draw = state => {
clear()
// axes and labels
line(0, 0, 0, 127, 6)
line(0, 127, 127, 127, 6)
print(2, 0, 'baseball path simulator', 2)
print(2, 30, `exit velocity: ${Math.round(v)} m/s`, 3)
print(2, 40, `launch angle: ${Math.round(deg)} degrees`, 3)
print(2, 50, 'estimated distance', 2)
circFill(78, 52, 4, 2)
print(2, 60, 'realistic distance', 0)
circFill(78, 62, 4, 0)
// create points
const points = range(0, 10, 0.25)
.map(t => {
let x = 1
let y = 1
x = (v * cos(a)/k)*(1 - exp(-k*t))
y = (1/k)*(v*sin(a)-g*t)+(1/(k*k))*(g - exp(-k*t)*(g + k*v*sin(a)))
//x = v * vt * cos(a) * (1 - exp(-g*t/vt)) / g
return [x, y]
})
.filter(p => p[1] >= 0)
// draw line...
points.forEach((p, i, a) => {
circFill(...project(p), 1, 0)
if (i < a.length - 1) {
const next = a[i + 1]
line(...project(p), ...project(next), 3)
}
// and last circle
if (i === a.length - 1) {
circFill(...project(p), 2, 0)
print(85, 60, `(${Math.round(p[0])} m)`, 0)
}
})
circFill(...project([R, 0]), 4, 0)
print(85, 50, `(${Math.round(R)} m)`, 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment