How Marshmallow checks if its VirtualBullets has hit |
* /RobotUtilsCode?
How Marshmallow checks if its VirtualBullets has hit or missed |
private Ellipse2D vincinity = new Ellipse2D.Double(); private double precision = 20.0; |
private Point2D oldRobotLocation?; private double radius = 20.0; |
boolean hasHit() { ... vincinity.setFrameFromCenter?(enemyLocation, new Point2D.Double(enemyLocation.getX() - precision, enemyLocation.getY() - precision)); if (vincinity.contains(bulletLocation)) { ... return true; |
boolean hasHit() { // calculate bulletLocation here if (bulletLocation.distance(enemy.location) < radius) { active = false; return true; } else { return false; } } boolean hasMissed() { // calculate bulletLocation here if (oldRobotLocation?.distance(bulletLocation) > oldRobotLocation?.distance(enemy.location)) { active = false; return true; } else { return false; } } </pre> I used to calculate a circle (Ellipse2D) around the enemyLocation and check to see if the bulletLocation was contained in that circle, but suddenly realised this way is simpler and achives the exact same results. To calculate the bulletLocation we need: * bulletBearing * bulletVelocity * firedTime * nowTime * oldRobotLocation With these we can:
Of course some of this should be encapsulated into a generic function. Marshmallow has a robot utilities class with a function like this: <pre> static void setLocationFromVector?(double angle, double length, Point2D sourceLocation, Point2D targetLocation) { double X = sourceLocation.getX() + sin(angle) * length; double Y = sourceLocation.getY() + cos(angle) * length; targetLocation.setLocation(X, Y); |
else { return false; |
</pre> And sin/cos functions like these: <pre> static double cos(double a) { return Math.cos(Math.toRadians(a)); } static double sin(double a) { return Math.sin(Math.toRadians(a)); |
} |
The code not shown isn't TopSecret? as such, its just not relevant to the example. Feel free to use this code. Give me credit if you think I deserve it. -- PEZ |
Feel free to use this code. Give me credit if you think I deserve it. -- PEZ |