我几乎没有编码经验,对于我的大学项目原型,我需要编写一个图像(正在处理的PImage )来使用我的手(Leap Motion Controller输入)移动。
目前,我被困在如何让我的图像使用我的手的位置来移动,但是错误弹出,告诉我图像只能使用浮点数作为输入,因为手的位置是使用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();
}
}发布于 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。
您可以简单地重命名局部变量:
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();
// 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);
}或者你可以使用第一只手,例如:
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);
}
}有很多方法可以做到这一点,这取决于什么对你的项目的用户体验有意义。
发布于 2020-10-07 15:16:44
如果我没弄错你的问题,你可以这样做:
image(hand, hpos.x, hpos.y)https://stackoverflow.com/questions/64238897
复制相似问题