We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I am wanting to edit my sketch so that the attraction of the nodes is disabled when there is no hand detected by kinect. So the attraction would start with a force of 0, but turn to 1 when the wave gesture had been completed and a hand was being tracked, then it would become 0 again onLostHand. A way I thought this would be possible would be to add if statements to the kinect related void's near the end of the sketch, but i'm unsure about how to do that.
I am still a processing baby so any help would be super appreciated even if it's just to tell me it won't work. Thank you!!!
Windows 8 Processing 2.1.2
page1
class Attractor {
// position
float x=0, y=0;
// radius of impact
float radius = 500;
// strength: positive for attraction, negative for repulsion
float strength = 1;
// parameter that influences the form of the function
float ramp = 0.5; //// 0.01 - 0.99
Attractor(float theX, float theY) {
x = theX;
y = theY;
}
void attract(Node theNode) {
// calculate distance
float dx = x - theNode.x;
float dy = y - theNode.y;
float d = mag(dx, dy);
if (d > 0 && d < radius) {
// calculate force
float s = pow(d / radius, 1 / ramp);
float f = s * 9 * 1 * (1 / (s + 1) + ((s - 3) / 4)) / d;
// apply force to node velocity
theNode.velocity.x += dx * f * 3;
theNode.velocity.y += dy * f * 3;
}
}
}
code page 2
//gravity
import generativedesign.*;
//kinect
import java.util.Map;
import java.util.Iterator;
import SimpleOpenNI.*;
// gravity
int xCount = 70;
int yCount = 70;
float gridSize = 600;
Node[] myNodes = new Node[xCount*yCount];
Attractor myAttractor;
// kinect
SimpleOpenNI context;
int handVecListSize = 20;
Map<Integer,ArrayList<PVector>> handPathList = new HashMap<Integer,ArrayList<PVector>>();
color[] userClr = new color[]{ color(255,0,0),
color(0,255,0),
color(0,0,255),
color(255,255,0),
color(255,0,255),
color(0,255,255)
};
void setup() {
size(600,600);
background(230);
smooth();
noStroke();
fill(0);
//kinect
context = new SimpleOpenNI(this);
if(context.isInit() == false)
{
println("Can't init SimpleOpenNI, maybe the camera is not connected!");
exit();
return;
}
context.enableDepth();
context.setMirror(true);
context.enableHand();
context.startGesture(SimpleOpenNI.GESTURE_WAVE);
//gravity
initGrid();
myAttractor = new Attractor(0, 0);
myAttractor.strength = -3;
myAttractor.ramp = 2;
}
void draw() {
background(230);
//kinect
context.update();
Iterator itr = handPathList.entrySet().iterator();
while(itr.hasNext())
{
Map.Entry mapEntry = (Map.Entry)itr.next();
int handId = (Integer)mapEntry.getKey();
ArrayList<PVector> vecList = (ArrayList<PVector>)mapEntry.getValue();
PVector p;
PVector p2d = new PVector();
Iterator itrVec = vecList.iterator();
p = (PVector) itrVec.next();
context.convertRealWorldToProjective(p,p2d);
//gravity
myAttractor.x = p2d.x;
myAttractor.y = p2d.y;
//kinect
p = vecList.get(0);
context.convertRealWorldToProjective(p,p2d);
//point(p2d.x,p2d.y);
}
//gravity
for (int i = 0; i < myNodes.length; i++) {
myAttractor.attract(myNodes[i]);
myNodes[i].update();
rect(myNodes[i].x, myNodes[i].y, 2, 2);
}
}
void initGrid() {
int i = 0;
for (int y = 0; y < yCount; y++) {
for (int x = 0; x < xCount; x++) {
float xPos = x*(gridSize/(xCount-1))+(width-gridSize)/2;
float yPos = y*(gridSize/(yCount-1))+(height-gridSize)/2;
myNodes[i] = new Node(xPos, yPos);
myNodes[i].setBoundary(0, 0, width, height);
myNodes[i].setDamping(0.8); //// 0.0 - 1.0
i++;
}
}
}
void keyPressed() {
if (key=='r' || key=='R') {
initGrid();
background(230);
}
}
void onNewHand(SimpleOpenNI curContext,int handId,PVector pos)
{
println("onNewHand - handId: " + handId + ", pos: " + pos);
ArrayList<PVector> vecList = new ArrayList<PVector>();
vecList.add(pos);
handPathList.put(handId,vecList);
}
void onTrackedHand(SimpleOpenNI curContext,int handId,PVector pos)
{
//println("onTrackedHand - handId: " + handId + ", pos: " + pos );
ArrayList<PVector> vecList = handPathList.get(handId);
if(vecList != null)
{
vecList.add(0,pos);
if(vecList.size() >= handVecListSize)
// remove the last point
vecList.remove(vecList.size()-1);
}
}
void onLostHand(SimpleOpenNI curContext,int handId)
{
println("onLostHand - handId: " + handId);
handPathList.remove(handId);
}
// -----------------------------------------------------------------
// gesture events
void onCompletedGesture(SimpleOpenNI curContext,int gestureType, PVector pos)
{
println("onCompletedGesture - gestureType: " + gestureType + ", pos: " + pos);
int handId = context.startTrackingHand(pos);
println("hand stracked: " + handId);
}
Answers
If I understand the problem correctly there is no need for if statements.
Create a global variable at line 11
float attrForce = 0;
then in the
onLostHand
method add the statementattrForce = 0;
then in the
onCompletedGesture
method add the statementattrForce = 1;
Hi @quark, thanks so much for the speedy reply! Not entirely sure what you mean here, any chance you would potentially be able to write your alterations into the initial code? thank you for your help!
oop! spoke to soon managed to get it working after all. Thanks so much for your help :)
In your formula the
* 1
is doing nothing (because x = 1 * x for all x), but you could use this for 'scaling'f
e.g.float f = s * 10 * (1 / (s + 1) + ((s - 3) / 4)) / d;
scales the force by a factor of 10. Try changing the scaleing value 10 to get the desired effect.
@rossdickinson
Please do not delete from existing comments. My last comment was in reply to a question you posted but no longer exists. Answers are not much use without the question.
Oh sorry, my original reply said that the changes i made worked but they hadn't so didn't want to close off the communication when it was still busted. I will just add edits in the future. thanks again!