好的,我想旋转我在这个方法中拥有的 PVector。 此方法用 PVector 的 x 和 y 替换 posX 和 posY。 运动是由来自arduino的操纵杆决定的,它在x和y方向上移动图像,但我想根据操纵杆正在寻找的轴来转动 vector
public void moverPjUno(PVector coordenadas) {
if(areaXad==-1 && areaXat==-1){
miPersonaje.setPosX((miPersonaje.getPosX())+(int)coordenadas.x);
}
if(areaYab==-1 && areaYar==-1){
miPersonaje.setPosY((miPersonaje.getPosY())+(int)coordenadas.y);
}
}
最佳答案
我没有连接Arduino,我不知道你的操纵杆给你提供什么样的信息,所以我做了一个使用鼠标模仿操纵杆的处理示例:
int rad = 100;
void setup() {
size(400, 400);
}
void draw() {
background(255);
ellipse(width/2, height/2, rad*2, rad*2);
// Using the mouse to mimic the position of the joystick
float theta = atan2(mouseY-height/2, mouseX-width/2);
// Get the new position
float x = width/2+cos(theta)*rad;
float y = height/2+sin(theta)*rad;
// Show the new position
ellipse(x, y, 30, 30);
}
atan2 函数给出鼠标位置的角度,将参数替换为操纵杆位置的等效值。绘制的较小的椭圆
显示了您的miPersonaje
将根据代码中前面的x
和y
设置的位置。 rad
变量是任意的,仅用于显示目的,您可以将其设置为您想要的任何内容(如果需要的话)。
关于java - 旋转 PVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16390947/