Geometry Include

The Geometry Include file contains a whole bunch of various classes used for defining points in space and shapes. At present there are only 3 classes contained within the file, but all three are used extensivly through the game;

  • Vector2d
  • Vector3d
  • Rectangle

Vector2d is used throughout for defining velocity of objects.  This has a bunch of utility functions like add, normalise, dotproduct etc which are pretty standard methods which I saw come up again and again across various math websites and that I used in various other approaches to collisions.

function Vector2d(x, y) {
  this.x = x;
  this.y = y;
}
Vector2d.prototype.Add = function(v) {
  return new Vector2d(parseFloat(this.x) + parseFloat(v.x), parseFloat(this.y) + parseFloat(v.y));
}
// http://gpwiki.org/index.php/VB:Tutorials:Building_A_Simple_Physics_Engine#Collision_Detection
Vector2d.prototype.Magnitude = function () {
  return Math.sqrt(this.x * this.x + this.y * this.y);
}
Vector2d.prototype.Normalize = function () {
  var mag = Math.sqrt(A.X * A.X + A.Y * A.Y)
  if (mag != 0) {
   this.x = this.x / mag;
   this.y = this.y / mag;
  }
}
Vector2d.prototype.DotProduct = function(v) {
  return this.x * v.x + this.y * v.y;
}
Vector2d.prototype.Distance = function(v) {
  return Math.sqrt((this.x - v.x) * (this.x - v.x) + (this.y - v.y) * (this.y - v.y));
}

Vector3d inherits from Vector2d adding the extra dimention and is used to define the position.  Even though it was a totally 2d game, I used a 3D vector for position becuase the Z axis was used to define which order the shapes were drawn in- the higher the Z value, the later the object is drawn- so these objects would appear to stack on top of objects with a lower Z value.

function Vector3d(x, y, z) {
  this.base = Vector2d;
  this.base( x, y );
  this.z = z;
}
Vector3d.prototype = new Vector2d;
Vector2d.prototype.Add = function(v) {
  return new Vector3d(parseFloat(this.x) + parseFloat(v.x), parseFloat(this.y) + parseFloat(v.y), parseFloat(this.z) + parseFloat(v.z));
}

Finally Rectangle; this was perhaps the most pivotal of the classes as it’s responsible for making the collisions work, being the class used to define each of the GameObject’s collisionBody property.  For more information about the IntersectDepthVector method (responsible for Collision Detection), see How I tackled Collision Detection

/* Rectangle Class - Bare in mind, a rotated rectangle is no longer a rectangle, but a polygon- so this deals only with pure Rectangles! */
function Rectangle(p, w, h) {
  this.Position = p; // Vector2d - top left corner
  this.Width = w;
  this.Height = h;

  this.HalfWidth = w/2;
  this.HalfHeight = h/2;

  this.Top = p.y;
  this.Right = p.x + w;
  this.Bottom = p.y + h;
  this.Left = p.x;

  this.MiddleX = p.x + (w/2);
}
Rectangle.prototype.SetPosition = function(p) {
  this.Position = p;
  this.Top = p.y;
  this.Right = p.x + this.Width;
  this.Bottom = p.y + this.Height;
  this.Left = p.x;
  this.MiddleX = p.x + (this.Width/2);
}
Rectangle.prototype.IntersectDepthVector = function (r) {
  // Calculate centers.
  centerA = new Vector2d(this.Left + this.HalfWidth, this.Top + this.HalfHeight);
  centerB = new Vector2d(r.Left + r.HalfWidth, r.Top + r.HalfHeight);

  // Calculate current and minimum-non-intersecting distances between centers.
  distanceX = centerA.x - centerB.x;
  distanceY = centerA.y - centerB.y;
  minDistanceX = this.HalfWidth + r.HalfWidth;
  minDistanceY = this.HalfHeight + r.HalfHeight;

  // If we are not intersecting at all, return (0, 0).
  if (Math.abs(distanceX) >= minDistanceX || Math.abs(distanceY) >= minDistanceY)
    return new Vector2d(0,0);

  // Calculate and return intersection depths.
  depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
  depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
  return new Vector2d(depthX, depthY);
}
  1. No comments yet.
(will not be published)