package seng302.Model; import org.geotools.referencing.GeodeticCalculator; import seng302.Constants; import seng302.Controllers.RaceController; import seng302.GPSCoordinate; import java.awt.geom.Point2D; import java.util.ArrayList; /** * Created by cbt24 on 6/03/17. */ public class ConstantVelocityRace extends Race { /** * Initialiser for a constant velocity race * * @param startingBoats in race * @param legs in race * @param controller of race * @param scaleFactor of race */ public ConstantVelocityRace(BoatInRace[] startingBoats, ArrayList legs, RaceController controller, int scaleFactor) { super(startingBoats, legs, controller, scaleFactor); } /** * Calculates the distance a boat has travelled and updates its current position according to this value. * * @param boat to be updated * @param millisecondsElapsed since last update */ protected void updatePosition(BoatInRace boat, int millisecondsElapsed) { //distanceTravelled = velocity (nm p hr) * time taken to update loop double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000; double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg(); boolean finish = boat.getCurrentLeg().getName().equals("Finish"); if (!finish) { //update boat's distance travelled boat.setDistanceTravelledInLeg(totalDistanceTravelled); //Calculate boat's new position by adding the distance travelled onto the start point of the leg boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartMarker().getAverageGPSCoordinate(), totalDistanceTravelled, boat.calculateAzimuth())); } } /** * Calculates the boats next GPS position based on its distance travelled and heading * * @param oldCoordinates GPS coordinates of the boat's starting position * @param distanceTravelled distance in nautical miles * @param azimuth boat's current direction. Value between -180 and 180 * @return The boat's new coordinate */ public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) { //Find new coordinate using current heading and distance GeodeticCalculator geodeticCalculator = new GeodeticCalculator(); //Load start point into calculator Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude()); geodeticCalculator.setStartingGeographicPoint(startPoint); //load direction and distance tranvelled into calculator geodeticCalculator.setDirection(azimuth, distanceTravelled * Constants.NMToMetersConversion); //get new point Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint(); return new GPSCoordinate(endPoint.getY(), endPoint.getX()); } }