HI
Im trying to build a game where I can direct a ball with the motion of my hand.
The hand motion is captured by the kinect (depth image and code from an example which looks at current and previous hand positions). When the hand comes close to a ball, the ball moves in the direction of the hand motion.
As of now, the ball does move according to the hand motion, but only in one direction (towards left) instead moving in both directions.
Any help for this would be great. Thank you!
The code is:
mport SimpleOpenNI.*;
Im trying to build a game where I can direct a ball with the motion of my hand.
The hand motion is captured by the kinect (depth image and code from an example which looks at current and previous hand positions). When the hand comes close to a ball, the ball moves in the direction of the hand motion.
As of now, the ball does move according to the hand motion, but only in one direction (towards left) instead moving in both directions.
Any help for this would be great. Thank you!
The code is:
mport SimpleOpenNI.*;
SimpleOpenNI kinect;
Mover mover;
ArrayList<PVector> handPositions;
PVector currentHand;
PVector previousHand;
void setup() {
size(640, 480);
mover = new Mover();
smooth();
kinect = new SimpleOpenNI(this);
kinect.setMirror(true);
kinect.enableDepth();
kinect.enableGesture();
kinect.enableHands();
kinect.addGesture("RaiseHand")
;
handPositions = new ArrayList();
stroke(255, 0, 0);
strokeWeight(2);
}
void draw() {
kinect.update();
image(kinect.depthImage(), 0, 0);
for (int i = 1; i < handPositions.size(); i++) {
fill(255, 10);
rect(0, 0, width, height);
mover.display();
currentHand = handPositions.get(i);
previousHand = handPositions.get(i-1);
//line(previousHand.x, previousHand.y, currentHand.x, currentHand.y);
point(currentHand.x, currentHand.y);
//float angle= currentHand.heading2D();
//float angle= PVector.angleBetween(
currentHand, previousHand);
float speed= currentHand.mag() - previousHand.mag();
if (dist(mover.location.x, mover.location.y, currentHand.x, currentHand.y) < 60)
{
mover.update(previousHand, currentHand);
mover.checkEdges();
}
}
}
// hand events
void onCreateHands(int handId, PVector position, float time) {
kinect.
convertRealWorldToProjective(
position, position);
handPositions.add(position);
}
void onUpdateHands(int handId, PVector position, float time) {
kinect.
convertRealWorldToProjective(
position, position);
handPositions.add(position);
}
void onDestroyHands(int handId, float time) {
handPositions.clear();
kinect.addGesture("RaiseHand")
;
}
// gesture events
void onRecognizeGesture(String strGesture,
PVector idPosition,
PVector endPosition)
{
kinect.startTrackingHands(
endPosition);
kinect.removeGesture("
RaiseHand");
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover() {
location = new PVector(width/2, height/2);
velocity = new PVector(0, 0);
topspeed = 20.0;
}
void update(PVector previousHand, PVector currentHand) {
// Step 1: direction
PVector dir = PVector.sub(currentHand, previousHand);
// Step 2: normalize
dir.normalize();
// Step 3: scale
dir.mult(0.5);
// Step 4: accelerate
acceleration = dir;
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x, location.y, 50, 50);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
}
}
1