Difference between revisions of "Wall Distance"

From Robowiki
Jump to navigation Jump to search
m (Tidy up a bit)
m (replaced some)
Line 2: Line 2:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
    static double calcWallSpace(final Point eCenter, double eGoing) {
+
static int isPositive(final double value) {
eGoing = Utils.normalAbsoluteAngle(eGoing);
+
return value < 0 ? 0 : 1;
final double wallDistLat = (eGoing <= Math.PI ? (PLAY_WIDTH-eCenter.getX())
+
}
/ Math.abs(Math.cos((Math.PI/2d - eGoing)))
+
: eCenter.getX() / Math.abs(Math.cos((Math.PI/2d - eGoing))))
+
static double calcWallSpace(final Point eCenter, double eGoing) {
/ PLAY_WIDTH;
 
 
eGoing = Utils.normalRelativeAngle(eGoing);
 
eGoing = Utils.normalRelativeAngle(eGoing);
final double wallDistVirt = (Math.abs(eGoing) <= Math.PI / 2d ?(PLAY_HEIGHT-eCenter.getY())
+
final int isRight = isPositive(eGoing);
/ Math.abs(Math.cos(eGoing))
+
final int isDown = isPositive(Math.abs(eGoing) - Math.PI*0.5d);
: eCenter.getY() / Math.abs(Math.cos(Math.PI - eGoing)))
+
final double wallDistLat = ((eCenter.getX() * ((isRight+1)&1)) + (FiveHunna.PLAY_WIDTH-eCenter.getX()) * isRight) / Math.abs(Math.cos(Math.PI*0.5d-eGoing));
/ PLAY_HEIGHT;
+
final double wallDistVirt = ((eCenter.getY() * (isDown) + (FiveHunna.PLAY_HEIGHT-eCenter.getY()) * ((isDown+1)&1))) / Math.abs(Math.cos(eGoing));
return(float)(Math.min(wallDistLat, wallDistVirt));
+
return Math.min(wallDistLat, wallDistVirt) / FiveHunna.MAX_DIST;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In order to limit numbers to between (0,1), divide by MAX_DIST on both instead of PLAY_WIDTH or PLAY_HEIGHT;
+
In a typical 800x600 arena, using this should result in a measurement in Kilo-pixels.
In a typical 800x600 arena, using MAX_DIST to calculate this should result in a measurement in Kilo-pixels.
 
  
WallDistLat is checking to see if the bot is overall facing left or right, and WallDistVirt up or down.
+
Cosine similarity between heading and 'direct to the wall vector.'
The factual distance to the wall from the point can be considered to be a vector pointing straight at the wall from the robot.
 
This is then scaled by the inverse of the similarity between the bots real heading to that side and the direct vector to the wall.
 
  
 
Can be pretty useful for various reasons.
 
Can be pretty useful for various reasons.
P.S. The Math.abs() calls seem to eliminate bugs present from near-miss situations that might accidentally return a negative value.
 
  
 
-- Damij
 
-- Damij

Revision as of 10:28, 20 January 2026

An example to calculate wall distance in percentage of width or height, whichever is closer and 'more pertinent'

	static int isPositive(final double value) {
		return value < 0 ? 0 : 1;
	}
	
	static double calcWallSpace(final Point eCenter, double eGoing) {
		eGoing = Utils.normalRelativeAngle(eGoing);
		final int isRight = isPositive(eGoing);
		final int isDown = isPositive(Math.abs(eGoing) - Math.PI*0.5d);
		final double wallDistLat = ((eCenter.getX() * ((isRight+1)&1)) +  (FiveHunna.PLAY_WIDTH-eCenter.getX()) * isRight) / Math.abs(Math.cos(Math.PI*0.5d-eGoing));
		final double wallDistVirt = ((eCenter.getY() * (isDown) + (FiveHunna.PLAY_HEIGHT-eCenter.getY()) * ((isDown+1)&1))) / Math.abs(Math.cos(eGoing));
		return Math.min(wallDistLat, wallDistVirt) / FiveHunna.MAX_DIST;
	}

In a typical 800x600 arena, using this should result in a measurement in Kilo-pixels.

Cosine similarity between heading and 'direct to the wall vector.'

Can be pretty useful for various reasons.

-- Damij