This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vatgas
Vatgas wrote ...

2016/2/24

Help with automating cars(move())

Vatgas Vatgas

2016/2/24

#
Hello, I am having a problem getting cars in my simulation to move. When cars move in my simulation, they can choose to take a short road, long road, or one of two equidistant medium roads. The cars that go on the short road are fine and make it to the house(PointB in the code), but all the other ones stop in place. I use a list of x and y coordinates to make them move, using turnToward() and move(). The code used to make all the cars move correctly but now it doesn't for some reason. Here is my relevant code
    public Car() {
        a = new Attitude();
        kc = new KnowledgeableChoice();
        ilc = new IllogicalChoice();
        lr = 0;
        sr = 0;
        erb = 0;
        ert = 0;
        hun = 100;
        double one = 1;
        XLongRoad = new int[5];
        XLongRoad[0] = 96;
        XLongRoad[1] = 371;
        XLongRoad[2] = 548;
        XLongRoad[3] = 563;
        XLongRoad[4] = 490;
        YLongRoad = new int [5];
        YLongRoad[0] = 301;
        YLongRoad[1] = 383;
        YLongRoad[2] = 334;
        YLongRoad[3] = 254;
        YLongRoad[4] = 185;
        XEqRoadBottom = new int [3];
        XEqRoadBottom[0] = 336;
        XEqRoadBottom[1] = 478;
        XEqRoadBottom[2] = 490;
        YEqRoadBottom = new int [3];
        YEqRoadBottom[0] = 305;
        YEqRoadBottom[1] = 241;
        YEqRoadBottom[2] = 185;
        XEqRoadTop = new int [3];
        XEqRoadTop[0] = 315;
        XEqRoadTop[1] = 497;
        XEqRoadTop[2] = 490;
        YEqRoadTop = new int [3];
        YEqRoadTop[0] = 75;
        YEqRoadTop[1] = 184;
        YEqRoadTop[2] = 185;
        XShortRoad = new int [2];
        XShortRoad[0] = 335;
        XShortRoad[1] = 490;
        YShortRoad = new int [2];
        YShortRoad[0] = 206;
        YShortRoad[1] = 185;
    }

    public void act() 
    {if (a.IsLogical() == true) { 
            turnTowards(XShortRoad[sr], YShortRoad[sr]);
            move(1);

            if (this.getX() == XShortRoad[sr] && this.getY() == YShortRoad[sr] && sr < 1){
                sr = sr + 1;
            }
        }
        if (a.IsKnowledgeable() == true && kc.ChooseBottom() == true) {
            turnTowards(XEqRoadBottom[erb], YEqRoadBottom[erb]);
            move(1);

            if (this.getX() == XEqRoadBottom[erb] && this.getY() == YEqRoadBottom[erb] && erb < 2){
                erb = erb + 1;
                turnTowards(XEqRoadBottom[erb], YEqRoadBottom[erb]);
            }
        }
        if (a.IsKnowledgeable() == true && kc.ChooseTop() == true) {
            turnTowards(XEqRoadTop[ert], YEqRoadTop[ert]);
            move(1);

            if (this.getX() == XEqRoadTop[ert] && this.getY() == YEqRoadTop[ert] && ert < 2){
                ert = ert + 1;

            }
        }
        if (a.IsIllogical() && ilc.chooseLong()){
            turnTowards(XLongRoad[lr], YLongRoad[lr]);
            move(1);

            if (this.getX() == XLongRoad[lr] && this.getY() == YLongRoad[lr] && lr < 4){
                lr = lr + 1;
            }
        }    
        if (a.IsIllogical() == true && ilc.chooseEqTop() == true) {
            turnTowards(XEqRoadTop[ert], YEqRoadTop[ert]);
            move(1);
            if (this.getX() == XEqRoadTop[ert] && this.getY() == YEqRoadTop[ert] && ert < 2){
                ert = ert + 1;
            }
        }
        if (a.IsIllogical() == true && ilc.chooseShort() == true) { 
            turnTowards(XShortRoad[sr], YShortRoad[sr]);
            move(1);
            if (this.getX() == XShortRoad[sr] && this.getY() == YShortRoad[sr] && sr < 1){
                sr = sr + 1;
            }
        }
        if (a.IsIllogical() == true && ilc.chooseEqBottom() == true) {
            turnTowards(XEqRoadBottom[erb], YEqRoadBottom[erb]);
            move(1);
            if (this.getX() == XEqRoadBottom[erb] && this.getY() == YEqRoadBottom[erb] && erb < 2){
                erb = erb + 1;
            }
        }
    }
}

class Attitude {
    int AttitudeSelect = Greenfoot.getRandomNumber(11);
    boolean IsLogical(){
        return AttitudeSelect >= 0;
    }

    boolean IsIllogical(){
        return AttitudeSelect == 1;
    }

    boolean IsKnowledgeable(){
        return AttitudeSelect == 0;
    }
}

class KnowledgeableChoice {
    int kChoice = Greenfoot.getRandomNumber(2);

    boolean ChooseTop(){
        return kChoice == 1;
    }

    boolean ChooseBottom(){
        return kChoice == 0;
    }    
}

class IllogicalChoice {
    int iChoice = Greenfoot.getRandomNumber(3);
    boolean chooseEqTop(){
        return iChoice == 0;
    }

    boolean chooseEqBottom(){
        return iChoice == 1;
    }

    boolean chooseLong(){
        return iChoice == 2;
    }

    boolean chooseShort(){
        return iChoice == 3;
    }
}
How do I stop the other cars from being stuck?
davmac davmac

2016/2/24

#
Well, on line 60 for instance:
            if (this.getX() == XEqRoadBottom[erb] && this.getY() == YEqRoadBottom[erb] && erb < 2)
you are checking whether the car is exactly on some specified point. I think you want to move between the points that define the road, however, I don't see any guarantee that the car will actually reach those points - your 'move(1)' on line 58 moves in the direction of the point, but might move past it. In that case the car would, in the next cycle, turn back towards the point and move again, but most likely would again overshoot, and so on.
Vatgas Vatgas

2016/2/25

#
davmac wrote...
Well, on line 60 for instance:
            if (this.getX() == XEqRoadBottom[erb] && this.getY() == YEqRoadBottom[erb] && erb < 2)
you are checking whether the car is exactly on some specified point. I think you want to move between the points that define the road, however, I don't see any guarantee that the car will actually reach those points - your 'move(1)' on line 58 moves in the direction of the point, but might move past it. In that case the car would, in the next cycle, turn back towards the point and move again, but most likely would again overshoot, and so on.
So how can I make it so that it will reach the point?
davmac davmac

2016/2/25

#
Hmm. Looking again at your code, you are only moving a distance of 1, so you shouldn't actually have that problem. Probably this needs to be debugged properly. Maybe if you post the full scenario, someone can help.
Vatgas Vatgas

2016/2/26

#
davmac wrote...
Hmm. Looking again at your code, you are only moving a distance of 1, so you shouldn't actually have that problem. Probably this needs to be debugged properly. Maybe if you post the full scenario, someone can help.
I uploaded it here: http://www.greenfoot.org/scenarios/16033 Also I overwrote the move command before to make it accept doubles, but now the overwrite isn't there so it shouldn't change anything
davmac davmac

2016/2/26

#
You did not upload the source code, so there is no way we can look at it. Please upload it again and check the "publish source code" checkbox.
Vatgas Vatgas

2016/2/27

#
davmac wrote...
You did not upload the source code, so there is no way we can look at it. Please upload it again and check the "publish source code" checkbox.
oh sorry here: http://www.greenfoot.org/scenarios/16037
danpost danpost

2016/2/27

#
The track data, or points along the paths that the cars should take, would be better placed in the track class -- it is, after all, track data. Right now, every car acquires the data for all four tracks. This is wasteful and quite unnecessary. Using the four track classes as you have now, each one could contain its own point coordinates. Each car could just hold a reference to the specific track it is to follow. To make things much easier and to make the code much less bulky, you could add a Track class between the individual track classes you now have and the Actor class. You can declare the array in it so that each track subclass can set its point coordinate array to it and the Car class can hold a common field referencing a Track object. Next, the addition of the extra classes within the wall call seems excessive for what they do. ********************** At any rate, for now, with what you have in the act method of the Car class, if you
// change this
if (a.IsLogical() == true)
// to this
if (a.IsLogical() == true && a.IsKnowledgeable() == false && a.IsIllogical() == false)
and then un-comment this line:
turnTowards(XLongRoad[lr], YLongRoad[lr]);
and add this line immediately after that 'turnTowards' statement:
move(1);
then, you should be a lot closer to what you are wanting.
Vatgas Vatgas

2016/2/28

#
I did everything you said and I did not work. The cars just stay in place. Here is the edited scenario: http://www.greenfoot.org/scenarios/16041
danpost danpost

2016/2/28

#
Vatgas wrote...
I did everything you said and I did not work. The cars just stay in place. Here is the edited scenario: http://www.greenfoot.org/scenarios/16041
You did more than I said. You added 'a.IsLogical() == false' to all the 'if' statements. I never said to do that. Anyway 'a.IsLogical()' will ALWAYS return a true value because the random number generated will always be greater than or equal to zero (check the method out). This is why your cars are behaving that way with the extra changes you made.
Vatgas Vatgas

2016/2/29

#
Thank you so much! I got it to work.
You need to login to post a reply.