Re-implemented collision tests.

#test #bug
This commit is contained in:
Calum
2017-08-16 01:17:40 +12:00
parent 720ce0ae5b
commit dc8baa09a3
2 changed files with 32 additions and 59 deletions
@@ -43,10 +43,9 @@ public class GameState implements Runnable {
public static Double ROUNDING_DISTANCE = 50d; // TODO: 14/08/17 wmu16 - Look into this value further public static Double ROUNDING_DISTANCE = 50d; // TODO: 14/08/17 wmu16 - Look into this value further
public static final Double MARK_COLLISION_DISTANCE = 15d; public static final Double MARK_COLLISION_DISTANCE = 15d;
public static final Double YACHT_COLLISION_DISTANCE = 25.0; public static final Double YACHT_COLLISION_DISTANCE = 25.0;
private static final Double BOUNCE_DISTANCE_MARK = 20.0; public static final Double BOUNCE_DISTANCE_MARK = 20.0;
private static final Double BOUNCE_DISTANCE_YACHT = 30.0; public static final Double BOUNCE_DISTANCE_YACHT = 30.0;
private static final Integer COLLISION_UPDATE_INTERVAL = 100; public static final Double COLLISION_VELOCITY_PENALTY = 0.3;
private static final Double COLLISION_VELOCITY_PENALTY = 0.3;
private static Long previousUpdateTime; private static Long previousUpdateTime;
public static Double windDirection; public static Double windDirection;
@@ -244,7 +243,7 @@ public class GameState implements Runnable {
} }
private void checkForCollision(ServerYacht serverYacht) { public static void checkForCollision(ServerYacht serverYacht) {
ServerYacht collidedYacht = checkCollision(serverYacht); ServerYacht collidedYacht = checkCollision(serverYacht);
if (collidedYacht != null) { if (collidedYacht != null) {
GeoPoint originalLocation = serverYacht.getLocation(); GeoPoint originalLocation = serverYacht.getLocation();
@@ -259,7 +258,7 @@ public class GameState implements Runnable {
); );
collidedYacht.setCurrentVelocity( collidedYacht.setCurrentVelocity(
collidedYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY collidedYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
);; );
notifyMessageListeners( notifyMessageListeners(
new YachtEventCodeMessage(serverYacht.getSourceId()) new YachtEventCodeMessage(serverYacht.getSourceId())
); );
@@ -504,7 +503,7 @@ public class GameState implements Runnable {
} }
private Mark markCollidedWith(ServerYacht yacht) { private static Mark markCollidedWith(ServerYacht yacht) {
Set<Mark> marksInRace = GameState.getMarks(); Set<Mark> marksInRace = GameState.getMarks();
for (Mark mark : marksInRace) { for (Mark mark : marksInRace) {
if (GeoUtility.getDistance(yacht.getLocation(), mark) if (GeoUtility.getDistance(yacht.getLocation(), mark)
@@ -520,7 +519,7 @@ public class GameState implements Runnable {
* *
* @return The boats new position * @return The boats new position
*/ */
private GeoPoint calculateBounceBack(ServerYacht yacht, GeoPoint collidedWith, Double bounceDistance) { private static GeoPoint calculateBounceBack(ServerYacht yacht, GeoPoint collidedWith, Double bounceDistance) {
Double heading = GeoUtility.getBearing(yacht.getLocation(), collidedWith); Double heading = GeoUtility.getBearing(yacht.getLocation(), collidedWith);
// Invert heading // Invert heading
heading -= 180; heading -= 180;
@@ -534,7 +533,7 @@ public class GameState implements Runnable {
* *
* @return yacht to compare to all other yachts. * @return yacht to compare to all other yachts.
*/ */
private ServerYacht checkCollision(ServerYacht yacht) { private static ServerYacht checkCollision(ServerYacht yacht) {
for (ServerYacht otherYacht : GameState.getYachts().values()) { for (ServerYacht otherYacht : GameState.getYachts().values()) {
if (otherYacht != yacht) { if (otherYacht != yacht) {
@@ -562,7 +561,7 @@ public class GameState implements Runnable {
notifyMessageListeners(markRoundingMessage); notifyMessageListeners(markRoundingMessage);
} }
private void notifyMessageListeners(Message message) { private static void notifyMessageListeners(Message message) {
for (NewMessageListener mpl : markListeners) { for (NewMessageListener mpl : markListeners) {
mpl.notify(message); mpl.notify(message);
} }
@@ -29,65 +29,39 @@ public class UpdateYachtTest {
public void testUpdateYachtWithCollision() { public void testUpdateYachtWithCollision() {
// Yacht 1 heading towards 90 degrees heading // Yacht 1 heading towards 90 degrees heading
yacht1.setLocation(geoPoint1); yacht1.setLocation(geoPoint1);
yacht1.setHeading(90.0);
yacht1.setCurrentVelocity(1000d);
// Yacht 2 heading towards 270 degrees heading // Yacht 2 heading towards 270 degrees heading
yacht2.setLocation(geoPoint2); yacht2.setLocation(geoPoint1);
yacht2.setHeading(270.0);
yacht1.setCurrentVelocity(1000d);
// Start yacht 1 and rest yacht 2 // Start yacht 1 and rest yacht 2
if (!yacht1.getSailIn()) { if (!yacht1.getSailIn()) {
yacht1.toggleSailIn(); yacht1.toggleSailIn();
} }
GameState.checkForCollision(yacht1);
for (int i = 0; i < 6; i++) { double moved = GeoUtility.getDistance(yacht1.getLocation(), geoPoint1);
// Assert.assertEquals(GameState.BOUNCE_DISTANCE_YACHT, moved, 0.1);
// // Making sure boat is moving
double moved = GeoUtility.getDistance(yacht1.getLocation(), geoPoint1);
Assert.assertTrue(moved > 0);
//
// // Making sure no collision
// Double distance = GeoUtility.getDistance(yacht1.getLocation(), geoPoint2);
//
// Assert.assertTrue(distance > Math.min(Yacht.MARK_COLLISION_DISTANCE, Yacht.YACHT_COLLISION_DISTANCE));
}
} }
@Test @Test
public void testUpdateYachtWithoutCollision() { public void testUpdateYachtWithoutCollision() {
// // Yacht 1 heading towards 90 degrees heading // Yacht 1 heading towards 90 degrees heading
// yacht1.setLocation(geoPoint1); yacht1.setLocation(geoPoint1);
// yacht1.updateLocation(geoPoint1.getLat(), geoPoint1.getLng(), 90.0, 5.0);
// // Yacht 2 heading towards 270 degrees heading
// // Yacht 2 heading towards 90 degrees heading yacht2.setLocation(geoPoint2);
// yacht2.setLocation(geoPoint2);
// yacht2.updateLocation(geoPoint2.getLat(), geoPoint2.getLng(), 90.0, 5.0); // Start yacht 1 and rest yacht 2
// if (!yacht1.getSailIn()) {
// // Start yacht 1 and yacht 2 yacht1.toggleSailIn();
// if (!yacht1.getSailIn()) { }
// yacht1.toggleSailIn(); GameState.checkForCollision(yacht1);
// } Assert.assertTrue(
// if (!yacht2.getSailIn()) { GameState.YACHT_COLLISION_DISTANCE < GeoUtility.getDistance(geoPoint1, geoPoint2
// yacht2.toggleSailIn(); )
// } ); //Check that yachts are actually far enough apart for no collision.
// Assert.assertEquals(geoPoint1.getLat(), yacht1.getLocation().getLat(), 0.001);
// double previousDistance1 = 0; Assert.assertEquals(geoPoint1.getLng(), yacht1.getLocation().getLng(), 0.001);
// double previousDistance2 = 0; Assert.assertEquals(geoPoint2.getLat(), yacht1.getLocation().getLat(), 0.001);
// Assert.assertEquals(geoPoint2.getLng(), yacht1.getLocation().getLng(), 0.001);
// for (int i = 0; i < 6; i++) {
// yacht1.update((long) 1000);
// yacht2.update((long) 1000);
//
// // Making sure boat is moving
// double yachtMoved1 = GeoUtility.getDistance(yacht1.getLocation(), geoPoint1);
// Assert.assertTrue(yachtMoved1 > previousDistance1);
// previousDistance1 = yachtMoved1;
//
// double yachtMoved2 = GeoUtility.getDistance(yacht2.getLocation(), geoPoint2);
// Assert.assertTrue(yachtMoved2 > previousDistance2);
// previousDistance2 = yachtMoved2;
// }
} }
} }