Merge remote-tracking branch 'origin/heading-visualisation'

# Conflicts:
#	src/main/java/seng302/Controllers/RaceController.java
#	src/main/java/seng302/Model/Boat.java
#	src/main/java/seng302/Model/Race.java
#	src/main/java/seng302/Model/ResizableRaceCanvas.java
#	src/main/resources/scenes/racepane.fxml
main
Fan-Wu Yang 9 years ago
commit edd9c2a48d

@ -21,6 +21,8 @@ public class Constants {
public static final GPSCoordinate finishLineMarker1 = new GPSCoordinate(32.317379, -64.839291); public static final GPSCoordinate finishLineMarker1 = new GPSCoordinate(32.317379, -64.839291);
public static final GPSCoordinate finishLineMarker2 = new GPSCoordinate(32.317257, -64.836260); public static final GPSCoordinate finishLineMarker2 = new GPSCoordinate(32.317257, -64.836260);
public static final double wakeScale = 10;
public static final BoatInRace[] OFFICIAL_AC35_COMPETITORS = new BoatInRace[] public static final BoatInRace[] OFFICIAL_AC35_COMPETITORS = new BoatInRace[]
{new BoatInRace("Oracle Team USA", 30.0, Color.BLUEVIOLET, "Oracle"), {new BoatInRace("Oracle Team USA", 30.0, Color.BLUEVIOLET, "Oracle"),
new BoatInRace("Land Rover BAR", 23.0, Color.BLACK, "BGR"), new BoatInRace("Land Rover BAR", 23.0, Color.BLACK, "BGR"),

@ -73,7 +73,7 @@ public class RaceController extends Controller{
public void updateMap(ObservableList<BoatInRace> boats) { public void updateMap(ObservableList<BoatInRace> boats) {
BoatInRace[] boatInRaces = new BoatInRace[boats.size()]; BoatInRace[] boatInRaces = new BoatInRace[boats.size()];
raceMap.setBoats(boats.toArray(boatInRaces)); raceMap.setBoats(boats.toArray(boatInRaces));
raceMap.drawRaceMap(); raceMap.update();
} }
/** /**
@ -146,7 +146,6 @@ public class RaceController extends Controller{
});*/ });*/
} }
/** /**
* Initializes and runs the race, based on the user's chosen scale factor * Initializes and runs the race, based on the user's chosen scale factor
* Currently uses an example racecourse * Currently uses an example racecourse
@ -173,6 +172,7 @@ public class RaceController extends Controller{
raceMap.widthProperty().bind(canvasBase.widthProperty()); raceMap.widthProperty().bind(canvasBase.widthProperty());
raceMap.heightProperty().bind(canvasBase.heightProperty()); raceMap.heightProperty().bind(canvasBase.heightProperty());
raceMap.setBoats(boats); raceMap.setBoats(boats);
raceMap.drawBoats();
raceMap.drawRaceMap(); raceMap.drawRaceMap();
raceMap.setVisible(true); raceMap.setVisible(true);
@ -192,7 +192,7 @@ public class RaceController extends Controller{
public void changed(ObservableValue<? extends Boolean> ov, public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) { Boolean old_val, Boolean new_val) {
raceMap.toggleAnno(); raceMap.toggleAnno();
raceMap.drawRaceMap(); raceMap.update();
} }
}); });
} }
@ -200,6 +200,7 @@ public class RaceController extends Controller{
/** /**
* Function for the Bermuda Race. * Function for the Bermuda Race.
*
* @return legs in the Bermuda Race. * @return legs in the Bermuda Race.
*/ */
private ArrayList<Leg> generateBermudaCourseLegs() { private ArrayList<Leg> generateBermudaCourseLegs() {
@ -249,5 +250,4 @@ public class RaceController extends Controller{
*/ */
public void setFrames(String fps) { FPS.setText((fps)); } public void setFrames(String fps) { FPS.setText((fps)); }
} }

@ -48,6 +48,15 @@ public class Boat {
return velocity; return velocity;
} }
/**
* Sets the speed of the boat in knots.
* @param velocity speed in knots
*/
public void setVelocity(double velocity) {
this.velocity = velocity;
this.velocityProp.setValue(velocity);
}
/** /**
* Print method prints the name of the boat * Print method prints the name of the boat
* @return Name of the boat. * @return Name of the boat.

@ -4,8 +4,10 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import org.geotools.referencing.GeodeticCalculator; import org.geotools.referencing.GeodeticCalculator;
import seng302.Constants;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
import java.awt.geom.Point2D;
/** /**
* Boat in the Race extends Boat. * Boat in the Race extends Boat.
@ -73,6 +75,25 @@ public class BoatInRace extends Boat {
return calculateHeading(azimuth); return calculateHeading(azimuth);
} }
/**
* Returns the position of the end of the boat's wake, which is 180 degrees
* from the boat's heading, and whose length is proportional to the boat's
* speed.
* @return GPSCoordinate of wake endpoint.
*/
public GPSCoordinate getWake() {
double reverseHeading = calculateHeading() - 180;
double distance = Constants.wakeScale * getVelocity();
GeodeticCalculator calc = new GeodeticCalculator();
calc.setStartingGeographicPoint(
new Point2D.Double(getCurrentPosition().getLongitude(), getCurrentPosition().getLatitude())
);
calc.setDirection(reverseHeading, distance);
Point2D endpoint = calc.getDestinationGeographicPoint();
return new GPSCoordinate(endpoint.getY(), endpoint.getX());
}
/** /**
* @return Scaled velocity of the boat * @return Scaled velocity of the boat
*/ */

@ -118,15 +118,18 @@ public abstract class Race implements Runnable {
long timeLoopEnded; long timeLoopEnded;
while (currentTime <= startTime) { while (currentTime <= startTime) {
//if (controller != null) controller.updateMap(startingBoats);
timeLeft = startTime - currentTime; timeLeft = startTime - currentTime;
if (timeLeft == 0 && controller != null) {
updateTime("Race is starting...");
} else {
currentTimeInSeconds = timeLeft / 1000; currentTimeInSeconds = timeLeft / 1000;
minutes = currentTimeInSeconds / 60; minutes = currentTimeInSeconds / 60;
remainingSeconds = currentTimeInSeconds % 60; remainingSeconds = currentTimeInSeconds % 60;
hours = minutes / 60; hours = minutes / 60;
minutes = minutes % 60; minutes = minutes % 60;
if (controller != null) { if (controller != null) {
updateTime(String.format("Time until race starts: %02d:%02d:%02d", hours, minutes, remainingSeconds)); updateTime(String.format("Race clock: -%02d:%02d:%02d", hours, minutes, remainingSeconds));
}
} }
try { try {
timeLoopEnded = System.currentTimeMillis(); timeLoopEnded = System.currentTimeMillis();
@ -162,6 +165,16 @@ public abstract class Race implements Runnable {
*/ */
protected void updateTime(String time){ protected void updateTime(String time){
Platform.runLater(() -> {controller.setTimer(time);}); Platform.runLater(() -> {controller.setTimer(time);});
=======
* Updates the GUI race clock
* @param time
*/
protected void updateTime(String time) {
Platform.runLater(() -> {
controller.setTimer(time);
});
>>>>>>> Temporary merge branch 2
} }
/** /**
@ -183,6 +196,8 @@ public abstract class Race implements Runnable {
*/ */
private void simulateRace() { private void simulateRace() {
System.setProperty("javafx.animation.fullspeed", "true");
new AnimationTimer() { new AnimationTimer() {
long timeRaceStarted = System.currentTimeMillis(); //start time of loop long timeRaceStarted = System.currentTimeMillis(); //start time of loop
@ -242,7 +257,6 @@ public abstract class Race implements Runnable {
*/ */
protected void checkPosition(BoatInRace boat, long timeElapsed) { protected void checkPosition(BoatInRace boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){ if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
// updateController();
//boat has passed onto new leg //boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) { if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished //boat has finished
@ -254,13 +268,17 @@ public abstract class Race implements Runnable {
boat.setFinished(true); boat.setFinished(true);
boat.setCurrentLeg(new Leg("DNF",-1)); boat.setCurrentLeg(new Leg("DNF",-1));
} else { } else {
//Calculate how much the boat overshot the marker by
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance()); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg() - boat.getCurrentLeg().getDistance());
//Move boat on to next leg
Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1); Leg nextLeg = legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg); boat.setCurrentLeg(nextLeg);
//Add overshoot distance into the distance travelled for the next leg
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg()); boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
} }
FXCollections.sort(startingBoats, (a,b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber()); //Update the boat display table in the GUI to reflect the leg change
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
} }
} }
@ -282,9 +300,9 @@ public abstract class Race implements Runnable {
} }
/** /**
* This function is a function that generates the Race and populates the events list. * Updates the boat's gps coordinates depending on time elapsed
* Is automatically called by the initialiser function, so that simulateRace() does not return an empty race. * @param boat
* @see Race#simulateRace() * @param millisecondsElapsed
*/ */
protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed); protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed);

@ -40,7 +40,6 @@ public class ResizableRaceCanvas extends Canvas {
this.boats = boats; this.boats = boats;
} }
public ResizableRaceCanvas(RaceMap map) { public ResizableRaceCanvas(RaceMap map) {
this.map = map; this.map = map;
gc = this.getGraphicsContext2D(); gc = this.getGraphicsContext2D();
@ -73,9 +72,24 @@ public class ResizableRaceCanvas extends Canvas {
* @see Color * @see Color
* @see Paint * @see Paint
*/ */
private void displayMark(GraphCoordinate graphCoordinate, Paint paint) { public void displayMark(GraphCoordinate graphCoordinate, Paint paint){
double d = 25;
gc.setFill(paint); gc.setFill(paint);
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 25, 25); gc.fillOval(graphCoordinate.getX() - (d/2), graphCoordinate.getY() - (d/2), d, d);
}
public void displayBoat(BoatInRace boat, double angle) {
GraphCoordinate pos = this.map.convertGPS(boat.getCurrentPosition());
Paint paint = boat.getColour();
double[] x = {pos.getX() - 6, pos.getX(), pos.getX() + 6};
double[] y = {pos.getY() + 12, pos.getY() - 12, pos.getY() + 12};
gc.setFill(paint);
gc.save();
rotate(angle, pos.getX(), pos.getY());
gc.fillPolygon(x, y, 3);
gc.restore();
} }
/** /**
@ -117,10 +131,10 @@ public class ResizableRaceCanvas extends Canvas {
*/ */
private void displayArrow(GraphCoordinate coordinate, int angle) { private void displayArrow(GraphCoordinate coordinate, int angle) {
gc.save(); gc.save();
rotate(angle, coordinate.getX(),coordinate.getY()); rotate(angle, coordinate.getX(), coordinate.getY());
gc.setFill(Color.BLACK); gc.setFill(Color.BLACK);
gc.fillPolygon(new double[]{coordinate.getX()-12, coordinate.getX()-6, coordinate.getX(), coordinate.getX()-4, coordinate.getX()-4, coordinate.getX()-8, coordinate.getX()-8}, gc.fillPolygon(new double[]{coordinate.getX() - 12, coordinate.getX() - 6, coordinate.getX(), coordinate.getX() - 4, coordinate.getX() - 4, coordinate.getX() - 8, coordinate.getX() - 8},
new double[]{coordinate.getY()-5, coordinate.getY()-20, coordinate.getY()-5, coordinate.getY()-5, coordinate.getY()+20, coordinate.getY()+20, coordinate.getY()-5}, new double[]{coordinate.getY() - 5, coordinate.getY() - 20, coordinate.getY() - 5, coordinate.getY() - 5, coordinate.getY() + 20, coordinate.getY() + 20, coordinate.getY() - 5},
7); 7);
gc.restore(); gc.restore();
} }
@ -156,6 +170,13 @@ public class ResizableRaceCanvas extends Canvas {
gc.fillText(text, xCoord, yCoord); gc.fillText(text, xCoord, yCoord);
} }
/**
* Draws race map with up to date data.
*/
public void update() {
this.drawRaceMap();
this.updateBoats();
}
/** /**
* Draws the Race Map * Draws the Race Map
@ -168,9 +189,6 @@ public class ResizableRaceCanvas extends Canvas {
gc.clearRect(0, 0, width, height); gc.clearRect(0, 0, width, height);
//System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height); //System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height);
this.map = new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) width, (int) height); this.map = new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) width, (int) height);
if (map == null) {
return;
}
//finish line //finish line
gc.setLineWidth(2); gc.setLineWidth(2);
@ -208,18 +226,39 @@ public class ResizableRaceCanvas extends Canvas {
displayArrow(new GraphCoordinate((int)getWidth()-40, 40), 150); displayArrow(new GraphCoordinate((int)getWidth()-40, 40), 150);
} }
/** /**
* Toggle the raceAnno value * Toggle the raceAnno value
*/ */
public void toggleAnno(){ public void toggleAnno() {
if (raceAnno){ if (raceAnno) {
raceAnno = false; raceAnno = false;
} else { } else {
raceAnno = true; raceAnno = true;
} }
} }
/**
* Draws boats while race in progress, when leg heading is set.
*/
public void updateBoats() {
if (boats != null) {
for (BoatInRace boat : boats) {
boolean finished = boat.getCurrentLeg().getName().equals("Finish") || boat.getCurrentLeg().getName().equals("DNF");
if (!finished) {
displayBoat(boat, boat.calculateHeading());
GraphCoordinate wakeFrom = this.map.convertGPS(boat.getCurrentPosition());
GraphCoordinate wakeTo = this.map.convertGPS(boat.getWake());
displayLine(wakeFrom, wakeTo, boat.getColour());
}
else {
displayBoat(boat, 0);
}
if (raceAnno) displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
}
}
}
/** /**
* Set the Canvas to resizable. * Set the Canvas to resizable.
* *
@ -252,4 +291,17 @@ public class ResizableRaceCanvas extends Canvas {
return getHeight(); return getHeight();
} }
/**
* Draws boats during race setup, when leg heading is not set.
*/
public void drawBoats() {
if (boats != null) {
for (BoatInRace boat : boats) {
if (boat != null) {
displayBoat(boat, 0);
if (raceAnno) displayText(boat.getAbbrev(), boat.getVelocity(), this.map.convertGPS(boat.getCurrentPosition()));
}
}
}
}
} }

@ -6,8 +6,8 @@
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?> <?import javafx.scene.text.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.Controllers.RaceController"> fx:controller="seng302.Controllers.RaceController">
<children> <children>
<GridPane fx:id="startScreen" prefHeight="600.0" prefWidth="780.0"> <GridPane fx:id="startScreen" prefHeight="600.0" prefWidth="780.0">
<columnConstraints> <columnConstraints>
@ -27,7 +27,7 @@
<children> <children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Your Race Scaling:" GridPane.columnIndex="1" GridPane.rowIndex="1"> <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Select Your Race Scaling:" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font> <font>
<Font size="23.0" /> <Font size="23.0"/>
</font> </font>
</Text> </Text>
<Button mnemonicParsing="false" onAction="#startRace1Min" text="15x faster" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Button mnemonicParsing="false" onAction="#startRace1Min" text="15x faster" GridPane.columnIndex="1" GridPane.rowIndex="2" />
@ -35,26 +35,26 @@
<Button mnemonicParsing="false" onAction="#startRace5Min" text="3x faster" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <Button mnemonicParsing="false" onAction="#startRace5Min" text="3x faster" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<Label alignment="CENTER" text="Race will take ~1 minute" GridPane.columnIndex="1" GridPane.rowIndex="3"> <Label alignment="CENTER" text="Race will take ~1 minute" GridPane.columnIndex="1" GridPane.rowIndex="3">
<opaqueInsets> <opaqueInsets>
<Insets /> <Insets/>
</opaqueInsets> </opaqueInsets>
<font> <font>
<Font size="10.0" /> <Font size="10.0"/>
</font> </font>
</Label> </Label>
<Label alignment="CENTER" layoutX="99.0" layoutY="407.0" text="Race will take ~5 minutes" GridPane.columnIndex="2" GridPane.rowIndex="3"> <Label alignment="CENTER" layoutX="99.0" layoutY="407.0" text="Race will take ~5 minutes" GridPane.columnIndex="2" GridPane.rowIndex="3">
<font> <font>
<Font size="10.0" /> <Font size="10.0"/>
</font> </font>
<opaqueInsets> <opaqueInsets>
<Insets /> <Insets/>
</opaqueInsets> </opaqueInsets>
</Label> </Label>
<Label alignment="CENTER" layoutX="279.0" layoutY="407.0" text="Race will take ~15 minutes" GridPane.columnIndex="3" GridPane.rowIndex="3"> <Label alignment="CENTER" layoutX="279.0" layoutY="407.0" text="Race will take ~15 minutes" GridPane.columnIndex="3" GridPane.rowIndex="3">
<font> <font>
<Font size="10.0" /> <Font size="10.0"/>
</font> </font>
<opaqueInsets> <opaqueInsets>
<Insets /> <Insets/>
</opaqueInsets> </opaqueInsets>
</Label> </Label>
</children> </children>
@ -87,10 +87,10 @@
<children> <children>
<TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="600.0" prefWidth="264.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0"> <TableView fx:id="boatInfoTable" layoutX="-2.0" prefHeight="600.0" prefWidth="264.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="-2.0" AnchorPane.rightAnchor="-62.0" AnchorPane.topAnchor="0.0">
<columns> <columns>
<TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place" /> <TableColumn fx:id="boatPlacingColumn" prefWidth="50.0" text="Place"/>
<TableColumn fx:id="boatTeamColumn" prefWidth="100.0" text="Team" /> <TableColumn fx:id="boatTeamColumn" prefWidth="100.0" text="Team"/>
<TableColumn fx:id="boatMarkColumn" prefWidth="130.0" text="Mark" /> <TableColumn fx:id="boatMarkColumn" prefWidth="130.0" text="Mark"/>
<TableColumn fx:id="boatSpeedColumn" prefWidth="75.0" text="Speed" /> <TableColumn fx:id="boatSpeedColumn" prefWidth="75.0" text="Speed"/>
</columns> </columns>
</TableView> </TableView>
</children> </children>
@ -98,4 +98,5 @@
</items> </items>
</SplitPane> </SplitPane>
</children> </children>
>>>>>>> Temporary merge branch 2
</AnchorPane> </AnchorPane>

