调试对象巡逻屏幕

时间:2017-01-31 16:52:14

标签: java oop rotation processing

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

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

我尝试编程的动作是Patrol,这意味着只需绕过屏幕边缘。 预期的行为是前进直到它到达墙壁,向左转(逆时针90度),然后再前进。 该项目与我们的Math课程相结合。话虽这么说,我们不能使用像rotate(),translate(),pushMatrix()和popMatrix()这样的方法来帮助旋转表格(每个机器人都是一个三角形)。

因此,旋转三角形的步骤是: 1)使用相同的平移将其中心点转换为原点(0,0)和顶点; 2)旋转所有点,其中(x,y)变为(y,-x); 3)转换回正确的位置(步骤1翻译的倒数)。

我设置了一些边界if 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();
    }
}

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

非常感谢您在此之前阅读,如果我不够清楚,请提前抱歉。这是我的第一个问题!

古斯塔沃

1 个答案:

答案 0 :(得分:1)

我认为您的问题与变量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;

从这一点看,您认为centralPointXcentralPointY表示三角形的中心......但是,请看它们的定义位置:

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坐标。

我不会为你实现这个,因为正如你所说,这是功课。但是,我认为这绝对应该指向正确的方向。另外,保持良好的工作。我希望每个第一个问题都经过深思熟虑。