java - 旋转 PVector

标签 java vector rotation arduino processing

好的,我想旋转我在这个方法中拥有的 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将根据代码中前面的xy设置的位置。 rad 变量是任意的,仅用于显示目的,您可以将其设置为您想要的任何内容(如果需要的话)。

关于java - 旋转 PVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16390947/

相关文章:

java - java中对应的.net usercontrol是什么?

Java双重错误

c++ - 使用与 double 数组的内存匹配的 std::vector

iOS:设备旋转后如何运行功能(Swift)

css - 如何缩放和旋转圆形搜索按钮?

android - 制作一个箭头始终指向地球地面的应用程序

java - 通过 JS 调用 JSF 方法

Java String split ("|") 方法调用无法正常工作

search - MATLAB:在向量和向量数组之间计算均方根误差的最快方法

c++ - priority_queue 上的 reinterpret_cast 是否迭代底层 vector 非标准?