Java 旋转PVector

Java 旋转PVector,java,vector,rotation,arduino,processing,Java,Vector,Rotation,Arduino,Processing,好了,伙计们,我想旋转一个PVector,我用这个方法。 此方法用PVector的x和y替换posX和posY。 运动是由arduino的操纵杆决定的,它在x和y方向上移动图像,但我想根据操纵杆所看的轴来转动矢量 public void moverPjUno(PVector coordenadas) { if(areaXad==-1 && areaXat==-1){ miPersonaje.setPosX((miPersonaje.getPosX())+(int)coorde

好了,伙计们,我想旋转一个PVector,我用这个方法。 此方法用PVector的x和y替换posX和posY。 运动是由arduino的操纵杆决定的,它在x和y方向上移动图像,但我想根据操纵杆所看的轴来转动矢量

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
函数给出鼠标位置的角度,用相当于操纵杆位置的参数替换参数。正在绘制的较小的
椭圆
显示将根据代码前面的
x
y
设置
miPersonaje
的位置。
rad
变量是任意的,仅用于显示目的,您可以将其设置为任意值(如果需要)