初学者编程者: Java (Processing) 和 Leap Motion 手势控制器教程?

huangapple go评论72阅读模式
英文:

Beginner Coder: Java (Processing) and Leap Motion Controller Tutorials?

问题

我在编码方面经验很少在我的大学项目原型中我需要编写代码使图像在Processing中为PImage能够通过我的手移动Leap Motion手势控制器输入)。

目前我遇到了一个问题即如何根据我的手的位置使图像移动但出现了错误指出图像只能使用浮点数作为输入因为手的位置是使用PVector类计算的我尝试使用`float[] (name) = handpos.array();`将其转换为浮点数但然后无法从所需用于图像的位置获取x和y值

以下是我迄今为止完成的代码在for循环中我计划添加图像以使其出现

```java
import de.voidplus.leapmotion.*;

LeapMotion leap;

// 资源
PImage hand;
// 颜色
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(); 
  }
}

<details>
<summary>英文:</summary>

I have very little experience with coding and for my University Project Prototype, I need to code an image (a PImage in Processing) to move using my hands (Leap Motion Controller Input).

At the moment I am stuck on how to get my image to move using the position of my hand, but the error pops up saying that the image can only use floats as inputs since the position of the hand is calculated using the PVector class. I tried to use `float[] (name) = handpos.array();` to make it a float value but then I can&#39;t get the x and y value from the position needed for the image. 

Here is the code I have done so far, in the for loop, was where I was planning on adding the image to appear. 

    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(&quot;images/hand.png&quot;);
      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(); 
    }
     
    }



</details>


# 答案1
**得分**: 2

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

有一些注意事项。
你遇到的错误(期望像这样的参数:“image(PImage, float, float)”)是由于[变量屏蔽][1]造成的。

你可能正试图在for循环内渲染`hand`图像,然而,循环使用了一个同名的局部变量(`hand`),在这种情况下是LeapMotion的`Hand`实例,而不是`PImage`。

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

错误发生是因为它试图渲染LeapMotion的`Hand`而不是你的`PImage`。

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

```java
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, 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();
  for (Hand hand : leap.getHands ()) {
    Finger index = hand.getIndexFinger();
    PVector hpos = hand.getPosition(); 
    PVector ipos = index.getPosition();
    handPosition.set(hand.getPosition());
  }
  image(hand, handPosition.x, handPosition.y);
}

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

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 firstHand = leap.getHand(0);
  if(firstHand != null){
    PVector handPosition = hand.getPosition();
    image(hand, handPosition.x, handPosition.y);
  }
}

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

英文:

Dietrich's answer is correct (+1).

There are a couple of caveats.
The error you're getting (expects parameters like: "image(PImage, float, float)") is due to variable shadowing

You're probably trying to render the hand image inside the for loop, however, the loop uses a local variable with the same name (hand) which in this case is a LeapMotion Hand instance, not a PImage.

The for loop variable (Hand instance) is shadowing the global variable(PImage variable).

The error happens because it attempts to render a LeapMotion Hand instead of your PImage.

You could simply rename the local variable:

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(&quot;images/hand.png&quot;);
  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);
  }
}

This would render multiple images per hand.

If you want to render a single image for a single hand you could do multiple things.

You could extract the position out of the loop which means the most recent hand will control the image position:

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(&quot;images/hand.png&quot;);
  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);
  
}

or you could use the first hand for example:

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(&quot;images/hand.png&quot;);
  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&#39;s position
    PVector handPosition = hand.getPosition();
    // render image to hand&#39;s position
    image(hand, handPosition.x, handPosition.y);
  }
  
}

There are many ways of doing this, it depends on what makes sense for your project's user experience.

答案2

得分: 1

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

image(hand, hpos.x, hpos.y)
英文:

If I understand your problem correctly, you can do it like this:

image(hand, hpos.x, hpos.y)

huangapple
  • 本文由 发表于 2020年10月7日 15:04:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64238897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定