初学者:Java处理和Leap Motion控制器教程?

问题描述

我几乎没有编码经验,对于我的大学项目原型,我需要编码图像(处理中的PImage)才能用手移动(跳跃运动控制器输入)。

此刻,我仍然停留在如何使用手的位置来移动图像的位置上,但是弹出错误消息,指出图像只能使用浮点数作为输入,因为手的位置是使用PVector计算的类。我尝试使用float[] (name) = handpos.array();将其设为浮点值,但随后无法从图像所需的位置获取x和y值。

这是我到目前为止在for循环中完成的代码,是我计划添加显示的图像的地方。

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50,113,250);

void setup(){
  size(700,700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
background (ocean);
int fps = leap.getFrameRate();
  for (Hand hand : leap.getHands ()) {
    Finger index = hand.getIndexFinger();
    PVector hpos = hand.getPosition(); 
    PVector ipos = index.getPosition(); 
}
 
}

解决方法

如果我正确理解了您的问题,则可以这样做:

vertex[x].neighbors_set.insert(1);
,

迪特里希的答案是正确的(+1)。

有两个警告。 您收到的错误(预期参数如:“ image(PImage,float,float)”)是由于variable shadowing

您可能正在尝试在for循环中渲染hand图像,但是,该循环使用具有相同名称(hand)的局部变量,本例中为LeapMotion {{ 1}}实例,而不是Hand

for循环变量(PImage实例)遮盖了全局变量(Hand变量)。

发生错误是因为它尝试呈现LeapMotion PImage而不是您的Hand

您可以简单地重命名本地变量:

PImage

这将每手渲染多张图像。

如果您想用一只手绘制一张图像,则可以做很多事情。

您可以从循环中提取位置,这意味着最近的一只手将控制图像位置:

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50,113,250);

void setup() {
  size(700,700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
  background (ocean);
  int fps = leap.getFrameRate();
  for (Hand leapHand : leap.getHands ()) {
    Finger index = leapHand.getIndexFinger();
    PVector hpos = leapHand.getPosition(); 
    PVector ipos = index.getPosition();
    image(hand,hpos.x,hpos.y);
  }
}

或者您可以使用第一手来作为示例:

import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50,250);

PVector handPosition = new PVector();

void setup() {
  size(700,700);
  background(255);
  leap = new LeapMotion(this);
  hand = loadImage("images/hand.png");
  imageMode(CENTER);
}

void draw() {
  background (ocean);
  int fps = leap.getFrameRate();
  // hand inside is the loop is a Leap Hand instance,shadowing the PImage
  for (Hand hand : leap.getHands ()) {
    Finger index = hand.getIndexFinger();
    PVector hpos = hand.getPosition(); 
    PVector ipos = index.getPosition();
    handPosition.set(hand.getPosition());
  }
  // hand here is your PImage
  image(hand,handPosition.x,handPosition.y);
  
}

执行此操作的方法有很多,这取决于对您项目的用户体验有意义的内容。