From aaf2e6a3f0587e94c8a428112cf01984d1edfc85 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Fri, 19 May 2017 16:57:29 +1200 Subject: [PATCH] Added boat wakes back to the visualiser #story[923] --- src/main/java/seng302/App.java | 3 + .../controllers/RaceViewController.java | 2 +- .../controllers/StartScreenController.java | 1 + src/main/java/seng302/models/BoatGroup.java | 48 ++++++++ src/main/java/seng302/models/Wake.java | 103 ++++++++++-------- 5 files changed, 108 insertions(+), 49 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index ac264db6..3e464525 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -17,6 +17,9 @@ public class App extends Application { primaryStage.setTitle("RaceVision"); primaryStage.setScene(new Scene(root)); primaryStage.setMaximized(true); + primaryStage.setFullScreenExitHint(""); + primaryStage.setFullScreen(true); + primaryStage.show(); primaryStage.setOnCloseRequest(e -> { diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index 8492015b..43c04a9b 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -105,7 +105,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel // Load FXML and set CSS fxmlLoader .setLocation(getClass().getResource("/views/importantAnnotationSelectView.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 469, 248); + Scene scene = new Scene(fxmlLoader.load(), 469, 298); scene.getStylesheets().add(getClass().getResource("/css/master.css").toString()); stage.initStyle(StageStyle.UNDECORATED); diff --git a/src/main/java/seng302/controllers/StartScreenController.java b/src/main/java/seng302/controllers/StartScreenController.java index 94851d05..bf1456a8 100644 --- a/src/main/java/seng302/controllers/StartScreenController.java +++ b/src/main/java/seng302/controllers/StartScreenController.java @@ -132,6 +132,7 @@ public class StartScreenController implements Initializable { } public void switchToRaceView() { + StreamParser.boatPositions.clear(); switchedToRaceView = true; setContentPane("/views/RaceView.fxml"); } diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 28e9561f..0f606f4d 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -39,6 +39,7 @@ public class BoatGroup extends Group{ private double xIncrement; private double yIncrement; private long lastTimeValid = 0; + private Double lastRotation = 0.0; private long framesToMove; //Graphical objects private Yacht boat; @@ -159,6 +160,9 @@ public class BoatGroup extends Group{ estTimeToNextMarkObject.setLayoutY(estTimeToNextMarkObject.getLayoutY() + dy); legTimeObject.setLayoutX(legTimeObject.getLayoutX() + dx); legTimeObject.setLayoutY(legTimeObject.getLayoutY() + dy); + //////// + wake.setLayoutX(wake.getLayoutX() + dx); + wake.setLayoutY(wake.getLayoutY() + dy); } @@ -179,6 +183,11 @@ public class BoatGroup extends Group{ estTimeToNextMarkObject.setLayoutY(y); legTimeObject.setLayoutX(x); legTimeObject.setLayoutY(y); + ///////// + wake.setLayoutX(x); + wake.setLayoutY(y); + wake.rotate(rotation); + } public void rotateTo (double rotation) { @@ -191,8 +200,35 @@ public class BoatGroup extends Group{ if (framesToMove <= 0){ isStopped = true; } + //////////// + wake.updatePosition(1000/60); } + /////////// + /** + * Calculates the rotational velocity required to reach the rotationalGoal from the currentRotation. + */ + protected Double calculateRotationalVelocity (Double rotationalGoal) { + Double rotationalVelocity = 0.0; + + if (Math.abs(rotationalGoal - lastRotation) > 180) { + if (rotationalGoal - lastRotation >= 0.0) { + rotationalVelocity = ((rotationalGoal - lastRotation) - 360) / 200; + } else { + rotationalVelocity = (360 + (rotationalGoal - lastRotation)) / 200; + } + } else { + rotationalVelocity = (rotationalGoal - lastRotation) / 200; + } + + //Sometimes the rotation is too large to be realistic. In that case just do it instantly. + if (Math.abs(rotationalVelocity) > 1) { + rotationalVelocity = 0.0; + } + + return rotationalVelocity; + } + /** * Sets the destination of the boat and the headng it should have once it reaches * @param newXValue The X co-ordinate the boat needs to move to. @@ -208,13 +244,25 @@ public class BoatGroup extends Group{ framesToMove = Math.round((frameRate/(1000.0f/(timeValid-lastTimeValid)))); double dx = newXValue - boatPoly.getLayoutX(); double dy = newYValue - boatPoly.getLayoutY(); + xIncrement = dx/framesToMove; yIncrement = dy/framesToMove; + + Double rotationalVelocity = calculateRotationalVelocity(rotation); + + if (Math.abs(rotationalVelocity) > 0.075) { + rotationalVelocity = 0.0; + wake.rotate(rotation); + } + rotateTo(rotation); + wake.setRotationalVelocity(rotationalVelocity, groundSpeed); velocityObject.setText(String.format("%.2f m/s", groundSpeed)); lastTimeValid = timeValid; isStopped = false; + + lastRotation = rotation; } diff --git a/src/main/java/seng302/models/Wake.java b/src/main/java/seng302/models/Wake.java index 55d4381c..886dfba8 100644 --- a/src/main/java/seng302/models/Wake.java +++ b/src/main/java/seng302/models/Wake.java @@ -1,30 +1,34 @@ package seng302.models; +import javafx.scene.CacheHint; import javafx.scene.Group; import javafx.scene.paint.Color; import javafx.scene.shape.Arc; import javafx.scene.shape.ArcType; +import javafx.scene.shape.StrokeLineCap; import javafx.scene.transform.Rotate; /** - * By default wake is a group containing 5 arcs. Each arc starts from the same point. Each arc is larger and more - * transparent than the last. On calling updatePositions() arcs rotate at velocities given by setRotationalVelocity(). - * The larger and more transparent an arc is the longer the delay before it rotates at the latest velocity. It is - * assumed that rotationalVelocities() are set regularly as wakes do not stop rotating and an array of velocities needs - * to be populated for the class to work as expected. + * A group containing objects used to represent wakes onscreen. Contains functionality for their animation. */ class Wake extends Group { - private int numWakes = 5; - private double[] velocities = new double[13]; + //The number of wakes + private int numWakes = 8; + //The total possible difference between the first wake and the last. Increasing/Decreasing this will make wakes fan out more/less. + private final double MAX_DIFF = 75; + //Increasing/decreasing this will alter the speed that wakes converge when the heading stop changing. Anything over about 1500 may cause oscillation. + private final int UNIFICATION_SPEED = 750; + + private Arc[] arcs = new Arc[numWakes]; + private double[] rotationalVelocities = new double[numWakes]; private double[] rotations = new double[numWakes]; - private int[] velocityIndices = new int[numWakes]; - private double sum = 0; - private static double max; + private double baseRad; /** * Create a wake at the given location. + * * @param startingX x location where the tip of wake arcs will be. * @param startingY y location where the tip of wake arcs will be. */ @@ -34,74 +38,77 @@ class Wake extends Group { Arc arc; for (int i = 0; i < numWakes; i++) { //Default triangle is -110 deg out of phase with a default wake and has angle of 40 deg. - arc = new Arc(0,0,0,0,-110,40); - //Opacity increases from 0.5 -> 0 evenly over the 5 wake arcs. - arc.setFill(new Color(0.18, 0.7, 1.0, 1.0 + -0.175 * i)); - arc.setType(ArcType.ROUND); + arc = new Arc(0, 0, 0, 0, -110, 40); + arc.setCache(true); + arc.setCacheHint(CacheHint.SPEED); + arc.setType(ArcType.OPEN); + arc.setStroke(new Color(0.18, 0.7, 1.0, 1.0 + (-0.99 / numWakes * i))); + arc.setStrokeWidth(3.0); + arc.setStrokeLineCap(StrokeLineCap.ROUND); + arc.setFill(new Color(0.0, 0.0, 0.0, 0.0)); + baseRad = (20 / numWakes); arcs[i] = arc; } super.getChildren().addAll(arcs); } /** - * Sets the rotationalVelocity of each arc. Each arc is 3 velocities behind the next smallest arc. The smallest uses - * the latest given velocity. + * Sets the rotationalVelocity of each arc. + * * @param rotationalVelocity The rotationalVelocity the wake should move at. - * @param rotationGoal Where the wake will rotate to if the wake is calculated to be on a straight section. This is - * used to prevent desynchronisation with the Boat polygon. - * @param velocity The real world velocity of the boat in m/s. + * @param velocity The real world velocity of the boat in m/s. */ - void setRotationalVelocity (double rotationalVelocity, double rotationGoal, double velocity) { - sum -= Math.abs(velocities[(velocityIndices[0] + 10) % 13]); - sum += Math.abs(rotationalVelocity); - max = Math.max(max, rotationalVelocity); - if (sum < (max / 3)) - rotate (rotationGoal); //In relatively straight segments the wake snaps to match the boats current position. - //This stops the wake from eventually becoming out of sync with the boat. - //This accounts for rogue rotations that are greater than what would be realistic. Value is kinda rough. - //Basically just for our internal mock. - if (Math.abs(rotationalVelocity) > 0.05) { - rotationalVelocity = 0; - rotate(rotationGoal); - } - //Update the index of the array of recent velocities that each wake uses. Each wake is 3 velocities behind the - //next smallest wake. - velocityIndices[0] = (13 + (velocityIndices[0] - 1) % 13) % 13; - velocities[velocityIndices[0]] = rotationalVelocity; - for (int i = 1; i < numWakes; i++) - velocityIndices[i] = (velocityIndices[0] + 3 * i) % 13; + void setRotationalVelocity(double rotationalVelocity, double velocity) { + rotationalVelocities[0] = rotationalVelocity; + for (int i = 1; i < numWakes; i++) { + double wakeSeparationRad = Math.toRadians(rotations[i - 1] - rotations[i]); + double shortestDistance = Math.atan2( + Math.sin(wakeSeparationRad), + Math.cos(wakeSeparationRad) + ); + double distDeg = Math.toDegrees(shortestDistance); - //Scale wakes based on velocity. - double baseRad = 20; - double rad; - for (Arc arc :arcs) { - rad = baseRad + velocity; + if (rotationalVelocities[i - 1] < 0.01 && rotationalVelocities[i - 1] > -0.01) { + rotationalVelocities[i] = distDeg / UNIFICATION_SPEED * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); + + } else { + if (distDeg < (MAX_DIFF / numWakes)) + rotationalVelocities[i] = rotationalVelocities[i - 1] * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes); + else + rotationalVelocities[i] = rotationalVelocities[i - 1]; + } + } + + double rad = baseRad + velocity; + for (Arc arc : arcs) { arc.setRadiusX(rad); arc.setRadiusY(rad); - baseRad += 5 + (velocity / 2); + rad += (20 / numWakes) + (velocity / 2); } } /** * Arcs rotate based on the distance they would have travelled over the supplied time interval. + * * @param timeInterval the time interval, in microseconds, that the wake should move. */ - void updatePosition (long timeInterval) { + void updatePosition(long timeInterval) { for (int i = 0; i < numWakes; i++) { - rotations[i] = rotations[i] + velocities[velocityIndices[i]] * timeInterval; + rotations[i] = rotations[i] + rotationalVelocities[i] * timeInterval; arcs[i].getTransforms().setAll(new Rotate(rotations[i])); } } /** * Rotate all wakes to the given rotation. + * * @param rotation the from north angle in degrees to rotate to. */ - void rotate (double rotation) { + void rotate(double rotation) { for (int i = 0; i < arcs.length; i++) { rotations[i] = rotation; + rotationalVelocities[i] = 0; arcs[i].getTransforms().setAll(new Rotate(rotation)); } } - }