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
+40 -4
View File
@@ -14,6 +14,7 @@ import javafx.beans.property.ReadOnlyLongWrapper;
import javafx.scene.paint.Color;
import seng302.gameServer.GameState;
import seng302.model.mark.CompoundMark;
import seng302.utilities.GeoUtility;
/**
* Yacht class for the racing boat.
@@ -110,7 +111,15 @@ public class Yacht {
}
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) {
@@ -215,12 +224,16 @@ public class Yacht {
public Integer getSourceId() {
//@TODO Remove and merge with Creating Game Loop
if (sourceId == null) return 0;
if (sourceId == null) {
return 0;
}
return sourceId;
}
public String getHullID() {
if (hullID == null) return "";
if (hullID == null) {
return "";
}
return hullID;
}
@@ -233,7 +246,9 @@ public class Yacht {
}
public String getCountry() {
if (country == null) return "";
if (country == null) {
return "";
}
return country;
}
@@ -410,4 +425,25 @@ public class Yacht {
public void setLocation(GeoPoint 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;
}
}