Skip to content

Thrunium / Reference

Collider

Collision checks for rectangles and circles, for games with moving parts. Shapes are plain objects. A rect has x, y, width and height; a circle has x, y and radius. x and y are the centre of the shape. Instead of x and y a shape can have a pos, usually a THRUNIUM.Vector, whose x and y are used.

A shape that is missing a field is an error: the check returns false and calls THRUNIUM.throwError(), which also stops the game loop.

General

isCircle(circle)

  • circle CircleShape

    The object to check.

returns boolean

Returns true if the object has the x, y and radius of a circle. It does not look at pos; call fixPos() first for a shape that uses one.

JavaScript
THRUNIUM.Collider.isCircle({ x: 100, y: 100, radius: 50 }); // true
THRUNIUM.Collider.isCircle({ x: 100, y: 100 }); // false

isRect(rect)

  • rect RectShape

    The object to check.

returns boolean

Returns true if the object has the x, y, width and height of a rect. It does not look at pos; call fixPos() first for a shape that uses one.

JavaScript
THRUNIUM.Collider.isRect({ x: 100, y: 100, width: 50, height: 50 }); // true
THRUNIUM.Collider.isRect({ x: 100, y: 100, radius: 50 }); // false

fixPos(object)

  • object S

    The shape to update.

returns S

Copies pos.x and pos.y onto the shape's own x and y, and returns the same object. A shape without a pos is returned unchanged. The result has the same shape you passed in, so a circle stays a circle and a rect stays a rect. The collision checks call it for you.

JavaScript
var rect = { pos: new THRUNIUM.Vector(100, 100), width: 50, height: 50 };
THRUNIUM.Collider.fixPos(rect);
// rect.x is now 100 and rect.y is 100

rect(a, b)

  • a RectShape

    The first rect.

  • b RectShape

    The second rect.

returns boolean

Returns true if two rects overlap. Rects whose edges just touch count as colliding. Each rect needs x, y, width and height, or pos, width and height.

JavaScript
var rect1 = { x: 100, y: 100, width: 50, height: 50 };
var rect2 = { x: 140, y: 140, width: 50, height: 50 };
if (THRUNIUM.Collider.rect(rect1, rect2)) {
	console.log('Collision!');
}
JavaScript
var rect1 = { pos: new THRUNIUM.Vector(100, 100), width: 50, height: 50 };
var rect2 = { pos: new THRUNIUM.Vector(140, 140), width: 50, height: 50 };
if (THRUNIUM.Collider.rect(rect1, rect2)) {
	console.log('Collision!');
}

circle(a, b)

  • a CircleShape

    The first circle.

  • b CircleShape

    The second circle.

returns boolean

Returns true if two circles overlap. Circles that just touch do not count as colliding. Each circle needs x, y and radius, or pos and radius.

JavaScript
var circle1 = { x: 100, y: 100, radius: 50 };
var circle2 = { x: 140, y: 140, radius: 50 };
if (THRUNIUM.Collider.circle(circle1, circle2)) {
	console.log('Collision!');
}
JavaScript
var circle1 = { pos: new THRUNIUM.Vector(100, 100), radius: 50 };
var circle2 = { pos: new THRUNIUM.Vector(140, 140), radius: 50 };
if (THRUNIUM.Collider.circle(circle1, circle2)) {
	console.log('Collision!');
}

circleRect(circle, rect)

  • circle CircleShape

    The circle.

  • rect RectShape

    The rect.

returns boolean

Returns true if a circle and a rect overlap. A circle that just touches a side of the rect does not count as colliding. The circle needs x, y and radius (or pos and radius); the rect needs x, y, width and height (or pos, width and height).

JavaScript
var circle = { x: 100, y: 100, radius: 50 };
var rect = { x: 140, y: 140, width: 50, height: 50 };
if (THRUNIUM.Collider.circleRect(circle, rect)) {
	console.log('Collision!');
}
JavaScript
var circle = { pos: new THRUNIUM.Vector(100, 100), radius: 50 };
var rect = { pos: new THRUNIUM.Vector(140, 140), width: 50, height: 50 };
if (THRUNIUM.Collider.circleRect(circle, rect)) {
	console.log('Collision!');
}