You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5 KiB

package seng302.Model;
import seng302.Constants;
import seng302.Controllers.RaceController;
import java.awt.geom.Point2D;
import org.geotools.referencing.GeodeticCalculator;
import seng302.GPSCoordinate;
import java.util.ArrayList;
/**
* Created by cbt24 on 6/03/17.
*/
public class ConstantVelocityRace extends Race {
/**
* Initialiser for a Race with constant velocity.
* @param startingBoats array of boats
* @param marks array of RaceMarkers that the boats need to pass in order to finish the course.
* @see Boat
* @see Leg
*/
public ConstantVelocityRace(BoatInRace[] startingBoats, ArrayList<Leg> marks, RaceController controller) {
super(startingBoats, marks, controller);
}
public ConstantVelocityRace(BoatInRace[] startingBoats, ArrayList<Leg> marks) {
super(startingBoats, marks);
}
protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
//distanceTravelled = velocity (nm p hr) * time taken to update loop
double distanceTravelled = boat.getVelocity() * millisecondsElapsed/3600000;
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
boolean finish = boat.getCurrentLeg().getName().equals("Finish");
if (!finish) {
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartGraphCoordinate(),
totalDistanceTravelled, boat.calculateAzimuth()));
}
}
/**
*
* @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
*/
public static GPSCoordinate calculatePosition(GPSCoordinate oldCoordinates, double distanceTravelled, double azimuth) {
//Find new coordinate using current heading and distance
GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
Point2D startPoint = new Point2D.Double(oldCoordinates.getLongitude(), oldCoordinates.getLatitude());
geodeticCalculator.setStartingGeographicPoint(startPoint);
geodeticCalculator.setDirection(azimuth,distanceTravelled * Constants.NMToMetersConversion);
Point2D endPoint = geodeticCalculator.getDestinationGeographicPoint();
return new GPSCoordinate(endPoint.getY(), endPoint.getX());
}
}