提问者:小点点

毛刺绳物理(处理2.2.1)


我做了一些东西来模拟绳子,当绳子抓住的东西在绳子抓住的东西下面时,它会出现故障。这在不在for循环中时有效,它只是一个片段,但我希望能够模拟多个片段。

代码:

//Declaring variables
int linkCount = 3;
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 = 50;
float velMX;
float velMY;
float spring = 1;
void setup() {
  size(1280, 500, P3D);
  stroke(0);
  fill(0);
}
void draw() {
  background(255);
  // Updating velocitys
  for(int i=1; i < linkCount; i++) {
      x[i] = x[i] + xVel[i];
      y[i] = y[i] + yVel[i];
  }
  // The two lines below are not needed and will most likely will be used in the futre
  velMX =  pmouseX - mouseX;
  velMY = pmouseY - mouseY;
  // Setting the start of the rope to the mouse
  x[0] = mouseX;
  y[0] = mouseY;
//  if(mousePressed) {
    calcRopes();
//  }
}
void calcRopes() {
  for(int i = 1; i < linkCount; i++) {
  // Getting a radian that points toward the last subrope
  R = atan2(-(x[i] - x[i-1]), -(y[i] - x[i-1]));
  // Drawing the rope
  line(x[i], y[i], x[i - 1], y[i - 1]);
  // If the segment is farther than the rope length it moves it inside the rope length based on the R radian
  if(dist(x[i], y[i], x[i - 1], y[i - 1]) > ropeLength) {
  x[i] = x[i] + ((dist(x[i], y[i], x[i - 1], y[i - 1]) - (ropeLength)) * sin(R));
  y[i] = y[i] + ((dist(x[i], y[i], x[i - 1], y[i - 1]) - (ropeLength)) * cos(R));
  //      xVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength + 10)) * sin(R));
  //      yVel[i] =  ((dist(x[i], y[i], x[i-1], y[i-1]) - (ropeLength + 10)) * cos(R));
  }
  }
}

我如何解决这个问题?


共2个答案

匿名用户

首先:不要使用变量来设置屏幕尺寸,处理2.2.1不喜欢这样,处理3甚至不允许这样。

其次:

  line(x[i], y[i], x[i--], y[i--]);

calcRope()中这样的语句将导致ArrayIndexOutOfBoundsException。你为什么使用i--for?每次你使用它,i都会减少一次。所以从i=2开始,在“line(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;
      }
    }
  }
}

匿名用户

我发现了这一行:

R = atan2(-(x[i] - x[i-1]), -(y[i] - x[i-1]));

有一个错误导致它错误地计算了弧度

R = atan2(-(x[i] - x[i-1]), -(y[i] - y[i-1]));

这是我想要的。我仍然无法使它看起来真实,但我可以自己解决。