Wall Distance

From Robowiki
Revision as of 09:12, 20 January 2026 by DamijForgotHisAccountCreds (talk | contribs) (Tidy up a bit)
Jump to navigation Jump to search

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

    static double calcWallSpace(final Point eCenter, double eGoing) {
		eGoing = Utils.normalAbsoluteAngle(eGoing);
		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))))
					 / PLAY_WIDTH;
		eGoing = Utils.normalRelativeAngle(eGoing);
		final double wallDistVirt = (Math.abs(eGoing) <= Math.PI / 2d  ?(PLAY_HEIGHT-eCenter.getY())
			/ Math.abs(Math.cos(eGoing))
				: eCenter.getY() / Math.abs(Math.cos(Math.PI - eGoing)))
					 / PLAY_HEIGHT;
		 return(float)(Math.min(wallDistLat, wallDistVirt));
	}

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 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. 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. P.S. The Math.abs() calls seem to eliminate bugs present from near-miss situations that might accidentally return a negative value.

-- Damij