2016-08-20 2 views
1

ロープをシミュレートするものを作って、ロープに抱かれている物がロープを持っている物の下にあるときにグリッチします。これは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)); 
    } 
    } 
} 

どのように私はこの問題を解決することができますか?

答えて

0

まず、画面サイズを設定するために変数を使用しないでください。処理2.2.1はそれを気に入らず、処理3ではそれも許されません。第二に

:calcRopesからこのような

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

書()は、ArrayIndexOutOfBoundsExceptionが発生します。なぜ私は〜を使っていますか?あなたがそれを使うたびに、私は一度減額されます。したがって、i = 2から始めると、 "line(x [i]、y [i]、x [i--]、y [i--]);"

編集: これは私にとってはうまくいったが、まだ調整が必要だと思う。

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; 
     } 
    } 
    } 
} 
+0

私は最後の行にリンクするためにそれを使用していましたが、変数を変更せずに減分する方法がありました。 – 8o7wer

+0

助けてくれてありがとう、今私はロープのように見えるようにする方法を理解する必要があります – 8o7wer

+0

@ 8o7werそれは私が言ったように思ったことですが、あなたが使いたいものは何ですか?それとも、0になるまで本当にiを使いたいのですか?その場合、単純なカウンターを入れ、それをiと同じにし、0になるまで減らしてください.i--=を使用すると、i == 0をチェックしても無限ループになります。決してi == linkCountではありません。 –

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])); 

は私が欲しかったものです誤算作られたエラーが発生しました。 Imはまだ現実的に見えるようにすることに問題がありますが、私はそれを自分で解決することができます。

関連する問題