Ray casting now only supports distance specification.
#story[1100]
This commit is contained in:
@@ -207,12 +207,13 @@ public class MockBoat extends Boat implements Collider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean rayCast(MockBoat boat, Collision collision) {
|
public boolean rayCast(MockBoat boat, double distance) {
|
||||||
double distance = GPSCoordinate.calculateDistanceMeters(boat.getCurrentPosition(), this.getCurrentPosition());
|
double actualDistance = GPSCoordinate.calculateDistanceMeters(boat.getCurrentPosition(), this.getCurrentPosition());
|
||||||
Bearing bearing = GPSCoordinate.calculateBearing(boat.getCurrentPosition(), this.getCurrentPosition());
|
Bearing absolute = GPSCoordinate.calculateBearing(boat.getCurrentPosition(), this.getCurrentPosition());
|
||||||
|
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
|
||||||
|
|
||||||
if(distance <= collision.getDistance() && (int)bearing.degrees() == (int)collision.getBearing().degrees()) {
|
if(actualDistance <= distance) {
|
||||||
onCollisionEnter(collision);
|
onCollisionEnter(new Collision(relative, actualDistance));
|
||||||
return true;
|
return true;
|
||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
@@ -220,7 +221,7 @@ public class MockBoat extends Boat implements Collider {
|
|||||||
@Override
|
@Override
|
||||||
public boolean rayCast(MockBoat boat) {
|
public boolean rayCast(MockBoat boat) {
|
||||||
if(boat != this) {
|
if(boat != this) {
|
||||||
return rayCast(boat, new Collision(Bearing.fromDegrees(0), 100));
|
return rayCast(boat, 100);
|
||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ import mock.model.MockBoat;
|
|||||||
*/
|
*/
|
||||||
public interface Collider {
|
public interface Collider {
|
||||||
/**
|
/**
|
||||||
* Indicates whether a ray cast from a boat to a target collider triggers a collision with the specified parameters.
|
* Indicates whether a ray cast from a boat to a target collider is within the specified length.
|
||||||
* @param boat potentially colliding with target
|
* @param boat potentially colliding with target
|
||||||
* @param collision parameters of valid collision
|
* @param distance distance for valid collision
|
||||||
* @return whether or not a collision has occurred
|
* @return whether or not a collision has occurred
|
||||||
*/
|
*/
|
||||||
boolean rayCast(MockBoat boat, Collision collision);
|
boolean rayCast(MockBoat boat, double distance);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether a ray cast from a boat to a target collider triggers a collision. Parameters are set by the object.
|
* Indicates whether a ray cast from a boat to a target collider triggers a collision. Distance is set by the object.
|
||||||
* @param boat potentially colliding with target
|
* @param boat potentially colliding with target
|
||||||
* @return whether or not a collision has occurred
|
* @return whether or not a collision has occurred
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public class ColliderRegistry implements Collider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean rayCast(MockBoat boat, Collision collision) {
|
public boolean rayCast(MockBoat boat, double distance) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -146,27 +146,28 @@ public class CompoundMark implements Collider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean rayCast(MockBoat boat, Collision collision) {
|
public boolean rayCast(MockBoat boat, double distance) {
|
||||||
GPSCoordinate position = boat.getCurrentPosition();
|
GPSCoordinate position = boat.getCurrentPosition();
|
||||||
double distance = GPSCoordinate.calculateDistanceMeters(position, closestMark(position).getPosition());
|
double actualDistance = GPSCoordinate.calculateDistanceMeters(position, closestMark(position).getPosition());
|
||||||
// Compass direction of collider
|
// Compass direction of collider
|
||||||
Bearing absolute = Bearing.fromAzimuth(GPSCoordinate.calculateAzimuth(position, closestMark(position).getPosition()));
|
Bearing absolute = Bearing.fromAzimuth(GPSCoordinate.calculateAzimuth(position, closestMark(position).getPosition()));
|
||||||
// Direction of collider from heading
|
// Direction of collider from heading
|
||||||
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
|
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
|
||||||
|
|
||||||
if(distance <= collision.getDistance() && (int)relative.degrees() == (int)collision.getBearing().degrees()) {
|
if(actualDistance <= distance) {
|
||||||
onCollisionEnter(collision);
|
onCollisionEnter(new Collision(relative, distance));
|
||||||
return true;
|
return true;
|
||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean rayCast(MockBoat boat) {
|
public boolean rayCast(MockBoat boat) {
|
||||||
return rayCast(boat, new Collision(Bearing.fromDegrees(0), 100));
|
return rayCast(boat, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCollisionEnter(Collision e) {
|
public void onCollisionEnter(Collision e) {
|
||||||
System.out.println(e.getBearing().degrees());
|
if(e.getBearing().degrees() < 90) System.out.println("Starboard");
|
||||||
|
else if(e.getBearing().degrees() > 270) System.out.println("Port");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
public class MockBoatTest {
|
public class MockBoatTest {
|
||||||
private MockBoat boat;
|
private MockBoat boat;
|
||||||
private CompoundMark ahead;
|
private CompoundMark near;
|
||||||
private CompoundMark behind;
|
private CompoundMark far;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -21,17 +21,17 @@ public class MockBoatTest {
|
|||||||
boat.setCurrentPosition(new GPSCoordinate(0,0));
|
boat.setCurrentPosition(new GPSCoordinate(0,0));
|
||||||
boat.setBearing(Bearing.fromDegrees(180));
|
boat.setBearing(Bearing.fromDegrees(180));
|
||||||
|
|
||||||
ahead = new CompoundMark(0, "Ahead", new Mark(0, "Ahead", new GPSCoordinate(-.0001, 0)));
|
near = new CompoundMark(0, "Near", new Mark(0, "Near", new GPSCoordinate(-.0001, 0)));
|
||||||
behind = new CompoundMark(1, "Behind", new Mark(0, "Behind", new GPSCoordinate(.0001, 0)));
|
far = new CompoundMark(1, "Far", new Mark(0, "Far", new GPSCoordinate(.001, 0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void markAheadOfBoat0() {
|
public void nearMarkWithin100m() {
|
||||||
assertTrue(ahead.rayCast(boat, new Collision(Bearing.fromDegrees(0), 100)));
|
assertTrue(near.rayCast(boat, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void markBehindBoat180() {
|
public void farMarkBeyond100m() {
|
||||||
assertTrue(behind.rayCast(boat, new Collision(Bearing.fromDegrees(180), 100)));
|
assertFalse(far.rayCast(boat, 100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user