TajBot (Robocode bot)

Copyright Tristan Aubrey-Jones December 2005.

BotMaths.java

home Home   up Up   ( Download )


package tajy; public class BotMaths { /** * Returns the absolute coordinate of the target given: * @param m - My robot's coordinate. * @param myHeading - The bearing my robot is facing in. * @param targetBearing - The angle between my heading and the target * @param targetDistance - The distance between my robot's centre and the target * @return The vector from the origin of the target. */ public static Vector getTargetCoord(Vector m, double myHeading, double targetBearing, double targetDistance) { double angle = myHeading + targetBearing; Vector r = Vector.fromAngleDegrees(angle, targetDistance); Vector t = m.add(r); return t; } /** * Returns the target's vector relative to ours. * @param m - The coordinate of my robot. * @param t - The coordinate of the target. * @return - The vector (t - m) */ public static Vector getTargetVector(Vector m, Vector t) { Vector r = t.subtract(m); return r; } public static double getBulletTime(double distance, double energy) { return distance / (20 - (3 * energy)); } /** * Estimates the coordinate of the target after time t. * @param start - The position of the target at t = 0 * @param heading - The direction vector of the target at t = 0 * @param time - The time elapsed since last scan. * @param velocity - The velocity of the target along it's vector. * @return - The estimated new position of the target. */ public static Vector estimateTargetCoord(Vector start, Vector heading, double t) { double distance = heading.getDistance() * t; if (distance > 0) { double bearing = heading.getBearingRadians(); Vector r = Vector.fromAngleRadians(bearing, distance); return start.add(r); } else return start; } public static double getAimBearing(Vector m, Vector t) { return getTargetVector(m, t).getBearingDegrees(); } }