Skip to content

Commit

Permalink
Reworked some things.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Morris-Pearce committed Nov 15, 2011
1 parent ddcf1ce commit a69c38e
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 261 deletions.
130 changes: 28 additions & 102 deletions src/collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,118 +27,44 @@
var meta = root.meta2d;
if (!meta) throw 'Could not find main namespace.';

/**
* @abstract
* @class Collision
*/
var Collision = function() {
meta.Modifiable.call(this, new meta.CollisionType());
};
Collision.prototype.collides = function(surface, obj1, obj2) {
throw 'Unimplemented collision.';
};

meta.mixSafely(meta, {Collision: Collision});

var Everywhere = function() {};
Everywhere.prototype = new Collision();
// @override
Everywhere.prototype.collides = function() {
return true;
// functions that take a rect arg and return true/false

// always collides
var always = function() {
return function() {return true;};
};

var Nowhere = function() {};
Nowhere.prototype = new Collision();
// @override
Nowhere.prototype.collides = function() {
return false;
// never collides
var never = function() {
return function() {return false;};
};

var BBox = function() {};
BBox.prototype = new Collision();
// @override
BBox.prototype.collides = function(undefined, r1, r2) {
if (meta.undef(r2.x) ||
meta.undef(r2.y)) return false;
if (meta.undef(r1.x) ||
meta.undef(r1.y)) return false;
if (meta.undef(r2.w)) r2.w = 0;
if (meta.undef(r2.h)) r2.h = 0;
if (meta.undef(r1.w)) r1.w = 0;
if (meta.undef(r1.h)) r1.h = 0;
if (r2.x > (r1.x + r1.w)) return false;
if ((r2.x + r2.w) < r1.x) return false;
if (r2.y > (r1.y + r1.h)) return false;
if ((r2.y + r2.h) < r1.y) return false;
return true;
};

var Alpha = function() {};
Alpha.prototype = new Collision();
// @override
Alpha.prototype.collides = function(surface, data, point) {
if (meta.undef(point.dx) ||
meta.undef(point.dy))
return false;
if (meta.undef(data.dx) ||
meta.undef(data.dy))
return false;
if (!data.image)
return false;
var image = surface.getImageByName(data.image);
if (!image)
return false;
var xpercent = (point.dx - data.dx),
ypercent = (point.dy - data.dy),
sw = image.w,
sh = image.h,
sx = data.sx,
sy = data.sy;
if (meta.undef(sx))
sx = 0;
if (meta.undef(sy))
sy = 0;
if (!meta.undef(data.sw))
sw = data.sw;
if (!meta.undef(data.sh))
sh = data.sh;
if (!meta.undef(data.dw)) {
xpercent /= data.dw;
} else {
xpercent /= sw;
}
if (!meta.undef(data.dh)) {
ypercent /= data.dh;
} else {
ypercent /= sh;
}
if (xpercent < 0 || xpercent >= 1 ||
ypercent < 0 || ypercent >= 1)
return false;
var col = sx + xpercent * sw;
var row = sy + ypercent * sh;
col = col.round();
row = row.round();
var pixel = image.getPixel(col, row);
return pixel.a > 0;
// rectangle intersection
var bbox = function(rect) {
var f = function(r) {
return meta.math.rect.intersect(rect, r);
};
return f;
};

var dynamicAlpha = function(frame) {
var o = function() {};
o.prototype = new Collision();
// @override
o.prototype.collides = function(surface, data, point) {
return meta.collision.ALPHA.collides(surface, frame, point);
// drawing is {ctx, transform}
var alpha = function(d) {
var f = function (r) {
var t = meta.math.affine.transform(
[1, 0, 0, 1, r[0], r[1]],
d.transform);
x = t[4],
y = t[5];
return d.ctx.canvas.getImageData(x, y, 1, 1).data[3] > 0;
};
return new o();
return f;
};

meta.collision = meta.declareSafely(meta.collision, {
EVERYWHERE: new Everywhere(),
NOWHERE: new Nowhere(),
BBOX: new BBox(),
ALPHA: new Alpha(),
dynamicAlpha: dynamicAlpha
always: always,
never: never,
bbox: bbox,
alpha: alpha,
});

}).call(this);
112 changes: 63 additions & 49 deletions src/math/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,93 @@
var meta = root.meta2d;
if (!meta) throw 'Could not find main namespace';

var tblr = function(rect) {
return {
t: rect.y,
b: rect.y + rect.h,
l: rect.x,
r: rect.x + rect.w
};
var xywh_to_tblr = function(xywh) {
return [
xywh[1],
xywh[1] + xywh[3],
xywh[0],
xywh[0] + xywh[2]
];
};

// @constructor
// @param (x, y, w ,h) | [{x, y, w, h}]
var Rect = function() {
if (arguments.length === 1) {
this.x = arguments[0].x || 0;
this.y = arguments[0].y || 0;
this.w = arguments[0].w || 0;
this.h = arguments[0].h || 0;
} else {
this.x = arguments[0] || 0;
this.y = arguments[1] || 0;
this.w = arguments[2] || 0;
this.h = arguments[3] || 0;
// return [x, y, w, h] from arguments
var rect = function() {
var args = meta.args(arguments);
if (args.length === 4) {
return args;
} else if (args.length === 3) {
return [args[0], args[1], args[2], 0];
} else if (args.length === 2) {
return [args[0], args[1], 0, 0];
} else if (args.length === 1) {
if (meta.isObject(args[0])) {
meta.mixSafely(args[0], {x: 0, y: 0, w: 0, h: 0});
return [args[0].x, args[0].y, args[0].w, args[0].h];
}
}
};
return [0, 0, 0, 0];
}

// @return [null | meta::math::Rect]
Rect.prototype.intersect = function(r) {
var tblr1 = tblr(this),
tblr2 = tblr(r),
t = Math.max(tblr1.t, tblr2.t),
b = Math.min(tblr1.b, tblr2.b),
l = Math.max(tblr1.l, tblr2.l),
r = Math.min(tblr1.r, tblr2.r);
// @return [Rect | null]
var intersect = function(r1, r2) {
var tblr1 = xywh_to_tblr(r1),
tblr2 = xywh_to_tblr(r2),
t = Math.max(tblr1[0], tblr2[0]),
b = Math.min(tblr1[1], tblr2[1]),
l = Math.max(tblr1[2], tblr2[2]),
r = Math.min(tblr1[3], tblr2[3]);

if (b < t || r < l) return null;
if (t === b) {
if (t === tblr1.t && t >= tblr2.b) return null;
if (t === tblr2.t && t >= tblr1.b) return null;
if (t === tblr1[0] && t >= tblr2[1]) return null;
if (t === tblr2[0] && t >= tblr1[1]) return null;
}
if (l === r) {
if (l === tblr1.l && l >= tblr2.r) return null;
if (l === tblr2.l && l >= tblr1.r) return null;
if (l === tblr1[2] && l >= tblr2[3]) return null;
if (l === tblr2[2] && l >= tblr1[3]) return null;
}

return new Rect(l, t, r - l, b - t);
return [
l,
t,
r - l,
b - t
];
};

// @return [Boolean]
Rect.prototype.contains = function(r) {
return r.x >= this.x &&
r.y >= this.y &&
r.x + r.w <= this.x + this.w &&
r.y + r.h <= this.y + this.h;
var contains = function(r1, r2) {
return r2[0] >= r1[0] &&
r2[1] >= r1[1] &&
r2[0] + r2[2] <= r1[0] + r1[2] &&
r2[1] + r2[3] <= r1[1] + r1[3];
};

// @return [Boolean]
Rect.prototype.containedBy = function(r) {
return r.contains(this);
var containedBy = function(r1, r2) {
return contains(r2, r1);
};

// @return [Boolean]
Rect.prototype.sameAs = function(r) {
return r.x === this.x &&
r.y === this.y &&
r.w === this.w &&
r.h === this.h;
var equal = function(r1, r2) {
return r1[0] === r2[0] &&
r1[1] === r2[1] &&
r1[2] === r2[2] &&
r1[3] === r2[3];
};

// @return [Number]
Rect.prototype.area = function() {
return this.w * this.h;
var area = function(r) {
return r[2] * r[3];
};

meta.math = meta.declareSafely(meta.math, {Rect: Rect});
meta.math.rect = meta.declareSafely(meta.math.rect, {
rect: rect,
intersect: intersect,
contains: contains,
containedBy: containedBy,
equal: equal,
area: area
});

}).call(this);
13 changes: 6 additions & 7 deletions src/math/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
var meta = root.meta2d;
if (!meta) throw 'Could not find main namespace.';

/**
* @class Vector
* : Array
*/
var Vector = Array;
// a Vector is an Array
var vector = function() {
var ret = new Array();
return ret.concat(meta.args(arguments));
};

/** and 2 values */
var ander = function(a, b) {
Expand Down Expand Up @@ -109,9 +109,8 @@
return v.map(function(e) {return 1/e;});
};

meta.math = meta.declareSafely(meta.math, {
Vector: Vector});
meta.math.vector = meta.declareSafely(meta.math.vector, {
vector: vector,
equal: equal,
plus: plus,
minus: minus,
Expand Down
18 changes: 7 additions & 11 deletions src/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@
var meta = root.meta2d;
if (!meta) throw 'Could not find main namespace.';

/**
* @class Segment
* Segment === Array of length 2.
*/
var Segment = function(start, end) {
var segment = function(start, end) {
if (meta.undef(start))
start = Number.NEGATIVE_INFINITY;
if (meta.undef(end))
end = Number.POSITIVE_INFINITY;
this.push(start, end);
return [start, end];
};
Segment.prototype = new Array();

var start = function(seg) {
return seg[0];
Expand Down Expand Up @@ -76,12 +71,13 @@
});
};

meta.mixSafely(meta, {
Segment: Segment
});
var always = function() {
return segment();
};

meta.segment = meta.declareSafely(meta.segment, {
ALWAYS: [Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY],
segment: segment,
always: always,
start: start,
end: end,
isForward: is_forward,
Expand Down
2 changes: 1 addition & 1 deletion test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ document.addEventListener('DOMContentLoaded',
var image;

var loadfail = function() {
this.printhead('meta2d::Context');
this.printheader('meta2d::Context');
assert('Could not load images.');
summarize();
};
Expand Down
25 changes: 12 additions & 13 deletions test/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ document.addEventListener('DOMContentLoaded',
(function() {
this.printheader('meta2d::Projection');

var proj1 = meta2d.projection.flat(),
var v = meta2d.math.vector,
proj1 = meta2d.projection.flat(),
proj2 = meta2d.projection.iso2d(20, 10),
vec1 = new meta2d.math.Vector(0, 0),
vec1_p = new meta2d.math.Vector(0, 0),
vec2 = new meta2d.math.Vector(-1, -1),
vec2_p = new meta2d.math.Vector(0, -10),
vec3 = new meta2d.math.Vector(3, 3),
vec3_p = new meta2d.math.Vector(0, 30),
vec4 = new meta2d.math.Vector(4, 1),
vec4_p = new meta2d.math.Vector(30, 25),
vec5 = new meta2d.math.Vector(1, 4),
vec5_p = new meta2d.math.Vector(-30, 25);

var v = meta2d.math.vector;
vec1 = v.vector(0, 0),
vec1_p = v.vector(0, 0),
vec2 = v.vector(-1, -1),
vec2_p = v.vector(0, -10),
vec3 = v.vector(3, 3),
vec3_p = v.vector(0, 30),
vec4 = v.vector(4, 1),
vec4_p = v.vector(30, 25),
vec5 = v.vector(1, 4),
vec5_p = v.vector(-30, 25);

printsection('flat projection');
assert('forward',
Expand Down
Loading

0 comments on commit a69c38e

Please sign in to comment.