Hi. I replaced mouse function like as 'pressed and point' to changed 'grab and released'.
but it was not work on. what is wrong?
ArrayList<Particle> particles = new ArrayList<Particle>();
int NUM = 1200;
int[] pgpx;
Particle p;
PVector fc;
PVector fp;
import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup()
{
fullScreen(P3D, 2);
//size(800, 800, P3D);
noStroke();
process(new String("HELLO"));
fc=new PVector();
leap = new LeapMotion(this);
}
void draw()
{
background(0);
lights();
translate(width/2, height/2);
rotate(millis() * 0.00001 * TWO_PI /5);
for (Particle p : particles)
{
p.draw();
p.update();
p.bounceOffWalls();
}
for (Hand hand : leap.getHands()) {
for (Finger finger : hand.getFingers()) {
float handGrab = hand.getGrabStrength();
fp = finger.getPosition();
fc.set(fp.x, fp.y, 0);
if (handGrab > 0.7) {
pushMatrix();
//translate(x, y, z);
translate(fp.x, fp.y, 0);
PVector handVec = new PVector(fp.x, fp.y, 0);
fc.add(handVec);
popMatrix();
}
}
}
}
void hand() {
for (int i = 0; i < NUM; i++)
{
p = particles.get(i);
for (Hand hand : leap.getHands()) {
for (Finger finger : hand.getFingers()) {
float handGrab = hand.getGrabStrength();
fp = finger.getPosition();
if (handGrab > 0.7) {
} else {
PVector handVec;
{
if (handGrab <= 0.7)
{
handVec = new PVector(fp.x, fp.y, 0);
p.pre();
} else {
handVec = new PVector(
p.position.x - p.position.x - fp.x,
p.position.y - p.position.y - fp.y,
0);
}
p.addAttractionForce(handVec, width, 1.0);
p.retract=true; //REMARK: ### CHANGED
}
}
}
}
}
}
void createParticles()
{
particles = new ArrayList<Particle>();
while (particles.size () < NUM)
{
p = new Particle(particles.size ());
particles.add(p);
}
}
class Particle
{
final float SPEED_MIN = .12, SPEED_MAX = .25, h = 40;
PVector position = new PVector(width/5.0, height/5.0, 0);
PVector origin;
PVector velocity = new PVector(0, 0, 0);
PVector speed = new PVector (0, 0, 0);
PVector target = new PVector(0, 0, 0);
int rank;
float n, nz;//noise
Boolean stuck = false;//against a wall
PVector acceleration = new PVector(0, 0, 0);
float friction = 0.01;
float closeEnoughTarget = 50;
float maxSpeed = 4.0;
float maxForce = 0.1;
boolean retract;
float mass = 1.0;
PVector min= new PVector(0, 0, 0);
PVector max= new PVector(width, height, height);
Particle(int p_rank)
{
rank = p_rank;
init();
}
void init()
{
float theta = random(TWO_PI);
Boolean done = false;
while (!done)
{
position = new PVector(random(width), random(height));
if (green(pgpx[(int)position.y * width + (int)position.x]) > 100)
{
position.z = random(-h/2, h/2);
done = true;
}
}
}
void draw() {
pushMatrix();
translate(position.x-width/2, position.y-height/2, position.z);
noStroke();
//sphere(2);
ellipse(0, 0, 5, 5);
popMatrix();
}
void addForce(PVector force)
{
force.div(mass);
acceleration.add(force);
}
void bounceOffWalls() {
if (position.x > max.x) {
position.x = max.x;
velocity.x *= -1;
}
if (position.x < min.x) {
position.x = min.x;
velocity.x *= -1;
}
if (position.y > max.y) {
position.y = max.y;
velocity.y *= -1;
}
if (position.y < min.y) {
position.y = min.y;
velocity.y *= -1;
}
if (position.z > max.z) {
position.z = max.z;
velocity.z *= -1;
}
if (position.z < min.z) {
position.z = min.z;
velocity.z *= -1;
}
}
void throughOffWalls() {
if (position.x < min.x) {
position.x = max.x;
}
if (position.y < min.y) {
position.y = max.y;
}
if (position.z < min.z) {
position.z = max.z;
}
if (position.x > max.x) {
position.x = min.x;
}
if (position.y > max.y) {
position.y = min.y;
}
if (position.z > max.z) {
position.z = min.z;
}
}
void addAttractionForce(PVector force, float radius, float scale) {
float length = PVector.dist(position, force);
PVector diff = new PVector();
diff = position.get();
diff.sub(force);
boolean bAmCloseEnough = true;
if (radius > 0) {
if (length > radius) {
bAmCloseEnough = false;
}
}
if (bAmCloseEnough == true) {
float pct = 1 - (length / radius);
diff.normalize();
diff.mult(scale);
diff.mult(pct);
speed.add(diff);
}
}
void pre() {
PVector steer = new PVector(0, 0, 0);
steer.sub(this.velocity);
steer.normalize();
steer.mult(this.maxForce);
this.acceleration.add(steer);
}
void update() {
for (Hand hand : leap.getHands()) {
for (Finger finger : hand.getFingers()) {
float handGrab = hand.getGrabStrength();
fp = finger.getPosition();
if (handGrab > 0.7 && retract==true) {
speed.mult(-1); //INVERT direction... done only once
retract=false;
}
position.add(speed);
}
}
}
}
void process(String c)
{
PGraphics pg = createGraphics(width, height, P3D);
pg.beginDraw();
pg.translate(-width/2, -height/1.9);
pg.background(0);
pg.textSize(150);//500
pg.fill(color(0, 255, 0));
pg.textAlign(CENTER, CENTER);
pg.text(c, width, height);
PFont font = createFont("Futura Book", 100);
pg.textFont(font);
pg.endDraw();
pgpx = new int[width * height];
pg.clear();
pg.loadPixels();
arrayCopy(pg.pixels, pgpx);
pg.updatePixels();
createParticles();
}