2017-01-31 6 views
1

私はロボットとロボットのテスト環境のプロジェクトに取り組んでいる初年度のコンピュータプログラミングの学生です。 私はStackoverflowを初めて使っていますが、私は割り当てや宿題を手伝ってはいけないことをよく知っています。 なぜこのような動作が起こっているのか理解できないので、なぜそれが起こっているのかを理解するだけでは大変役に立ちます。Javaのデバッグ/オブジェクトパトロール画面の出力処理

処理ライブラリを使用してJavaでプログラミングしています。 私たちの仕事は、ロボットテスト環境を作り、異なる動きをする3つのロボットを作り出すことです。

私がプログラムしようとしている動きは、パトロールです。これは単に画面の端を回ることを意味します。 予想される動作は、壁に到達するまで前進し、左に回し(反時計回りに90度)、再び前に進みます。 このプロジェクトは、Mathクラスと連携しています。つまり、rotate()、translate()、pushMatrix()、popMatrix()などのメソッドを使用してフォームを回転させることはできません(各ロボットは三角形です)。

したがって、三角形を回転させる手順は次のとおりです。 1)同じ変換を使用して、その中心点を原点(0,0)と頂点に変換します。 2)すべての点を回転させます。ここで(x、y)は(y、-x)になります。 3)正しい位置に戻します(ステップ1の逆翻訳)。

私はいくつかの境界if文を設定して、回転後に何かがスクリーンから外れた場合に三角形を画面に戻します。

私の問題
は投入後、三角形はテレポートのような、奇妙な位置に表示されています。

私は各頂点の座標、それが行く方向、および方向が変更されたときに検出できるように、いくつかの行を追加しました。

コード
TestRobots、Robotの2つのファイルがあります。

ロボット:

import processing.core.PApplet; 

public class Robot{ 
    int colour; 
    String name; 
    float width; 
    float height; 
    float x1; 
    float y1; 
    float x2; 
    float y2; 
    float x3; 
    float y3; 
    int direction; 
    int speed; 
    float centralPointX = width/2; 
    float centralPointY = height/2; 
    PApplet parent; 

    public Robot(PApplet parent, String name, int colour, float x1, float y1, float x2, float y2, float x3, float y3, int speed){ 
     this.parent = parent; 
     this.name = name; 
     this.colour = colour; 
     this.x1 = x1; 
     this.y1 = y1; 
     this.x2 = x2; 
     this.y2 = y2; 
     this.x3 = x3; 
     this.y3 = y3; 
     this.speed = speed; 
     direction=4; 

     width = x2-x3; 
     if (width < 0){ 
      width *= -1; 
     } 
     if (y2 > y3){ 
      height = y2-y1; 
     }else{ 
      height = y3-y1; 
     } 
     if (height < 0){ 
      height *= -1; 
     } 

     if (y1<y2 && y1<y3){ 
      direction=4; 
     } 
    } 

    public void drawRobot(){ 
     parent.fill(colour); 
     parent.triangle(x1, y1, x2, y2, x3, y3); 
     parent.ellipseMode(parent.CENTER); 
     parent.ellipse(x1, y1, 3, 3); 
    } 

    public void moveForward(){ 
     if(x1<parent.width || y1 <parent.height || x1 > 0 || y1 > 0){     
      switch (direction){ 
       case 1: 
        x1 += speed; 
        x2 += speed; 
        x3 += speed; 
        break; 
       case 2: 
        y1 += speed; 
        y2 += speed; 
        y3 += speed; 
        break; 
       case 3: 
        x1 -= speed; 
        x2 -= speed; 
        x3 -= speed; 
        break; 
       case 4: 
        y1 -= speed; 
        y2 -= speed; 
        y3 -= speed; 
        break; 
      } 
     } 
    } 

