Discussion:
simple wall avoidance
aatomik
2008-10-02 08:21:28 UTC
Permalink
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;
}


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
mailto:Robocode-***@yahoogroups.com
mailto:Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Julian Kent
2008-10-02 14:01:48 UTC
Permalink
I'm not sure what's wrong with your code, but there are better ways of
avoiding walls than simply reversing. For example, you can test 3 points
in front of you, one straight, one to the left, one to the right, and
see which ones are inside the field. If the one straight ahead is in the
field keep going straight, otherwise try the one on the right, if that's
in the field turn right, otherwise turn left. There is also a method
called "wall smoothing" where you see how far you would have to 'rotate'
this point straight in front of you towards the enemy in order for it to
be inside the field. A good value for the distance of the point is 160.

Good luck with the project,
Julian (AKA Skilgannon on RoboWiki)
Post by aatomik
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.
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;
}
------------------------------------
Yahoo! Groups Links
--
Julian Kent
***@fastmail.net
--
http://www.fastmail.fm - The way an email service should be


------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
mailto:Robocode-***@yahoogroups.com
mailto:Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Thomas Dalton
2008-10-06 23:04:00 UTC
Permalink
Post by aatomik
// 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;
}
One problem I can see with that is that if you are approaching a
corner you will reverse direction twice and end up carrying on in the
same direction. The second "if" should be an "else if". Other than
that, it looks like your method should work fairly well in most
situations (I assume you are initialising moveDirection to 1
somewhere). You would be better off taking the direction of travel
into account, though. If you're really close to the wall but moving
roughly parallel to it, there is no need to change direction (while
changing direction you're roughly stationary which makes you easier to
hit by head on targetting, which I guess most of your opponents are
using).

------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/Robocode/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/Robocode/join
(Yahoo! ID required)

<*> To change settings via email:
mailto:Robocode-***@yahoogroups.com
mailto:Robocode-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
Robocode-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Continue reading on narkive:
Loading...