Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added maximumTiltAngle to limit how much the camera can tilt #12169

Merged
merged 7 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This is an npm-only release to extra source maps included in 1.121
- Added `WaterMask` globe material, which visualizes areas of water or land based on the terrain's water mask. [#12149](https://github.com/CesiumGS/cesium/pull/12149)
- Made the `time` parameter optional for `Property`, using `JulianDate.now()` as default. [#12099](https://github.com/CesiumGS/cesium/pull/12099)
- Exposes `ScreenSpaceCameraController.zoomFactor` to allow adjusting the zoom factor (speed). [#9145](https://github.com/CesiumGS/cesium/pull/9145)
- Added `ScreenSpaceCameraController.maximumTiltAngle` to limit how much the camera can tilt.

##### Fixes :wrench:

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
- [Tang Xiaofei](https://github.com/vtxf)
- [Maxar](https://www.maxar.com)
- [Erik Dahlström](https://github.com/erikdahlstrom)
- [Albin Ekberg](https://github.com/albek446)
- [Novatron Oy](https://novatron.fi/en/)
- [Jussi Hirvonen](https://github.com/VsKatshuma)
- [Sierra Nevada Corp](https://www.sncorp.com/)
Expand Down
16 changes: 15 additions & 1 deletion packages/engine/Source/Scene/ScreenSpaceCameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ function ScreenSpaceCameraController(scene) {
* @default true
*/
this.enableCollisionDetection = true;
/**
* If set, the camera will not be able to tilt past this angle, expressed in radians.
* @type {number}
*/
lukemckinstry marked this conversation as resolved.
Show resolved Hide resolved
this.maximumTiltAngle = undefined;

this._scene = scene;
this._globe = undefined;
Expand Down Expand Up @@ -2058,7 +2063,16 @@ function rotate3D(
);

const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 2.0;
const deltaTheta = rotateRate * thetaWindowRatio * Math.PI;
let deltaTheta = rotateRate * thetaWindowRatio * Math.PI;

if (defined(constrainedAxis) && defined(controller.maximumTiltAngle)) {
const maximumTiltAngle = controller.maximumTiltAngle;
const dotProduct = Cartesian3.dot(camera.direction, constrainedAxis);
const tilt = Math.PI - Math.acos(dotProduct) + deltaTheta;
if (tilt > maximumTiltAngle) {
deltaTheta -= tilt - maximumTiltAngle;
}
}

if (!rotateOnlyVertical) {
camera.rotateRight(deltaPhi);
Expand Down
73 changes: 73 additions & 0 deletions packages/engine/Specs/Scene/ScreenSpaceCameraControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,79 @@ describe("Scene/ScreenSpaceCameraController", function () {
expect(camera.direction).not.toEqual(direction);
});

it("maximum tilt angle is 0 in 3D", function () {
setUp3D();
const originalCameraPosition = Cartesian3.clone(camera.position);
const originalCameraDirection = Cartesian3.clone(camera.direction);
controller.maximumTiltAngle = 0;
const startPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 2
);
const endPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 4
);

moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
updateController();

expect(camera.position).toEqualEpsilon(
originalCameraPosition,
CesiumMath.EPSILON8
);
expect(camera.position).not.toEqualEpsilon(
originalCameraDirection,
CesiumMath.EPSILON8
);

const dotProduct = Cartesian3.dot(
camera.direction,
originalCameraDirection
);
const acos = Math.acos(dotProduct);
const angle = (acos * 180) / Math.PI;
expect(angle).toEqual(0);
});

it("maximum tilt angle is 45 degrees in 3D", function () {
setUp3D();
const originalCameraPosition = Cartesian3.clone(camera.position);
const originalCameraDirection = Cartesian3.clone(camera.direction);
controller.maximumTiltAngle = (45 * Math.PI) / 180;
const startPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 2
);
const endPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 4
);

moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why repeat this mouse move? If it is valid can you add a comment to explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly.
The reason why I repeated the mouse move was because I could not find a way to get the tilting angle to 45 degrees (0.25π radians) in just one move. I tried to set the endPosition to different values but could not get the final tilt angle high enough to the limit I wanted to test. Maybe I am missing something, and there is a better way?

updateController();
moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
updateController();

expect(camera.position).not.toEqualEpsilon(
originalCameraPosition,
CesiumMath.EPSILON8
);
expect(camera.position).not.toEqualEpsilon(
originalCameraDirection,
CesiumMath.EPSILON8
);

const dotProduct = Cartesian3.dot(
camera.direction,
originalCameraDirection
);
const acos = Math.acos(dotProduct);
const angle = (acos * 180) / Math.PI;
expect(angle).toBeLessThanOrEqual(45);
expect(angle).toBeGreaterThan(44);
});

it("looks in 3D", function () {
setUp3D();
const position = Cartesian3.clone(camera.position);
Expand Down