@ -1,6 +1,8 @@
package seng302.Model; package seng302.Model;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
@ -109,4 +111,43 @@ public class BoatInRaceTest {
assertEquals(testBoat.getVelocity(), 20.0); assertEquals(testBoat.getVelocity(), 20.0);
} }
@Test
public void getWakeAtProperHeading() throws Exception {
BoatInRace boat = new BoatInRace("Test", 1, Color.ALICEBLUE, "tt");
// Construct leg of 0 degrees
GPSCoordinate startPoint = new GPSCoordinate(0, 0);
GPSCoordinate endPoint = new GPSCoordinate(50, 0);
Leg leg0deg = new Leg("Start", startPoint, endPoint, 0);
boat.setCurrentLeg(leg0deg);
boat.setCurrentPosition(new GPSCoordinate(0,0));
assertEquals(0, boat.calculateHeading(), 1e-8);
// Construct leg from wake - heading should be 180 degrees
Leg leg180deg = new Leg("Start", startPoint, boat.getWake(), 0);
boat.setCurrentLeg(leg180deg);
assertEquals(180, boat.calculateHeading(), 1e-8);
}
@Test
public void getWakeProportionalToVelocity() throws Exception {
BoatInRace boat = new BoatInRace("Test", 10, Color.ALICEBLUE, "tt");
// Construct leg of 0 degrees at 0 N
GPSCoordinate startPoint = new GPSCoordinate(0, 0);
GPSCoordinate endPoint = new GPSCoordinate(50, 0);
Leg leg0deg = new Leg("Start", startPoint, endPoint, 0);
boat.setCurrentLeg(leg0deg);
boat.setCurrentPosition(new GPSCoordinate(0,0));
// Get latitude of endpoint of wake at 10 kn (longitude is 0)
double endpointAt10Kn = boat.getWake().getLatitude();
// Latitude of endpoint at 20 kn should be twice endpoint at 10 kn
boat.setVelocity(20);
assertEquals(2*endpointAt10Kn, boat.getWake().getLatitude(), 1e-8);
}
} }
Loading…
Cancel
Save