// forked from monakaz's "konashiの初歩" http://jsdo.it/monakaz/vkGq // this code was autogenerated from PJS var spring = 0.05; var sketch=(function($p) { var Ball = (function() { function Ball() { var $this_1 = this; function $superCstr() { $p.extendClassChain($this_1) } $this_1.x = 0; $this_1.y = 0; $this_1.diameter = 0; $this_1.vx = 0; $this_1.vy = 0; $this_1.id = 0; $this_1.others = null; function collide$0() { for (var i = $this_1.id + 1; i < numBalls; i++) { var dx = $this_1.others[i].x - $this_1.x; var dy = $this_1.others[i].y - $this_1.y; var distance = $p.sqrt(dx * dx + dy * dy); var minDist = $this_1.others[i].diameter / 2 + $this_1.diameter / 2; if (distance < minDist) { var angle = $p.atan2(dy, dx); var targetX = $this_1.x + $p.cos(angle) * minDist; var targetY = $this_1.y + $p.sin(angle) * minDist; var ax = (targetX - $this_1.others[i].x) * spring; var ay = (targetY - $this_1.others[i].y) * spring; $this_1.vx -= ax; $this_1.vy -= ay; $this_1.others[i].vx += ax; $this_1.others[i].vy += ay; } } } $p.addMethod($this_1, 'collide', collide$0); function move$0() { $this_1.vy += gravity; $this_1.x += $this_1.vx; $this_1.y += $this_1.vy; if ($this_1.x + $this_1.diameter / 2 > $p.width) { $this_1.x = $p.width - $this_1.diameter / 2; $this_1.vx += -0.9; } else if ($this_1.x - $this_1.diameter / 2 < 0) { $this_1.x = $this_1.diameter / 2; $this_1.vx *= -0.9; } if ($this_1.y + $this_1.diameter / 2 > $p.height) { $this_1.y = $p.height - $this_1.diameter / 2; $this_1.vy *= -0.9; } else if ($this_1.y - $this_1.diameter / 2 < 0) { $this_1.y = $this_1.diameter / 2; $this_1.vy *= -0.9; } } $p.addMethod($this_1, 'move', move$0); function display$0() { $p.fill(255, 204); $p.ellipse($this_1.x, $this_1.y, $this_1.diameter, $this_1.diameter); } $p.addMethod($this_1, 'display', display$0); function $constr_5(xin, yin, din, idin, oin) { $superCstr(); $this_1.x = xin; $this_1.y = yin; $this_1.diameter = din; $this_1.id = idin; $this_1.others = oin; } function $constr() { if (arguments.length === 5) { $constr_5.apply($this_1, arguments); } else $superCstr(); } $constr.apply(null, arguments); } return Ball; })(); $p.Ball = Ball; var numBalls = 12; var gravity = 0.03; var balls = $p.createJavaArray('Ball', [numBalls]); function setup() { $p.size(200, 200); $p.noStroke(); $p.smooth(); for (var i = 0; i < numBalls; i++) { balls[i] = new Ball($p.random($p.width), $p.random($p.height), $p.random(20, 40), i, balls); } } $p.setup = setup; function draw() { $p.background(0); for (var i = 0; i < numBalls; i++) { balls[i].collide(); balls[i].move(); balls[i].display(); } } $p.draw = draw; }); var canvas=document.getElementById("canvas-processing"); // attaching the sketch to the canvas var p = new Processing(canvas, sketch); // konashiと接続できたら実行される k.ready(function(){ // I/Oの設定 k.pinMode(k.S1, k.INPUT); }); // 入力が変化した時の処理 k.updatePioInput(function(value){ k.digitalRead(k.S1, function(value){ if(value==k.HIGH){ spring=0.5; } else { spring=0.05; } }); }); // まわりにあるkonashiを探す k.find();