Source: Renderables/TextureRenderable_PixelCollision.js

  1. /* File: TextureRenderable_PixelCollision.js
  2. *
  3. * Implements the pixelTouches() and related supporting functions of TextureRenderable
  4. */
  5. /*jslint node: true, vars: true */
  6. /*global gEngine, TextureRenderable, vec2 */
  7. /* find out more about jslint: http://www.jslint.com/help.html */
  8. "use strict"; // Operate in Strict mode such that variables must be declared before used!
  9. /**
  10. * Implements the pixelTouches() and related supporting functions of TextureRenderable
  11. * @class TextureRenderable
  12. * @param {TextureRenderable} other to check for collision with
  13. * @param {vec2} wcTouchPos world coordinate position of first collision
  14. * @returns {Boolean} true if collision is detected
  15. */
  16. TextureRenderable.prototype.pixelTouches = function(other, wcTouchPos) {
  17. var pixelTouch = false;
  18. var xIndex = 0, yIndex;
  19. var otherIndex = [0, 0];
  20. var xDir = [1, 0];
  21. var yDir = [0, 1];
  22. var otherXDir = [1, 0];
  23. var otherYDir = [0, 1];
  24. vec2.rotate(xDir, xDir, this.mXform.getRotationInRad());
  25. vec2.rotate(yDir, yDir, this.mXform.getRotationInRad());
  26. vec2.rotate(otherXDir, otherXDir, other.mXform.getRotationInRad());
  27. vec2.rotate(otherYDir, otherYDir, other.mXform.getRotationInRad());
  28. while ((!pixelTouch) && (xIndex < this.mTexWidth)) {
  29. yIndex = 0;
  30. while ((!pixelTouch) && (yIndex < this.mTexHeight)) {
  31. if (this._pixelAlphaValue(xIndex, yIndex) > 0) {
  32. this._indexToWCPosition(wcTouchPos, xIndex, yIndex, xDir, yDir);
  33. other._wcPositionToIndex(otherIndex, wcTouchPos, otherXDir, otherYDir);
  34. if ((otherIndex[0] >= 0) && (otherIndex[0] < other.mTexWidth) &&
  35. (otherIndex[1] >= 0) && (otherIndex[1] < other.mTexHeight)) {
  36. pixelTouch = other._pixelAlphaValue(otherIndex[0], otherIndex[1]) > 0;
  37. }
  38. }
  39. yIndex++;
  40. }
  41. xIndex++;
  42. }
  43. return pixelTouch;
  44. };
  45. /**
  46. * Get the color array from the GPU and set it to the renderables Color Array
  47. * @memberOf TextureRenderable
  48. * @returns {void}
  49. */
  50. TextureRenderable.prototype.setColorArray = function () {
  51. if (this.mColorArray === null) {
  52. this.mColorArray = gEngine.Textures.getColorArray(this.mTexture);
  53. }
  54. };
  55. TextureRenderable.prototype._pixelAlphaValue = function (x, y) {
  56. y += this.mTexBottomIndex;
  57. x += this.mTexLeftIndex;
  58. x = x * 4;
  59. y = y * 4;
  60. return this.mColorArray[(y * this.mTextureInfo.mWidth) + x + 3];
  61. };
  62. TextureRenderable.prototype._wcPositionToIndex = function (returnIndex, wcPos, xDir, yDir) {
  63. // use wcPos to compute the corresponding returnIndex[0 and 1]
  64. var delta = [];
  65. vec2.sub(delta, wcPos, this.mXform.getPosition());
  66. var xDisp = vec2.dot(delta, xDir);
  67. var yDisp = vec2.dot(delta, yDir);
  68. returnIndex[0] = this.mTexWidth * (xDisp / this.mXform.getWidth());
  69. returnIndex[1] = this.mTexHeight * (yDisp / this.mXform.getHeight());
  70. // recall that xForm.getPosition() returns center, yet
  71. // Texture origin is at lower-left corner!
  72. returnIndex[0] += this.mTexWidth / 2;
  73. returnIndex[1] += this.mTexHeight / 2;
  74. returnIndex[0] = Math.floor(returnIndex[0]);
  75. returnIndex[1] = Math.floor(returnIndex[1]);
  76. };
  77. TextureRenderable.prototype._indexToWCPosition = function (returnWCPos, i, j, xDir, yDir) {
  78. var x = i * this.mXform.getWidth() / this.mTexWidth;
  79. var y = j * this.mXform.getHeight() / this.mTexHeight;
  80. var xDisp = x - (this.mXform.getWidth() * 0.5);
  81. var yDisp = y - (this.mXform.getHeight() * 0.5);
  82. var xDirDisp = [];
  83. var yDirDisp = [];
  84. vec2.scale(xDirDisp, xDir, xDisp);
  85. vec2.scale(yDirDisp, yDir, yDisp);
  86. vec2.add(returnWCPos, this.mXform.getPosition(), xDirDisp);
  87. vec2.add(returnWCPos, returnWCPos, yDirDisp);
  88. };
  89. //--- end of Public Methods
  90. //</editor-fold>