40. [V] As Caleb or Gemma I'd like to zoom the view to follow a boat (my boat) around the course (revised). Note: in this story we will select a boat and zoom the view to place the boat at the center of a display that shows only the boat's immediate surrounds. The default option will be to zoom on the player's boat. Acceptance criteria: - Any particular boat can be chosen (at any time during the race.) - When a boat is selected for zooming, the view should change to show the boat at the center, with the scale of the view increased. - The view should remain this way until the boat is unselected, at which point it should revert to the usual display. - Other information (e.g., the position list) may be removed from the zoomed view. - Zoomed view follows selected boat as a third person mode - There must be a keyboard option to zoom in on the player's boat_ - The zoom amount can be fixed (jump between zoom in and zoom out) or scrollable (like control-mousewheel scrolling.) See merge request !38main
commit
14aaf26b73
@ -0,0 +1,65 @@
|
||||
package visualiser.layout;
|
||||
|
||||
import javafx.scene.shape.Shape3D;
|
||||
import javafx.scene.transform.Rotate;
|
||||
import javafx.scene.transform.Translate;
|
||||
|
||||
/**
|
||||
* Wrapper for controlling the position and heading of rendered 3D models.
|
||||
*/
|
||||
public class Subject3D {
|
||||
/**
|
||||
* Rendered mesh
|
||||
*/
|
||||
private Shape3D mesh;
|
||||
|
||||
/**
|
||||
* Position translation updated by state listeners
|
||||
*/
|
||||
private Translate position;
|
||||
|
||||
/**
|
||||
* Heading rotation updated by state listeners
|
||||
*/
|
||||
private Rotate heading;
|
||||
|
||||
/**
|
||||
* Constructor for view subject wrapper
|
||||
* @param mesh to be rendered
|
||||
*/
|
||||
public Subject3D(Shape3D mesh) {
|
||||
this.mesh = mesh;
|
||||
this.position = new Translate();
|
||||
this.heading = new Rotate(0, Rotate.Y_AXIS);
|
||||
|
||||
this.mesh.getTransforms().addAll(position, heading, new Rotate(90, Rotate.X_AXIS), new Rotate(180, Rotate.Y_AXIS));
|
||||
}
|
||||
|
||||
public Shape3D getMesh() {
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public Translate getPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public Rotate getHeading() {
|
||||
return heading;
|
||||
}
|
||||
|
||||
public void setX(double x) {
|
||||
position.setX(x);
|
||||
}
|
||||
|
||||
public void setY(double y) {
|
||||
position.setY(y);
|
||||
}
|
||||
|
||||
public void setZ(double z) {
|
||||
position.setZ(z);
|
||||
}
|
||||
|
||||
public void setHeading(double angle) {
|
||||
heading.setAngle(angle);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,260 @@
|
||||
package visualiser.layout;
|
||||
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.PerspectiveCamera;
|
||||
import javafx.scene.SubScene;
|
||||
import javafx.scene.input.PickResult;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Shape3D;
|
||||
import javafx.scene.transform.Rotate;
|
||||
import javafx.scene.transform.Translate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Control for rendering 3D objects visible through a PerspectiveCamera. Implements Adapter Pattern to
|
||||
* interface with camera, and allows clients to add shapes to the scene. All scenes contain sea plane and
|
||||
* sky box, whose textures are set with special methods.
|
||||
*/
|
||||
public class View3D extends Pane {
|
||||
/**
|
||||
* Container for group and camera
|
||||
*/
|
||||
private SubScene scene;
|
||||
/**
|
||||
* Observable list of renderable items
|
||||
*/
|
||||
private ObservableList<Subject3D> items;
|
||||
/**
|
||||
* Map for selecting Subject3D from Shape3D
|
||||
*/
|
||||
private Map<Shape3D, Subject3D> selectionMap;
|
||||
/**
|
||||
* Subject tracked by camera
|
||||
*/
|
||||
private Subject3D target;
|
||||
/**
|
||||
* Rendering container for shapes
|
||||
*/
|
||||
private Group world;
|
||||
/**
|
||||
* Near limit of view frustum
|
||||
*/
|
||||
private double nearClip;
|
||||
/**
|
||||
* Far limit of view frustum
|
||||
*/
|
||||
private double farClip;
|
||||
/**
|
||||
* Camera origin
|
||||
*/
|
||||
private Translate pivot;
|
||||
/**
|
||||
* Distance of camera from pivot point
|
||||
*/
|
||||
private Translate distance;
|
||||
/**
|
||||
* Angle along ground between z-axis and camera
|
||||
*/
|
||||
private Rotate yaw;
|
||||
/**
|
||||
* Angle between ground plane and camera direction
|
||||
*/
|
||||
private Rotate pitch;
|
||||
/**
|
||||
* Single listener for subject heading changes
|
||||
*/
|
||||
private ChangeListener<? super Number> pivotHeading = (o, prev, curr) -> yaw.setAngle((double)curr);
|
||||
/**
|
||||
* Single listener for subject position (x) changes
|
||||
*/
|
||||
private ChangeListener<? super Number> pivotX = (o, prev, curr) -> pivot.setX((double)curr);
|
||||
/**
|
||||
* Single listener for subject position (y) changes
|
||||
*/
|
||||
private ChangeListener<? super Number> pivotY = (o, prev, curr) -> pivot.setY((double)curr);
|
||||
/**
|
||||
* Single listener for subject position (z) changes
|
||||
*/
|
||||
private ChangeListener<? super Number> pivotZ = (o, prev, curr) -> pivot.setZ((double)curr);
|
||||
/**
|
||||
* Distance to switch from third person to bird's eye
|
||||
*/
|
||||
private double THIRD_PERSON_LIMIT = 100;
|
||||
|
||||
/**
|
||||
* Default constructor for View3D. Sets up Scene and PerspectiveCamera.
|
||||
*/
|
||||
public View3D() {
|
||||
this.world = new Group();
|
||||
this.selectionMap = new HashMap<>();
|
||||
this.target = null;
|
||||
this.scene = new SubScene(world, 300, 300);
|
||||
|
||||
scene.widthProperty().bind(this.widthProperty());
|
||||
scene.heightProperty().bind(this.heightProperty());
|
||||
scene.setFill(new Color(0.2, 0.6, 1, 1));
|
||||
|
||||
scene.setCamera(buildCamera());
|
||||
|
||||
this.getChildren().add(scene);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up camera view frustum and binds transformations
|
||||
* @return perspective camera
|
||||
*/
|
||||
private PerspectiveCamera buildCamera() {
|
||||
PerspectiveCamera camera = new PerspectiveCamera(true);
|
||||
|
||||
// Set up view frustum
|
||||
nearClip = 0.1;
|
||||
farClip = 3000.0;
|
||||
camera.setNearClip(nearClip);
|
||||
camera.setFarClip(farClip);
|
||||
|
||||
// Set up transformations
|
||||
pivot = new Translate();
|
||||
distance = new Translate();
|
||||
yaw = new Rotate(0, Rotate.Y_AXIS);
|
||||
pitch = new Rotate(0, Rotate.X_AXIS);
|
||||
camera.getTransforms().addAll(pivot, yaw, pitch, distance);
|
||||
|
||||
return camera;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide the list of subjects to be automatically added or removed from the view as the list
|
||||
* changes.
|
||||
* @param items list managed by client
|
||||
*/
|
||||
public void setItems(ObservableList<Subject3D> items) {
|
||||
this.items = items;
|
||||
this.items.addListener((ListChangeListener<? super Subject3D>) c -> {
|
||||
while(c.next()) {
|
||||
if (c.wasRemoved() || c.wasAdded()) {
|
||||
for (Subject3D shape : c.getRemoved()) {
|
||||
world.getChildren().remove(shape.getMesh());
|
||||
selectionMap.remove(shape.getMesh());
|
||||
}
|
||||
for (Subject3D shape : c.getAddedSubList()) {
|
||||
world.getChildren().add(shape.getMesh());
|
||||
selectionMap.put(shape.getMesh(), shape);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept mouse clicks on subjects in view. The applied listener cannot be removed.
|
||||
*/
|
||||
public void enableTracking() {
|
||||
scene.setOnMousePressed(e -> {
|
||||
PickResult result = e.getPickResult();
|
||||
if(result != null && result.getIntersectedNode() != null && result.getIntersectedNode() instanceof Shape3D) {
|
||||
trackSubject(selectionMap.get(result.getIntersectedNode()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop camera from following the last selected subject
|
||||
*/
|
||||
private void untrackSubject() {
|
||||
if(target != null) {
|
||||
target.getPosition().xProperty().removeListener(pivotX);
|
||||
target.getPosition().yProperty().removeListener(pivotY);
|
||||
target.getPosition().zProperty().removeListener(pivotZ);
|
||||
target.getHeading().angleProperty().removeListener(pivotHeading);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set camera to follow the selected subject
|
||||
* @param subject to track
|
||||
*/
|
||||
private void trackSubject(Subject3D subject) {
|
||||
untrackSubject();
|
||||
target = subject;
|
||||
|
||||
updatePivot(target.getPosition());
|
||||
setYaw(target.getHeading().getAngle());
|
||||
|
||||
target.getPosition().xProperty().addListener(pivotX);
|
||||
target.getPosition().yProperty().addListener(pivotY);
|
||||
target.getPosition().zProperty().addListener(pivotZ);
|
||||
target.getHeading().angleProperty().addListener(pivotHeading);
|
||||
|
||||
this.setDistance(THIRD_PERSON_LIMIT);
|
||||
this.setPitch(20);
|
||||
}
|
||||
|
||||
public void setNearClip(double nearClip) {
|
||||
this.nearClip = nearClip;
|
||||
}
|
||||
|
||||
public void setFarClip(double farClip) {
|
||||
this.farClip = farClip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the coordinates of the camera pivot once.
|
||||
* @param pivot source of coordinates
|
||||
*/
|
||||
public void updatePivot(Translate pivot) {
|
||||
this.pivot.setX(pivot.getX());
|
||||
this.pivot.setY(pivot.getY());
|
||||
this.pivot.setZ(pivot.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set distance of camera from pivot
|
||||
* @param distance in units
|
||||
*/
|
||||
public void setDistance(double distance) {
|
||||
this.distance.setZ(-distance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds delta to current distance and changes camera mode if applicable.
|
||||
* Third person limit specifies the distance at which a third person camera
|
||||
* switches to bird's-eye, remaining focused on the same position.
|
||||
* @param delta amount to change distance by
|
||||
*/
|
||||
public void updateDistance(double delta) {
|
||||
double distance = -this.distance.getZ() + delta;
|
||||
|
||||
if(distance <= 0) {
|
||||
this.setDistance(0);
|
||||
} else if(distance > THIRD_PERSON_LIMIT) {
|
||||
untrackSubject();
|
||||
this.setYaw(0);
|
||||
this.setPitch(60);
|
||||
this.setDistance(distance);
|
||||
} else {
|
||||
this.setDistance(distance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set angle of camera from z-axis along ground
|
||||
* @param yaw in degrees
|
||||
*/
|
||||
public void setYaw(double yaw) {
|
||||
this.yaw.setAngle(yaw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set elevation of camera
|
||||
* @param pitch in degrees
|
||||
*/
|
||||
public void setPitch(double pitch) {
|
||||
this.pitch.setAngle(-pitch);
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
package visualiser.model;
|
||||
|
||||
import com.interactivemesh.jfx.importer.Importer;
|
||||
import javafx.scene.layout.Pane;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 29/08/17.
|
||||
*/
|
||||
public class BoatDisplay3D extends Pane {
|
||||
|
||||
|
||||
public BoatDisplay3D(String filePath){
|
||||
super();
|
||||
// Shape3D
|
||||
// this.getChildren().add();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,145 +0,0 @@
|
||||
package visualiser.model;
|
||||
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.PerspectiveCamera;
|
||||
import javafx.scene.SubScene;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Shape3D;
|
||||
import javafx.scene.transform.Rotate;
|
||||
import javafx.scene.transform.Translate;
|
||||
|
||||
/**
|
||||
* Control for rendering 3D objects visible through a PerspectiveCamera. Implements Adapter Pattern to
|
||||
* interface with camera, and allows clients to add shapes to the scene. All scenes contain sea plane and
|
||||
* sky box, whose textures are set with special methods.
|
||||
*/
|
||||
public class View3D extends Pane {
|
||||
/**
|
||||
* Observable list of renderable items
|
||||
*/
|
||||
private ObservableList<Shape3D> items;
|
||||
/**
|
||||
* Rendering container for shapes
|
||||
*/
|
||||
private Group world;
|
||||
/**
|
||||
* Near limit of view frustum
|
||||
*/
|
||||
private double nearClip;
|
||||
/**
|
||||
* Far limit of view frustum
|
||||
*/
|
||||
private double farClip;
|
||||
/**
|
||||
* Position camera pivots around
|
||||
*/
|
||||
private Translate pivot;
|
||||
/**
|
||||
* Distance of camera from pivot point
|
||||
*/
|
||||
private Translate distance;
|
||||
/**
|
||||
* Angle along ground between z-axis and camera
|
||||
*/
|
||||
private Rotate yaw;
|
||||
/**
|
||||
* Angle between ground plane and camera direction
|
||||
*/
|
||||
private Rotate pitch;
|
||||
|
||||
/**
|
||||
* Default constructor for View3D. Sets up Scene and PerspectiveCamera.
|
||||
*/
|
||||
public View3D() {
|
||||
world = new Group();
|
||||
|
||||
SubScene scene = new SubScene(world, 300, 300);
|
||||
scene.widthProperty().bind(this.widthProperty());
|
||||
scene.heightProperty().bind(this.heightProperty());
|
||||
scene.setFill(Color.BLACK);
|
||||
|
||||
scene.setCamera(buildCamera());
|
||||
|
||||
this.getChildren().add(scene);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up camera view frustum and binds transformations
|
||||
* @return perspective camera
|
||||
*/
|
||||
private PerspectiveCamera buildCamera() {
|
||||
PerspectiveCamera camera = new PerspectiveCamera(true);
|
||||
|
||||
// Set up view frustum
|
||||
nearClip = 0.1;
|
||||
farClip = 1000.0;
|
||||
camera.setNearClip(nearClip);
|
||||
camera.setFarClip(farClip);
|
||||
|
||||
// Set up transformations
|
||||
pivot = new Translate();
|
||||
distance = new Translate();
|
||||
yaw = new Rotate(0, Rotate.Y_AXIS);
|
||||
pitch = new Rotate(0, Rotate.X_AXIS);
|
||||
camera.getTransforms().addAll(pivot, yaw, pitch, distance);
|
||||
|
||||
return camera;
|
||||
}
|
||||
|
||||
public void setItems(ObservableList<Shape3D> items) {
|
||||
this.items = items;
|
||||
this.items.addListener((ListChangeListener<? super Shape3D>) c -> {
|
||||
while(c.next()) {
|
||||
if (c.wasRemoved() || c.wasAdded()) {
|
||||
for (Shape3D shape : c.getRemoved()) world.getChildren().remove(shape);
|
||||
for (Shape3D shape : c.getAddedSubList()) world.getChildren().add(shape);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setNearClip(double nearClip) {
|
||||
this.nearClip = nearClip;
|
||||
}
|
||||
|
||||
public void setFarClip(double farClip) {
|
||||
this.farClip = farClip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object to centre on camera
|
||||
* @param pivot centred object
|
||||
*/
|
||||
public void setPivot(Shape3D pivot) {
|
||||
this.pivot.setX(pivot.getTranslateX());
|
||||
this.pivot.setY(pivot.getTranslateY());
|
||||
this.pivot.setZ(pivot.getTranslateZ());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set distance of camera from pivot
|
||||
* @param distance in units
|
||||
*/
|
||||
public void setDistance(double distance) {
|
||||
this.distance.setZ(-distance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set angle of camera from z-axis along ground
|
||||
* @param yaw in degrees
|
||||
*/
|
||||
public void setYaw(double yaw) {
|
||||
this.yaw.setAngle(yaw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set elevation of camera
|
||||
* @param pitch in degrees
|
||||
*/
|
||||
public void setPitch(double pitch) {
|
||||
this.pitch.setAngle(-pitch);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,104 @@
|
||||
package visualiser.utils;
|
||||
|
||||
import shared.dataInput.RaceDataSource;
|
||||
import shared.model.GPSCoordinate;
|
||||
import visualiser.model.GraphCoordinate;
|
||||
|
||||
/**
|
||||
* Converts GPS coordinates to view volume coordinates. Longitudes are equally spaced at all latitudes,
|
||||
* which leads to inaccurate distance measurements close to the poles. This is acceptable as races are
|
||||
* not likely to be set there.
|
||||
*/
|
||||
public class GPSConverter {
|
||||
private double longRight;
|
||||
private double longLeft;
|
||||
private double latBottom;
|
||||
private double latTop;
|
||||
/**
|
||||
* Conversion factor from longitude to view units
|
||||
*/
|
||||
private double longitudeFactor;
|
||||
/**
|
||||
* Conversion factor from latitude to view units
|
||||
*/
|
||||
private double latitudeFactor;
|
||||
|
||||
/**
|
||||
* Set up projection with default view boundaries from RaceDataSource
|
||||
* @param source for view boundaries
|
||||
* @param longitudeFactor separation of a degree of longitude in view units
|
||||
* @param latitudeFactor separation of a degree of latitude in view units
|
||||
*/
|
||||
public GPSConverter(RaceDataSource source, double longitudeFactor, double latitudeFactor) {
|
||||
this.latTop = source.getMapTopLeft().getLatitude();
|
||||
this.longLeft = source.getMapTopLeft().getLongitude();
|
||||
this.latBottom = source.getMapBottomRight().getLatitude();
|
||||
this.longRight = source.getMapBottomRight().getLongitude();
|
||||
this.longitudeFactor = longitudeFactor;
|
||||
this.latitudeFactor = latitudeFactor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts GPS coordinates to coordinates for container.
|
||||
* It is assumed that the provided GPSCoordinate will always be within the GPSCoordinate boundaries of the RaceMap.
|
||||
*
|
||||
* @param lat GPS latitude
|
||||
* @param lon GPS longitude
|
||||
* @return GraphCoordinate (pair of doubles)
|
||||
* @see GraphCoordinate
|
||||
*/
|
||||
private GraphCoordinate convertGPS(double lat, double lon) {
|
||||
|
||||
//Calculate the width/height, in gps coordinates, of the map.
|
||||
double longWidth = longRight - longLeft;
|
||||
double latHeight = latBottom - latTop;
|
||||
|
||||
//Calculate the distance between the specified coordinate and the edge of the map.
|
||||
double longDelta = lon - longLeft;
|
||||
double latDelta = lat - latTop;
|
||||
|
||||
//Calculate the proportion along horizontally, from the left, the coordinate should be.
|
||||
double longProportion = longDelta / longWidth;
|
||||
//Calculate the proportion along vertically, from the top, the coordinate should be.
|
||||
double latProportion = latDelta / latHeight;
|
||||
|
||||
//Check which metric dimension of our map is smaller. We use this to ensure that any rendered stuff retains its correct aspect ratio, and that everything is visible on screen.
|
||||
double smallerDimension = Math.min(longitudeFactor, latitudeFactor);
|
||||
|
||||
//Calculate the x and y pixel coordinates.
|
||||
//We take the complement of latProportion to flip it.
|
||||
int x = (int) (longProportion * smallerDimension);
|
||||
int y = (int) (latProportion * smallerDimension);
|
||||
|
||||
//Because we try to maintain the correct aspect ratio, we will end up with "spare" pixels along the larger dimension (e.g., width 800, height 600, 200 extra pixels along width).
|
||||
double extraDistance = Math.abs(longitudeFactor - latitudeFactor);
|
||||
//We therefore "center" the coordinates along this larger dimension, by adding half of the extra pixels.
|
||||
if (longitudeFactor > latitudeFactor) {
|
||||
x += extraDistance / 2;
|
||||
} else {
|
||||
y += extraDistance / 2;
|
||||
}
|
||||
|
||||
|
||||
//Finally, create the GraphCoordinate.
|
||||
GraphCoordinate graphCoordinate = new GraphCoordinate(x, y);
|
||||
|
||||
|
||||
return graphCoordinate;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the GPS Coordinate to GraphCoordinate.
|
||||
* It is assumed that the provided GPSCoordinate will always be within the GPSCoordinate boundaries of the RaceMap.
|
||||
*
|
||||
* @param coordinate GPSCoordinate representation of Latitude and Longitude.
|
||||
* @return GraphCoordinate that the GPS is coordinates are to be displayed on the map.
|
||||
* @see GraphCoordinate
|
||||
* @see GPSCoordinate
|
||||
*/
|
||||
public GraphCoordinate convertGPS(GPSCoordinate coordinate) {
|
||||
return convertGPS(coordinate.getLatitude(), coordinate.getLongitude());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue