Using Hand Movements to move an circle (Simple OpenNI, Kinect, Vectors)
in
Contributed Library Questions
•
1 year ago
Hi Everyone
I'm a bit of noob, so bear with me.
I'm trying to make a game where the movement of users hand (as detected by the kinect) moves a ball on the screen. The concept is to have a football game, where the user can direct the ball into both the goals.
My current code detects the hand positions and moves the ball away from the hand using vectors (its almost like someone is kicking the ball away).
The problem is that the ball also reacts to previous hand positions. I find this very strange since, while I am storing all hand positions, I am only using the current position, previous positions are unused. Any help on this would be great!
Thanks in advance!
//for using the kinect
import SimpleOpenNI.*;
SimpleOpenNI kinect;
//storing current hand positions
ArrayList<PVector> handPositions;
PVector currentHand;
PFont f;
PImage b;
Thing t;
void setup() {
size(1000,660);
// for scores
f = loadFont("Serif-48.vlw");
smooth();
// Create the thing object
PVector a = new PVector(0.0,0.0);
PVector v = new PVector(0.0,0.0);
PVector l = new PVector(width/2,height/2);
t = new Thing(a,v,l);
//for background image
b = loadImage("footballfield.jpg");
//innitialize the kinect
kinect = new SimpleOpenNI(this);
kinect.setMirror(true);
kinect.enableDepth();
kinect.enableGesture();
kinect.enableHands();
kinect.addGesture("RaiseHand");
handPositions = new ArrayList();
}
void draw() {
//getting information from the kinect
kinect.update();
//scale(2);
//image(kinect.depthImage(), 0, 0);
background(b);
fill(0);
//player1
ellipse (width/3, height/4, 30, 50);
//player2
ellipse (width/3, 3*height/4, 30, 50);
//player3
ellipse (width/4, height/2, 30, 50);
//player4
ellipse (2*width/3, height/4, 30, 50);
//player5
ellipse (2*width/3, 3*height/4, 30, 50);
//player6
ellipse (3*width/4, height/2, 30, 50);
//acceleration of the ball
float factor;
//start collecting information from the kinect
for (int i = 1; i < handPositions.size(); i++)
{
currentHand = handPositions.get(i);
//draw an ellipse at where the hand is
ellipse(currentHand.x, currentHand.y,20,20);
// Run the Thing object
t.go();
if (dist(currentHand.x, currentHand.y, t.loc.x, t.loc.y) < 60 ) {
// Compute difference vector between handposition and object location
// (1) Get hand Location, (2) Get Difference Vector, (3) Normalize difference vector
PVector m = new PVector(currentHand.x,currentHand.y);
PVector diff = PVector.sub(t.getLoc(),m);
diff.normalize();
/*if hand is on the ball, stop it
if (dist(currentHand.x,currentHand.y, t.loc.x, t.loc.y) < 20 )
{
t.vel.x = 0.0;
t.vel.y = 0.0;
}
*/
factor = 0.005; //acceleration constant
diff.mult(factor);
//object accelerates away from kinect hand position
t.setAcc(diff);
}
else {
t.setAcc(new PVector(0,0));
}
textFont(f, 50);
fill(255);
//display scores
text(t.rgoal,50,100);
text(t.lgoal,width-75,100);
}
}
1