MikaFil commited on
Commit
fe0acb1
·
verified ·
1 Parent(s): a2c327b

Update deplacement_dans_env/ctrl_camera_pr_env.js

Browse files
deplacement_dans_env/ctrl_camera_pr_env.js CHANGED
@@ -540,9 +540,9 @@ OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
540
 
541
  // =================== Orbit Camera Input Keyboard ========================
542
  // Mappages demandés :
543
- // - Flèches : déplacement avant/arrière et droite/gauche (au sol)
544
- // - Ctrl+flèches : orbite autour du pivot
545
- // - Maj+flèches : rotation de la caméra sur place (position fixe)
546
  var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
547
 
548
  // Vitesses de déplacement (relatives à la distance pour un ressenti constant)
@@ -550,8 +550,8 @@ OrbitCameraInputKeyboard.attributes.add('forwardSpeed', { type: 'number', defaul
550
  OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.8, title: 'Strafe Speed (rel. to distance)' });
551
 
552
  // Réglages d’orbite / rotation
553
- OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90, title: 'Orbit Pitch Speed (deg/s) [Ctrl+Up/Down]' });
554
- OrbitCameraInputKeyboard.attributes.add('orbitYawSpeedDeg', { type: 'number', default: 120, title: 'Orbit Yaw Speed (deg/s) [Ctrl+Left/Right]' });
555
  OrbitCameraInputKeyboard.attributes.add('rotatePitchSpeedDeg', { type: 'number', default: 90, title: 'Self Rotate Pitch (deg/s) [Shift+Up/Down]' });
556
  OrbitCameraInputKeyboard.attributes.add('rotateYawSpeedDeg', { type: 'number', default: 120, title: 'Self Rotate Yaw (deg/s) [Shift+Left/Right]' });
557
 
@@ -571,20 +571,17 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
571
  var shift = this.keyboard.isPressed(pc.KEY_SHIFT);
572
  var ctrl = this.keyboard.isPressed(pc.KEY_CONTROL);
573
 
574
- // -------- Ctrl + flèches : ORBITER autour du pivot --------
575
  if (ctrl && (up || dn || lt || rt)) {
576
- var yawDir = (rt ? 1 : 0) - (lt ? 1 : 0); // droite = +1, gauche = -1
577
- var pitchDir = (up ? 1 : 0) - (dn ? 1 : 0); // haut = +1, bas = -1
578
 
579
  if (yawDir !== 0) {
580
- var dYaw = yawDir * this.orbitYawSpeedDeg * dt;
581
- this.orbitCamera.yaw = this.orbitCamera.yaw + dYaw;
582
  }
583
 
584
  if (pitchDir !== 0) {
585
- var dPitch = pitchDir * this.orbitPitchSpeedDeg * dt;
586
-
587
- // Empêche de passer sous minY pendant l’orbite en pitch
588
  var currPitch = this.orbitCamera.pitch;
589
  var currYaw = this.orbitCamera.yaw;
590
  var currDist = this.orbitCamera.distance;
@@ -594,7 +591,7 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
594
  var forward = new pc.Vec3(); camQuat.transformVector(pc.Vec3.FORWARD, forward);
595
  var preY = currPivot.y + (-forward.y) * currDist;
596
 
597
- var testPitch = currPitch + dPitch;
598
  var testQuat = new pc.Quat().setFromEulerAngles(testPitch, currYaw, 0);
599
  var testForward = new pc.Vec3(); testQuat.transformVector(pc.Vec3.FORWARD, testForward);
600
  var proposedY = currPivot.y + (-testForward.y) * currDist;
@@ -609,50 +606,47 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
609
  return;
610
  }
611
 
612
- // -------- Maj + flèches : ROTATION SUR PLACE (caméra immobile) --------
613
  if (shift && (up || dn || lt || rt)) {
614
- var yawDirR = (rt ? 1 : 0) - (lt ? 1 : 0);
615
  var pitchDirR = (up ? 1 : 0) - (dn ? 1 : 0);
616
 
 
617
  var camPos = this.entity.getPosition().clone();
618
- var dist = this.orbitCamera.distance;
619
-
620
- // Applique yaw/pitch désirés
621
- var newYaw = this.orbitCamera.yaw + yawDirR * this.rotateYawSpeedDeg * dt;
622
- var newPitch = this.orbitCamera.pitch + pitchDirR * this.rotatePitchSpeedDeg * dt;
623
 
624
- this.orbitCamera.yaw = newYaw;
625
- this.orbitCamera.pitch = newPitch;
626
-
627
- // Force l’absence d’inertie pour conserver la position exacte
628
- this.orbitCamera._removeInertia();
629
 
630
- // Recalcule le pivot pour garder la position de la caméra constante
631
  var q = new pc.Quat().setFromEulerAngles(this.orbitCamera._pitch, this.orbitCamera._yaw, 0);
632
  var forward = new pc.Vec3(); q.transformVector(pc.Vec3.FORWARD, forward);
633
  var newPivot = camPos.clone().add(forward.clone().mulScalar(dist));
634
- this.orbitCamera.pivotPoint = newPivot;
635
 
636
- // Met à jour immédiatement la position (qui doit rester égale à camPos)
 
637
  this.orbitCamera._updatePosition();
638
  this.entity.setPosition(camPos);
639
 
640
  return;
641
  }
642
 
643
- // -------- Flèches seules : DÉPLACEMENT (avant/arrière & droite/gauche) --------
644
  var moveForward = (up ? 1 : 0) - (dn ? 1 : 0);
645
  var moveRight = (rt ? 1 : 0) - (lt ? 1 : 0);
646
  if (moveForward === 0 && moveRight === 0) return;
647
 
648
- // Vecteurs avant/droite projetés sur le plan XZ (y=0) pour rester "au sol"
649
- var fwd = this.entity.forward.clone(); fwd.y = 0;
650
- var right = this.entity.right.clone(); right.y = 0;
651
-
652
- if (fwd.lengthSq() > 1e-8) fwd.normalize(); else fwd.set(0, 0, -1);
653
- if (right.lengthSq() > 1e-8) right.normalize(); else right.set(1, 0, 0);
654
 
655
- var camDist = Math.max(0.1, this.orbitCamera.distance);
656
  var speedF = this.forwardSpeed * camDist;
657
  var speedR = this.strafeSpeed * camDist;
658
 
@@ -661,12 +655,12 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
661
  if (moveRight !== 0) delta.add(right.mulScalar(moveRight * speedR * dt));
662
 
663
  if (delta.lengthSq() > 0) {
664
- var proposedPivot = this.orbitCamera.pivotPoint.clone().add(delta);
665
- var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
666
- if (resultingY >= this.orbitCamera.minY - 1e-4) {
667
- this.orbitCamera.pivotPoint = proposedPivot;
668
- } else {
669
- // Si jamais cela ferait passer la caméra sous minY (peu probable car delta.y=0), on annule
670
  }
 
671
  }
672
  };
 
540
 
541
  // =================== Orbit Camera Input Keyboard ========================
542
  // Mappages demandés :
543
+ // - Flèches : déplacement avant/arrière & droite/gauche dans le repère de la caméra (3D)
544
+ // - Ctrl+flèches : orbiter autour du pivot
545
+ // - Maj+flèches : rotation libre sur place (pas de limites yaw/pitch)
546
  var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
547
 
548
  // Vitesses de déplacement (relatives à la distance pour un ressenti constant)
 
550
  OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.8, title: 'Strafe Speed (rel. to distance)' });
551
 
552
  // Réglages d’orbite / rotation
553
+ OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90, title: 'Orbit Pitch Speed (deg/s) [Ctrl+Up/Down]' });
554
+ OrbitCameraInputKeyboard.attributes.add('orbitYawSpeedDeg', { type: 'number', default: 120, title: 'Orbit Yaw Speed (deg/s) [Ctrl+Left/Right]' });
555
  OrbitCameraInputKeyboard.attributes.add('rotatePitchSpeedDeg', { type: 'number', default: 90, title: 'Self Rotate Pitch (deg/s) [Shift+Up/Down]' });
556
  OrbitCameraInputKeyboard.attributes.add('rotateYawSpeedDeg', { type: 'number', default: 120, title: 'Self Rotate Yaw (deg/s) [Shift+Left/Right]' });
557
 
 
571
  var shift = this.keyboard.isPressed(pc.KEY_SHIFT);
572
  var ctrl = this.keyboard.isPressed(pc.KEY_CONTROL);
573
 
574
+ // -------- Ctrl + flèches : ORBITER autour du pivot (clamp actif) --------
575
  if (ctrl && (up || dn || lt || rt)) {
576
+ var yawDir = (rt ? 1 : 0) - (lt ? 1 : 0); // droite = +1, gauche = -1
577
+ var pitchDir = (up ? 1 : 0) - (dn ? 1 : 0); // haut = +1, bas = -1
578
 
579
  if (yawDir !== 0) {
580
+ this.orbitCamera.yaw = this.orbitCamera.yaw + yawDir * this.orbitYawSpeedDeg * dt;
 
581
  }
582
 
583
  if (pitchDir !== 0) {
584
+ // on laisse le clamp faire son travail via le setter
 
 
585
  var currPitch = this.orbitCamera.pitch;
586
  var currYaw = this.orbitCamera.yaw;
587
  var currDist = this.orbitCamera.distance;
 
591
  var forward = new pc.Vec3(); camQuat.transformVector(pc.Vec3.FORWARD, forward);
592
  var preY = currPivot.y + (-forward.y) * currDist;
593
 
594
+ var testPitch = currPitch + pitchDir * this.orbitPitchSpeedDeg * dt;
595
  var testQuat = new pc.Quat().setFromEulerAngles(testPitch, currYaw, 0);
596
  var testForward = new pc.Vec3(); testQuat.transformVector(pc.Vec3.FORWARD, testForward);
597
  var proposedY = currPivot.y + (-testForward.y) * currDist;
 
606
  return;
607
  }
608
 
609
+ // -------- Maj + flèches : ROTATION SUR PLACE SANS LIMITE --------
610
  if (shift && (up || dn || lt || rt)) {
611
+ var yawDirR = (rt ? 1 : 0) - (lt ? 1 : 0);
612
  var pitchDirR = (up ? 1 : 0) - (dn ? 1 : 0);
613
 
614
+ // Position actuelle à préserver
615
  var camPos = this.entity.getPosition().clone();
616
+ var dist = this.orbitCamera._distance;
 
 
 
 
617
 
618
+ // *** Bypass des clamps: on écrit directement dans _yaw/_pitch et leurs cibles ***
619
+ this.orbitCamera._yaw += yawDirR * this.rotateYawSpeedDeg * dt;
620
+ this.orbitCamera._pitch += pitchDirR * this.rotatePitchSpeedDeg * dt;
621
+ this.orbitCamera._targetYaw = this.orbitCamera._yaw;
622
+ this.orbitCamera._targetPitch = this.orbitCamera._pitch;
623
 
624
+ // Recalcule le pivot pour garder la même position de caméra
625
  var q = new pc.Quat().setFromEulerAngles(this.orbitCamera._pitch, this.orbitCamera._yaw, 0);
626
  var forward = new pc.Vec3(); q.transformVector(pc.Vec3.FORWARD, forward);
627
  var newPivot = camPos.clone().add(forward.clone().mulScalar(dist));
628
+ this.orbitCamera._pivotPoint.copy(newPivot);
629
 
630
+ // Pas d’inertie pour être exact tout de suite
631
+ this.orbitCamera._removeInertia();
632
  this.orbitCamera._updatePosition();
633
  this.entity.setPosition(camPos);
634
 
635
  return;
636
  }
637
 
638
+ // -------- Flèches seules : DÉPLACEMENT DANS LE REPÈRE DE LA CAMÉRA --------
639
  var moveForward = (up ? 1 : 0) - (dn ? 1 : 0);
640
  var moveRight = (rt ? 1 : 0) - (lt ? 1 : 0);
641
  if (moveForward === 0 && moveRight === 0) return;
642
 
643
+ // Vecteurs avant/droite "complets" (incluent Y) mouvement 3D relatif caméra
644
+ var fwd = this.entity.forward.clone();
645
+ var right = this.entity.right.clone();
646
+ if (fwd.lengthSq() > 1e-8) fwd.normalize();
647
+ if (right.lengthSq() > 1e-8) right.normalize();
 
648
 
649
+ var camDist = Math.max(0.1, this.orbitCamera._distance);
650
  var speedF = this.forwardSpeed * camDist;
651
  var speedR = this.strafeSpeed * camDist;
652
 
 
655
  if (moveRight !== 0) delta.add(right.mulScalar(moveRight * speedR * dt));
656
 
657
  if (delta.lengthSq() > 0) {
658
+ // Respecte minY : camY_new = camY_cur + delta.y (car yaw/pitch/distance inchangés)
659
+ var currCamY = this.orbitCamera.worldCameraYForPivot(this.orbitCamera._pivotPoint);
660
+ var minY = this.orbitCamera.minY;
661
+ if (currCamY + delta.y < minY) {
662
+ delta.y = minY - currCamY; // on limite juste ce qu'il faut
 
663
  }
664
+ this.orbitCamera._pivotPoint.add(delta);
665
  }
666
  };