- Boat is now collider - Collider subclasses specify their own collision ray and collision handler #story[1100]main
parent
f44929f376
commit
79f3c41d45
@ -1,29 +1,43 @@
|
|||||||
package mock.model.collider;
|
package mock.model.collider;
|
||||||
|
|
||||||
import mock.model.MockBoat;
|
import shared.model.Bearing;
|
||||||
|
import shared.model.Boat;
|
||||||
|
import shared.model.GPSCoordinate;
|
||||||
|
import shared.model.Locatable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for all objects sensitive to collision in a race.
|
* Interface for all objects sensitive to collision in a race.
|
||||||
*/
|
*/
|
||||||
public interface Collider {
|
public abstract class Collider implements Locatable {
|
||||||
/**
|
/**
|
||||||
* Indicates whether a ray cast from a boat to a target collider is within the specified length.
|
* 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 distance distance for 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, double distance);
|
public boolean rayCast(Boat boat, double distance) {
|
||||||
|
double actualDistance = GPSCoordinate.calculateDistanceMeters(boat.getPosition(), this.getPosition());
|
||||||
|
// Compass direction of collider
|
||||||
|
Bearing absolute = Bearing.fromAzimuth(GPSCoordinate.calculateAzimuth(boat.getPosition(), this.getPosition()));
|
||||||
|
// Direction of collider from heading
|
||||||
|
Bearing relative = Bearing.fromDegrees(absolute.degrees() - boat.getBearing().degrees());
|
||||||
|
|
||||||
|
if(actualDistance <= distance) {
|
||||||
|
onCollisionEnter(new Collision(relative, distance));
|
||||||
|
return true;
|
||||||
|
} else return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether a ray cast from a boat to a target collider triggers a collision. Distance is 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
|
||||||
*/
|
*/
|
||||||
boolean rayCast(MockBoat boat);
|
public abstract boolean rayCast(Boat boat);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a collision event
|
* Handle a collision event
|
||||||
* @param e details of collision
|
* @param e details of collision
|
||||||
*/
|
*/
|
||||||
void onCollisionEnter(Collision e);
|
public abstract void onCollisionEnter(Collision e);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue