Processing 滑溜绳物理(处理2.2.1)

Processing 滑溜绳物理(处理2.2.1),processing,Processing,我做了一些东西来模拟一根绳子,当绳子固定的东西在绳子固定的东西下面时,它会出现故障。当不在for循环中,并且它只有一个段时,这是可行的,但我希望能够模拟多个段 代码: //声明变量 int linkCount=3; 浮动旋转=0; 浮动R=弧度(旋转); 浮动x[]=新浮动[linkCount]; 浮动y[]=新浮动[linkCount]; float xVel[]=新的float[linkCount]; 浮动yVel[]=新浮动[linkCount]; 浮绳长度=50; 浮动velMX; 飘浮

我做了一些东西来模拟一根绳子,当绳子固定的东西在绳子固定的东西下面时,它会出现故障。当不在for循环中,并且它只有一个段时,这是可行的,但我希望能够模拟多个段

代码:

//声明变量
int linkCount=3;
浮动旋转=0;
浮动R=弧度(旋转);
浮动x[]=新浮动[linkCount];
浮动y[]=新浮动[linkCount];
float xVel[]=新的float[linkCount];
浮动yVel[]=新浮动[linkCount];
浮绳长度=50;
浮动velMX;
飘浮天鹅绒;
浮簧=1;
无效设置(){
尺寸(1280500,P3D);
冲程(0);
填充(0);
}
作废提款(){
背景(255);
//更新速度
对于(int i=1;i长度){
x[i]=x[i]+((距离(x[i],y[i],x[i-1],y[i-1])-(长度))*sin(R));
y[i]=y[i]+((距离(x[i],y[i],x[i-1],y[i-1])-(长度))*cos(R));
//xVel[i]=((距离(x[i],y[i],x[i-1],y[i-1])-(长度+10))*sin(R));
//yVel[i]=((dist(x[i],y[i],x[i-1],y[i-1])-(ropeLength+10))*cos(R));
}
}
}

如何解决这个问题?

首先:不要使用变量来设置屏幕大小,processing 2.2.1不喜欢这样,processing 3甚至不允许这样

第二:

  line(x[i], y[i], x[i--], y[i--]);
calcRopes()中的类似语句将导致ArrayIndexOutOfBoundsException。你为什么用我来做这个?每次你用它,我就递减一次。从i=2开始,在“行(x[i],y[i],x[i--],y[i--])之后,i的值变成-1

编辑: 这对我来说很有效,但我认为它仍然需要一些调整

int linkCount = 10;
float Rotation = 0;
float R = radians(Rotation);
float x[] = new float[linkCount];
float y[] = new float[linkCount];
float xVel[] = new float[linkCount];
float yVel[] = new float[linkCount];
float ropeLength = 100;
float velMX;
float velMY;
float spring = 1;
void setup() {
  size(1280, 500); //CHANGED THIS***********************
}
void draw() {
  stroke(0);
  fill(0);
  background(255);
  for (int i=0; i < linkCount; i++) {
    x[i] = x[i] + xVel[i];
    y[i] = y[i] + yVel[i];
  }
  velMX =  pmouseX - mouseX;
  velMY = pmouseY - mouseY;
  x[1] = mouseX;
  y[1] = mouseY;
  //  if(mousePressed) {
  calcRopes();
  //  }
}

