@galacean/engine-physics-lite 1.4.0-alpha.0 → 1.4.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -94,6 +94,7 @@
94
94
  /** @internal */ this._worldScale = new engine.Vector3(1, 1, 1);
95
95
  /** @internal */ this._transform = new LiteTransform();
96
96
  /** @internal */ this._invModelMatrix = new engine.Matrix();
97
+ this._rotation = new engine.Vector3();
97
98
  this._transform.owner = this;
98
99
  this._inverseWorldMatFlag = this._transform.registerWorldChangeFlag();
99
100
  }
@@ -101,7 +102,8 @@
101
102
  /**
102
103
  * {@inheritDoc IColliderShape.setRotation }
103
104
  */ _proto.setRotation = function setRotation(rotation) {
104
- console.log("Physics-lite don't support setRotation. Use Physics-PhysX instead!");
105
+ var rotationInRadians = this._rotation.set(engine.MathUtil.degreeToRadian(rotation.x), engine.MathUtil.degreeToRadian(rotation.y), engine.MathUtil.degreeToRadian(rotation.z));
106
+ engine.Quaternion.rotationEuler(rotationInRadians.x, rotationInRadians.y, rotationInRadians.z, this._transform.rotationQuaternion);
105
107
  };
106
108
  /**
107
109
  * {@inheritDoc IColliderShape.setPosition }
@@ -176,8 +178,12 @@
176
178
  };
177
179
  return LiteColliderShape;
178
180
  }();
179
- LiteColliderShape._ray = new engine.Ray();
181
+ LiteColliderShape._tempPos = new engine.Vector3();
182
+ LiteColliderShape._tempRot = new engine.Quaternion();
183
+ LiteColliderShape._tempScale = new engine.Vector3();
180
184
  LiteColliderShape._tempPoint = new engine.Vector3();
185
+ LiteColliderShape._tempVector4 = new engine.Vector4();
186
+ LiteColliderShape._ray = new engine.Ray();
181
187
 
182
188
  /**
183
189
  * Used to implement transformation related functions.
@@ -486,11 +492,13 @@
486
492
  */ _proto.addShape = function addShape(shape) {
487
493
  var oldCollider = shape._collider;
488
494
  if (oldCollider !== this) {
495
+ var _this__scene;
489
496
  if (oldCollider) {
490
497
  oldCollider.removeShape(shape);
491
498
  }
492
499
  this._shapes.push(shape);
493
500
  shape._collider = this;
501
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._addColliderShape(shape);
494
502
  }
495
503
  };
496
504
  /**
@@ -498,8 +506,10 @@
498
506
  */ _proto.removeShape = function removeShape(shape) {
499
507
  var index = this._shapes.indexOf(shape);
500
508
  if (index !== -1) {
509
+ var _this__scene;
501
510
  this._shapes.splice(index, 1);
502
511
  shape._collider = null;
512
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._removeColliderShape(shape);
503
513
  }
504
514
  };
505
515
  /**
@@ -545,6 +555,23 @@
545
555
  }
546
556
  var _proto = LiteDynamicCollider.prototype;
547
557
  /**
558
+ * {@inheritDoc IDynamicCollider.getInertiaTensor }
559
+ */ _proto.getInertiaTensor = function getInertiaTensor(out) {
560
+ engine.Logger.error("Physics-lite don't support getInertiaTensor. Use Physics-PhysX instead!");
561
+ return out;
562
+ };
563
+ /**
564
+ * {@inheritDoc IDynamicCollider.getCenterOfMass }
565
+ */ _proto.getCenterOfMass = function getCenterOfMass(out) {
566
+ engine.Logger.error("Physics-lite don't support getCenterOfMass. Use Physics-PhysX instead!");
567
+ return out;
568
+ };
569
+ /**
570
+ * {@inheritDoc IDynamicCollider.setMassAndUpdateInertia }
571
+ */ _proto.setMassAndUpdateInertia = function setMassAndUpdateInertia(mass) {
572
+ engine.Logger.error("Physics-lite don't support setMassAndUpdateInertia. Use Physics-PhysX instead!");
573
+ };
574
+ /**
548
575
  * {@inheritDoc IDynamicCollider.addForce }
549
576
  */ _proto.addForce = function addForce(force) {
550
577
  throw "Physics-lite don't support addForce. Use Physics-PhysX instead!";
@@ -565,74 +592,111 @@
565
592
  throw "Physics-lite don't support putToSleep. Use Physics-PhysX instead!";
566
593
  };
567
594
  /**
595
+ * {@inheritDoc IDynamicCollider.isSleeping }
596
+ */ _proto.isSleeping = function isSleeping() {
597
+ throw "Physics-lite don't support isSleeping. Use Physics-PhysX instead!";
598
+ };
599
+ /**
600
+ * {@inheritDoc IDynamicCollider.getAngularDamping }
601
+ */ _proto.getAngularDamping = function getAngularDamping() {
602
+ throw "Physics-lite don't support getAngularDamping. Use Physics-PhysX instead!";
603
+ };
604
+ /**
568
605
  * {@inheritDoc IDynamicCollider.setAngularDamping }
569
606
  */ _proto.setAngularDamping = function setAngularDamping(value) {
570
- throw "Physics-lite don't support setAngularDamping. Use Physics-PhysX instead!";
607
+ engine.Logger.error("Physics-lite don't support setAngularDamping. Use Physics-PhysX instead!");
608
+ };
609
+ /**
610
+ * {@inheritDoc IDynamicCollider.getAngularVelocity }
611
+ */ _proto.getAngularVelocity = function getAngularVelocity(out) {
612
+ engine.Logger.error("Physics-lite don't support getAngularVelocity. Use Physics-PhysX instead!");
613
+ return out;
571
614
  };
572
615
  /**
573
616
  * {@inheritDoc IDynamicCollider.setAngularVelocity }
574
617
  */ _proto.setAngularVelocity = function setAngularVelocity(value) {
575
- throw "Physics-lite don't support setAngularVelocity. Use Physics-PhysX instead!";
618
+ engine.Logger.error("Physics-lite don't support setAngularVelocity. Use Physics-PhysX instead!");
576
619
  };
577
620
  /**
578
621
  * {@inheritDoc IDynamicCollider.setCenterOfMass }
579
622
  */ _proto.setCenterOfMass = function setCenterOfMass(value) {
580
- throw "Physics-lite don't support setCenterOfMass. Use Physics-PhysX instead!";
623
+ engine.Logger.error("Physics-lite don't support setCenterOfMass. Use Physics-PhysX instead!");
581
624
  };
582
625
  /**
583
626
  * {@inheritDoc IDynamicCollider.setCollisionDetectionMode }
584
627
  */ _proto.setCollisionDetectionMode = function setCollisionDetectionMode(value) {
585
- throw "Physics-lite don't support setCollisionDetectionMode. Use Physics-PhysX instead!";
628
+ engine.Logger.error("Physics-lite don't support setCollisionDetectionMode. Use Physics-PhysX instead!");
586
629
  };
587
630
  /**
588
631
  * {@inheritDoc IDynamicCollider.setConstraints }
589
632
  */ _proto.setConstraints = function setConstraints(flags) {
590
- throw "Physics-lite don't support setConstraints. Use Physics-PhysX instead!";
633
+ engine.Logger.error("Physics-lite don't support setConstraints. Use Physics-PhysX instead!");
591
634
  };
592
635
  /**
593
636
  * {@inheritDoc IDynamicCollider.setInertiaTensor }
594
637
  */ _proto.setInertiaTensor = function setInertiaTensor(value) {
595
- throw "Physics-lite don't support setInertiaTensor. Use Physics-PhysX instead!";
638
+ engine.Logger.error("Physics-lite don't support setInertiaTensor. Use Physics-PhysX instead!");
639
+ };
640
+ /**
641
+ * {@inheritDoc IDynamicCollider.setUseGravity }
642
+ */ _proto.setUseGravity = function setUseGravity(value) {
643
+ throw "Physics-lite don't support setUseGravity. Use Physics-PhysX instead!";
596
644
  };
597
645
  /**
598
646
  * {@inheritDoc IDynamicCollider.setIsKinematic }
599
647
  */ _proto.setIsKinematic = function setIsKinematic(value) {
600
- throw "Physics-lite don't support setIsKinematic. Use Physics-PhysX instead!";
648
+ engine.Logger.error("Physics-lite don't support setIsKinematic. Use Physics-PhysX instead!");
649
+ };
650
+ /**
651
+ * {@inheritDoc IDynamicCollider.setLinearDamping }
652
+ */ _proto.getLinearDamping = function getLinearDamping() {
653
+ throw "Physics-lite don't support getLinearDamping. Use Physics-PhysX instead!";
601
654
  };
602
655
  /**
603
656
  * {@inheritDoc IDynamicCollider.setLinearDamping }
604
657
  */ _proto.setLinearDamping = function setLinearDamping(value) {
605
- throw "Physics-lite don't support setLinearDamping. Use Physics-PhysX instead!";
658
+ engine.Logger.error("Physics-lite don't support setLinearDamping. Use Physics-PhysX instead!");
659
+ };
660
+ /**
661
+ * {@inheritDoc IDynamicCollider.getLinearVelocity }
662
+ */ _proto.getLinearVelocity = function getLinearVelocity(out) {
663
+ engine.Logger.error("Physics-lite don't support getLinearVelocity. Use Physics-PhysX instead!");
664
+ return out;
606
665
  };
607
666
  /**
608
667
  * {@inheritDoc IDynamicCollider.setLinearVelocity }
609
668
  */ _proto.setLinearVelocity = function setLinearVelocity(value) {
610
- throw "Physics-lite don't support setLinearVelocity. Use Physics-PhysX instead!";
669
+ engine.Logger.error("Physics-lite don't support setLinearVelocity. Use Physics-PhysX instead!");
611
670
  };
612
671
  /**
613
672
  * {@inheritDoc IDynamicCollider.setMass }
614
673
  */ _proto.setMass = function setMass(value) {
615
- throw "Physics-lite don't support setMass. Use Physics-PhysX instead!";
674
+ engine.Logger.error("Physics-lite don't support setMass. Use Physics-PhysX instead!");
616
675
  };
617
676
  /**
618
677
  * {@inheritDoc IDynamicCollider.setMaxAngularVelocity }
619
678
  */ _proto.setMaxAngularVelocity = function setMaxAngularVelocity(value) {
620
- throw "Physics-lite don't support setMaxAngularVelocity. Use Physics-PhysX instead!";
679
+ engine.Logger.error("Physics-lite don't support setMaxAngularVelocity. Use Physics-PhysX instead!");
680
+ };
681
+ /**
682
+ * {@inheritDoc IDynamicCollider.setMaxDepenetrationVelocity }
683
+ */ _proto.getMaxDepenetrationVelocity = function getMaxDepenetrationVelocity() {
684
+ throw "Physics-lite don't support getMaxDepenetrationVelocity. Use Physics-PhysX instead!";
621
685
  };
622
686
  /**
623
687
  * {@inheritDoc IDynamicCollider.setMaxDepenetrationVelocity }
624
688
  */ _proto.setMaxDepenetrationVelocity = function setMaxDepenetrationVelocity(value) {
625
- throw "Physics-lite don't support setMaxDepenetrationVelocity. Use Physics-PhysX instead!";
689
+ engine.Logger.error("Physics-lite don't support setMaxDepenetrationVelocity. Use Physics-PhysX instead!");
626
690
  };
627
691
  /**
628
692
  * {@inheritDoc IDynamicCollider.setSleepThreshold }
629
693
  */ _proto.setSleepThreshold = function setSleepThreshold(value) {
630
- throw "Physics-lite don't support setSleepThreshold. Use Physics-PhysX instead!";
694
+ engine.Logger.error("Physics-lite don't support setSleepThreshold. Use Physics-PhysX instead!");
631
695
  };
632
696
  /**
633
697
  * {@inheritDoc IDynamicCollider.setSolverIterations }
634
698
  */ _proto.setSolverIterations = function setSolverIterations(value) {
635
- throw "Physics-lite don't support setSolverIterations. Use Physics-PhysX instead!";
699
+ engine.Logger.error("Physics-lite don't support setSolverIterations. Use Physics-PhysX instead!");
636
700
  };
637
701
  /**
638
702
  * {@inheritDoc IDynamicCollider.wakeUp }
@@ -690,11 +754,11 @@
690
754
 
691
755
  /**
692
756
  * Box collider shape in Lite.
693
- */ var LiteBoxColliderShape = /*#__PURE__*/ function(LiteColliderShape) {
694
- _inherits(LiteBoxColliderShape, LiteColliderShape);
757
+ */ var LiteBoxColliderShape = /*#__PURE__*/ function(LiteColliderShape1) {
758
+ _inherits(LiteBoxColliderShape, LiteColliderShape1);
695
759
  function LiteBoxColliderShape(uniqueID, size, material) {
696
760
  var _this;
697
- _this = LiteColliderShape.call(this) || this, _this._halfSize = new engine.Vector3(), _this._sizeScale = new engine.Vector3(1, 1, 1), /** @internal */ _this._boxMin = new engine.Vector3(-0.5, -0.5, -0.5), /** @internal */ _this._boxMax = new engine.Vector3(0.5, 0.5, 0.5);
761
+ _this = LiteColliderShape1.call(this) || this, _this._halfSize = new engine.Vector3(), _this._sizeScale = new engine.Vector3(1, 1, 1), /** @internal */ _this._boxMin = new engine.Vector3(-0.5, -0.5, -0.5), /** @internal */ _this._boxMax = new engine.Vector3(0.5, 0.5, 0.5);
698
762
  _this._id = uniqueID;
699
763
  _this._halfSize.set(size.x * 0.5, size.y * 0.5, size.z * 0.5);
700
764
  _this._setBondingBox();
@@ -704,13 +768,13 @@
704
768
  /**
705
769
  * {@inheritDoc IColliderShape.setPosition }
706
770
  */ _proto.setPosition = function setPosition(position) {
707
- LiteColliderShape.prototype.setPosition.call(this, position);
771
+ LiteColliderShape1.prototype.setPosition.call(this, position);
708
772
  this._setBondingBox();
709
773
  };
710
774
  /**
711
775
  * {@inheritDoc IColliderShape.setWorldScale }
712
776
  */ _proto.setWorldScale = function setWorldScale(scale) {
713
- LiteColliderShape.prototype.setWorldScale.call(this, scale);
777
+ LiteColliderShape1.prototype.setWorldScale.call(this, scale);
714
778
  this._sizeScale.set(Math.abs(scale.x), Math.abs(scale.y), Math.abs(scale.z));
715
779
  this._setBondingBox();
716
780
  };
@@ -721,6 +785,44 @@
721
785
  this._setBondingBox();
722
786
  };
723
787
  /**
788
+ * {@inheritDoc IColliderShape.pointDistance }
789
+ */ _proto.pointDistance = function pointDistance(point) {
790
+ var position = LiteColliderShape._tempPos;
791
+ var rotation = LiteColliderShape._tempRot;
792
+ this._transform.worldMatrix.decompose(position, rotation, LiteColliderShape._tempScale);
793
+ var _this__transform = this._transform, shapePosition = _this__transform.position;
794
+ var m = LiteBoxColliderShape._tempMatrix;
795
+ var invM = LiteBoxColliderShape._tempInvMatrix;
796
+ var p = LiteColliderShape._tempPoint;
797
+ var scale = this._sizeScale;
798
+ var boundingBox = LiteBoxColliderShape._tempBox;
799
+ var _this = this, _boxMin = _this._boxMin, _boxMax = _this._boxMax;
800
+ p.copyFrom(_boxMin);
801
+ p.subtract(shapePosition);
802
+ p.divide(scale);
803
+ boundingBox.min.copyFrom(p);
804
+ p.copyFrom(_boxMax);
805
+ p.subtract(shapePosition);
806
+ p.divide(scale);
807
+ boundingBox.max.copyFrom(p);
808
+ engine.Matrix.affineTransformation(scale, rotation, position, m);
809
+ engine.Matrix.invert(m, invM);
810
+ engine.Vector3.transformCoordinate(point, invM, p);
811
+ var min = boundingBox.min;
812
+ var max = boundingBox.max;
813
+ p.x = Math.max(min.x, Math.min(p.x, max.x));
814
+ p.y = Math.max(min.y, Math.min(p.y, max.y));
815
+ p.z = Math.max(min.z, Math.min(p.z, max.z));
816
+ engine.Vector3.transformCoordinate(p, m, p);
817
+ var res = LiteColliderShape._tempVector4;
818
+ if (engine.Vector3.equals(p, point)) {
819
+ res.set(point.x, point.y, point.z, 0);
820
+ } else {
821
+ res.set(p.x, p.y, p.z, engine.Vector3.distanceSquared(p, point));
822
+ }
823
+ return res;
824
+ };
825
+ /**
724
826
  * @internal
725
827
  */ _proto._raycast = function _raycast(ray, hit) {
726
828
  var localRay = this._getLocalRay(ray);
@@ -747,14 +849,16 @@
747
849
  return LiteBoxColliderShape;
748
850
  }(LiteColliderShape);
749
851
  LiteBoxColliderShape._tempBox = new engine.BoundingBox();
852
+ LiteBoxColliderShape._tempMatrix = new engine.Matrix();
853
+ LiteBoxColliderShape._tempInvMatrix = new engine.Matrix();
750
854
 
751
855
  /**
752
856
  * Sphere collider shape in Lite.
753
- */ var LiteSphereColliderShape = /*#__PURE__*/ function(LiteColliderShape) {
754
- _inherits(LiteSphereColliderShape, LiteColliderShape);
857
+ */ var LiteSphereColliderShape = /*#__PURE__*/ function(LiteColliderShape1) {
858
+ _inherits(LiteSphereColliderShape, LiteColliderShape1);
755
859
  function LiteSphereColliderShape(uniqueID, radius, material) {
756
860
  var _this;
757
- _this = LiteColliderShape.call(this) || this, _this._radius = 1, _this._maxScale = 1;
861
+ _this = LiteColliderShape1.call(this) || this, _this._radius = 1, _this._maxScale = 1;
758
862
  _this._radius = radius;
759
863
  _this._id = uniqueID;
760
864
  return _this;
@@ -768,10 +872,31 @@
768
872
  /**
769
873
  * {@inheritDoc IColliderShape.setWorldScale }
770
874
  */ _proto.setWorldScale = function setWorldScale(scale) {
771
- LiteColliderShape.prototype.setWorldScale.call(this, scale);
875
+ LiteColliderShape1.prototype.setWorldScale.call(this, scale);
772
876
  this._maxScale = Math.max(Math.abs(scale.x), Math.abs(scale.y), Math.abs(scale.z));
773
877
  };
774
878
  /**
879
+ * {@inheritDoc IColliderShape.pointDistance }
880
+ */ _proto.pointDistance = function pointDistance(point) {
881
+ var position = LiteColliderShape._tempPos;
882
+ var worldRadius = this.worldRadius;
883
+ this._transform.worldMatrix.decompose(position, LiteColliderShape._tempRot, LiteColliderShape._tempScale);
884
+ var p = LiteColliderShape._tempPoint;
885
+ engine.Vector3.subtract(point, position, p);
886
+ var distanceFromCenter = p.lengthSquared();
887
+ var direction = p.normalize();
888
+ engine.Vector3.scale(direction, worldRadius, p);
889
+ p.add(position);
890
+ var res = LiteColliderShape._tempVector4;
891
+ var distanceSquared = engine.Vector3.distanceSquared(p, point);
892
+ if (distanceFromCenter <= worldRadius * worldRadius) {
893
+ res.set(point.x, point.y, point.z, 0);
894
+ } else {
895
+ res.set(p.x, p.y, p.z, distanceSquared);
896
+ }
897
+ return res;
898
+ };
899
+ /**
775
900
  * @internal
776
901
  */ _proto._raycast = function _raycast(ray, hit) {
777
902
  var boundingSphere = LiteSphereColliderShape._tempSphere;
@@ -822,41 +947,26 @@
822
947
  console.log("Physics-lite don't support gravity. Use Physics-PhysX instead!");
823
948
  };
824
949
  /**
825
- * {@inheritDoc IPhysicsManager.addColliderShape }
826
- */ _proto.addColliderShape = function addColliderShape(colliderShape) {
827
- this._eventMap[colliderShape._id] = {};
828
- };
829
- /**
830
- * {@inheritDoc IPhysicsManager.removeColliderShape }
831
- */ _proto.removeColliderShape = function removeColliderShape(colliderShape) {
832
- var _this = this, eventPool = _this._eventPool, currentEvents = _this._currentEvents, eventMap = _this._eventMap;
833
- var id = colliderShape._id;
834
- currentEvents.forEach(function(event, i) {
835
- if (event.index1 == id) {
836
- currentEvents.deleteByIndex(i);
837
- eventPool.push(event);
838
- } else if (event.index2 == id) {
839
- currentEvents.deleteByIndex(i);
840
- eventPool.push(event);
841
- // If the shape is big index, should clear from the small index shape subMap
842
- eventMap[event.index1][id] = undefined;
843
- }
844
- });
845
- delete eventMap[id];
846
- };
847
- /**
848
950
  * {@inheritDoc IPhysicsManager.addCollider }
849
951
  */ _proto.addCollider = function addCollider(actor) {
952
+ actor._scene = this;
850
953
  var colliders = actor._isStaticCollider ? this._staticColliders : this._dynamicColliders;
851
954
  colliders.push(actor);
955
+ var shapes = actor._shapes;
956
+ for(var i = 0, n = shapes.length; i < n; i++){
957
+ this._addColliderShape(shapes[i]);
958
+ }
852
959
  };
853
960
  /**
854
961
  * {@inheritDoc IPhysicsManager.removeCollider }
855
962
  */ _proto.removeCollider = function removeCollider(collider) {
963
+ collider._scene = null;
856
964
  var colliders = collider._isStaticCollider ? this._staticColliders : this._dynamicColliders;
857
965
  var index = colliders.indexOf(collider);
858
- if (index !== -1) {
859
- colliders.splice(index, 1);
966
+ index > -1 && colliders.splice(index, 1);
967
+ var shapes = collider._shapes;
968
+ for(var i = 0, n = shapes.length; i < n; i++){
969
+ this._removeColliderShape(shapes[i]);
860
970
  }
861
971
  };
862
972
  /**
@@ -904,6 +1014,29 @@
904
1014
  */ _proto.removeCharacterController = function removeCharacterController(characterController) {
905
1015
  throw "Physics-lite don't support removeCharacterController. Use Physics-PhysX instead!";
906
1016
  };
1017
+ /**
1018
+ * @internal
1019
+ */ _proto._addColliderShape = function _addColliderShape(colliderShape) {
1020
+ this._eventMap[colliderShape._id] = {};
1021
+ };
1022
+ /**
1023
+ * @internal
1024
+ */ _proto._removeColliderShape = function _removeColliderShape(colliderShape) {
1025
+ var _this = this, eventPool = _this._eventPool, currentEvents = _this._currentEvents, eventMap = _this._eventMap;
1026
+ var id = colliderShape._id;
1027
+ currentEvents.forEach(function(event, i) {
1028
+ if (event.index1 == id) {
1029
+ currentEvents.deleteByIndex(i);
1030
+ eventPool.push(event);
1031
+ } else if (event.index2 == id) {
1032
+ currentEvents.deleteByIndex(i);
1033
+ eventPool.push(event);
1034
+ // If the shape is big index, should clear from the small index shape subMap
1035
+ eventMap[event.index1][id] = undefined;
1036
+ }
1037
+ });
1038
+ delete eventMap[id];
1039
+ };
907
1040
  _proto._getTrigger = function _getTrigger(index1, index2) {
908
1041
  var event;
909
1042
  if (this._eventPool.length) {
@@ -1165,7 +1298,7 @@
1165
1298
  }();
1166
1299
 
1167
1300
  //@ts-ignore
1168
- var version = "1.4.0-alpha.0";
1301
+ var version = "1.4.0-alpha.2";
1169
1302
  console.log("Galacean PhysicsLite version: " + version);
1170
1303
 
1171
1304
  exports.LitePhysics = LitePhysics;