ロープをシミュレートするものを作って、ロープに抱かれている物がロープを持っている物の下にあるときにグリッチします。これはforループとその1つのセグメントではなく、複数のセグメントをシミュレートできるようにしたいときに機能します。グリッチロープフィジックス(処理2.2.1)
コード:
//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));
}
}
}
どのように私はこの問題を解決することができますか?
私は最後の行にリンクするためにそれを使用していましたが、変数を変更せずに減分する方法がありました。 – 8o7wer
助けてくれてありがとう、今私はロープのように見えるようにする方法を理解する必要があります – 8o7wer
@ 8o7werそれは私が言ったように思ったことですが、あなたが使いたいものは何ですか?それとも、0になるまで本当にiを使いたいのですか?その場合、単純なカウンターを入れ、それをiと同じにし、0になるまで減らしてください.i--=を使用すると、i == 0をチェックしても無限ループになります。決してi == linkCountではありません。 –