我做了一些模拟绳子的东西,当绳子抓住的东西在拿着绳子的东西下面时,它会出现故障。这不适用于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));
}
}
}
我该如何解决这个问题?
答案 0 :(得分:0)
首先:不要使用变量设置屏幕大小,处理2.2.1不喜欢,处理3甚至不允许。
其次:
line(x[i], y[i], x[i--], y[i--]);
来自calcRopes()的这样的语句将导致ArrayIndexOutOfBoundsException。你为什么用i--?每次你使用它,我会减少一次。所以从i = 2开始,你的i的值在“line(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;
}
}
}
}
答案 1 :(得分:0)
我发现了这一行:
R = atan2(-(x[i] - x[i-1]), -(y[i] - x[i-1]));
有一个错误导致错误计算弧度
R = atan2(-(x[i] - x[i-1]), -(y[i] - y[i-1]));
是我想要的。 我仍然有问题让它看起来很逼真,但我可以自己解决。