Java 手部位置未按预期更新-处理时跳跃运动
我正在尝试使用Leap Motion controller through Processing(Java)制作一个theremin。到目前为止,我已经非常接近了。我可以通过一只手在控制器上保持静止,而另一只手移动来改变音调和振幅。我希望这样做,一只手可以做到,或者左手控制振幅,右手控制音调 事实上,当另一只手移动时,你必须保持一只手完全静止。如果只使用一只手,则仅当手完全离开并重新进入控制器视图时,它才会更新音调。我想知道是否有人能告诉我为什么会发生这种情况,以及我能做些什么使它与手的位置保持一致 代码如下:Java 手部位置未按预期更新-处理时跳跃运动,java,processing,leap-motion,Java,Processing,Leap Motion,我正在尝试使用Leap Motion controller through Processing(Java)制作一个theremin。到目前为止,我已经非常接近了。我可以通过一只手在控制器上保持静止,而另一只手移动来改变音调和振幅。我希望这样做,一只手可以做到,或者左手控制振幅,右手控制音调 事实上,当另一只手移动时,你必须保持一只手完全静止。如果只使用一只手,则仅当手完全离开并重新进入控制器视图时,它才会更新音调。我想知道是否有人能告诉我为什么会发生这种情况,以及我能做些什么使它与手的位置保持
import processing.sound.*;
import de.voidplus.leapmotion.*;
LeapMotion leap;
ArrayList<PVector> points;
PVector fp;
float freq;
float amp;
TriOsc tri;
SinOsc sin;
SqrOsc sqr;
SawOsc saw;
void setup () {
size(640,800);
leap = new LeapMotion(this);
points = new ArrayList<PVector>();
tri = new TriOsc(this);
sin = new SinOsc(this);
sqr = new SqrOsc(this);
saw = new SawOsc(this);
}
void leapOnInit() {
// println("Leap Motion Init");
}
void leapOnConnect() {
// println("Leap Motion Connect");
}
void leapOnFrame() {
// println("Leap Motion Frame");
}
void leapOnDisconnect() {
// println("Leap Motion Disconnect");
}
void leapOnExit() {
// println("Leap Motion Exit");
}
void draw() {
tri.freq(freq);
tri.amp(amp);
tri.play();
for (Hand hand : leap.getHands()) {
fp = hand.getPosition();
if (fp.z <= 30) {
points = new ArrayList<PVector>();
}
else if (fp.z > 30) {
points.add(new PVector(fp.x, fp.y));
}
}
for (int i = points.size()-1; i >= 0; i--) {
PVector p = points.get(i);
amp = map(width-p.x, 0, width, 1, .01); //Volume based on x-axis
freq = map(height-p.y, 0, height, 40, 880); //Pitch based on y-axis
}
}
导入处理。声音。*;
导入de.voidplus.leapmotion.*;
跳跃运动;
阵列列表点;
PVector fp;
浮动频率;
浮动放大器;
TriOsc-tri;
SinOsc sin;
SqrOsc sqr;
锯切机;
无效设置(){
规模(640800);
leap=新的leap运动(本);
points=新的ArrayList();
tri=新的TriOsc(本);
sin=新的SinOsc(本);
sqr=新的SqrOsc(本);
saw=新的SAWSOC(本);
}
void leapOnInit(){
//println(“跳跃运动初始”);
}
void leapOnConnect(){
//println(“跳跃运动连接”);
}
void leapOnFrame(){
//println(“跳跃运动帧”);
}
void leapOnDisconnect(){
//println(“跳跃运动断开”);
}
void leapOnExit(){
//println(“跳跃运动退出”);
}
作废提款(){
三频(freq);
三安培;
tri.play();
for(Hand-Hand:leap.getHands()){
fp=hand.getPosition();
如果(fp.z 30){
添加(新PVector(fp.x,fp.y));
}
}
对于(int i=points.size()-1;i>=0;i--){
PVector p=点。获取(i);
amp=贴图(宽度-p.x,0,宽度,1.01);//基于x轴的体积
freq=map(height-p.y,0,height,40880);//基于y轴的俯仰
}
}
好的,我知道了。顺便说一句,请注意,Leap Motion SDK上的文档使用的库与您在处理时选择“Sketch>Import LIBRARY…>Leap Motion for Processing”时实际导入的库完全不同。我花了半天的时间试图弄明白为什么他们的代码示例使用类而不告诉您其中包含什么
文档的使用令人沮丧,所以我没有使用它,也没有制作自己的交互框。相反,我玩弄了我的代码,发现有一个“else”语句把我的阅读弄乱了。一旦我摆脱了它,那该死的东西就像一个符咒。我甚至用它来区分我左右手的“amp”和“freq”
下面是它的样子:
import processing.sound.*;
import de.voidplus.leapmotion.*;
LeapMotion leap;
Theremin leapTheremin;
ArrayList<PVector> points;
PVector handPos;
TriOsc tri;
void setup () {
size(640,800);
leap = new LeapMotion(this);
points = new ArrayList<PVector>();
tri = new TriOsc(this);
leapTheremin = new Theremin(tri);
}
void draw() {
leapTheremin.renderSound();
for (Hand hand : leap.getHands()) {
handPos = hand.getPosition();
boolean handIsLeft = hand.isLeft();
boolean handIsRight = hand.isRight();
if (handPos.z <= 75) {
points = new ArrayList<PVector>();
points.add(new PVector(handPos.x, handPos.y));
background((handPos.x)/2.5,(width-handPos.x)/3,handPos.y-(height/3.5));
}
if (hand.isRight()) {
leapTheremin.setPitch();
}
if (hand.isLeft()) {
leapTheremin.setVolume();
}
}
}
class Theremin {
float freq;
float amp;
int sound;
Theremin (TriOsc tri_g) {
setPitch();
sound = 1;
tri = tri_g;
}
void setPitch () {
for (int i = points.size()-1; i >= 0; i--) {
PVector p = points.get(i);
freq = map((height-handPos.y)+10, 0, height, 40, 880); //"upright antenna", aka "the right one that controls pitch"
// To match the colors with the moaods of the pitches
}
}
void setVolume() {
for (int i = points.size()-1; i >= 0; i--) {
PVector p = points.get(i);
amp = map(width-p.x, 0, width, 1, .01); //"loop antenna", aka "the left one that controls volume"
}
}
void renderSound() {
tri.freq(freq);
tri.amp(amp);
tri.play();
}
}
导入处理。声音。*;
导入de.voidplus.leapmotion.*;
跳跃运动;
特莱明;特莱明;
阵列列表点;
PVector-handPos;
TriOsc-tri;
无效设置(){
规模(640800);
leap=新的leap运动(本);
points=新的ArrayList();
tri=新的TriOsc(本);
leapTheremin=新Theremin(tri);
}
作废提款(){
leapTheremin.renderSound();
for(Hand-Hand:leap.getHands()){
handPos=hand.getPosition();
布尔handIsLeft=hand.isLeft();
布尔handIsRight=hand.isRight();
如果(handPos.z=0;i--){
PVector p=点。获取(i);
freq=map((height handPos.y)+10,0,height,40880);/“直立天线”,也称为“控制音高的右侧天线”
//使颜色与音高的颜色相匹配
}
}
void setVolume(){
对于(int i=points.size()-1;i>=0;i--){
PVector p=点。获取(i);
amp=map(width-p.x,0,width,1.01);/“环形天线”,又名“控制音量的左侧天线”
}
}
void renderSound(){
三频(freq);
三安培;
tri.play();
}
}
我还想提到,我在以下网站上对此问题进行了一些研究:()你确定这是JavaScript代码吗?它看起来真的像Java代码……我错了,是的,你是对的。我编辑它是为了反映它是Java,不是JavaScript。如果是这样的话,那么您第一条评论中的第二个链接也可能有点误导,因为它是JavaScript的开发人员门户,它可能会为您实际使用的平台提供完全不合适或不完整的代码。这是一个公平的观点。意识到这个错误后,我查阅了这个链接:()然而,我还没有找到任何我认为可以解决这个问题的东西。编辑后删除了一些我不再需要的代码或导致错误的代码。