//REPLACED ALL i-- with i-1 (just to see if it works)***********************
void calcRopes() {
  for (int i = 1; i < linkCount; i++) {
    if (i<x.length && i<y.length) {
      R = atan2(-(x[i] - mouseX), -(y[i] - mouseY));
      line(x[i], y[i], x[i-1], y[i-1]);
      if (dist(x[i], y[i], x[i - 1], y[i - 1]) > ropeLength) {
        xVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength - 1)) * sin(R)) / spring;
        yVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength - 1)) * cos(R)) / spring;
      }
    }
  }
}
int linkCount=10;
浮动旋转=0;
浮动R=弧度(旋转);
浮动x[]=新浮动[linkCount];
浮动y[]=新浮动[linkCount];
float xVel[]=新的float[linkCount];
浮动yVel[]=新浮动[linkCount];
浮绳长度=100;
浮动velMX;
飘浮天鹅绒;
浮簧=1;
无效设置(){
大小(1280500);//更改此值***********************
}
作废提款(){
冲程(0);
填充(0);
背景(255);
对于(int i=0;i如果(i首先:不要使用变量来设置屏幕大小,processing 2.2.1不喜欢这样,processing 3甚至不允许这样

第二:

  line(x[i], y[i], x[i--], y[i--]);
calcRopes()中的此类语句将导致ArrayIndexOutOfBoundsException。为什么要使用i--for?每次使用它时,i都会递减一次。因此,从i=2开始,i的值在“行(x[i],y[i],x[i--],y[i--])之后变为-1

编辑: 这对我来说很有效,但我认为它仍然需要一些调整

int linkCount = 10;
float Rotation = 0;
float R = radians(Rotation);
float x[] = new float[linkCount];
float y[] = new float[linkCount];
float xVel[] = new float[linkCount];
float yVel[] = new float[linkCount];
float ropeLength = 100;
float velMX;
float velMY;
float spring = 1;
void setup() {
  size(1280, 500); //CHANGED THIS***********************
}
void draw() {
  stroke(0);
  fill(0);
  background(255);
  for (int i=0; i < linkCount; i++) {
    x[i] = x[i] + xVel[i];
    y[i] = y[i] + yVel[i];
  }
  velMX =  pmouseX - mouseX;
  velMY = pmouseY - mouseY;
  x[1] = mouseX;
  y[1] = mouseY;
  //  if(mousePressed) {
  calcRopes();
  //  }
}

//REPLACED ALL i-- with i-1 (just to see if it works)***********************
void calcRopes() {
  for (int i = 1; i < linkCount; i++) {
    if (i<x.length && i<y.length) {
      R = atan2(-(x[i] - mouseX), -(y[i] - mouseY));
      line(x[i], y[i], x[i-1], y[i-1]);
      if (dist(x[i], y[i], x[i - 1], y[i - 1]) > ropeLength) {
        xVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength - 1)) * sin(R)) / spring;
        yVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength - 1)) * cos(R)) / spring;
      }
    }
  }
}
int linkCount=10;
浮动旋转=0;
浮动R=弧度(旋转);
浮动x[]=新浮动[linkCount];
浮动y[]=新浮动[linkCount];
float xVel[]=新的float[linkCount];
浮动yVel[]=新浮动[linkCount];
浮绳长度=100;
浮动velMX;
飘浮天鹅绒;
浮簧=1;
无效设置(){
大小(1280500);//更改此值***********************
}
作废提款(){
冲程(0);
填充(0);
背景(255);
对于(int i=0;i
R = atan2(-(x[i] - x[i-1]), -(y[i] - x[i-1]));
有一个错误使得它误算了弧度

R = atan2(-(x[i] - x[i-1]), -(y[i] - y[i-1]));
这就是我想要的。 我在使它看起来逼真方面仍然有问题,但我可以自己解决。

我发现了这句话:

R = atan2(-(x[i] - x[i-1]), -(y[i] - x[i-1]));
有一个错误使得它误算了弧度

R = atan2(-(x[i] - x[i-1]), -(y[i] - y[i-1]));
这就是我想要的。
我在使它看起来更真实方面仍然有问题,但我自己可以解决。

我用它链接到最后一行,我会找到一种方法在不改变变量的情况下减少它。谢谢你的帮助,现在我需要弄清楚如何使它看起来更像一个rope@8o7wer这就是我的想法,但正如我所说的,每次你使用i--。这不是你想要使用的i-1吗?或者你真的想使用i--,直到它为0吗?在这种情况下,放一个简单的计数器,使它等于i,然后递减它直到它等于0。使用i的另一个问题是,即使你检查了i==0,这将是一个无限循环,因为它永远不会是i==linkCount。@8o7wer没有问题!看起来像这样这是个不错的主意。我不认为需要花费更多的时间来完成它。你也可以考虑一个静态的绳子长度,把这个长度分成一定数量的较小的线条。你也可以使用大小(1280, 500,p3d)。使用GPU的强大功能,你可以通过更多的子线创建更精确的运动。是的,这正是我想要的,但现在当绳子抓住自己时,我遇到了一个小故障?我用它链接到最后一行,我会找到一种方法,在不改变变量的情况下减小它。