diff --git a/src/main/java/seng302/controllers/CanvasController.java b/src/main/java/seng302/controllers/CanvasController.java index 4d57559b..37f45f1a 100644 --- a/src/main/java/seng302/controllers/CanvasController.java +++ b/src/main/java/seng302/controllers/CanvasController.java @@ -106,7 +106,7 @@ public class CanvasController { // TODO: 1/05/17 wmu16 - Change this call to now draw the marks as from the xml - drawBoats(); + initializeBoats(); timer = new AnimationTimer() { @Override @@ -157,8 +157,8 @@ public class CanvasController { SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); Limit thisPoint2 = courseLimits.get(i+1); SingleMark thisMark2 = new SingleMark("", thisPoint2.getLat(), thisPoint2.getLng(), thisPoint2.getSeqID()); - Point2D borderPoint1 = findScaledXY(thisMark1); - Point2D borderPoint2 = findScaledXY(thisMark2); + Point2D borderPoint1 = latLonToXY(thisMark1.getLatitude(), thisMark1.getLongitude()); + Point2D borderPoint2 = latLonToXY(thisMark2.getLatitude(), thisMark2.getLongitude()); gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), borderPoint2.getX(), borderPoint2.getY()); xBoundaryPoints[i] = borderPoint1.getX(); @@ -168,8 +168,8 @@ public class CanvasController { SingleMark thisMark1 = new SingleMark("", thisPoint1.getLat(), thisPoint1.getLng(), thisPoint1.getSeqID()); Limit thisPoint2 = courseLimits.get(0); SingleMark thisMark2 = new SingleMark("", thisPoint2.getLat(), thisPoint2.getLng(), thisPoint2.getSeqID()); - Point2D borderPoint1 = findScaledXY(thisMark1); - Point2D borderPoint2 = findScaledXY(thisMark2); + Point2D borderPoint1 = latLonToXY(thisMark1.getLatitude(), thisMark1.getLongitude()); + Point2D borderPoint2 = latLonToXY(thisMark2.getLatitude(), thisMark2.getLongitude()); gc.strokeLine(borderPoint1.getX(), borderPoint1.getY(), borderPoint2.getX(), borderPoint2.getY()); xBoundaryPoints[courseLimits.size()-1] = borderPoint1.getX(); @@ -207,8 +207,8 @@ public class CanvasController { singleMark1.getLongitude()); markGroup = new MarkGroup(thisGateMark, - findScaledXY(thisGateMark.getSingleMark1()), - findScaledXY(thisGateMark.getSingleMark2())); + latLonToXY(thisGateMark.getSingleMark1().getLatitude(), thisGateMark.getSingleMark1().getLongitude()), + latLonToXY(thisGateMark.getSingleMark2().getLatitude(), thisGateMark.getSingleMark2().getLongitude())); raceObjects.add(markGroup); raceMarks.add(thisGateMark); @@ -221,7 +221,7 @@ public class CanvasController { singleMark.getTargetLng(), singleMark.getSourceID()); - markGroup = new MarkGroup(thisSingleMark, findScaledXY(thisSingleMark)); + markGroup = new MarkGroup(thisSingleMark, latLonToXY(thisSingleMark.getLatitude(), thisSingleMark.getLongitude())); raceObjects.add(markGroup); raceMarks.add(thisSingleMark); @@ -245,27 +245,30 @@ public class CanvasController { private void move(long id, RaceObject raceObject){ PriorityBlockingQueue movementQueue = StreamParser.boatPositions.get(id); if (movementQueue.size() > 0){ - BoatPositionPacket positionPacket = movementQueue.peek(); +// BoatPositionPacket positionPacket = movementQueue.peek(); //this code adds a delay to reading from the movementQueue //in case things being put into the movement queue are slightly //out of order - int delayTime = 1000; - int loopTime = delayTime * 10; - long timeDiff = (System.currentTimeMillis()%loopTime - positionPacket.getTimeValid()%loopTime); - if (timeDiff < 0){ - timeDiff = loopTime + timeDiff; - } - if (timeDiff > delayTime) { - try { - positionPacket = movementQueue.take(); - Point2D p2d = latLonToXY(positionPacket.getLat(), positionPacket.getLon()); - double heading = 360.0 / 0xffff * positionPacket.getHeading(); - raceObject.setDestination(p2d.getX(), p2d.getY(), heading, positionPacket.getGroundSpeed(), (int) id); - } catch (InterruptedException e){ - e.printStackTrace(); +// int delayTime = 1000; +// int loopTime = delayTime * 10; +// long timeDiff = (System.currentTimeMillis()%loopTime - positionPacket.getTimeValid()%loopTime); +// if (timeDiff < 0){ +// timeDiff = loopTime + timeDiff; +// } +// if (timeDiff > delayTime) { + try { + BoatPositionPacket positionPacket = movementQueue.take(); + if (id == 104){ + System.out.println(positionPacket.getTimeValid()); } + Point2D p2d = latLonToXY(positionPacket.getLat(), positionPacket.getLon()); + double heading = 360.0 / 0xffff * positionPacket.getHeading(); + raceObject.setDestination(p2d.getX(), p2d.getY(), heading, positionPacket.getGroundSpeed(), (int) id); + } catch (InterruptedException e){ + e.printStackTrace(); } +// } } } @@ -320,20 +323,13 @@ public class CanvasController { /** * Draws all the boats. */ - private void drawBoats() { -// Map timelineInfos = raceViewController.getTimelineInfos(); -// List boats = raceViewController.getStartingBoats(); + private void initializeBoats() { Map boats = StreamParser.getBoats(); - Double startingX = raceObjects.get(0).getLayoutX(); - Double startingY = raceObjects.get(0).getLayoutY(); Group boatAnnotations = new Group(); for (Yacht boat : boats.values()) { -// for (Boat boat : boats) { boat.setColour(Colors.getColor()); BoatGroup boatGroup = new BoatGroup(boat, boat.getColour()); - boatGroup.moveTo(startingX, startingY, 0d); - //boatGroup.setStage(raceViewController.getStage()); raceObjects.add(boatGroup); boatAnnotations.getChildren().add(boatGroup.getLowPriorityAnnotations()); } @@ -456,10 +452,12 @@ public class CanvasController { if (!processed.contains(mark)) { if (mark.getMarkType() != MarkType.SINGLE_MARK) { GateMark gateMark = (GateMark) mark; - markGroup = new MarkGroup(mark, findScaledXY(gateMark.getSingleMark1()), findScaledXY(gateMark.getSingleMark2())); + markGroup = new MarkGroup(mark, + latLonToXY(gateMark.getSingleMark1().getLatitude(), gateMark.getSingleMark1().getLongitude()), + latLonToXY(gateMark.getSingleMark2().getLatitude(), gateMark.getSingleMark2().getLongitude())); raceObjects.add(markGroup); } else { - markGroup = new MarkGroup(mark, findScaledXY(mark)); + markGroup = new MarkGroup(mark, latLonToXY(mark.getLatitude(), mark.getLongitude())); raceObjects.add(markGroup); } processed.add(mark); @@ -467,19 +465,18 @@ public class CanvasController { } } - private Point2D findScaledXY (Mark unscaled) { - return findScaledXY (minLatPoint.getLatitude(), minLatPoint.getLongitude(), - unscaled.getLatitude(), unscaled.getLongitude()); - } - private Point2D findScaledXY (double latA, double lonA, double latB, double lonB) { + + private Point2D latLonToXY (double latitude, double longitude){ double distanceFromReference; double angleFromReference; int xAxisLocation = (int) referencePointX; int yAxisLocation = (int) referencePointY; - angleFromReference = Mark.calculateHeadingRad(latA, lonA, latB, lonB); - distanceFromReference = Mark.calculateDistance(latA, lonA, latB, lonB); + double minLat = minLatPoint.getLatitude(); + double minLon = minLatPoint.getLongitude(); + angleFromReference = Mark.calculateHeadingRad(minLat, minLon, latitude, longitude); + distanceFromReference = Mark.calculateDistance(minLat, minLon, latitude, longitude); if (angleFromReference >= 0 && angleFromReference <= Math.PI / 2) { xAxisLocation += (int) Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference); yAxisLocation -= (int) Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference); @@ -529,10 +526,6 @@ public class CanvasController { } } - private Point2D latLonToXY (double latitude, double longitude) { - return findScaledXY(minLatPoint.getLatitude(), minLatPoint.getLongitude(), latitude, longitude); - } - List getRaceObjects() { return raceObjects; } diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 57dd48db..327ae1ba 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -122,7 +122,8 @@ public class BoatGroup extends RaceObject{ velocityObject.setLayoutY(velocityObject.getLayoutY() + dy); wake.setLayoutX(wake.getLayoutX() + dx); wake.setLayoutY(wake.getLayoutY() + dy); - rotateTo(rotation + currentRotation); + currentRotation = rotation + currentRotation; + boatPoly.getTransforms().setAll(new Rotate(rotation)); } /** @@ -132,16 +133,8 @@ public class BoatGroup extends RaceObject{ * @param rotation The heading in degrees from north the boat should rotate to. */ public void moveTo (double x, double y, double rotation) { - rotateTo(rotation); - moveTo(x, y); - } - - /** - * Moves the boat and its children annotations to coordinates specified - * @param x The X coordinate to move the boat to - * @param y The Y coordinate to move the boat to - */ - public void moveTo (double x, double y) { + currentRotation = rotation; + boatPoly.getTransforms().setAll(new Rotate(rotation)); boatPoly.setLayoutX(x); boatPoly.setLayoutY(y); teamNameObject.setLayoutX(x); @@ -165,6 +158,7 @@ public class BoatGroup extends RaceObject{ double rotation = rotationalVelocity * timeInterval; distanceTravelled += Math.abs(dx) + Math.abs(dy); moveGroupBy(dx, dy, rotation); + //Draw a new section of the trail every 20 pixels of movement. if (distanceTravelled > 20) { distanceTravelled = 0; @@ -203,12 +197,6 @@ public class BoatGroup extends RaceObject{ currentRotation = 360 - currentRotation; double dx = newXValue - boatPoly.getLayoutX(); double dy = newYValue - boatPoly.getLayoutY(); - //Check movement is reasonable. Assumes a 1000 * 1000 canvas - if (Math.abs(dx) > 50 || Math.abs(dy) > 50) { - dx = 0; - dy = 0; - moveTo(newXValue, newYValue); - } pixelVelocityX = dx / expectedUpdateInterval; pixelVelocityY = dy / expectedUpdateInterval; @@ -217,7 +205,6 @@ public class BoatGroup extends RaceObject{ if (wakeGenerationDelay > 0) { wake.rotate(rotationalGoal); - rotateTo(rotationalGoal); //Need to test with this removed. rotationalVelocity = 0; wakeGenerationDelay--; } else { @@ -254,31 +241,6 @@ public class BoatGroup extends RaceObject{ } } - public void setDestination (double newXValue, double newYValue, double groundSpeed, int... raceIDs) { - destinationSet = true; - - if (hasRaceId(raceIDs)) { - double rotation = Math.abs( - Math.toDegrees( - Math.atan( - (newYValue - boatPoly.getLayoutY()) / (newXValue - boatPoly.getLayoutX()) - ) - ) - ); - setDestination(newXValue, newYValue, rotation, groundSpeed, raceIDs); - } - } - - public void rotateTo (double rotation) { - currentRotation = rotation; - boatPoly.getTransforms().setAll(new Rotate(rotation)); - } - - public void forceRotation () { - rotateTo (rotationalGoal); - wake.rotate(rotationalGoal); - } - public void setTeamNameObjectVisible(Boolean visible) { teamNameObject.setVisible(visible); } diff --git a/src/main/java/seng302/models/RaceObject.java b/src/main/java/seng302/models/RaceObject.java index 91f02971..706bfd48 100644 --- a/src/main/java/seng302/models/RaceObject.java +++ b/src/main/java/seng302/models/RaceObject.java @@ -47,10 +47,10 @@ public abstract class RaceObject extends Group { this.rotationalVelocity = (rotationalGoal - currentRotation) / expectedUpdateInterval; } //Sometimes the rotation is too large to be realistic. In that case just do it instantly. - if (Math.abs(rotationalVelocity) > 1) { - rotationalVelocity = 0; - rotateTo(rotationalGoal); - } +// if (Math.abs(rotationalVelocity) > 1) { +// rotationalVelocity = 0; +// rotateTo(rotationalGoal); +// } } /** @@ -62,25 +62,13 @@ public abstract class RaceObject extends Group { * @param raceIds RaceID of the object to move. */ public abstract void setDestination (double x, double y, double rotation, double groundSpeed, int... raceIds); - /** - * Sets the destination of everything within the RaceObject that has an ID in the array raceIds. The destination is - * set to the co-ordinates (x, y). - * @param x X co-ordinate to move the graphic to. - * @param y Y co-ordinate to move the graphic to. - * @param raceIds RaceID to the object to move. - */ - public abstract void setDestination (double x, double y, double groundSpeed, int... raceIds); public abstract void updatePosition (long timeInterval); public abstract void moveTo (double x, double y, double rotation); - public abstract void moveTo (double x, double y); - public abstract void moveGroupBy(double x, double y, double rotation); - public abstract void rotateTo (double rotation); - public abstract boolean hasRaceId (int... raceIds); public abstract int[] getRaceIds (); diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/models/mark/MarkGroup.java index 29931e01..6bcd40f4 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/models/mark/MarkGroup.java @@ -92,16 +92,12 @@ public class MarkGroup extends RaceObject { } public void setDestination (double x, double y, double rotation, double groundSpeed, int... raceIds) { - setDestination(x, y, 0, raceIds); - this.rotationalGoal = rotation; - calculateRotationalVelocity(); - } - - public void setDestination (double x, double y, double groundSpeed, int... raceIds) { for (int i = 0; i < marks.size(); i++) for (int id : raceIds) if (id == marks.get(i).getId()) setDestinationChild(x, y, 0, Math.max(0, i-1)); + this.rotationalGoal = rotation; + calculateRotationalVelocity(); } diff --git a/src/main/java/seng302/models/parsers/StreamPacket.java b/src/main/java/seng302/models/parsers/StreamPacket.java deleted file mode 100644 index 5c2c0706..00000000 --- a/src/main/java/seng302/models/parsers/StreamPacket.java +++ /dev/null @@ -1,44 +0,0 @@ -package seng302.models.parsers; - -/** - * Created by kre39 on 23/04/17. - */ -public class StreamPacket { - - //Change int to an ENUM for the type - private PacketType type; - - private long messageLength; - private long timeStamp; - private byte[] payload; - - StreamPacket(int type, long messageLength, long timeStamp, byte[] payload) { - this.type = PacketType.assignPacketType(type); - this.messageLength = messageLength; - this.timeStamp = timeStamp; - this.payload = payload; -// System.out.println("type = " + this.type.toString()); - //switch the packet type to deal with what ever specific packet you want to deal with -// if (this.type == PacketType.XML_MESSAGE){ -// //System.out.println("--------"); -// System.out.println(new String(payload)); -// //StreamParser.parsePacket(this); -// } - } - - PacketType getType() { - return type; - } - - public long getMessageLength() { - return messageLength; - } - - byte[] getPayload() { - return payload; - } - - long getTimeStamp() { - return timeStamp; - } -} diff --git a/src/main/java/seng302/models/parsers/StreamParser.java b/src/main/java/seng302/models/parsers/StreamParser.java index deea3184..890f37f6 100644 --- a/src/main/java/seng302/models/parsers/StreamParser.java +++ b/src/main/java/seng302/models/parsers/StreamParser.java @@ -66,20 +66,20 @@ public class StreamParser extends Thread{ Thread.sleep(1); } while (appRunning){ - StreamPacket packet = StreamReceiver.packetBuffer.peek(); +// StreamPacket packet = StreamReceiver.packetBuffer.peek(); //this code adds a delay to reading from the packetBuffer so //out of order packets have time to order themselves in the queue - int delayTime = 1000; - int loopTime = delayTime * 10; - long transitTime = (System.currentTimeMillis()%loopTime - packet.getTimeStamp()%loopTime); - if (transitTime < 0){ - transitTime = loopTime + transitTime; - } - if (transitTime < delayTime) { - long sleepTime = delayTime - (transitTime); - Thread.sleep(sleepTime); - } - packet = StreamReceiver.packetBuffer.take(); +// int delayTime = 1000; +// int loopTime = delayTime * 10; +// long transitTime = (System.currentTimeMillis()%loopTime - packet.getTimeStamp()%loopTime); +// if (transitTime < 0){ +// transitTime = loopTime + transitTime; +// } +// if (transitTime < delayTime) { +// long sleepTime = delayTime - (transitTime); +// Thread.sleep(sleepTime); +// } + StreamPacket packet = StreamReceiver.packetBuffer.take(); parsePacket(packet); Thread.sleep(1); while (StreamReceiver.packetBuffer.peek() == null) { diff --git a/src/test/java/seng302/models/parsers/StreamReceiverTest.java b/src/test/java/seng302/models/parsers/StreamReceiverTest.java index c7951e3b..d2dc6b82 100644 --- a/src/test/java/seng302/models/parsers/StreamReceiverTest.java +++ b/src/test/java/seng302/models/parsers/StreamReceiverTest.java @@ -2,6 +2,7 @@ package seng302.models.parsers; import org.junit.Before; import org.junit.Test; +import seng302.models.parsers.packets.StreamPacket; import java.io.*; import java.lang.reflect.Method;