/js/model/Point.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 222 lines · 107 code · 9 blank · 106 comment · 9 complexity · 9bb6157d46d45c3a1b613f172c784beb MD5 · raw file

  1. /**
  2. * @author Hyperandroid || http://hyperandroid.com/
  3. *
  4. * Hold a 2D point information.
  5. * Think about the possibility of turning CAAT.Point into {x:,y:}.
  6. *
  7. * (This is stolen from Hyperandroid's CAAT)
  8. **/
  9. (function () {
  10. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
  11. /**
  12. *
  13. * A point defined by two coordinates.
  14. *
  15. * @param xpos {number}
  16. * @param ypos {number}
  17. *
  18. * @constructor
  19. */
  20. RealtimeMultiplayerGame.model.Point = function (xpos, ypos) {
  21. this.x = xpos || 0;
  22. this.y = ypos || 0;
  23. return this;
  24. };
  25. RealtimeMultiplayerGame.model.Point.prototype = {
  26. x: 0,
  27. y: 0,
  28. /**
  29. * Sets this point coordinates.
  30. * @param x {number}
  31. * @param y {number}
  32. *
  33. * @return this
  34. */
  35. set: function (x, y) {
  36. this.x = x;
  37. this.y = y;
  38. return this;
  39. },
  40. /**
  41. * Create a new RealtimeMultiplayerGame.model.Point equal to this one.
  42. * @return {RealtimeMultiplayerGame.model.Point}
  43. */
  44. clone: function () {
  45. var p = new RealtimeMultiplayerGame.model.Point();
  46. p.set(this.x, this.y);
  47. return p;
  48. },
  49. /**
  50. * Translate this point to another position. The final point will be (point.x+x, point.y+y)
  51. * @param x {number}
  52. * @param y {number}
  53. *
  54. * @return this
  55. */
  56. translate: function (x, y) {
  57. this.x += x;
  58. this.y += y;
  59. return this;
  60. },
  61. /**
  62. * Translate this point to another point.
  63. * @param aPoint {RealtimeMultiplayerGame.model.Point}
  64. * @return this
  65. */
  66. translatePoint: function (aPoint) {
  67. this.x += aPoint.x;
  68. this.y += aPoint.y;
  69. return this;
  70. },
  71. /**
  72. * Substract a point from this one.
  73. * @param aPoint {RealtimeMultiplayerGame.model.Point}
  74. * @return this
  75. */
  76. subtract: function (aPoint) {
  77. this.x -= aPoint.x;
  78. this.y -= aPoint.y;
  79. return this;
  80. },
  81. /**
  82. * Substract a point from this one
  83. * Returns a new point with the difference
  84. * @param aPoint {RealtimeMultiplayerGame.model.Point}
  85. * @return {RealtimeMultiplayerGame.model.Point}
  86. */
  87. subtractClone: function (aPoint) {
  88. return new RealtimeMultiplayerGame.model.Point(this.x - aPoint.x, this.y - aPoint.y)
  89. },
  90. /**
  91. * Multiply this point by a scalar.
  92. * @param factor {number}
  93. * @return this
  94. */
  95. multiply: function (factor) {
  96. this.x *= factor;
  97. this.y *= factor;
  98. return this;
  99. },
  100. /**
  101. * Rotate this point by an angle. The rotation is held by (0,0) coordinate as center.
  102. * @param angle {number}
  103. * @return this
  104. */
  105. rotate: function (angle) {
  106. var x = this.x, y = this.y;
  107. this.x = x * Math.cos(angle) - Math.sin(angle) * y;
  108. this.y = x * Math.sin(angle) + Math.cos(angle) * y;
  109. return this;
  110. },
  111. /**
  112. *
  113. * @param angle {number}
  114. * @return this
  115. */
  116. setAngle: function (angle) {
  117. var len = this.getLength();
  118. this.x = Math.cos(angle) * len;
  119. this.y = Math.sin(angle) * len;
  120. return this;
  121. },
  122. /**
  123. *
  124. * @param length {number}
  125. * @return this
  126. */
  127. setLength: function (length) {
  128. var len = this.getLength();
  129. if (len)this.multiply(length / len);
  130. else this.x = this.y = length;
  131. return this;
  132. },
  133. /**
  134. * Normalize this point, that is, both set coordinates proportionally to values raning 0..1
  135. * @return this
  136. */
  137. normalize: function () {
  138. var len = this.getLength();
  139. this.x /= len;
  140. this.y /= len;
  141. return this;
  142. },
  143. /**
  144. * Return the angle from -Pi to Pi of this point.
  145. * @return {number}
  146. */
  147. getAngle: function () {
  148. return Math.atan2(this.y, this.x);
  149. },
  150. /**
  151. * Set this point coordinates proportinally to a maximum value.
  152. * @param max {number}
  153. * @return this
  154. */
  155. limit: function (max) {
  156. var aLenthSquared = this.getLengthSquared();
  157. if (aLenthSquared + 0.01 > max * max) {
  158. var aLength = Math.sqrt(aLenthSquared);
  159. this.x = (this.x / aLength) * max;
  160. this.y = (this.y / aLength) * max;
  161. }
  162. return this;
  163. },
  164. /**
  165. * Get this point's lenght.
  166. * @return {number}
  167. */
  168. getLength: function () {
  169. var length = Math.sqrt(this.x * this.x + this.y * this.y);
  170. if (length < 0.005 && length > -0.005) return 0.000001;
  171. return length;
  172. },
  173. /**
  174. * Get this point's squared length.
  175. * @return {number}
  176. */
  177. getLengthSquared: function () {
  178. var lengthSquared = this.x * this.x + this.y * this.y;
  179. if (lengthSquared < 0.005 && lengthSquared > -0.005) return 0;
  180. return lengthSquared;
  181. },
  182. /**
  183. * Get the distance between two points.
  184. * @param point {RealtimeMultiplayerGame.model.Point}
  185. * @return {number}
  186. */
  187. getDistance: function (point) {
  188. var deltaX = this.x - point.x;
  189. var deltaY = this.y - point.y;
  190. return Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
  191. },
  192. /**
  193. * Get the squared distance between two points.
  194. * @param point {RealtimeMultiplayerGame.model.Point}
  195. * @return {number}
  196. */
  197. getDistanceSquared: function (point) {
  198. var deltaX = this.x - point.x;
  199. var deltaY = this.y - point.y;
  200. return (deltaX * deltaX) + (deltaY * deltaY);
  201. },
  202. /**
  203. * Get a string representation.
  204. * @return {string}
  205. */
  206. toString: function () {
  207. return "(RealtimeMultiplayerGame.model.Point)" +
  208. " x:'" + String(Math.round(Math.floor(this.x * 10)) / 10) +
  209. " y:" + String(Math.round(Math.floor(this.y * 10)) / 10);
  210. }
  211. };
  212. RealtimeMultiplayerGame.model.Point.prototype.ZERO = new RealtimeMultiplayerGame.model.Point(0, 0);
  213. })();