Calculation of upwind downwind leg given a boat, wind and next mark now works. created GeometryUtils class

Can accurately calculate if a boat is going upwind or downward using a line function for the wind vector from the gate and the boat position from the gate.

Requires knowledge of the next mark which requires the boat to have passed a mark. This could be fixed by extracting the leg number from the race status packet and mapping these to gates in an initalisation step

tags: #story[956]
This commit is contained in:
William Muir
2017-05-24 14:57:22 +12:00
parent 89464e033e
commit 1cac7cc189
7 changed files with 200 additions and 30 deletions
+39 -24
View File
@@ -12,7 +12,11 @@ import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import seng302.GeometryUtils;
import seng302.controllers.CanvasController;
import seng302.models.mark.GateMark;
import seng302.models.mark.Mark;
import seng302.models.mark.SingleMark;
import seng302.models.stream.StreamParser;
import java.text.DateFormat;
@@ -364,33 +368,44 @@ public class BoatGroup extends Group {
}
public void calculateLegDirection() {
Mark lastMark = boat.getLastMarkRounded();
/**
* This function works out if a boat is going upwind or down wind. It looks at the boats current position, the next
* gates position and the current wind
* If bot the wind vector from the next gate and the boat from the next gate lay on the same side, then the boat is
* going up wind, if they are on different sides of the gate, then the boat is going downwind
* @param canvasController
*/
public Boolean isUpwindLeg(CanvasController canvasController) {
Mark nextMark = boat.getNextMark();
if (lastMark == null || nextMark == null) {
return;
Double windAngle = StreamParser.getWindDirection();
GateMark thisGateMark = (GateMark) nextMark;
SingleMark nextMark1 = thisGateMark.getSingleMark1();
SingleMark nextMark2 = thisGateMark.getSingleMark2();
Point2D nextMarkPoint1 = canvasController.findScaledXY(nextMark1.getLatitude(), nextMark1.getLongitude());
Point2D nextMarkPoint2 = canvasController.findScaledXY(nextMark2.getLatitude(), nextMark2.getLongitude());
Point2D boatCurrentPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY());
Point2D windTestPoint = GeometryUtils.makeArbitraryVectorPoint(nextMarkPoint1, windAngle, 10d);
Integer boatLineFuncResult = GeometryUtils.lineFunction(nextMarkPoint1, nextMarkPoint2, boatCurrentPoint);
Integer windLineFuncResult = GeometryUtils.lineFunction(nextMarkPoint1, nextMarkPoint2, windTestPoint);
/*
If both the wind vector from the gate and the boat from the gate are on the same side of that gate, then the
boat is travelling into the wind. thus upwind. Otherwise if they are on different sides, then the boat is going
with the wind.
*/
System.out.println("Boat Line func result: " + boatLineFuncResult);
System.out.println("Wind Line func result: " + windLineFuncResult);
if (boatLineFuncResult == windLineFuncResult) {
return true;
} else {
return false;
}
Double windDirection = StreamParser.getWindDirection();
Double arbitraryDistance = 10d;
Point2D lastMarkMidPoint = new Point2D(lastMark.getLatitude(), lastMark.getLongitude());
Point2D nextMarkMidPoint = new Point2D(nextMark.getLatitude(), nextMark.getLongitude());
Double windDirX = lastMarkMidPoint.getX() + (lastMarkMidPoint.getX() + arbitraryDistance -lastMarkMidPoint.getX())*Math.cos(windDirection) - (lastMarkMidPoint.getY() + arbitraryDistance -lastMarkMidPoint.getY())*Math.sin(windDirection);
Double windDirY = lastMarkMidPoint.getY() + (lastMarkMidPoint.getX() + arbitraryDistance -lastMarkMidPoint.getX())*Math.sin(windDirection) + (lastMarkMidPoint.getY() + arbitraryDistance -lastMarkMidPoint.getY())*Math.cos(windDirection);
Point2D windDirPoint = new Point2D(windDirX, windDirY);
Double angle = lastMarkMidPoint.angle(nextMarkMidPoint, windDirPoint);
if (angle <= 90) {
System.out.println(lastMark.getName() + " is downwind");
System.out.println(nextMark.getName() + " is upwind");
}
// if (lastMarkMidPoint.angle(nextMarkMidPoint, windDirPoint) <= 90) {
// boat.getNextMark().s
// }
}
@@ -161,4 +161,8 @@ public class MarkGroup extends Group {
idArray[i++] = mark.getId();
return idArray;
}
public Mark getMainMark() {
return mainMark;
}
}