Hi everyone! I'm currently trying to develop a processing app using Leap Motion for a uni project. The app that I'm trying to do is an image where it show various animals and if the user uses the leap motion to hover over the animal, it will then play the sound of the animal.
I've managed to get the mouse coordinates so it would be easier to know where the coordinates for each animal are, I also added an ellipse where I want these animals to be at. But it seems that once I use the leap to look for the sound and when I hover over the animal won't work as the coordinates suddenly are somewhere else in the screen.
I even tried to make the image resolution the same size as mine instead of trying to go for a default to present at uni.
Is there a way to add an invisible hit box that will activate with the leap movement, instead of adding the coordinates to the fingerPosition.x/y.
I've tried multiple things, but can't find the solution. Can anyone spot where the issue is?
Thank you
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import de.voidplus.leapmotion.*;
LeapMotion leap;
// Declaring the mp3 file function things
Minim minim;
AudioPlayer[] playlist;
AudioPlayer player;
PImage img;
PImage img2;
//A true or false variable to determine whether the menu screen should be active or not
boolean menu = true;
//Feel free to change the size to anything, it should still work
int size = 750;
//declaring the color arrays
float INTERACTION_SPACE_WIDTH = 200; // left-right from user
float INTERACTION_SPACE_DEPTH = 150; // away-and-toward user
float FINGER_DOT = 30;
void setup () {
size(displayWidth, displayHeight, P3D);
img = loadImage("farmmenuDMF.png");
img2 = loadImage("farmDMF2.png");
background(0);
minim = new Minim(this);
noStroke(); fill(50);
leap = new LeapMotion(this);
//defining the mp3 names and assigning them to reference items
playlist = new AudioPlayer [6];
playlist[0] = minim.loadFile("cat.wav"); //cat
playlist[1] = minim.loadFile("dog.wav"); //dog
playlist[2] = minim.loadFile("chicken.wav"); //chicken
playlist[3] = minim.loadFile("pig2.wav"); //pig
playlist[4] = minim.loadFile("chicken.wav"); //sheep
playlist[5] = minim.loadFile("dog.wav"); //cow
}
void draw() {
background (255);
//the call to the leapFunction
leapFunction ();
image(img2, 0, 0);
fill(234,86,250); //pink/cat
ellipse(1200, 350, 120,80);//cat
fill(255,0,0); //red/dog
ellipse(322, 337, 100,100);
fill(241,253,72); //yellow/chicken
ellipse(1126, 659, 100,100);
fill(0,255,0); //green/pig
ellipse(473, 447, 150,150);
fill(255,0,255); //sheep
ellipse(328, 591, 150,150);
fill(0,0,255); //blue/cow
ellipse(814, 469, 180,180);
//The start Screen:
if(menu == true) {
image(img, 0, 0);
fill(150);
rect(560,600,100,50);
fill(0);
text("Play", 593, 630);
if ( mousePressed == true && mouseX > 510 && mouseX < 610 && mouseY > 550 && mouseY < 650){
menu = false;
}
}
int fps = leap.getFrameRate();
fill(#00E310);
text(fps + " fps", 20, 20);
for (Hand hand : leap.getHands ()) {
PVector indexTip = hand.getIndexFinger().getRawPositionOfJointTip();
handleFinger(indexTip," ");
}
ellipse( mouseX, mouseY, 2, 2 );
fill(0);
text( "x: " + mouseX + " y: " + mouseY, mouseX, mouseY );
}
void handleFinger(PVector pos, String id) {
// map finger tip position to 2D surface
float x = map(pos.x, -INTERACTION_SPACE_WIDTH,
INTERACTION_SPACE_WIDTH, 0, width);
float y = map(pos.z, -INTERACTION_SPACE_DEPTH,
INTERACTION_SPACE_DEPTH, 0, height);
fill(#00E310);
noStroke();
ellipse(x, y, FINGER_DOT, FINGER_DOT);
}
//Leap function that senses finger position and assigns it to the keys
void leapFunction () {
//int finger_positions = leap.getFrameRate();
// HANDS
for(Hand hand : leap.getHands()){
// FINGERS
for(Finger finger : hand.getFingers()){
// Basics
//finger.draw();
//int finger_id = finger.getId();
PVector finger_position = finger.getPosition();
if(finger_position.x > 1160 && finger_position.x < 1200 && finger_position.z > 300 && finger_position.z < 380 ){ //cat
playlist[0].play();
}
else
playlist[0].pause();
/*
if (finger_position.x > 300 && finger_position.x < 350 && finger_position.y > 300 && finger_position.y > 300 && finger_position.y < 380) { //dog
playlist[1].play();
}
else
playlist[1].pause();
if (finger_position.x > 1070 && finger_position.x < 1080 && finger_position.y < 715) { //chicken
//playlist[2].play();
}
else
playlist[2].pause();
if (finger_position.x > 390 && finger_position.x < 550 && finger_position.y < 525) { //pig
playlist[3].play();
}
else
playlist[3].pause();
if ( finger_position.x > 250 && finger_position.x < 400 && finger_position.y < 670){ //sheep
playlist[4].play();
}
else
playlist[4].pause();
if ( finger_position.x > 720 && finger_position.x < 900 && finger_position.y < 560){ //cow
playlist[5].play();
}
else
playlist[4].pause(); */
// Touch Emulation
int touch_zone = finger.getTouchZone();
//float touch_distance = finger.getTouchDistance();
switch(touch_zone){
case -1: // None
break;
case 0: // Hovering
// println("Hovering (#"+finger_id+"): "+touch_distance);
break;
case 1: // Touching
// println("Touching (#"+finger_id+")");
break;
}
}
}
}
void stop()
{
minim.stop();
//super.stop();
}