调试Java/处理对象巡更屏幕的输出

调试Java/处理对象巡更屏幕的输出,java,oop,rotation,processing,Java,Oop,Rotation,Processing,我是一名计算机编程一年级的学生,从事机器人和机器人测试环境项目。 虽然我是Stackoverflow的新手,但我很清楚我们不应该帮助完成作业或家庭作业。 我正在努力理解为什么会发生这样的行为,所以理解为什么会发生这样的行为真的很有帮助 我们使用处理库来用Java编程。 我们的任务是创建一个机器人测试环境,并创建三个执行不同动作的机器人 我试图编程的运动是巡逻,这意味着只需在屏幕边缘走动。 预期的行为是向前走,直到它到达一堵墙,向左拐(逆时针90度),然后再次向前走。 这个项目是与我们的数学课相结

我是一名计算机编程一年级的学生,从事机器人和机器人测试环境项目。 虽然我是Stackoverflow的新手,但我很清楚我们不应该帮助完成作业或家庭作业。 我正在努力理解为什么会发生这样的行为,所以理解为什么会发生这样的行为真的很有帮助

我们使用处理库来用Java编程。 我们的任务是创建一个机器人测试环境,并创建三个执行不同动作的机器人

我试图编程的运动是巡逻,这意味着只需在屏幕边缘走动。 预期的行为是向前走,直到它到达一堵墙,向左拐(逆时针90度),然后再次向前走。 这个项目是与我们的数学课相结合的。也就是说,我们不能使用像rotate()、translate()、pushMatrix()和popMatrix()这样的方法来帮助旋转表单(每个机器人都是三角形)

因此,我旋转三角形的步骤如下: 1) 使用相同的平移将其中心点平移到原点(0,0)和顶点; 2) 旋转所有点,其中(x,y)变成(y,-x); 3) 平移回正确位置(与步骤1平移相反)

我设置了一些边界if语句,以便在旋转后,如果有任何东西离开屏幕,则将三角形放回屏幕

我的问题
旋转后,三角形出现在奇怪的位置,就像传送一样

我添加了几条线,这样我们就可以得到每个顶点的坐标,它的方向,并在方向改变时进行检测

代码
有两个文件:TestRobots和Robot

机器人:

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();
    }
}
这是生成的输出的打印。我剪掉了它改变方向的部分:

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
非常感谢您在此之前的阅读,如果我不够清楚,请提前道歉。这是我的第一个问题


Gustavo

我认为您的问题与变量
centralPointX
centralPointY
有关。看看你写的这段代码:

//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;
由此看来,您认为
centralPointX
centralPointY
表示三角形的中心。。。但是,请查看它们的定义:

float centralPointX = width/2;
float centralPointY = height/2;
所以它们实际上并不代表中心xy坐标。您需要做的是修复此部分:

//Calculate translation of the central point of triangle to the origin.
float xTranslation = 0 - centralPointX;
float yTranslation = 0 - centralPointY;
所以它实际上做了评论中说它应该做的事情,也就是说,计算三角形中心的x和y坐标


我不会为你实现这一点,因为正如你所说,这是家庭作业。然而,我认为这肯定会为你指明正确的方向。还有,继续做好工作。我希望第一个问题都能像这样考虑周全。

请使用您的代码将其简化为您问题的一部分。您当前的代码包含了许多与您的问题无关的内容—一个最小的样本通常看起来类似于一个好的单元测试:只执行一个任务,输入值指定用于再现性。这真的很有帮助。现在,我面临着不同的问题,但还没有足够的研究。谢谢。@Gustavolesa很乐意帮忙!别忘了向上投票并接受=]如果您有新问题,请随意提出另一个问题我还有另一个关于相同方法但不同行为的问题。我想让我的物体,一碰到墙,就停下来,转360度,向左转,然后再往前走,而不是只是转,然后往前走。它发生得如此之快以至于看不见。不过,移动的物体速度很快。你认为我应该发布另一个关于这个的问题吗?还是在这个上面加点什么?再次感谢你@GustavoLessa可能会提出一个新问题,这样就不会变得混乱和困惑。。。请随意在这里发布链接,我会看一看
//Calculate translation of the central point of triangle to the origin.
float xTranslation = 0 - centralPointX;
float yTranslation = 0 - centralPointY;