Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save masaakif/50485 to your computer and use it in GitHub Desktop.
Save masaakif/50485 to your computer and use it in GitHub Desktop.
/* For Youtube demo, Please see below.
http://jp.youtube.com/watch?v=8vXg_EUqFY0
*/
import processing.core.*;
import JMyron.*;
import MyObjects.Ball;
import ProcessingUtils.ColorUtil;
public class PopBall extends PApplet{
JMyron m;
ColorUtil cu = new ColorUtil();
static final long serialVersionUID = 100;
final int BLACK = cu.createColor(0,0,0);
final int MAX = 100;
Ball[] balls;
int idx = 0;
public void setup(){
size(320, 240);
balls = new Ball[MAX];
frameRate(30);
smooth();
m = new JMyron();
m.start(width, height);
m.adaptivity(100);
m.adapt();
noStroke();
}
public void stop() {
m.stop();
super.stop();
}
public void draw(){
m.update();
drawImage();
createBalls();
drawBalls();
}
private void drawBalls(){
for (int i = 0; i < MAX; i++) {
if (balls[i] != null) {
balls[i].update();
fill(balls[i].Color);
ellipse(balls[i].x, balls[i].y, balls[i].size, balls[i].size);
}
}
}
private void drawImage()
{
int[] img = m.image();
loadPixels();
for (int i = 0; i<width*height; i++){
pixels[i] = img[i];
}
updatePixels();
}
private void createBalls() {
int[] img = m.differenceImage();
for (int i = 0; i<width*height; i++) {
int cl = img[i];
if (cl != BLACK) {
int y = i / width;
int x = i % width;
balls[idx] = new Ball(x, y,random(10,15), this);
balls[idx].setInitialSpeed(random(-10,10), random(-10,10));
balls[idx].setColor(cu.createColor(random(0,255), random(0,255), random(0,255), random(150,255)));
idx++;
if (100 <= idx) idx = 0;
break;
}
}
}
}
package MyObjects;
import processing.core.*;
public class Ball {
private
float _x, _y;
float _speedy, _speedx;
float _size = 5;
PApplet _p;
int _color;
static final float g = (float)2;
public float x, y, size;
public int Color;
public Ball(float x, float y, float size, PApplet p){
_x = x;
_y = y;
_size = size;
_p = p;
_speedy = 0;
}
public void setInitialSpeed(float speedx, float speedy) {
_speedx = speedx;
_speedy = speedy;
}
public void setColor(int c){
_color = c;
Color = _color;
}
public void update()
{
calculateXPos();
calculateYPos();
x = _x;
y = _y;
size = _size;
}
private void calculateXPos()
{
float x1;
x1 = _x + _speedx;
if (x1 <= size/2) {
_speedx = (float) -1 * _speedx * (float)0.7;
x1 = size / 2;
} else if ((_p.width - size /2) <= x1){
_speedx = (float) -1 * _speedx * (float)0.7;
x1 = _p.width - size / 2;
}
_x = x1;
}
private void calculateYPos()
{
float y1;
_speedy += g ;
y1 = _y + _speedy;
if ((_p.height - size/2) <= y1) {
_speedy = (float) -1 * _speedy * (float)0.7;
y1 = _p.height - size / 2;
_speedx = _speedx * (float)0.95;
}
_y = y1;
}
}
package ProcessingUtils;
public class ColorUtil {
public ColorUtil() {
}
public int createColor(float c1, float c2, float c3, float c4) {
return createColor((int)c1, (int)c2, (int)c3, (int)c4);
}
public int createColor(int c1, int c2, int c3) {
return createColor(c1, c2, c3, 255);
}
public int createColor(float c1, float c2, float c3) {
return createColor((int)c1, (int)c2, (int)c3);
}
public int createColor(int c1, int c2, int c3, int c4) {
if (c1 < 0 || 255 < c1) {
c1 = 255;
}
if (c2 < 0 || 255 < c2) {
c2 = 255;
}
if (c3 < 0 || 255 < c3) {
c3 = 255;
}
if (c4 < 0 || 255 < c4) {
c4 = 255;
}
int _color = c4 << 24 | c1 << 16 | c2 << 8 | c3;
return _color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment