Implementing the boats movement throught a race

-Not complete! Still need to create function to calculate new coordinates
-Doesn't run because of problems with resource folder

#refactor
main
Erika Savell 9 years ago
parent b73fee2cb5
commit 6f509e25ea

@ -7,8 +7,7 @@ import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Line;
import seng302.Coordinate;
import seng302.GraphCoordinate;
import seng302.RaceMap;
import java.net.URL;
@ -28,23 +27,23 @@ public class RaceController extends Controller{
private GraphicsContext gc;
private RaceMap map;
public void displayBoat(Coordinate coordinate, Paint paint){
public void displayBoat(GraphCoordinate graphCoordinate, Paint paint){
gc.setFill(paint);
gc.fillOval(coordinate.getX(), coordinate.getY(), 15, 15);
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 15, 15);
}
public void displayLine(Coordinate coordinateA, Coordinate coordinateB, Paint paint){
public void displayLine(GraphCoordinate graphCoordinateA, GraphCoordinate graphCoordinateB, Paint paint){
gc.setStroke(paint);
gc.setFill(paint);
gc.fillOval(coordinateA.getX() - 3, coordinateA.getY() - 3, 6, 6);
gc.fillOval(coordinateB.getX() - 3, coordinateB.getY() - 3, 6, 6);
gc.strokeLine(coordinateA.getX(), coordinateA.getY(), coordinateB.getX(), coordinateB.getY());
gc.fillOval(graphCoordinateA.getX() - 3, graphCoordinateA.getY() - 3, 6, 6);
gc.fillOval(graphCoordinateB.getX() - 3, graphCoordinateB.getY() - 3, 6, 6);
gc.strokeLine(graphCoordinateA.getX(), graphCoordinateA.getY(), graphCoordinateB.getX(), graphCoordinateB.getY());
}
public void displayPoint(Coordinate coordinate, Paint paint){
public void displayPoint(GraphCoordinate graphCoordinate, Paint paint){
gc.setFill(paint);
gc.fillOval(coordinate.getX(), coordinate.getY(), 10, 10);
gc.fillOval(graphCoordinate.getX(), graphCoordinate.getY(), 10, 10);
}
@Override
@ -54,26 +53,26 @@ public class RaceController extends Controller{
this.gc = raceMap.getGraphicsContext2D();
this.map = new RaceMap(32.321989, -64.8553, 32.246, -64.831, (int)raceMap.getWidth(), (int)raceMap.getHeight());
//boat
Coordinate boat1coord = this.map.convertGPS(32.296577, -64.854304);
GraphCoordinate boat1coord = this.map.convertGPS(32.296577, -64.854304);
displayBoat(boat1coord, Color.AQUAMARINE);
//finish line
gc.setLineWidth(2);
Coordinate finishLineCoord1 = this.map.convertGPS(32.317379, -64.839291);
Coordinate finishLineCoord2 = this.map.convertGPS(32.317257, -64.836260);
GraphCoordinate finishLineCoord1 = this.map.convertGPS(32.317379, -64.839291);
GraphCoordinate finishLineCoord2 = this.map.convertGPS(32.317257, -64.836260);
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
//marks
Coordinate markCoord = this.map.convertGPS(32.293039, -64.843983);
Coordinate southGate1 = this.map.convertGPS(32.284680, -64.850045);
Coordinate southGate2 = this.map.convertGPS(32.280164, -64.847591);
Coordinate northGate1 = this.map.convertGPS(32.309693, -64.835249);
Coordinate northGate2 = this.map.convertGPS(32.308046, -64.831785);
GraphCoordinate markCoord = this.map.convertGPS(32.293039, -64.843983);
GraphCoordinate southGate1 = this.map.convertGPS(32.284680, -64.850045);
GraphCoordinate southGate2 = this.map.convertGPS(32.280164, -64.847591);
GraphCoordinate northGate1 = this.map.convertGPS(32.309693, -64.835249);
GraphCoordinate northGate2 = this.map.convertGPS(32.308046, -64.831785);
displayBoat(boat1coord, Color.AQUAMARINE);
displayBoat(markCoord, Color.GOLD);
displayLine(southGate1, southGate2, Color.DARKCYAN);
displayLine(northGate1, northGate2, Color.DARKVIOLET);
//start line
Coordinate startline1 = this.map.convertGPS(32.296577, -64.854304);
Coordinate startline2 = this.map.convertGPS(32.293771, -64.855242);
GraphCoordinate startline1 = this.map.convertGPS(32.296577, -64.854304);
GraphCoordinate startline2 = this.map.convertGPS(32.293771, -64.855242);
displayLine(startline1, startline2, Color.GREEN);
displayPoint(this.map.convertGPS(32.293771, -64.855242), Color.BLACK);

@ -1,79 +0,0 @@
//package seng302;
//
///**
// * Created by fwy13 on 3/03/17.
// */
//public class Event {
// private Leg leg;
// private Boat boat;
// private int time;
// private Leg goalMarker;
//
// /**
// * Initaliser for Racemaker without a goal node (such as the Finish line).
// * @param leg current Racemarker that has just been passed.
// * @param boat Boat that has been passed.
// * @param time time in seconds that the event occurred.
// * @see Leg
// * @see seng302.Boat
// */
// public Event(Leg leg, Boat boat, int time){
// this.leg = leg;
// this.boat = boat;
// this.time = time;
// }
//
// /**
// * Initaliser for Racemaker with a goal node.
// * @param leg current Racemarker that has just been passed.
// * @param boat Boat that has been passed.
// * @param time time in seconds that the event occurred.
// * @param goalMarker the next marker that the boat is aiming for.
// * @see Leg
// * @see seng302.Boat
// */
// public Event(Leg leg, Boat boat, int time, Leg goalMarker){
// this.leg = leg;
// this.boat = boat;
// this.time = time;
// this.goalMarker = goalMarker;
// }
//
//
// /**
// *
// * @return the leg that the boat has just passed.
// */
// public Leg getLeg() {
// return leg;
// }
//
// /**
// *
// * @return the boat that this event occured for.
// */
// public Boat getBoat() {
// return boat;
// }
//
// /**
// *
// * @return the time the event occurred
// */
// public int getTime(){
// return this.time;
// }
//
// /**
// *
// * @return the event as a string in format {Boat Name} passed {Leg Name} at {Time Event Occurred} seconds at heading {Heading}.
// */
// public String toString() {
// String stringToReturn = boat.getName() + " passed " + leg.toString() + " at " + time + " seconds";
// if (goalMarker != null){
// stringToReturn += " at heading " + (int) calculateHeading();
// }
// stringToReturn += ".";
// return stringToReturn;
// }
//}

@ -0,0 +1,22 @@
package seng302;
/**
* Created by esa46 on 15/03/17.
*/
public class GPSCoordinate {
private double latitude;
private double longitude;
public GPSCoordinate(double latitude, double longitude) { this.latitude = latitude; this.longitude = longitude; }
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}

@ -3,11 +3,11 @@ package seng302;
/**
* Created by cbt24 on 15/03/17.
*/
public class Coordinate {
public class GraphCoordinate {
private int x;
private int y;
public Coordinate(int x, int y) { this.x = x; this.y = y; }
public GraphCoordinate(int x, int y) { this.x = x; this.y = y; }
public int getX() {
return x;

@ -1,4 +1,4 @@
package seng302;
package seng302.Model;
import java.util.ArrayList;
@ -42,4 +42,5 @@ public class Boat {
public String toString(){
return getName();
}
}

@ -1,4 +1,7 @@
package seng302;
package seng302.Model;
import seng302.GPSCoordinate;
import seng302.GraphCoordinate;
/**
* Created by esa46 on 15/03/17.
@ -6,36 +9,43 @@ package seng302;
public class BoatInRace extends Boat {
private Leg currentLeg;
private double distanceTravelledInLeg;
private GPSCoordinate currentPosition;
BoatInRace(String name, double velocity) {
super(name, velocity);
}
public Leg getCurrentLeg() {
return currentLeg;
}
public void setCurrentLeg(Leg currentLeg) {
this.currentLeg = currentLeg;
}
public double getDistanceTravelledInLeg() {
return distanceTravelledInLeg;
}
public Coordinate getCurrentPosition() {
return currentPosition;
public void setCurrentPosition(GPSCoordinate position) {
this.currentPosition = position;
}
private double distanceTravelledInLeg;
private Coordinate currentPosition;
BoatInRace(String name, double velocity) {
super(name, velocity);
public void setDistanceTravelledInLeg(double distanceTravelledInLeg) {
this.distanceTravelledInLeg = distanceTravelledInLeg;
}
/**
* Calculates the bearing of the travel via map coordinates of the raceMarkers
* @return
*/
public double calculateHeading(){
//to be changed to coordinates when used to match reality.
double thetaHat = Math.atan2((currentLeg.getEndCoordinate().getX() - currentPosition.getX()), (currentLeg.getEndCoordinate().getY() - currentPosition.getY()));
double thetaHat = Math.atan2((currentLeg.getEndGraphCoordinate().getLatitude() - currentLeg.getStartGraphCoordinate().getLongitude()),
(currentLeg.getEndGraphCoordinate().getLatitude() - currentLeg.getStartGraphCoordinate().getLongitude()));
return thetaHat >= 0 ? Math.toDegrees(thetaHat): Math.toDegrees(thetaHat + 2 * Math.PI);
}
}

@ -1,6 +1,8 @@
package seng302;
package seng302.Model;
import seng302.GPSCoordinate;
import seng302.GraphCoordinate;
import java.sql.Time;
import java.util.concurrent.TimeUnit;
/**
@ -12,23 +14,31 @@ public class ConstantVelocityRace extends Race {
* @param startingBoats array of boats
* @param marks array of RaceMarkers that the boats need to pass in order to finish the course.
* @param timescale integer that the race is at timescale = 1000ms
* @see seng302.Boat
* @see Boat
* @see Leg
*/
public ConstantVelocityRace(BoatInRace[] startingBoats, Leg[] marks, int timescale) {
super(startingBoats, marks, timescale);
}
protected void updatePosition(BoatInRace boat, int millisecondsElapsed) {
//distanceTravelled = velocity (nm p hr) * time taken to update loop
double distanceTravelled = boat.getVelocity() * TimeUnit.MILLISECONDS.toHours(millisecondsElapsed);
double totalDistanceTravelled = distanceTravelled + boat.getDistanceTravelledInLeg();
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
boat.setCurrentPosition(calculate_position(totalDistanceTravelled, boat.calculateHeading()));
//Calculate new coordinates based on boat's heading for the leg, and distance traveled
}
protected GPSCoordinate calculate_position(double distanceTravelled, double heading) {
//Find new coordinate using current heading and distance
return new GPSCoordinate(-1, -1);
//Calculate new coordinates based on boat's heading for the leg, and distance traveled
}

@ -1,4 +1,7 @@
package seng302;
package seng302.Model;
import seng302.GPSCoordinate;
import seng302.GraphCoordinate;
/**
* Created by cbt24 on 6/03/17.
@ -8,8 +11,9 @@ public class Leg {
private double distance;
private Coordinate startCoordinate;
private Coordinate endCoordinate;
private int legNumber;
private GPSCoordinate startGPSCoordinate;
private GPSCoordinate endGPSCoordinate;
/**
* Leg Initialiser
@ -18,11 +22,12 @@ public class Leg {
* @param latitude Latitude of where it exists in the race (x coordinate)
* @param longitude Longitude of where it exists in the race (y coordinate)
*/
public Leg(String name, double distance, double latitude, double longitude, Coordinate start, Coordinate end) {
public Leg(String name, double distance, double latitude, double longitude, GPSCoordinate start, GPSCoordinate end, int number) {
this.name = name;
this.distance = distance;
this.startCoordinate = start;
this.endCoordinate = end;
this.startGPSCoordinate = start;
this.endGPSCoordinate = end;
this.legNumber = number;
}
/**
@ -54,15 +59,19 @@ public class Leg {
*
* @return the coordinate of the start of the leg )
*/
public Coordinate getStartCoordinate() {
return startCoordinate;
public GPSCoordinate getStartGraphCoordinate() {
return startGPSCoordinate;
}
/**
*
* @return the coordinate of the end of the leg
*/
public Coordinate getEndCoordinate() {
return endCoordinate;
public GPSCoordinate getEndGraphCoordinate() {
return endGPSCoordinate;
}
public int getLegNumber() {
return legNumber;
}
}

@ -1,4 +1,4 @@
package seng302;
package seng302.Model;
import java.util.*;
@ -10,7 +10,7 @@ import java.util.*;
public abstract class Race {
protected BoatInRace[] startingBoats;
protected ArrayList<BoatInRace> finishingBoats = new ArrayList<>();
protected Leg[] marks;
protected Leg[] legs;
protected int timescale = 1000;
private int SLEEP_TIME = 1000; //time in milliseconds to pause in a paced loop
@ -23,7 +23,7 @@ public abstract class Race {
*/
public Race(BoatInRace[] boats, Leg[] marks, int timescale) {
this.startingBoats = boats;
this.marks = marks;
this.legs = marks;
this.timescale = timescale;
}
@ -75,6 +75,16 @@ public abstract class Race {
protected void checkPosition(BoatInRace boat) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
//boat has passed onto new leg
Leg nextLeg = legs[boat.getCurrentLeg().getLegNumber() + 1];
boat.setCurrentLeg(nextLeg);
if (boat.getCurrentLeg().getLegNumber() > legs.length) {
//boat has finished
finishingBoats.add(boat);
}
}
}
/**

@ -1,7 +1,5 @@
package seng302;
import java.util.ArrayList;
/**
* Created by cbt24 on 15/03/17.
*/
@ -17,10 +15,10 @@ public class RaceMap {
* Converts GPS coordinates to coordinates for container
* @param lat GPS latitude
* @param lon GPS longitude
* @return Coordinate (pair of doubles)
* @see seng302.Coordinate
* @return GraphCoordinate (pair of doubles)
* @see GraphCoordinate
*/
public Coordinate convertGPS(double lat, double lon) {
return new Coordinate((int) (width * (lat - x1) / (x2 - x1)), (int) (height - (height * (lon - y1) / (y2 - y1))));
public GraphCoordinate convertGPS(double lat, double lon) {
return new GraphCoordinate((int) (width * (lat - x1) / (x2 - x1)), (int) (height - (height * (lon - y1) / (y2 - y1))));
}
}

@ -1,38 +0,0 @@
//package seng302;
//
//import java.util.Collections;
//import java.util.Random;
//
///**
// * Created by cbt24 on 6/03/17.
// */
//public class RandomisedRace extends Race {
// /**
// * Randomise race, this is a race that the boats cross each mark randomly, and is a extension of Race.
// * @param boats
// * @param marks
// * @param timescale
// * @see seng302.Race
// */
// public RandomisedRace(Boat[] boats, Leg[] marks, int timescale) {
// super(boats, marks, timescale);
// }
//
// /**
// * Generates the Race to be reported when Simulate Race in the Race class is called.
// * @see Race#simulateRace()
// */
// protected void generateRace() {
// Random rand = new Random();
// for (Boat boat: this.boats){
// events.add(new Event(new Leg("Start", 0, 0, 0), boat, 0));
// int prevTime = 0;
// for (Leg raceMarker: marks){
// int time = rand.nextInt(12) + 6;
// events.add(new Event(raceMarker, boat, prevTime + time));
// prevTime += time;
// }
// }
// Collections.sort(events, (o1, o2) -> o1.getTime() - o2.getTime());
// }
//}
Loading…
Cancel
Save