Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BezierAnimation extends Animation {
  2.     constructor(scene, speed, controlPoints) {
  3.         super(scene);
  4.         this.speed = speed;
  5.         this.P1 = controlPoints[0];
  6.         this.P2 = controlPoints[1];
  7.         this.P3 = controlPoints[2];
  8.         this.P4 = controlPoints[3];
  9.  
  10.         this.prevCoordinates = this._getCurrentCoordinates(-0.001);
  11.         this.prevAngle = 0;
  12.  
  13.         this.totalDistance = this._getTotalDistance();
  14.         this.totalTime = this.totalDistance / this.speed;
  15.     }
  16.  
  17.     getTransform(t) {
  18.         if (t > 1) {
  19.             t = 1;
  20.         }
  21.         let currentCoordinates = this._getCurrentCoordinates(t);
  22.         let deltaCoords = subtractArrays(currentCoordinates, this.prevCoordinates);
  23.         let angle = this._getXZOrientation(deltaCoords);
  24.         if (deltaCoords[0] == 0 && deltaCoords[2] == 0) {
  25.             angle = this.prevAngle;
  26.         }
  27.  
  28.         this.scene.pushMatrix();
  29.             this.scene.loadIdentity();
  30.             this.scene.translate(currentCoordinates[0],
  31.                                 currentCoordinates[1],
  32.                                 currentCoordinates[2]);
  33.             this.scene.rotate(angle, 0, 1, 0);
  34.             let transformMatrix = this.scene.getMatrix();
  35.         this.scene.popMatrix();
  36.        
  37.         if (t < 1) {
  38.             this.prevCoordinates = currentCoordinates;
  39.             this.prevAngle = angle;
  40.         }
  41.         return transformMatrix;
  42.     }
  43.  
  44.     _getXZOrientation(deltaCoords) {
  45.         let z = deltaCoords[2];
  46.         let x = deltaCoords[0];
  47.         if (z == 0) {
  48.             return Math.PI / 2;
  49.         } else {
  50.             return Math.atan(x/z);
  51.         }
  52.     }
  53.  
  54.     _getCurrentCoordinates(t) {
  55.         let x = this._calcBezierCoordinate(t, 0);
  56.         let y = this._calcBezierCoordinate(t, 1);
  57.         let z = this._calcBezierCoordinate(t, 2);
  58.  
  59.         return [x, y, z];
  60.     }
  61.  
  62.     _calcBezierCoordinate(t, coordInd) {
  63.         return (1 - t*t*t) * this.P1[coordInd]
  64.                 + (3 * t * (1 - t*t)) * this.P2[coordInd]
  65.                 + (3 * t*t * (1 - t)) * this.P3[coordInd]
  66.                 + (t*t*t) * this.P4[coordInd];
  67.     }
  68.  
  69.     /**
  70.      * Estimated distance: Average between chord and control net.
  71.      * Chord = Distance between start and destination.
  72.      * Control net = Sum of distances between each consecutive points.
  73.      */
  74.     _getTotalDistance() {
  75.         let chord = this._getDistance(this.P4, this.P1);
  76.         let controlNet = this._getDistance(this.P1, this.P2)
  77.                         + this._getDistance(this.P2, this.P3)
  78.                         + this._getDistance(this.P3, this.P4);
  79.         return (chord + controlNet) / 2;
  80.     }
  81.  
  82.     _getDistance(point1, point0) {
  83.         let distance = Math.pow(point1[0] - point0[0], 2);
  84.             distance += Math.pow(point1[1] - point0[1], 2);
  85.             distance += Math.pow(point1[2] - point0[2], 2);
  86.             distance = Math.sqrt(distance);
  87.         return distance;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement