Created a simple collision detection by iterating each boats per update. Working but sequential checks can be costly.

#story[1117]
This commit is contained in:
Zhi You Tan
2017-08-07 12:01:26 +12:00
parent a4b22190c0
commit 79105a1bdc
+44 -8
View File
@@ -14,6 +14,7 @@ import javafx.beans.property.ReadOnlyLongWrapper;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import seng302.gameServer.GameState; import seng302.gameServer.GameState;
import seng302.model.mark.CompoundMark; import seng302.model.mark.CompoundMark;
import seng302.utilities.GeoUtility;
/** /**
* Yacht class for the racing boat. * Yacht class for the racing boat.
@@ -110,7 +111,15 @@ public class Yacht {
} }
Double metersCovered = velocity * secondsElapsed; Double metersCovered = velocity * secondsElapsed;
location = getGeoCoordinate(location, heading, metersCovered); GeoPoint calculatedPoint = getGeoCoordinate(location, heading, metersCovered);
// Collision detection. Update boat only if no collision.
Yacht collidedYacht = checkCollision(calculatedPoint);
if (collidedYacht != null) {
// System.out.println("Collision of boat " + this.getSourceId() + " and " + collidedYacht.getSourceId());
} else {
location = calculatedPoint;
}
} }
public void adjustHeading(Double amount) { public void adjustHeading(Double amount) {
@@ -215,12 +224,16 @@ public class Yacht {
public Integer getSourceId() { public Integer getSourceId() {
//@TODO Remove and merge with Creating Game Loop //@TODO Remove and merge with Creating Game Loop
if (sourceId == null) return 0; if (sourceId == null) {
return 0;
}
return sourceId; return sourceId;
} }
public String getHullID() { public String getHullID() {
if (hullID == null) return ""; if (hullID == null) {
return "";
}
return hullID; return hullID;
} }
@@ -233,7 +246,9 @@ public class Yacht {
} }
public String getCountry() { public String getCountry() {
if (country == null) return ""; if (country == null) {
return "";
}
return country; return country;
} }
@@ -321,7 +336,7 @@ public class Yacht {
this.nextMark = nextMark; this.nextMark = nextMark;
} }
public CompoundMark getNextMark(){ public CompoundMark getNextMark() {
return nextMark; return nextMark;
} }
@@ -366,7 +381,7 @@ public class Yacht {
this.timeSinceLastMarkProperty.set(timeSinceLastMark); this.timeSinceLastMarkProperty.set(timeSinceLastMark);
} }
public ReadOnlyLongProperty timeSinceLastMarkProperty () { public ReadOnlyLongProperty timeSinceLastMarkProperty() {
return timeSinceLastMarkProperty.getReadOnlyProperty(); return timeSinceLastMarkProperty.getReadOnlyProperty();
} }
@@ -392,7 +407,7 @@ public class Yacht {
this.velocity = velocity; this.velocity = velocity;
} }
public void updateLocation (double lat, double lon, double heading, double velocity) { public void updateLocation(double lat, double lon, double heading, double velocity) {
this.lat = lat; this.lat = lat;
this.lon = lon; this.lon = lon;
this.heading = heading; this.heading = heading;
@@ -403,11 +418,32 @@ public class Yacht {
} }
} }
public void addLocationListener (YachtLocationListener listener) { public void addLocationListener(YachtLocationListener listener) {
locationListeners.add(listener); locationListeners.add(listener);
} }
public void setLocation(GeoPoint geoPoint) { public void setLocation(GeoPoint geoPoint) {
location = geoPoint; location = geoPoint;
} }
/**
* Collision detection which iterates through all the yachts and check if any yacht collided
* with this yacht. Return collided yacht or null if no collision.
*
* @param calculatedPoint point will the yacht will move next
* @return yacht which collided with this yacht
*/
private Yacht checkCollision(GeoPoint calculatedPoint) {
Double COLLISIONFACTOR = 25.0; // Collision detection in meters
for (Yacht yacht : GameState.getYachts().values()) {
if (yacht != this) {
Double distance = GeoUtility.getDistance(yacht.getLocation(), calculatedPoint);
if (distance < COLLISIONFACTOR) {
return yacht;
}
}
}
return null;
}
} }