User:Rsim/Code snippets

From Robowiki
< User:Rsim
Revision as of 09:37, 1 July 2010 by RednaxelaBot (talk | contribs) (Using <syntaxhighlight>.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

My Bullet Shielding Code Snippet

This is how i check for bullet-hit-bullet collisions. If you find this code useful , have any questions or see ways to improve this code, please tell me :)

...
//moveAheadTime is the number of time units before this bot will fire
//moveAheadDistance is the distance (in direction heading) it will have moved during moveAheadTime
Point2D.Double enemyBulletLocation = Util.project(enemyShootingLocation,enemyBulletBearing,moveAheadTime*enemyBulletSpeed);
Point2D.Double shootingLocation = Util.project(bot.location,heading,moveAheadDistance);
for (int t=0;t<timeTillEnemyBulletHitBot-1-moveAheadTime;t++){
	// Move enemy bullet location half way
	Point2D.Double nextEnemyBulletLocation = Util.project(enemyBulletLocation,enemyBulletBearing,enemyBulletSpeed/2);
	double bearing = Util.absoluteBearing(shootingLocation,nextEnemyBulletLocation);
	double distance = shootingLocation.distance(nextEnemyBulletLocation);
	double optimalPower = (20.0-distance/(t+0.5))/3;
	optimalPower = Math.min(MAX_POWER,Math.max(0.1,optimalPower));
	double optimalSpeed = Rules.getBulletSpeed(optimalPower);
	// Move enemy bullet location half way remaining half way
	nextEnemyBulletLocation = Util.project(nextEnemyBulletLocation,enemyBulletBearing,enemyBulletSpeed/2);	
	Line2D.Double enemyBulletLine = new Line2D.Double(enemyBulletLocation,nextEnemyBulletLocation);							
	Point2D.Double botBulletLocation = Util.project(shootingLocation,bearing,t*optimalSpeed);
	Point2D.Double botNextBulletLocation = Util.project(botBulletLocation,bearing,optimalSpeed);
	Line2D.Double botBulletLine = new Line2D.Double(botBulletLocation,botNextBulletLocation);
	if(checkIntersection(enemyBulletLine,botBulletLine)){
		//we have a bullet-hit-bullet collision. do something useful
		...
	}
	enemyBulletLocation=nextEnemyBulletLocation;
}
...