Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Last active July 1, 2018 15:17
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/f8dfacb4fb778cc51f614c382af631b0 to your computer and use it in GitHub Desktop.
Save gabrielflorit/f8dfacb4fb778cc51f614c382af631b0 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: collisions
const r = 63
const { PI, sin, cos } = Math
initialState = {
collision: '',
actors: [
{
name: 'box',
x: 64 - 32,
y: 64 - 32,
width: 64,
height: 64
},
{
name: 'dots',
dots: range(128*2).map((d, i, a) => {
const angle = PI * 2 / a.length * i
const x = r * cos(angle)
const y = r * sin(angle)
const speedx = -x / (60)
const speedy = -y / (60)
return {
x: 64 + x,
y: 64 + y,
speedx: Math.abs(speedx),
speedy: Math.abs(speedy),
dx: speedx,
dy: speedy,
color: (i % 2)*5
}
})
}
]
}
const lineLine = (a, b) => {
const x1 = a[0][0]
const y1 = a[0][1]
const x2 = a[1][0]
const y2 = a[1][1]
const x3 = b[0][0]
const y3 = b[0][1]
const x4 = b[1][0]
const y4 = b[1][1]
// calculate the distance to intersection point
const uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1))
const uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1))
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
return true
} else {
return false
}
}
const collideWithBox = ({ dots, box }) => {
dots.dots.forEach(dot => {
if (
dot.x >= box.x &&
dot.x <= box.x + box.width &&
dot.y >= box.y &&
dot.y <= box.y + box.height
) {
const dotLine = [
[dot.x - dot.dx, dot.y - dot.dy],
[dot.x + dot.dx, dot.y + dot.dy]
]
const leftLine = [
[box.x, box.y],
[box.x, box.y + box.height]
]
const rightLine = [
[box.x + box.width, box.y],
[box.x + box.width, box.y + box.height]
]
const topLine = [
[box.x, box.y],
[box.x + box.width, box.y]
]
const bottomLine = [
[box.x, box.y + box.height],
[box.x + box.width, box.y + box.height]
]
if (lineLine(dotLine, leftLine) || lineLine(dotLine, rightLine)) {
dot.dx *= -1
}
if (lineLine(dotLine, topLine) || lineLine(dotLine, bottomLine)) {
dot.dy *= -1
}
}
})
}
const collideWithWall = ({ dots, state }) => {
dots.dots.forEach(dot => {
if (dot.x < 1 && dot.dx < 0) {
dot.dx = dot.speedx
} else if (dot.x > 126 && dot.dx > 0) {
dot.dx = -dot.speedx
} else if (dot.y < 1 && dot.dy < 0) {
dot.dy = dot.speedy
} else if (dot.y > 126 && dot.dy > 0) {
dot.dy = -dot.speedy
}
})
}
const move = dots => {
dots.dots.forEach(dot => {
dot.x += dot.dx
dot.y += dot.dy
})
}
update = state => {
const box = state.actors.find(d => d.name === 'box')
const dots = state.actors.find(d => d.name === 'dots')
move(dots)
collideWithWall({ dots, state })
collideWithBox({ dots, box })
}
const drawActor = (actor, fade) => {
if (actor.name === 'box') {
// rectFill(actor.x, actor.y, actor.width, actor.height, 6)
}
if (actor.name === 'dots') {
actor.dots.forEach(dot => {
line(dot.x - 2*dot.dx, dot.y - 2*dot.dy, dot.x, dot.y, dot.color)
circFill(dot.x, dot.y, 2, dot.color)
})
}
}
drawActors = (state, fade) => {
state.actors.forEach(actor => drawActor(actor, fade))
}
draw = state => {
clear()
print(0, 0, state.collision, 0)
// rectStroke(0, 0, 128, 128, 6)
// circStroke(64, 64, r, 6)
drawActors(state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment