Skip to content

Commit abf8075

Browse files
committed
Merge remote-tracking branch 'origin/feature/refactor-camera' into develop
2 parents 955e814 + 53a230b commit abf8075

3 files changed

Lines changed: 48 additions & 25 deletions

File tree

worldwind-tutorials/src/main/assets/camera_control_tutorial.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ <h3>CameraControlFragment.java</h3>
3838
<textarea id="java-code">
3939
public class CameraControlFragment extends BasicGlobeFragment {
4040

41+
private static final double COLLISION_CHECK_LIMIT = 8848.86; // Everest mountain altitude
42+
43+
private static final double COLLISION_THRESHOLD = 20.0; // 20m above surface
44+
4145
/**
4246
* Creates a new WorldWindow object with a custom WorldWindowController.
4347
*/
@@ -215,20 +219,30 @@ <h3>CameraControlFragment.java</h3>
215219
}
216220

217221
protected void applyLimits(Camera camera) {
218-
double distanceToExtents = this.wwd.distanceToViewGlobeExtents();
222+
Position position = camera.position;
219223

220224
double minAltitude = 100;
221-
camera.position.altitude = WWMath.clamp(camera.position.altitude, minAltitude, distanceToExtents);
225+
double maxAltitude = this.wwd.distanceToViewGlobeExtents();
226+
position.altitude = WWMath.clamp(position.altitude, minAltitude, maxAltitude);
227+
228+
// Check if camera altitude is not under the surface
229+
double ve = wwd.getVerticalExaggeration();
230+
if (position.altitude < COLLISION_CHECK_LIMIT * ve + COLLISION_THRESHOLD) {
231+
double elevation = this.wwd.getGlobe().getElevationAtLocation(position.latitude, position.longitude)
232+
* ve + COLLISION_THRESHOLD;
233+
if (elevation > position.altitude) {
234+
position.altitude = elevation;
235+
}
236+
}
222237

223238
// Limit the tilt to between nadir and the horizon (roughly)
224-
double r = wwd.getGlobe().getRadiusAt(camera.position.latitude, camera.position.latitude);
225-
double maxTilt = Math.toDegrees(Math.asin(r / (r + camera.position.altitude)));
239+
double r = wwd.getGlobe().getRadiusAt(position.latitude, position.latitude);
240+
double maxTilt = Math.toDegrees(Math.asin(r / (r + position.altitude)));
226241
double minTilt = 0;
227242
camera.tilt = WWMath.clamp(camera.tilt, minTilt, maxTilt);
228243
}
229244
}
230245
}
231-
232246
</textarea>
233247
</div>
234248

worldwind-tutorials/src/main/java/gov/nasa/worldwindx/CameraControlFragment.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
public class CameraControlFragment extends BasicGlobeFragment {
2020

21+
private static final double COLLISION_CHECK_LIMIT = 8848.86; // Everest mountain altitude
22+
2123
private static final double COLLISION_THRESHOLD = 20.0; // 20m above surface
2224

2325
/**
@@ -204,10 +206,13 @@ protected void applyLimits(Camera camera) {
204206
position.altitude = WWMath.clamp(position.altitude, minAltitude, maxAltitude);
205207

206208
// Check if camera altitude is not under the surface
207-
double elevation = this.wwd.getGlobe().getElevationAtLocation(position.latitude, position.longitude)
208-
* wwd.getVerticalExaggeration() + COLLISION_THRESHOLD;
209-
if (elevation > position.altitude) {
210-
position.altitude = elevation;
209+
double ve = wwd.getVerticalExaggeration();
210+
if (position.altitude < COLLISION_CHECK_LIMIT * ve + COLLISION_THRESHOLD) {
211+
double elevation = this.wwd.getGlobe().getElevationAtLocation(position.latitude, position.longitude)
212+
* ve + COLLISION_THRESHOLD;
213+
if (elevation > position.altitude) {
214+
position.altitude = elevation;
215+
}
211216
}
212217

213218
// Limit the tilt to between nadir and the horizon (roughly)

worldwind/src/main/java/gov/nasa/worldwind/WorldWindow.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public class WorldWindow extends GLSurfaceView implements Choreographer.FrameCal
5656

5757
protected static final int MSG_ID_SET_DEPTH_BITS = 4;
5858

59-
protected final static double COLLISION_THRESHOLD = 10.0; // 10m above surface
59+
protected static final double COLLISION_CHECK_LIMIT = 8848.86; // Everest mountain altitude
60+
61+
protected static final double COLLISION_THRESHOLD = 10.0; // 10m above surface
6062

6163
/**
6264
* Planet or celestial object displayed by this WorldWindow.
@@ -460,21 +462,23 @@ public void cameraFromLookAt(LookAt lookAt) {
460462

461463
// Check if camera altitude is not under the surface
462464
Position position = this.camera.position;
463-
double elevation = globe.getElevationAtLocation(position.latitude, position.longitude) * verticalExaggeration + COLLISION_THRESHOLD;
464-
if(elevation > position.altitude) {
465-
// Set camera altitude above the surface
466-
position.altitude = elevation;
467-
// Compute new camera point
468-
globe.geographicToCartesian(position.latitude, position.longitude, position.altitude, scratchPoint);
469-
// Compute look at point
470-
globe.geographicToCartesian(lookAt.position.latitude, lookAt.position.longitude, lookAt.position.altitude, scratchRay.origin);
471-
// Compute normal to globe in look at point
472-
globe.geographicToCartesianNormal(lookAt.position.latitude, lookAt.position.longitude, scratchRay.direction);
473-
// Calculate tilt angle between new camera point and look at point
474-
scratchPoint.subtract(scratchRay.origin).normalize();
475-
double dot = scratchRay.direction.dot(scratchPoint);
476-
if (dot >= -1 || dot <= 1) {
477-
this.camera.tilt = Math.toDegrees(Math.acos(dot));
465+
if (position.altitude < COLLISION_CHECK_LIMIT * verticalExaggeration + COLLISION_THRESHOLD) {
466+
double elevation = globe.getElevationAtLocation(position.latitude, position.longitude) * verticalExaggeration + COLLISION_THRESHOLD;
467+
if (elevation > position.altitude) {
468+
// Set camera altitude above the surface
469+
position.altitude = elevation;
470+
// Compute new camera point
471+
globe.geographicToCartesian(position.latitude, position.longitude, position.altitude, scratchPoint);
472+
// Compute look at point
473+
globe.geographicToCartesian(lookAt.position.latitude, lookAt.position.longitude, lookAt.position.altitude, scratchRay.origin);
474+
// Compute normal to globe in look at point
475+
globe.geographicToCartesianNormal(lookAt.position.latitude, lookAt.position.longitude, scratchRay.direction);
476+
// Calculate tilt angle between new camera point and look at point
477+
scratchPoint.subtract(scratchRay.origin).normalize();
478+
double dot = scratchRay.direction.dot(scratchPoint);
479+
if (dot >= -1 || dot <= 1) {
480+
this.camera.tilt = Math.toDegrees(Math.acos(dot));
481+
}
478482
}
479483
}
480484
}

0 commit comments

Comments
 (0)