【问题标题】:Debugging Java/Processing output of an object patrolling screen对象巡视屏幕的调试 Java/处理输出
【发布时间】:2017-01-31 16:52:13
【问题描述】:

我是一年级计算机编程专业的学生,​​从事机器人和机器人测试环境项目。 虽然我是 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();
    }
}

测试机器人:

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

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

古斯塔沃

【问题讨论】:

  • edit 将您的代码简化为minimal reproducible example 的问题。您当前的代码包含许多与您的问题无关的内容 - 一个最小样本通常看起来类似于一个好的单元测试:只执行一项任务,并为可重复性指定输入值。

标签: java oop rotation processing


【解决方案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 坐标。

我不会为你实现这个,因为正如你所说,这是家庭作业。但是,我认为这绝对应该为您指明正确的方向。另外,继续做好工作。我希望每个第一个问题都经过深思熟虑。

【讨论】:

  • 这真的很有帮助。现在我面临不同的问题,但还没有足够的搜索。谢谢。
  • @GustavoLessa 乐于助人!不要忘记投票和接受 =] 如果您有新问题,请随时提出另一个问题
  • 我有另一个关于相同方法但行为不同的问题。我希望我的对象在它到达墙壁后立即停止,而不是仅仅转身向前,而是停下来,转身 360 度,然后左转然后走。它发生得如此之快以至于它不可见。不过,移动物体的速度很好。你认为我应该发布另一个关于这个的问题吗?或者只是添加一些东西到这个?再次感谢您!
  • @GustavoLessa 可能会提出一个新问题,这样它就不会变得混乱和混乱......随时在这里发布链接,我会看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多