Skip to content

Commit

Permalink
fix(PlanarControls): fix pan movement
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie authored and gchoqueux committed Feb 16, 2021
1 parent b3e81fc commit 5be30b7
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions src/Controls/PlanarControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,6 @@ class PlanarControls extends THREE.EventDispatcher {
* @ignore
*/
update(dt, updateLoopRestarted) {
// We test if camera collides to the geometry layer or is too close to the ground, and adjust its altitude in
// case
if (this.handleCollision) { // check distance to the ground/surface geometry (could be another geometry layer)
this.view.camera.adjustAltitudeToAvoidCollisionWithLayer(
this.view,
this.view.tileLayer,
this.minDistanceCollision,
);
}
// dt will not be relevant when we just started rendering. We consider a 1-frame move in this case
if (updateLoopRestarted) {
dt = 16;
Expand Down Expand Up @@ -336,6 +327,15 @@ class PlanarControls extends THREE.EventDispatcher {
default:
break;
}
// We test if camera collides to the geometry layer or is too close to the ground, and adjust its altitude in
// case
if (this.handleCollision) { // check distance to the ground/surface geometry (could be another geometry layer)
this.view.camera.adjustAltitudeToAvoidCollisionWithLayer(
this.view,
this.view.tileLayer,
this.minDistanceCollision,
);
}
if (onMovement) {
this.view.dispatchEvent({ type: PLANAR_CONTROL_EVENT.MOVED });
}
Expand Down Expand Up @@ -396,31 +396,9 @@ class PlanarControls extends THREE.EventDispatcher {
* @ignore
*/
handlePanMovement() {
// normalized (between 0 and 1) distance between groundLevel and maxAltitude
const distToGround = THREE.MathUtils.clamp(
(this.camera.position.z - this.groundLevel) / this.maxAltitude,
0,
1,
);

// pan movement speed, adjusted according to altitude
const panSpeed = THREE.MathUtils.lerp(
this.minPanSpeed,
this.maxPanSpeed,
distToGround,
);

// lateral movement (local x axis)
vect.set(panSpeed * -1 * deltaMousePosition.x, 0, 0);
this.camera.position.copy(this.camera.localToWorld(vect));

// vertical movement (world z axis)
const newAltitude = this.camera.position.z + panSpeed * deltaMousePosition.y;

// check if altitude is valid
if (this.groundLevel < newAltitude && newAltitude < this.maxAltitude) {
this.camera.position.z = newAltitude;
}
vect.set(-deltaMousePosition.x, deltaMousePosition.y, 0);
this.camera.localToWorld(vect);
this.camera.position.copy(vect);
}

/**
Expand Down

0 comments on commit 5be30b7

Please sign in to comment.