MikaFil commited on
Commit
92bb0d2
·
verified ·
1 Parent(s): 397bf7b

Update tooltips.js

Browse files
Files changed (1) hide show
  1. tooltips.js +24 -13
tooltips.js CHANGED
@@ -146,6 +146,13 @@ export async function initializeTooltips(options) {
146
  });
147
  }
148
 
 
 
 
 
 
 
 
149
  // Tween helper: smoothly move and reorient camera to focus the chosen tooltip sphere
150
  function tweenCameraToTooltip(tooltipEnt, duration) {
151
  const orbitCam = cameraEntity.script.orbitCamera;
@@ -164,8 +171,7 @@ export async function initializeTooltips(options) {
164
  const minZoom = orbitCam.distanceMin;
165
  const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
166
 
167
- // Compute target yaw/pitch from camera pointing at targetPos
168
- // Reuse reset logic: place a temp entity at camera’s current position, have it look at target
169
  const camWorldPos = cameraEntity.getPosition().clone();
170
  const tempEnt = new pc.Entity();
171
  tempEnt.setPosition(camWorldPos);
@@ -173,18 +179,23 @@ export async function initializeTooltips(options) {
173
  const rotation = tempEnt.getRotation();
174
  const forward = new pc.Vec3();
175
  rotation.transformVector(pc.Vec3.FORWARD, forward);
176
- const tgtYaw = Math.atan2(-forward.x, -forward.z) * pc.math.RAD_TO_DEG;
177
- const yawQuat = new pc.Quat().setFromEulerAngles(0, -tgtYaw, 0);
 
 
 
 
178
  const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
179
  const fNoYaw = new pc.Vec3();
180
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
181
- const tgtPitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
 
 
 
182
  tempEnt.destroy();
183
 
184
  // Target state:
185
  const endPivot = targetPos.clone();
186
- const endYaw = tgtYaw;
187
- const endPitch = tgtPitch;
188
  const endDist = desiredDistance;
189
 
190
  let elapsed = 0;
@@ -213,12 +224,12 @@ export async function initializeTooltips(options) {
213
  const newPitch = pc.math.lerp(orgPitch, endPitch, t);
214
  const newDist = pc.math.lerp(orgDist, endDist, t);
215
 
216
- orbitCam._targetYaw = newYaw;
217
- orbitCam._yaw = newYaw;
218
- orbitCam._targetPitch = newPitch;
219
- orbitCam._pitch = newPitch;
220
  orbitCam._targetDistance = newDist;
221
- orbitCam._distance = newDist;
222
 
223
  orbitCam._updatePosition();
224
 
@@ -231,4 +242,4 @@ export async function initializeTooltips(options) {
231
  currentTween = lerpUpdate;
232
  app.on("update", lerpUpdate);
233
  }
234
- }
 
146
  });
147
  }
148
 
149
+ // Helper to normalize angle difference into [-180, +180]
150
+ function shortestAngleDiff(target, current) {
151
+ let delta = target - current;
152
+ delta = ((delta + 180) % 360 + 360) % 360 - 180;
153
+ return delta;
154
+ }
155
+
156
  // Tween helper: smoothly move and reorient camera to focus the chosen tooltip sphere
157
  function tweenCameraToTooltip(tooltipEnt, duration) {
158
  const orbitCam = cameraEntity.script.orbitCamera;
 
171
  const minZoom = orbitCam.distanceMin;
172
  const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
173
 
174
+ // Compute raw target yaw/pitch from camera pointing at targetPos
 
175
  const camWorldPos = cameraEntity.getPosition().clone();
176
  const tempEnt = new pc.Entity();
177
  tempEnt.setPosition(camWorldPos);
 
179
  const rotation = tempEnt.getRotation();
180
  const forward = new pc.Vec3();
181
  rotation.transformVector(pc.Vec3.FORWARD, forward);
182
+ const rawTgtYaw = Math.atan2(-forward.x, -forward.z) * pc.math.RAD_TO_DEG;
183
+ const yawDelta = shortestAngleDiff(rawTgtYaw, startYaw);
184
+ const endYaw = startYaw + yawDelta;
185
+
186
+ // Pitch calculation and normalization
187
+ const yawQuat = new pc.Quat().setFromEulerAngles(0, -rawTgtYaw, 0);
188
  const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
189
  const fNoYaw = new pc.Vec3();
190
  rotNoYaw.transformVector(pc.Vec3.FORWARD, fNoYaw);
191
+ const rawTgtPitch = Math.atan2(fNoYaw.y, -fNoYaw.z) * pc.math.RAD_TO_DEG;
192
+ const pitchDelta = shortestAngleDiff(rawTgtPitch, startPitch);
193
+ const endPitch = startPitch + pitchDelta;
194
+
195
  tempEnt.destroy();
196
 
197
  // Target state:
198
  const endPivot = targetPos.clone();
 
 
199
  const endDist = desiredDistance;
200
 
201
  let elapsed = 0;
 
224
  const newPitch = pc.math.lerp(orgPitch, endPitch, t);
225
  const newDist = pc.math.lerp(orgDist, endDist, t);
226
 
227
+ orbitCam._targetYaw = newYaw;
228
+ orbitCam._yaw = newYaw;
229
+ orbitCam._targetPitch = newPitch;
230
+ orbitCam._pitch = newPitch;
231
  orbitCam._targetDistance = newDist;
232
+ orbitCam._distance = newDist;
233
 
234
  orbitCam._updatePosition();
235
 
 
242
  currentTween = lerpUpdate;
243
  app.on("update", lerpUpdate);
244
  }
245
+ }