Boat should move towards optimal angle upwind and downwind when pressing spacebar (VMG)

#story[988]
This commit is contained in:
Alistair McIntyre
2017-07-26 20:12:57 +12:00
parent 72a45f5984
commit 7db716f51c
4 changed files with 47 additions and 17 deletions
@@ -1,10 +1,11 @@
package seng302.gameServer; package seng302.gameServer;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import seng302.client.ClientPacketParser; import seng302.client.ClientPacketParser;
import seng302.models.Player; import seng302.models.Player;
import seng302.models.Yacht; import seng302.models.Yacht;
import seng302.server.messages.BoatActionType; import seng302.server.messages.BoatActionType;
@@ -27,7 +28,7 @@ public class GameState implements Runnable {
private static GameStages currentStage; private static GameStages currentStage;
public GameState(String hostIpAddress) { public GameState(String hostIpAddress) {
windDirection = 170d; windDirection = 180d;
windSpeed = 10000d; windSpeed = 10000d;
yachts = new HashMap<>(); yachts = new HashMap<>();
players = new ArrayList<>(); players = new ArrayList<>();
@@ -105,7 +106,6 @@ public class GameState implements Runnable {
case VMG: case VMG:
playerYacht.turnToVMG(); playerYacht.turnToVMG();
// System.out.println("Snapping to VMG"); // System.out.println("Snapping to VMG");
// TODO: 22/07/17 wmu16 - Add in the vmg calculation code here
break; break;
case SAILS_IN: case SAILS_IN:
playerYacht.toggleSailIn(); playerYacht.toggleSailIn();
+1 -2
View File
@@ -4,8 +4,6 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
/** /**
@@ -182,4 +180,5 @@ public final class PolarTable {
return closestAngle; return closestAngle;
} }
} }
+40 -9
View File
@@ -4,6 +4,7 @@ import static seng302.utilities.GeoUtility.getGeoCoordinate;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.HashMap;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import seng302.client.ClientPacketParser; import seng302.client.ClientPacketParser;
import seng302.controllers.RaceViewController; import seng302.controllers.RaceViewController;
@@ -136,9 +137,9 @@ public class Yacht {
if (velocity > 0d) { if (velocity > 0d) {
if (maxBoatSpeed != 0d) { if (maxBoatSpeed != 0d) {
velocity -= maxBoatSpeed / 25; velocity -= maxBoatSpeed / 600;
} else { } else {
velocity -= velocity / 25; velocity -= velocity / 100;
} }
if (velocity < 0) { if (velocity < 0) {
velocity = 0d; velocity = 0d;
@@ -164,8 +165,7 @@ public class Yacht {
} }
public void tackGybe(Double windDirection) { public void tackGybe(Double windDirection) {
Double normalizedHeading = heading - GameState.windDirection; Double normalizedHeading = normalizeHeading();
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360);
adjustHeading(-2 * normalizedHeading); adjustHeading(-2 * normalizedHeading);
} }
@@ -174,8 +174,7 @@ public class Yacht {
} }
public void turnUpwind() { public void turnUpwind() {
Double normalizedHeading = heading - GameState.windDirection; Double normalizedHeading = normalizeHeading();
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360);
if (normalizedHeading == 0) { if (normalizedHeading == 0) {
if (lastHeading < 180) { if (lastHeading < 180) {
adjustHeading(-TURN_STEP); adjustHeading(-TURN_STEP);
@@ -196,8 +195,7 @@ public class Yacht {
} }
public void turnDownwind() { public void turnDownwind() {
Double normalizedHeading = heading - GameState.windDirection; Double normalizedHeading = normalizeHeading();
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360);
if (normalizedHeading == 0) { if (normalizedHeading == 0) {
if (lastHeading < 180) { if (lastHeading < 180) {
adjustHeading(TURN_STEP); adjustHeading(TURN_STEP);
@@ -218,10 +216,43 @@ public class Yacht {
} }
public void turnToVMG() { public void turnToVMG() {
// TODO: 25/07/17 wmu16 - Fix this so it grabs the optimal value from the optimal Polar Double normalizedHeading = normalizeHeading();
Double optimalHeading;
HashMap<Double, Double> optimalPolarMap;
if (normalizedHeading >= 90 && normalizedHeading <= 270) { // Downwind
optimalPolarMap = PolarTable.getOptimalDownwindVMG(GameState.getWindSpeedKnots());
optimalHeading = optimalPolarMap.keySet().iterator().next();
} else {
optimalPolarMap = PolarTable.getOptimalUpwindVMG(GameState.getWindSpeedKnots());
optimalHeading = optimalPolarMap.keySet().iterator().next();
}
// Take optimal heading and turn into correct
optimalHeading =
optimalHeading + (double) Math.floorMod(GameState.getWindDirection().longValue(), 360L);
turnTowardsHeading(optimalHeading);
} }
private void turnTowardsHeading(Double newHeading) {
System.out.println(newHeading);
if (heading < 90 && newHeading > 270) {
adjustHeading(-TURN_STEP);
} else {
if (heading < newHeading) {
adjustHeading(TURN_STEP);
} else {
adjustHeading(-TURN_STEP);
}
}
}
private Double normalizeHeading() {
Double normalizedHeading = heading - GameState.windDirection;
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360L);
return normalizedHeading;
}
public String getBoatType() { public String getBoatType() {
return boatType; return boatType;
+1 -1
View File
@@ -1,4 +1,4 @@
Tws,Twa0,Bsp0,Twa1,Bsp1,UpTwa,UpBsp,Twa2,Bsp2,Twa3,Bsp3,Twa4,Bsp4,Twa5,Bsp5,Twa6,Bsp6,DnTwa,DnBsp,Twa7,Bsp7 Tws,Twa0,Bsp0,Twa1,Bsp1,UpTwa,UpBsp,Twa2,Bsp2,Twa3,Bsp3,Twa4,Bsp4,Twa5,Bsp5,Twa6,Bsp6,DnTwa,DnBsp,Twa7,Bsp7
4,0,0,30,4,45,8,60,9,75,10,90,10,115,10,145,10,155,10,175,4 4,0,0,30,4,45,8,60,9,75,10,90,10,115,10,145,10,155,10,175,4
8,0,0,30,7,43,10,60,11,75,11,90,11,115,12,145,12,153,12,175,10 8,0,0,30,7,43,10,60,11,75,11,90,11,115,12,145,12,153,12,175,10
12,0,0,30,11,43,14.4,60,16,75,20,90,23,115,24,145,23,153,21.6,175,14 12,0,0,30,11,43,14.4,60,16,75,20,90,23,115,24,145,23,153,21.6,175,14
1 Tws Twa0 Bsp0 Twa1 Bsp1 UpTwa UpBsp Twa2 Bsp2 Twa3 Bsp3 Twa4 Bsp4 Twa5 Bsp5 Twa6 Bsp6 DnTwa DnBsp Twa7 Bsp7
2 4 0 0 30 4 45 8 60 9 75 10 90 10 115 10 145 10 155 10 175 4
3 8 0 0 30 7 43 10 60 11 75 11 90 11 115 12 145 12 153 12 175 10
4 12 0 0 30 11 43 14.4 60 16 75 20 90 23 115 24 145 23 153 21.6 175 14