Advertisement
Guest User

dafadfs

a guest
Nov 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. /**
  2. * LinearAnimation
  3. * @constructor
  4. */
  5. function LinearAnimation(scene,args) {
  6. (args.length == 2 && args[0] === Array && args[1] === Number) ? null : console.log("Error");
  7. this.scene = scene;
  8. this.args = args;
  9. this.timeStart = new Date().getTime()/1000; //Conversão para segundos
  10. this.time = 0; //Tempo (em segundos) que decorreu na animação
  11. this.controlPoints = args[0];
  12. this.endFlag = false; //Flag para determinar se uma animação terminou
  13. this.speed = args[1];
  14. this.currentPoint = 0;
  15. this.timePerPoint = []; //Quanto tempo dura cada trajeto.
  16. this.timeOffset = 0; //Usado para simplificar o cálculo de distancia percorrida
  17. this.endMat = mat4.create();
  18. this.calculateTimePerPoint(this.controlPoints, this.speed);
  19. /*Estrutura de dados para guardar*/
  20. this.yDeg = []/*os ângulos de cada trajeto*/
  21. this.unitVectors = this.calculateUnitsDegrees(this.controlPoints);
  22. }
  23.  
  24. LinearAnimation.prototype = Object.create(Animation.prototype);
  25. LinearAnimation.prototype.constructor=LinearAnimation;
  26.  
  27.  
  28. LinearAnimation.prototype.animate = function(){
  29. if(!this.endFlag){ //Animação já terminou?
  30. if(this.time > this.timePerPoint[this.currentPoint]){ //Verificar se já excedemos o ponto de controlo atual
  31. this.currentPoint++;
  32. if(this.currentPoint == this.controlPoints.length - 1){ //Se chegamos ao fim retorna-mos a posição do ultimo ponto de controlo
  33. this.endFlag = true;
  34. mat4.translate(this.endMat, this.endMat, this.controlPoints[this.controlPoints.length - 1]);
  35. mat4.rotateY(this.endMat, this.endMat, this.yDeg[this.currentPoint-1])
  36. return this.endMat; //Retornar matrix com a posição final
  37. }
  38. this.timeOffset = this.timePerPoint[this.currentPoint - 1] //Apartir de cada novo ponto de controlo assumimos um deltaT inicial = 0
  39. }
  40. let cp = this.controlPoints[this.currentPoint]; //Ponto de controlo atual
  41. let uv = this.unitVectors[this.currentPoint]; //Vetor unitário de direcção entre os dois pontos de controlo
  42. var translate = [
  43. cp[0]+uv[0]*this.speed*(this.time - this.timeOffset),
  44. cp[1]+uv[1]*this.speed*(this.time - this.timeOffset),
  45. cp[2]+uv[2]*this.speed*(this.time - this.timeOffset)
  46. ];
  47. let tMat = mat4.create();
  48. mat4.translate(tMat, tMat, translate);
  49. mat4.rotateY(tMat, tMat, this.yDeg[this.currentPoint]);
  50.  
  51. this.time = new Date().getTime()/1000 - this.timeStart;
  52. return tMat;
  53. }
  54. else{
  55. return this.endMat; //Após o fim this.initMat tem valor igual ao ultimo ponto de controlo.
  56. }
  57. }
  58.  
  59. LinearAnimation.prototype.calculateTimePerPoint = function (traj, speed) {
  60. let length = 0;
  61. let totalTime = 0;
  62. for (var i = 0; i < traj.length - 1; i++) { //obter magnitudes de todos os control points
  63. v1 = traj[i];
  64. v2 = traj[i + 1];
  65. var mag = Math.sqrt(
  66. Math.pow((v2[0] - v1[0]), 2) +
  67. Math.pow((v2[1] - v1[1]), 2) +
  68. Math.pow((v2[2] - v1[2]), 2)
  69. );
  70. length += mag;
  71. totalTime += mag / speed;
  72. this.timePerPoint.push(totalTime); //Quanto tempo passar em cada caminho entre ponto de controlo
  73. }
  74. }
  75.  
  76. LinearAnimation.prototype.calculateUnitsDegrees = function (traj) { //Obter direcções e orientações do trajeto
  77. let uVectors = [];
  78. for (var i = 0; i < traj.length - 1; i++) { //Calcular vetor unitário para cada ponto de controlo
  79. v1 = traj[i];
  80. v2 = traj[i + 1];
  81. vs = [v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2]];
  82. var mag = Math.sqrt(
  83. Math.pow((vs[0]), 2) +
  84. Math.pow((vs[1]), 2) +
  85. Math.pow((vs[2]), 2)
  86. );
  87. uVec = [];
  88. for (var j = 0; j < 3; j++) {
  89. uVec.push(vs[j] / mag);
  90. }
  91. uVectors.push(uVec); //Adicionar vetor unitário
  92. this.yDeg.push(Math.atan2(vs[0], vs[2]));
  93. }
  94. return uVectors; //Retornar lista vetores unitários
  95. }
  96.  
  97. LinearAnimation.prototype.clone = function () {
  98. animationClone = new LinearAnimation(this.scene, this.args);
  99. return animationClone;
  100. }
  101.  
  102. LinearAnimation.prototype.restartTime = function () {
  103. this.timeStart = new Date().getTime() / 1000;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement