« Return to Thread: simple wall avoidance

simple wall avoidance

by Marko Väljaots :: Rate this Message:

Reply to Author | View in Thread

Hi, I'm relatively new to java and robocode as well, but slowly I am
getting the hang of it. At the moment I'm building a robot to
participate in our school competition, and it is evolving quite nicely.
It shoots the enemy using head on targeting and moves around it in a
circular way. The problem that I have now is that it collides with the
walls all the time and I'm trying to find some kind of solution to
solve it.
I came to an idea to calculate my x,y position and compare it to a
double wall_avoid_distance. At any point when my tank is closer to the
wall than wall_avoid_distance it should change it's direction.
Well, I put in code and it chages the direction with one major problem
- it collides with the wall and only after that it  seems to
understand that it's time to go the other way. Maybe someone here can
tell me, what am I doing wrong here.
This is a part of my code:

public void onScannedRobot(ScannedRobotEvent event) {
                // move around enemy tank in a circular way
                setTurnRight(event.getBearing() + 90);
                setAhead(100 * avoidWalls());
                double vastase_peilung = event.getBearing();
                double tuki_suund = getGunHeading();
                double minu_suund = getHeading();
                // this is for shooting and calculating enemy position
                double vastase_suund = minu_suund + vastase_peilung;
                double keeramisnurk = vastase_suund - tuki_suund;
                keeramisnurk = normalRelativeAngle(keeramisnurk);

                turnGunRight(keeramisnurk);

                setFire(3);
                execute();
        }
       
        public void onHitWall(HitWallEvent event) {
               out.println("Ouch, I hit a wall bearing " + event.getBearing()
+ " degrees.");
           }
       
        // when we get closer to a wall than "double wall_avoid_distance"
change direction and go back
        private double avoidWalls() {

                double wall_avoid_distance = 60;
        double fieldHeight = getBattleFieldHeight();
        double fieldWidth = getBattleFieldWidth();
        double centerX = (fieldWidth / 2);
        double centerY = (fieldHeight / 2);
        double currentHeading = getHeading();
        double x = getX();
        double y = getY();
        if (x < wall_avoid_distance || x > fieldWidth -
wall_avoid_distance) {
        moveDirection *= -1;
        }
        if (y < wall_avoid_distance || y > fieldHeight -
wall_avoid_distance) {
        moveDirection *= -1;
        }
        return moveDirection;
        }

 « Return to Thread: simple wall avoidance