    public void turnLeft(){ 
     //Store original coordinates. 
     float tempX1 = x1; 
     float tempY1 = y1; 
     float tempX2 = x2; 
     float tempY2 = y2;   
     float tempX3 = x3; 
     float tempY3 = y3; 

     //Calculate translation of the central point of triangle to the origin. 
     float xTranslation = 0 - centralPointX; 
     float yTranslation = 0 - centralPointY; 

     //Translate all points by the translation calculated. 
     float translatedX1 = tempX1 + xTranslation; 
     float translatedY1 = tempY1 + yTranslation; 
     float translatedX2 = tempX2 + xTranslation; 
     float translatedY2 = tempY2 + yTranslation; 
     float translatedX3 = tempX3 + xTranslation; 
     float translatedY3 = tempY3 + yTranslation; 

     //Rotate all points 90 degrees counterclockwise, (x, y) --> (y, -x). 
     float rotatedX1 = translatedY1; 
     float rotatedY1 = -translatedX1; 
     float rotatedX2 = translatedY2; 
     float rotatedY2 = -translatedX2; 
     float rotatedX3 = translatedY3; 
     float rotatedY3 = -translatedX3; 

     //Translate all points back. 
     x1 = rotatedX1 - xTranslation; 
     y1 = rotatedY1 - yTranslation; 
     x2 = rotatedX2 - xTranslation; 
     y2 = rotatedY2 - yTranslation; 
     x3 = rotatedX3 - xTranslation; 
     y3 = rotatedY3 - yTranslation; 

     //Check which y and which x are the smallest, in order to correct any negative numbers. 
     float minX; 
     float minY; 
     if (y1<y2 && y1<y3){ 
      minY = y1; 
     } else if (y2<y1 && y2<y3){ 
      minY = y2; 
     } else { 
      minY = y3; 
     } 
     if (x1<x2 && x1<x3){ 
      minX = x1; 
     } else if (x2<x1 && x2<x3){ 
      minX = x2; 
     } else { 
      minX = x3; 
     } 

     //Check which y and which x are the biggest, in order to correct any out-of-screen draws. 
     float maxX; 
     float maxY; 
     if (y1>y2 && y1>y3){ 
      maxY = y1; 
     } else if (y2>y1 && y2>y3){ 
      maxY = y2; 
     } else { 
      maxY = y3; 
     } 
     if (x1>x2 && x1>x3){ 
      maxX = x1; 
     } else if (x2>x1 && x2>x3){ 
      maxX = x2; 
     } else { 
      maxX = x3; 
     }  

     //Correct position if any coordinate is negative. 
     if((minY-speed)<=minY){ 
      float differenceY = -minY + 10; 
      y1 += differenceY; 
      y2 += differenceY; 
      y3 += differenceY; 
     } 
     if(x1<=(x1-speed)){ 
      float differenceX = -minX + 10; 
      x1 += differenceX; 
      x2 += differenceX; 
      x3 += differenceX; 
     } 

     //Correct position if any coordinate is bigger than the screen size. 
     if((parent.height<=(maxY+speed))){ 
      float differenceY = (-maxY+parent.height) + 10; 
      y1 -= differenceY; 
      y2 -= differenceY; 
      y3 -= differenceY; 
     } 
     if((x1+speed)>=parent.width){ 
      float differenceX = (-maxX+parent.width) + 10; 
      x1 -= differenceX; 
      x2 -= differenceX; 
      x3 -= differenceX; 
     } 

     //Change direction variable and adjust it between 0 and 4. 
     direction -=1; 
     if (direction == 0){ 
      direction = 4; 
     } 
    } 

    public void patrol(){ 
     System.out.println("Direction is: "+ direction); 
     if(((y1-speed)<= 0)||((y1+speed)>= parent.height) || ((x1+speed)>=parent.width)||((x1-speed)<=0)){ 
      turnLeft(); 
      System.out.println("The NEW direction is: "+ direction); 
     } 
     moveForward(); 
    } 
} 

TestRobots:

import processing.core.PApplet; 

public class TestRobots extends PApplet{ 
    Robot alice = new Robot(this, "Alice", 255, 257f, 389f, 309f, 450f, 209f, 450f, 3); 

    public static void main(String[] args){ 
     PApplet.main("TestRobots"); 
    } 

    public void settings(){ 
     size(1000, 500); 
    } 

    public void setup(){ 
     frameRate(30);  
    } 

