Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update source/Touch.js

  • Loading branch information...
commit db7655f0e0f6af18bac25c7d2f8546983cead6b5 1 parent 4e3787f
Mient-jan Stelling authored
Showing with 97 additions and 97 deletions.
  1. +97 −97 source/Touch.js
194 source/Touch.js
View
@@ -235,48 +235,48 @@ Touch.pos_move_v = null;
Touch.move_v = null;
/**
- * Vector2
- * ---------
+ * Vector2
+ * ---------
*
- * author Mient-jan Stelling <mientjan.stelling@gmail.com>
+ * author Mient-jan Stelling <mientjan.stelling@gmail.com>
**/
-
+
function Vector2(x,y)
{
- this.x = x;
- this.y = y;
+ this.x = x;
+ this.y = y;
}
Vector2.prototype.toString = function()
{
- return '(' + this.x + ', ' + this.y + ')';
+ return '(' + this.x + ', ' + this.y + ')';
}
-
+
// calls a trace of toString
Vector2.prototype.dump = function()
{
- console.log(this.toString());
+ console.log(this.toString());
}
-
- //----- these functions return Vector2s -----
-
+
+ //----- these functions return Vector2s -----
+
Vector2.prototype.clone = function()
{
- var v = new Vector2(this.x, this.y);
- return v;
+ var v = new Vector2(this.x, this.y);
+ return v;
}
-
+
Vector2.prototype.plus = function(v2)
{
- var v = new Vector2( this.x + v2.x, this.y + v2.y)
- return v;
+ var v = new Vector2( this.x + v2.x, this.y + v2.y)
+ return v;
}
-
- //return this-v2
+
+ //return this-v2
Vector2.prototype.minus = function(v2)
{
- var v = new Vector2( this.x - v2.x, this.y - v2.y)
- return v;
+ var v = new Vector2( this.x - v2.x, this.y - v2.y)
+ return v;
}
/**
@@ -285,87 +285,87 @@ Vector2.prototype.minus = function(v2)
* @return float
*/
Vector2.prototype.degre = function(v2){
- var v = this.minus(v2);
- var a = Math.atan2(v.y, v.x);
- return (a*(180/Math.PI))+180;
+ var v = this.minus(v2);
+ var a = Math.atan2(v.y, v.x);
+ return (a*(180/Math.PI))+180;
};
//return the righthand normal of this (using flash's coordinate system)
Vector2.prototype.normR = function()
{
- var v = new Vector2( this.y * -1, this.x);
- return v;
+ var v = new Vector2( this.y * -1, this.x);
+ return v;
}
-
+
//return the leftHand normal of this (using flash's coordinate system)
Vector2.prototype.normL = function()
{
- var v = new Vector2( this.y, -1 * this.x);
- return v;
+ var v = new Vector2( this.y, -1 * this.x);
+ return v;
}
-
+
//return the (unit) direction vector of this
Vector2.prototype.dir = function()
{
- var v = this.clone();
- v.normalize();
- return v;
+ var v = this.clone();
+ v.normalize();
+ return v;
}
-
- //return this projected _onto_ v2
+
+ //return this projected _onto_ v2
Vector2.prototype.proj = function(v2){
- var den = v2.dot(v2);
- if(den == 0)
- {
-  //zero-length v2
-  console.log("WARNING! Vector2.proj() was given a zero-length projection vector!");
-  return this.clone();//not sure how to gracefully recover but, hopefully this will be okay
- }
- else
- {
-  var v = v2.clone();
-  v.mult(this.dot(v2)/den);
-  return v;
- }
+ var den = v2.dot(v2);
+ if(den == 0)
+ {
+ //zero-length v2
+ console.log("WARNING! Vector2.proj() was given a zero-length projection vector!");
+ return this.clone();//not sure how to gracefully recover but, hopefully this will be okay
+ }
+ else
+ {
+ var v = v2.clone();
+ v.mult(this.dot(v2)/den);
+ return v;
+ }
}
//----- these functions return scalars -----
//return the magnitude (absval) of this projected onto v2
Vector2.prototype.projLen = function(v2){
- var den = v2.dot(v2);
- if(den == 0)
- {
-  //zero-length v2
-  console.log("WARNING! Vector2.projLen() was given a zero-length projection vector!");
-  return 0;
- }
- else
- {
-  return Math.abs(this.dot(v2)/v2.len());
- }
+ var den = v2.dot(v2);
+ if(den == 0)
+ {
+ //zero-length v2
+ console.log("WARNING! Vector2.projLen() was given a zero-length projection vector!");
+ return 0;
+ }
+ else
+ {
+ return Math.abs(this.dot(v2)/v2.len());
+ }
}
//return the dotprod of this and v2
Vector2.prototype.dot = function(v2){
- return ((this.x * v2.x) + (this.y * v2.y));
+ return ((this.x * v2.x) + (this.y * v2.y));
}
//return the crossprod of this and v2
//note that this is equivalent to the dotprod of this and the lefthand normal of v2
Vector2.prototype.cross = function(v2){
- return ((this.x * v2.y) - (this.y * v2.x));
+ return ((this.x * v2.y) - (this.y * v2.x));
}
///return the length of this
Vector2.prototype.len = function()
{
- return (Math.sqrt((this.x*this.x) + (this.y*this.y)));
+ return (Math.sqrt((this.x*this.x) + (this.y*this.y)));
}
///return the angle of the vector
Vector2.prototype.angle = function(v2){
- return (Math.acos((this.dot(v2))/(this.len()*v2.len()))/Math.PI * 180);
+ return (Math.acos((this.dot(v2))/(this.len()*v2.len()))/Math.PI * 180);
}
//----- these functions return nothing (they operate on this) -----
@@ -373,68 +373,68 @@ Vector2.prototype.angle = function(v2){
//change this to a duplicate of v2
Vector2.prototype.copy = function(v2)
{
- this.x = v2.x;
- this.y = v2.y;
+ this.x = v2.x;
+ this.y = v2.y;
}
//multiply this by a scalar s
Vector2.prototype.mult = function(s)
{
- this.x *= s;
- this.y *= s;
+ this.x *= s;
+ this.y *= s;
}
//convert this vector to a unit/direction vector
Vector2.prototype.normalize = function()
{
- var L = this.len();
- if(L != 0)
- {
-  this.x /= L;
-  this.y /= L;
- }
- else
- {
-  console.log("WARNING! Vector2.normalize() was called on a zero-length vector!");
- }
+ var L = this.len();
+ if(L != 0)
+ {
+ this.x /= L;
+ this.y /= L;
+ }
+ else
+ {
+ console.log("WARNING! Vector2.normalize() was called on a zero-length vector!");
+ }
}
//add v2 to this
Vector2.prototype.pluseq = function(v2) {
- this.x += v2.x;
- this.y += v2.y;
+ this.x += v2.x;
+ this.y += v2.y;
}
//subtract v2 from this
Vector2.prototype.minuseq = function(v2) {
- this.x -= v2.x;
- this.y -= v2.y;
+ this.x -= v2.x;
+ this.y -= v2.y;
}
Vector2.prototype.topLeft = function(v2){
- var x   = Math.min(this.x, v2.x);
- var y   = Math.min(this.y, v2.y);
-
- return new Vector2(x, y);
+ var x = Math.min(this.x, v2.x);
+ var y = Math.min(this.y, v2.y);
+
+ return new Vector2(x, y);
}
Vector2.prototype.topRight = function(v2){
- var x   = Math.max(this.x, v2.x);
- var y   = Math.min(this.y, v2.y);
-
- return new Vector2(x, y);
+ var x = Math.max(this.x, v2.x);
+ var y = Math.min(this.y, v2.y);
+
+ return new Vector2(x, y);
}
Vector2.prototype.bottomRight = function(v2){
- var x   = Math.max(this.x, v2.x);
- var y   = Math.max(this.y, v2.y);
-
- return new Vector2(x, y);
+ var x = Math.max(this.x, v2.x);
+ var y = Math.max(this.y, v2.y);
+
+ return new Vector2(x, y);
}
Vector2.prototype.bottomLeft = function(v2){
- var x   = Math.min(this.x, v2.x);
- var y   = Math.max(this.y, v2.y);
-
- return new Vector2(x, y);
+ var x = Math.min(this.x, v2.x);
+ var y = Math.max(this.y, v2.y);
+
+ return new Vector2(x, y);
}
Please sign in to comment.
Something went wrong with that request. Please try again.