collisions
by John Doe
JavaScript
function main(){
//https://en.wikipedia.org/wiki/Elastic_collision#Two-dimensional
var slider = document.createElement('input')
slider.type = 'range'
slider.id = 'time'
slider.name = 'time'
slider.min = -10
slider.step = 0.1
slider.max = 10
slider.oninput = function(){
console.log(slider.value)
}
document.body.appendChild(slider)
document.body.appendChild(document.createElement('br'))
var cvs = document.createElement('canvas')
cvs.style.width = 680
cvs.style.height = 480
cvs.style.backgroundColor = 'red'
document.body.appendChild(cvs)
var ctx = cvs.getContext('2d')
ctx.fillStyle = 'black'
ctx.rect(10, 20, 30, 40)
ctx.stroke()
console.log(elastic1d(1, 1, 1, -1))
// choose some initial poisitions
var b1 = [[0], [1]] // x, v
var b2 = [[3], [-1]]
console.log(next_collision_time_pairwise(...b1, ...b2)) //should be 0.5
console.log(next_collision_time_pairwise(...b1, ...b2, -1)) //should be -Infinity
}
window.onload = main
function interpolate(states, t){
//TODO get sequence of states of pairwise collisions and interpolate them
}
function compute_states(initState){ //a state is a list of balls //TODO implement ball class
//TODO: compute all past and future pairwise collisions
// return them all as a sequence of states (data structure to be defined)
// that can easily be interpolated (think about dt=0 collisions (simultaneous))
var currentTime = 0
var collision = null
var currentState = initState
var states = [initState]
while(true){
collision = next_collision_time(currentState)
if(collision === null){
break // no more collisions
} else {
currentTime += collision.min_t
//TODO: collide balls i and j from collision, create new state
//elasticnd(m1, x1, v1, m2, x2, v2)
}
}
}
function next_collision_time(state, timeDirection=1){
var min_t = time_direction * Infinity
var i_ind = null
var j_ind = null
for(var i=0; i<state.length;...