    public void draw(){ 
     background(255); 
     alice.patrol(); 
     System.out.println("x1 = "+ alice.x1); 
     System.out.println("y1 = "+ alice.y1); 

     System.out.println("x2 = "+ alice.x2); 
     System.out.println("y2 = "+ alice.y2); 

     System.out.println("x3 = "+ alice.x3); 
     System.out.println("y3 = "+ alice.y3); 

     alice.drawRobot(); 
    } 
} 

ここでは、生成される出力のプリントです。ここ

Direction is: 2 
x1 = 62.0 
y1 = 497.0 
x2 = 10.0 
y2 = 436.0 
x3 = 110.0 
y3 = 436.0 

The NEW direction is: 1 
x1 = 500.0 
y1 = 58.0 
x2 = 439.0 
y2 = 110.0 
x3 = 439.0 
y3 = 10.0 

同じ奇妙な行動:

Direction is: 4 
x1 = 257.0 
y1 = 2.0 
x2 = 309.0 
y2 = 63.0 
x3 = 209.0 
y3 = 63.0 

The NEW direction is: 3 
x1 = -1.0 
y1 = 62.0 
x2 = 60.0 
y2 = 10.0 
x3 = 60.0 
y3 = 110.0 

The NEW direction is: 2 
x1 = 62.0 
y1 = 74.0 
x2 = 10.0 
y2 = 13.0 
x3 = 110.0 
y3 = 13.0 

が、私は十分に明確でなかった場合には、事前にまでここに申し訳ありません読みいただき、誠にありがとうございます、私はそれが方向を変える部分を切り取ら。それは私の最初の質問です!

グスタボ

+0

コードを編集して問題の[mcve]にしてください。あなたの現在のコードには、問題の周辺にあるものが多く含まれています。通常、最小単位のサンプルは良い単位テストと似ています。 –

答えて

1

私はあなたの問題は、変数centralPointXcentralPointYに関連すると思います。だから、

float centralPointX = width/2; 
float centralPointY = height/2; 

:それはあなたがcentralPointXcentralPointYは、しかし、それらが定義されているところを見て...三角形の中心を示していると思うここから思わ

//Calculate translation of the central point of triangle to the origin. 
float xTranslation = 0 - centralPointX; 
float yTranslation = 0 - centralPointY; 

//Translate all points by the translation calculated. 
float translatedX1 = tempX1 + xTranslation; 
float translatedY1 = tempY1 + yTranslation; 
float translatedX2 = tempX2 + xTranslation; 
float translatedY2 = tempY2 + yTranslation; 
float translatedX3 = tempX3 + xTranslation; 
float translatedY3 = tempY3 + yTranslation; 

:あなたが書いたこのコードを見てくださいそれらは実際に中心x-y座標を表さない。それは実際にコメントがつまり、あなたの三角形の中心のxとy座標を計算し、それが何をすべき言うないように

//Calculate translation of the central point of triangle to the origin. 
float xTranslation = 0 - centralPointX; 
float yTranslation = 0 - centralPointY; 

:何をする必要があると、この部分を修正しています。

私があなたのためにこれを実装するつもりはありません、あなたが言ったように、それは宿題です。しかし、私はこれが間違いなくあなたを正しい方向に向けるべきだと思います。また、良い仕事を続けてください。私はすべての最初の質問がこれと同様に考えられたことを願っています。

+0

本当に助かりました。今私は別の問題に直面していますが、それについて十分に調査していません。ありがとう。 –

+0

@GustavoLessa助けて嬉しいです! upvoteとaccept =を忘れないでください。新しい問題がある場合は、別の質問を自由にしてください。 – nhouser9

+0

私は同じ方法に関して別の質問がありますが、動作は異なります。私は、360度回転して左に曲がって行くだけで、旋回して前進するのではなく、壁に達するとすぐに物体を欲しいです。 これは非常に速く起こるため、表示されません。しかし、動く物体はすばらしく速いです。 これについて別の質問を投稿する必要があると思いますか?または、これに何かを追加するだけですか? もう一度ありがとうございます! –

関連する問題