# User:Rsim/Code snippets

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## 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
// 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;
}
...```