首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初学者程序员: Java (处理)和Leap运动控制器教程?

初学者程序员: Java (处理)和Leap运动控制器教程?
EN

Stack Overflow用户
提问于 2020-10-07 15:04:54
回答 2查看 184关注 0票数 2

我几乎没有编码经验,对于我的大学项目原型,我需要编写一个图像(正在处理的PImage )来使用我的手(Leap Motion Controller输入)移动。

目前,我被困在如何让我的图像使用我的手的位置来移动,但是错误弹出,告诉我图像只能使用浮点数作为输入,因为手的位置是使用PVector类计算的。我尝试使用float[] (name) = handpos.array();将其设置为浮点值,但之后我无法从图像所需的位置获取x和y值。

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

代码语言:javascript
复制
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(); 
}
 
}
EN

回答 2

Stack Overflow用户

发布于 2020-10-07 17:26:06

Dietrich的答案是正确的(+1)。

这里有几个警告。您得到的错误(需要像这样的参数:"image(PImage,float,float)")是由variable shadowing引起的

您可能正在尝试在for循环中呈现hand图像,但是,该循环使用了一个同名的局部变量(hand),在本例中它是一个LeapMotion Hand实例,而不是PImage

for循环变量(Hand实例)隐藏了全局变量(PImage变量)。

出现这个错误是因为它试图呈现一个LeapMotion Hand,而不是你的PImage

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

代码语言:javascript
复制
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);
  }
}

这将渲染每个手部的多个图像。

如果你想为一只手渲染一张图片,你可以做多件事。

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

代码语言:javascript
复制
import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50, 113, 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);
  
}

或者你可以使用第一只手,例如:

代码语言:javascript
复制
import de.voidplus.leapmotion.*;

LeapMotion leap;

//Assets
PImage hand;
//Colours
color ocean = color(50, 113, 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();
  // try to read first hand data
  Hand firstHand = leap.getHand(0);
  // check if there is actual data (at least one hand is present)
  if(firstHand != null){
    // read hand's position
    PVector handPosition = hand.getPosition();
    // render image to hand's position
    image(hand, handPosition.x, handPosition.y);
  }
  
}

有很多方法可以做到这一点,这取决于什么对你的项目的用户体验有意义。

票数 2
EN

Stack Overflow用户

发布于 2020-10-07 15:16:44

如果我没弄错你的问题,你可以这样做:

代码语言:javascript
复制
image(hand, hpos.x, hpos.y)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64238897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档