Merge branch 'develop' into issue#10_unifying_marks

# Conflicts:
#	src/main/java/seng302/App.java
#	src/main/java/seng302/controllers/CanvasController.java
This commit is contained in:
Alistair McIntyre
2017-05-22 15:13:48 +12:00
8 changed files with 238 additions and 99 deletions
@@ -50,6 +50,7 @@ public class CanvasController {
private final int RHS_BUFFER = BUFFER_SIZE + MARK_SIZE / 2; private final int RHS_BUFFER = BUFFER_SIZE + MARK_SIZE / 2;
private final int TOP_BUFFER = BUFFER_SIZE; private final int TOP_BUFFER = BUFFER_SIZE;
private final int BOT_BUFFER = TOP_BUFFER + MARK_SIZE / 2; private final int BOT_BUFFER = TOP_BUFFER + MARK_SIZE / 2;
private boolean horizontalInversion = false;
private double distanceScaleFactor; private double distanceScaleFactor;
private ScaleDirection scaleDirection; private ScaleDirection scaleDirection;
@@ -188,9 +189,9 @@ public class CanvasController {
boatGroup.move(); boatGroup.move();
} }
for (MarkGroup markGroup : markGroups) { for (MarkGroup markGroup : markGroups) {
for (long id : markGroup.getRaceIds()) { for (int id : markGroup.getRaceIds()) {
if (StreamParser.markPositions.containsKey(id)) { if (StreamParser.boatPositions.containsKey(id)) {
UpdateMarkGroup(id, markGroup); updateMarkGroup(id, markGroup);
} }
} }
} }
@@ -223,8 +224,8 @@ public class CanvasController {
} }
} }
private void UpdateMarkGroup (long raceId, MarkGroup markGroup) { void updateMarkGroup (int raceId, MarkGroup markGroup) {
PriorityBlockingQueue<BoatPositionPacket> movementQueue = StreamParser.markPositions.get(raceId); PriorityBlockingQueue<BoatPositionPacket> movementQueue = StreamParser.boatPositions.get(raceId);
if (movementQueue.size() > 0){ if (movementQueue.size() > 0){
try { try {
BoatPositionPacket positionPacket = movementQueue.take(); BoatPositionPacket positionPacket = movementQueue.take();
@@ -353,16 +354,11 @@ public class CanvasController {
//If the course is on a point on the earth where longitudes wrap around. //If the course is on a point on the earth where longitudes wrap around.
Limit minLonMark = sortedPoints.get(0); Limit minLonMark = sortedPoints.get(0);
Limit maxLonMark = sortedPoints.get(sortedPoints.size()-1); Limit maxLonMark = sortedPoints.get(sortedPoints.size()-1);
SingleMark thisMinLon = new SingleMark(minLonMark.toString(), minLonMark.getLat(), minLonMark.getLng(), minLonMark.getSeqID()); minLonPoint = new SingleMark(minLonMark.toString(), minLonMark.getLat(), minLonMark.getLng(), minLonMark.getSeqID());
SingleMark thisMaxLon = new SingleMark(maxLonMark.toString(), maxLonMark.getLat(), maxLonMark.getLng(), maxLonMark.getSeqID()); maxLonPoint = new SingleMark(maxLonMark.toString(), maxLonMark.getLat(), maxLonMark.getLng(), maxLonMark.getSeqID());
// TODO: 30/03/17 cir27 - Correctly account for longitude wrapping around. if (maxLonPoint.getLongitude() - minLonPoint.getLongitude() > 180) {
if (thisMaxLon.getLongitude() - thisMinLon.getLongitude() > 180) { horizontalInversion = true;
SingleMark temp = thisMinLon;
thisMinLon = thisMaxLon;
thisMaxLon = temp;
} }
minLonPoint = thisMinLon;
maxLonPoint = thisMaxLon;
} }
/** /**
@@ -393,6 +389,9 @@ public class CanvasController {
referencePointX += distanceScaleFactor * Math.sin(referenceAngle) * Mark.calculateDistance(referencePoint, minLonPoint); referencePointX += distanceScaleFactor * Math.sin(referenceAngle) * Mark.calculateDistance(referencePoint, minLonPoint);
referencePointX += ((CANVAS_WIDTH - (LHS_BUFFER + RHS_BUFFER)) - (minLonToMaxLon * distanceScaleFactor)) / 2; referencePointX += ((CANVAS_WIDTH - (LHS_BUFFER + RHS_BUFFER)) - (minLonToMaxLon * distanceScaleFactor)) / 2;
} }
if(horizontalInversion) {
referencePointX = CANVAS_WIDTH - RHS_BUFFER - (referencePointX - LHS_BUFFER);
}
} }
@@ -452,6 +451,9 @@ public class CanvasController {
xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference); xAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
yAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference); yAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
} }
if(horizontalInversion) {
xAxisLocation = CANVAS_WIDTH - RHS_BUFFER - (xAxisLocation - LHS_BUFFER);
}
return new Point2D(xAxisLocation, yAxisLocation); return new Point2D(xAxisLocation, yAxisLocation);
} }
@@ -105,7 +105,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
// Load FXML and set CSS // Load FXML and set CSS
fxmlLoader fxmlLoader
.setLocation(getClass().getResource("/views/importantAnnotationSelectView.fxml")); .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()); scene.getStylesheets().add(getClass().getResource("/css/master.css").toString());
stage.initStyle(StageStyle.UNDECORATED); stage.initStyle(StageStyle.UNDECORATED);
@@ -58,10 +58,10 @@ public class StartScreenController implements Initializable {
contentPane.getChildren().addAll((Pane) FXMLLoader.load(getClass().getResource(jfxUrl))); contentPane.getChildren().addAll((Pane) FXMLLoader.load(getClass().getResource(jfxUrl)));
} }
catch(javafx.fxml.LoadException e){ catch(javafx.fxml.LoadException e){
System.err.println(e.getCause()); e.printStackTrace();
} }
catch(IOException e){ catch(IOException e){
System.err.println(e); e.printStackTrace();
} }
} }
@@ -132,6 +132,7 @@ public class StartScreenController implements Initializable {
} }
public void switchToRaceView() { public void switchToRaceView() {
StreamParser.boatPositions.clear();
switchedToRaceView = true; switchedToRaceView = true;
setContentPane("/views/RaceView.fxml"); setContentPane("/views/RaceView.fxml");
} }
+153 -21
View File
@@ -1,11 +1,14 @@
package seng302.models; package seng302.models;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.CacheHint; import javafx.scene.CacheHint;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.input.MouseDragEvent; import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon; import javafx.scene.shape.Polygon;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.scene.transform.Rotate; import javafx.scene.transform.Rotate;
@@ -39,6 +42,7 @@ public class BoatGroup extends Group{
private double xIncrement; private double xIncrement;
private double yIncrement; private double yIncrement;
private long lastTimeValid = 0; private long lastTimeValid = 0;
private Double lastRotation = 0.0;
private long framesToMove; private long framesToMove;
//Graphical objects //Graphical objects
private Yacht boat; private Yacht boat;
@@ -49,6 +53,10 @@ public class BoatGroup extends Group{
private Text estTimeToNextMarkObject; private Text estTimeToNextMarkObject;
private Text legTimeObject; private Text legTimeObject;
private Wake wake; private Wake wake;
private Double distanceTravelled = 0.0;
private Point2D lastPoint;
private boolean destinationSet;
private Color textColor = Color.RED;
private Boolean isSelected = true; //All boats are initalised as selected private Boolean isSelected = true; //All boats are initalised as selected
@@ -61,6 +69,7 @@ public class BoatGroup extends Group{
public BoatGroup (Yacht boat, Color color){ public BoatGroup (Yacht boat, Color color){
this.boat = boat; this.boat = boat;
initChildren(color); initChildren(color);
this.textColor = color;
} }
/** /**
@@ -76,12 +85,31 @@ public class BoatGroup extends Group{
initChildren(color, points); initChildren(color, points);
} }
/**
* Return a text object with caching and a color applied
* @param defaultText The default text to display
* @param fill The text fill color
* @return The text object
*/
private Text getTextObject(String defaultText, Color fill){
Text text = new Text(defaultText);
text.setFill(fill);
text.setCacheHint(CacheHint.SPEED);
text.setCache(true);
return text;
}
/** /**
* Creates the javafx objects that will be the in the group by default. * Creates the javafx objects that will be the in the group by default.
* @param color The colour of the boat polygon and the trailing line. * @param color The colour of the boat polygon and the trailing line.
* @param points An array of co-ordinates x1,y1,x2,y2,x3,y3... that will make up the boat polygon. * @param points An array of co-ordinates x1,y1,x2,y2,x3,y3... that will make up the boat polygon.
*/ */
private void initChildren (Color color, double... points) { private void initChildren (Color color, double... points) {
textColor = color;
destinationSet = false;
boatPoly = new Polygon(points); boatPoly = new Polygon(points);
boatPoly.setFill(color); boatPoly.setFill(color);
boatPoly.setOnMouseEntered(event -> boatPoly.setFill(Color.FLORALWHITE)); boatPoly.setOnMouseEntered(event -> boatPoly.setFill(Color.FLORALWHITE));
@@ -90,24 +118,8 @@ public class BoatGroup extends Group{
boatPoly.setCache(true); boatPoly.setCache(true);
boatPoly.setCacheHint(CacheHint.SPEED); boatPoly.setCacheHint(CacheHint.SPEED);
teamNameObject = getTextObject(boat.getShortName(), textColor);
teamNameObject = new Text(boat.getShortName()); velocityObject = getTextObject(boat.getVelocity().toString(), textColor);
teamNameObject.setCache(true);
teamNameObject.setCacheHint(CacheHint.SPEED);
velocityObject = new Text(String.valueOf(boat.getVelocity()));
DateFormat format = new SimpleDateFormat("mm:ss");
String timeToNextMark = format
.format(boat.getEstimateTimeAtNextMark() - StreamParser.getCurrentTimeLong());
estTimeToNextMarkObject = new Text("Next mark: " + timeToNextMark);
if (boat.getMarkRoundingTime() != null) {
String elapsedTime = format
.format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundingTime());
legTimeObject = new Text("Last mark: " + elapsedTime);
} else {
legTimeObject = new Text("Last mark: -");
}
velocityObject.setCache(true);
velocityObject.setCacheHint(CacheHint.SPEED);
teamNameObject.setX(TEAMNAME_X_OFFSET); teamNameObject.setX(TEAMNAME_X_OFFSET);
teamNameObject.setY(TEAMNAME_Y_OFFSET); teamNameObject.setY(TEAMNAME_Y_OFFSET);
@@ -117,15 +129,23 @@ public class BoatGroup extends Group{
velocityObject.setY(VELOCITY_Y_OFFSET); velocityObject.setY(VELOCITY_Y_OFFSET);
velocityObject.relocate(velocityObject.getX(), velocityObject.getY()); velocityObject.relocate(velocityObject.getX(), velocityObject.getY());
updateLastMarkRoundingTime();
updateTimeTillNextMark();
if (estTimeToNextMarkObject != null){
estTimeToNextMarkObject.setX(ESTTIMETONEXTMARK_X_OFFSET); estTimeToNextMarkObject.setX(ESTTIMETONEXTMARK_X_OFFSET);
estTimeToNextMarkObject.setY(ESTTIMETONEXTMARK_Y_OFFSET); estTimeToNextMarkObject.setY(ESTTIMETONEXTMARK_Y_OFFSET);
estTimeToNextMarkObject estTimeToNextMarkObject
.relocate(estTimeToNextMarkObject.getX(), estTimeToNextMarkObject.getY()); .relocate(estTimeToNextMarkObject.getX(), estTimeToNextMarkObject.getY());
}
if (legTimeObject != null){
legTimeObject.setX(LEGTIME_X_OFFSET); legTimeObject.setX(LEGTIME_X_OFFSET);
legTimeObject.setY(LEGTIME_Y_OFFSET); legTimeObject.setY(LEGTIME_Y_OFFSET);
legTimeObject.relocate(legTimeObject.getX(), legTimeObject.getY()); legTimeObject.relocate(legTimeObject.getX(), legTimeObject.getY());
}
wake = new Wake(0, -BOAT_HEIGHT); wake = new Wake(0, -BOAT_HEIGHT);
super.getChildren() super.getChildren()
.addAll(teamNameObject, velocityObject, boatPoly, estTimeToNextMarkObject, .addAll(teamNameObject, velocityObject, boatPoly, estTimeToNextMarkObject,
@@ -148,7 +168,7 @@ public class BoatGroup extends Group{
* @param dx The amount to move the X coordinate by * @param dx The amount to move the X coordinate by
* @param dy The amount to move the Y coordinate by * @param dy The amount to move the Y coordinate by
*/ */
public void moveGroupBy(double dx, double dy) { private void moveGroupBy(double dx, double dy) {
boatPoly.setLayoutX(boatPoly.getLayoutX() + dx); boatPoly.setLayoutX(boatPoly.getLayoutX() + dx);
boatPoly.setLayoutY(boatPoly.getLayoutY() + dy); boatPoly.setLayoutY(boatPoly.getLayoutY() + dy);
teamNameObject.setLayoutX(teamNameObject.getLayoutX() + dx); teamNameObject.setLayoutX(teamNameObject.getLayoutX() + dx);
@@ -159,6 +179,8 @@ public class BoatGroup extends Group{
estTimeToNextMarkObject.setLayoutY(estTimeToNextMarkObject.getLayoutY() + dy); estTimeToNextMarkObject.setLayoutY(estTimeToNextMarkObject.getLayoutY() + dy);
legTimeObject.setLayoutX(legTimeObject.getLayoutX() + dx); legTimeObject.setLayoutX(legTimeObject.getLayoutX() + dx);
legTimeObject.setLayoutY(legTimeObject.getLayoutY() + dy); legTimeObject.setLayoutY(legTimeObject.getLayoutY() + dy);
wake.setLayoutX(wake.getLayoutX() + dx);
wake.setLayoutY(wake.getLayoutY() + dy);
} }
@@ -167,7 +189,7 @@ public class BoatGroup extends Group{
* @param x The X coordinate to move the boat to * @param x The X coordinate to move the boat to
* @param y The Y coordinate to move the boat to * @param y The Y coordinate to move the boat to
*/ */
public void moveTo (double x, double y, double rotation) { private void moveTo(double x, double y, double rotation) {
rotateTo(rotation); rotateTo(rotation);
boatPoly.setLayoutX(x); boatPoly.setLayoutX(x);
boatPoly.setLayoutY(y); boatPoly.setLayoutY(y);
@@ -179,18 +201,111 @@ public class BoatGroup extends Group{
estTimeToNextMarkObject.setLayoutY(y); estTimeToNextMarkObject.setLayoutY(y);
legTimeObject.setLayoutX(x); legTimeObject.setLayoutX(x);
legTimeObject.setLayoutY(y); legTimeObject.setLayoutY(y);
wake.setLayoutX(x);
wake.setLayoutY(y);
wake.rotate(rotation);
} }
public void rotateTo (double rotation) { private void rotateTo(double rotation) {
boatPoly.getTransforms().setAll(new Rotate(rotation)); boatPoly.getTransforms().setAll(new Rotate(rotation));
} }
/**
* Updates the time until next mark label, will create a label if one doesn't exist
*/
private void updateTimeTillNextMark(){
if (estTimeToNextMarkObject == null){
estTimeToNextMarkObject = getTextObject("", textColor);
}
if (boat.getEstimateTimeAtNextMark() != null){
DateFormat format = new SimpleDateFormat("mm:ss");
String timeToNextMark = format
.format(boat.getEstimateTimeAtNextMark() - StreamParser.getCurrentTimeLong());
estTimeToNextMarkObject.setText("Next mark: " + timeToNextMark);
} else {
estTimeToNextMarkObject.setText("Next mark: -");
}
}
/**
* Updates the time since last mark rounding, will create a label if one doesn't exist
*/
private void updateLastMarkRoundingTime(){
if (legTimeObject == null){
legTimeObject = getTextObject("", textColor);
}
if (boat.getMarkRoundingTime() != null){
DateFormat format = new SimpleDateFormat("mm:ss");
String elapsedTime = format
.format(StreamParser.getCurrentTimeLong() - boat.getMarkRoundingTime());
legTimeObject.setText("Last mark: " + elapsedTime);
}
else{
legTimeObject.setText("Last mark: -");
}
}
public void move() { public void move() {
double dx = xIncrement * framesToMove;
double dy = yIncrement * framesToMove;
distanceTravelled += Math.abs(dx) + Math.abs(dy);
moveGroupBy(xIncrement, yIncrement); moveGroupBy(xIncrement, yIncrement);
framesToMove = framesToMove - 1; framesToMove = framesToMove - 1;
if (framesToMove <= 0){ if (framesToMove <= 0){
isStopped = true; isStopped = true;
} }
if (distanceTravelled > 70){
distanceTravelled = 0d;
if (lastPoint != null){
Line l = new Line(
lastPoint.getX(),
lastPoint.getY(),
boatPoly.getLayoutX(),
boatPoly.getLayoutY()
);
l.getStrokeDashArray().setAll(3d, 7d);
l.setStroke(boat.getColour());
l.setCache(true);
l.setCacheHint(CacheHint.SPEED);
lineGroup.getChildren().add(l);
}
if (destinationSet){
lastPoint = new Point2D(boatPoly.getLayoutX(), boatPoly.getLayoutY());
}
}
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;
} }
/** /**
@@ -208,13 +323,30 @@ public class BoatGroup extends Group{
framesToMove = Math.round((frameRate/(1000.0f/(timeValid-lastTimeValid)))); framesToMove = Math.round((frameRate/(1000.0f/(timeValid-lastTimeValid))));
double dx = newXValue - boatPoly.getLayoutX(); double dx = newXValue - boatPoly.getLayoutX();
double dy = newYValue - boatPoly.getLayoutY(); double dy = newYValue - boatPoly.getLayoutY();
xIncrement = dx/framesToMove; xIncrement = dx/framesToMove;
yIncrement = dy/framesToMove; yIncrement = dy/framesToMove;
destinationSet = true;
Double rotationalVelocity = calculateRotationalVelocity(rotation);
updateTimeTillNextMark();
updateLastMarkRoundingTime();
if (Math.abs(rotationalVelocity) > 0.075) {
rotationalVelocity = 0.0;
wake.rotate(rotation);
}
rotateTo(rotation); rotateTo(rotation);
wake.setRotationalVelocity(rotationalVelocity, groundSpeed);
velocityObject.setText(String.format("%.2f m/s", groundSpeed)); velocityObject.setText(String.format("%.2f m/s", groundSpeed));
lastTimeValid = timeValid; lastTimeValid = timeValid;
isStopped = false; isStopped = false;
lastRotation = rotation;
} }
+1 -1
View File
@@ -6,7 +6,7 @@ import javafx.scene.paint.Color;
* Created by ryan_ on 16/03/2017. * Created by ryan_ on 16/03/2017.
*/ */
public enum Colors { public enum Colors {
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE; RED, PERU, SEAGREEN, GREEN, BLUE, PURPLE;
static Integer index = 0; static Integer index = 0;
+50 -43
View File
@@ -1,30 +1,34 @@
package seng302.models; package seng302.models;
import javafx.scene.CacheHint;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Arc; import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType; import javafx.scene.shape.ArcType;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.transform.Rotate; 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 * A group containing objects used to represent wakes onscreen. Contains functionality for their animation.
* 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.
*/ */
class Wake extends Group { class Wake extends Group {
private int numWakes = 5; //The number of wakes
private double[] velocities = new double[13]; 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 Arc[] arcs = new Arc[numWakes];
private double[] rotationalVelocities = new double[numWakes];
private double[] rotations = new double[numWakes]; private double[] rotations = new double[numWakes];
private int[] velocityIndices = new int[numWakes]; private double baseRad;
private double sum = 0;
private static double max;
/** /**
* Create a wake at the given location. * Create a wake at the given location.
*
* @param startingX x location where the tip of wake arcs will be. * @param startingX x location where the tip of wake arcs will be.
* @param startingY y location where the tip of wake arcs will be. * @param startingY y location where the tip of wake arcs will be.
*/ */
@@ -35,73 +39,76 @@ class Wake extends Group {
for (int i = 0; i < numWakes; i++) { 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. //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); arc = new Arc(0, 0, 0, 0, -110, 40);
//Opacity increases from 0.5 -> 0 evenly over the 5 wake arcs. arc.setCache(true);
arc.setFill(new Color(0.18, 0.7, 1.0, 1.0 + -0.175 * i)); arc.setCacheHint(CacheHint.SPEED);
arc.setType(ArcType.ROUND); 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; arcs[i] = arc;
} }
super.getChildren().addAll(arcs); super.getChildren().addAll(arcs);
} }
/** /**
* Sets the rotationalVelocity of each arc. Each arc is 3 velocities behind the next smallest arc. The smallest uses * Sets the rotationalVelocity of each arc.
* the latest given velocity. *
* @param rotationalVelocity The rotationalVelocity the wake should move at. * @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) { void setRotationalVelocity(double rotationalVelocity, double velocity) {
sum -= Math.abs(velocities[(velocityIndices[0] + 10) % 13]); rotationalVelocities[0] = rotationalVelocity;
sum += Math.abs(rotationalVelocity); for (int i = 1; i < numWakes; i++) {
max = Math.max(max, rotationalVelocity); double wakeSeparationRad = Math.toRadians(rotations[i - 1] - rotations[i]);
if (sum < (max / 3)) double shortestDistance = Math.atan2(
rotate (rotationGoal); //In relatively straight segments the wake snaps to match the boats current position. Math.sin(wakeSeparationRad),
//This stops the wake from eventually becoming out of sync with the boat. Math.cos(wakeSeparationRad)
//This accounts for rogue rotations that are greater than what would be realistic. Value is kinda rough. );
//Basically just for our internal mock. double distDeg = Math.toDegrees(shortestDistance);
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;
//Scale wakes based on velocity. if (rotationalVelocities[i - 1] < 0.01 && rotationalVelocities[i - 1] > -0.01) {
double baseRad = 20; rotationalVelocities[i] = distDeg / UNIFICATION_SPEED * Math.log(Math.abs(distDeg) + 1) / Math.log(MAX_DIFF / numWakes);
double rad;
} 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) { for (Arc arc : arcs) {
rad = baseRad + velocity;
arc.setRadiusX(rad); arc.setRadiusX(rad);
arc.setRadiusY(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. * 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. * @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++) { 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])); arcs[i].getTransforms().setAll(new Rotate(rotations[i]));
} }
} }
/** /**
* Rotate all wakes to the given rotation. * Rotate all wakes to the given rotation.
*
* @param rotation the from north angle in degrees to rotate to. * @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++) { for (int i = 0; i < arcs.length; i++) {
rotations[i] = rotation; rotations[i] = rotation;
rotationalVelocities[i] = 0;
arcs[i].getTransforms().setAll(new Rotate(rotation)); arcs[i].getTransforms().setAll(new Rotate(rotation));
} }
} }
} }
+1 -1
View File
@@ -151,7 +151,7 @@ public class Yacht {
this.colour = colour; this.colour = colour;
} }
public double getVelocity() { public Double getVelocity() {
return velocity; return velocity;
} }
+1 -4
View File
@@ -8,14 +8,11 @@ import seng302.models.Colors;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/**
* Created by ryan_ on 16/03/2017.
*/
public class ColorsTest { public class ColorsTest {
@Test @Test
public void testNextColor() { public void testNextColor() {
Color expectedColors[] = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.PURPLE}; Color expectedColors[] = {Color.RED, Color.PERU, Color.SEAGREEN, Color.GREEN, Color.BLUE, Color.PURPLE};
for (int i = 0; i<6; i++) for (int i = 0; i<6; i++)
{ {
Assert.assertEquals(expectedColors[i], Colors.getColor()); Assert.assertEquals(expectedColors[i], Colors.getColor());