Skip to content

Commit

Permalink
fix collicions for 2 particles
Browse files Browse the repository at this point in the history
  • Loading branch information
kaliumxyz committed Feb 18, 2019
1 parent 6f0e28d commit f58644e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
10 changes: 7 additions & 3 deletions load.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const canvas = select('canvas')
const config = {
bounds: false,
gravity: "wiggly",
collisions: "care",
collisions: "boring",
type: "square",
G: 0.01
}
Expand Down Expand Up @@ -141,7 +141,11 @@ window.onresize = _ => {
term.cursor.location()
}

window.onload = window.onresize
window.onload = _ => {
window.onresize()
loadScript()

}

// the highest key number is 222, we need that many functions for our handler
const keys = new Array(222)
Expand All @@ -164,4 +168,4 @@ keys[13] = term.cursor.enter
window.onkeydown = e => keys[e.keyCode](e)
window.onclick = loadScript

//document.querySelectorAll('.option').forEach(el => el.onclick = ev => el.setAttribute('class','active'))
//document.querySelectorAll('.option').forEach(el => el.onclick = ev => el.setAttribute('class','active'))
25 changes: 16 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ const render = {
context.fillStyle = '#FFF';
context.fillRect(0, 0, canvas.width, canvas.height);
particleArr.forEach(particle => {
collisions[config.collisions](particle);
// make this a function that the particle has or refrences which contains these functions to allow for costum behaviour
gravity[config.gravity](particle);
collisions[config.collisions](particle);
checkWithinBounds(particle);
move(particle);
render[particle.type](particle);
Expand Down Expand Up @@ -360,20 +361,26 @@ function checkWithinBounds(entity) {
const collisions = {
boring: entity => {
particleArr.forEach(particle => {
if (particle !== entity)
if (entity.coords.x < particle.coords.x + particle.mass)
if (entity.coords.x > particle.coords.x - entity.mass)
if (entity.coords.y < particle.coords.y + particle.mass)
if (entity.coords.y > particle.coords.y - entity.mass) {
// don't allow particles to collide with themselves
if (particle !== entity) {
const x = entity.coords.x + entity.vol.x / entity.mass;
const y = entity.coords.y + entity.vol.y / entity.mass;
if (x < particle.coords.x + particle.mass)
if (x > particle.coords.x - entity.mass)
if (y < particle.coords.y + particle.mass)
if (y > particle.coords.y - entity.mass) {
entity.vol.x *= -1;
entity.vol.y *= -1;
const volX = (entity.vol.x + particle.vol.x) / 2;
const volY = (entity.vol.y + particle.vol.y) / 2;
entity.vol.x = volX;
entity.vol.y = volY;
particle.vol.x = volX;
particle.vol.y = volY;

// comment out the two lines below for a cool effect
// particle.vol.x = volX;
// particle.vol.y = volY;
}
}
});
},
care: entity => {
Expand Down Expand Up @@ -416,7 +423,7 @@ function load() {
**************************************/

function start() {
let i = 50;
let i = 20;

particleArr = [];

Expand Down

0 comments on commit f58644e

Please sign in to comment.