I have a question concerning the management of an arraylist in LeapMotion p5. I would like to compare my arraylist of objects with the one given by the getFingerList () function to add the new fingers and remove those that are no longer visible. Also, when a finger is no longer visible, I wish there was a fade effect and keeps the position when the finger is no longer detected . And, if I reposition the finger, another object is created and follow the finger. A strategy that I've used is to make the comparison between the arraylist of fingers, obtained with getFingerList () function, from the previous frame to the current frame with getFrameBeforeFrame () and getFrame () functions. So, if there is a difference between them, I delete or add an object in my arraylist. This strategy does not work very well, I have difficulty to manage my arraylist. So, I wanted to know if you know a better technique.
I have a version with a hashmap(with the comparison of the arraylist of fingers of two different frames) and another with an arraylist(comparison between the arraylist of finger and my arraylist of objects).
if (leap.getFrameBeforeFrame(leap.getFrame()) != null) {
//size of the arraylist of the previous frame
int nbFP = leap.getFingerList(leap.getFrameBeforeFrame(leap.getFrame())).size();
//size of the arraylist of the current frame
int nbFC = leap.getFingerList(leap.getFrame()).size();
for (Finger f : leap.getFingerList()) {
PVector fingerPos = leap.getTip(f);
if (!(tObjets.containsKey(f.id()))) {
tObjets.put(f.id(), new Objet(fingerPos));
}
}
// if the previous frame contain more than the current frame
if (nbFP > nbFC) {
for (Finger f : leap.getFingerList(leap.getFrameBeforeFrame(leap.getFrame()))) {
if (tObjets.containsKey(f.id()) && !(tObjets.containsKey(f.id()))) {
tObjets.remove(f.id());
//break;
}
}
}
}
for (Finger f : leap.getFingerList()) {
Objet o = (Objet)tObjets.get(f.id());
PVector fingerPos = leap.getTip(f);
if (o != null) {
o.setPosition(fingerPos);
}
}
// draw the object from the arraylist objets
Iterator i = tObjets.entrySet().iterator();
while (i.hasNext ()) {
Map.Entry o = (Map.Entry)i.next();
tObjets.get(o.getKey()).display();
}
}
I have a question concerning the management of an arraylist in LeapMotion p5. I would like to compare my arraylist of objects with the one given by the getFingerList () function to add the new fingers and remove those that are no longer visible. Also, when a finger is no longer visible, I wish there was a fade effect and keeps the position when the finger is no longer detected . And, if I reposition the finger, another object is created and follow the finger. A strategy that I've used is to make the comparison between the arraylist of fingers, obtained with getFingerList () function, from the previous frame to the current frame with getFrameBeforeFrame () and getFrame () functions. So, if there is a difference between them, I delete or add an object in my arraylist. This strategy does not work very well, I have difficulty to manage my arraylist. So, I wanted to know if you know a better technique.
I have a version with a hashmap(with the comparison of the arraylist of fingers of two different frames) and another with an arraylist(comparison between the arraylist of finger and my arraylist of objects).
MAIN (with a hashmap)
import com.onformative.leap.LeapMotionP5; import com.leapmotion.leap.Finger; import java.util.*; LeapMotionP5 leap; HashMap <Integer, Objet> tObjets;
public void setup() { size(500, 500, P3D); leap = new LeapMotionP5(this); tObjets = new HashMap <Integer, Objet>(); }
public void draw() { background(0); fill(255); for (Finger finger : leap.getFingerList()) { PVector fingerPos = leap.getTip(finger); ellipse(fingerPos.x, fingerPos.y, 10, 10); }
if (leap.getFrameBeforeFrame(leap.getFrame()) != null) { //size of the arraylist of the previous frame int nbFP = leap.getFingerList(leap.getFrameBeforeFrame(leap.getFrame())).size(); //size of the arraylist of the current frame int nbFC = leap.getFingerList(leap.getFrame()).size();
}
for (Finger f : leap.getFingerList()) { Objet o = (Objet)tObjets.get(f.id()); PVector fingerPos = leap.getTip(f); if (o != null) { o.setPosition(fingerPos); } }
// draw the object from the arraylist objets Iterator i = tObjets.entrySet().iterator(); while (i.hasNext ()) { Map.Entry o = (Map.Entry)i.next(); tObjets.get(o.getKey()).display(); } }
MAIN (with an arrayList)
import com.onformative.leap.LeapMotionP5; import com.leapmotion.leap.Finger;
LeapMotionP5 leap; ArrayList objets;
void setup() {
size(600, 600, P3D);
leap = new LeapMotionP5(this);
objets = new ArrayList();
}
void draw() { background(0); int nbFingers = leap.getFingerList().size(); int nbObjets = objets.size(); ArrayList listeFingers = leap.getFingerList();
// add objects in the arraylist objets if (nbFingers > nbObjets) { for (Finger finger : leap.getFingerList()) { PVector fingerPos = leap.getTip(finger); objets.add(new Objet(fingerPos, finger.id())); } }
else if (nbFingers == 0) { objets.clear(); }
else if (nbFingers <= nbObjets) { int c = 0; for (int i=0; i<leap.getFingerList().size(); i++) { Finger finger = leap.getFingerList().get(i); Objet o = objets.get(c); if (finger.id() != o.getId()) { objets.remove(c); break; } c++; } }
int c = 0; for (Finger finger : leap.getFingerList()) { PVector fingerPos = leap.getTip(finger); Objet o = objets.get(c); o.setPosition(fingerPos); o.draw(); } println(objets.size()); }
CLASS Objet (same for both)
public class Objet { private PVector position; private color couleur; private float opacite, cible, facteur;
public Objet(PVector _position) { position = _position; couleur = color(255, 0, 0); opacite = 255; cible = 255; facteur = 0.1; } public void display() { // for the fadeout opacite = (cible - opacite) * facteur + opacite; pushMatrix(); translate(position.x, position.y, 0); pushStyle(); stroke(255, 0, 0, opacite); noFill(); sphereDetail(int(random(3, 5))); sphere(20); popStyle(); popMatrix(); }
public boolean mort() { if (opacite <= 0) { return true; } else { return false; } }
public PVector getPosition() { return position; } public void setPosition(PVector pos) { position = pos.get(); }
// for the fadeout public void setCible(float _cible) { cible = _cible; } public float getOpacite() { return opacite; } }