关于Java:旋转PVector

Rotating a PVector

好的,我想旋转此方法中的PVector。
此方法用PVector的x和y替换posX和posY。
该运动由来自arduino的操纵杆确定,它在x和y中移动图像,但是我想根据操纵杆所看的轴来旋转矢量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,也不知道您的操纵杆会为您提供什么样的信息,因此我制作了一个处理示例,该示例使用鼠标来模仿操纵杆:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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函数将angular赋予鼠标位置,将参数替换为与操纵杆位置等效的参数。较小的ellipse会显示在代码前面根据xy设置miPersonaje的位置。 rad变量是任意的,仅出于显示目的,您可以将其设置为所需的任何变量(如果需要的话)。