UNPKG

fieldkit

Version:

Basic building blocks for computational design projects. Written in CoffeeScript for browser and server environments.

266 lines (211 loc) 6.22 kB
// Generated by CoffeeScript 1.6.3 (function() { var Area, Box, Collision, Constraint, Vec2, Vec3, Wrap2, Wrap3, physics, vector, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; vector = require('../math/vector'); Vec2 = vector.Vec2; Vec3 = vector.Vec3; physics = require('./physics'); Constraint = physics.Constraint; /* Keeps the particle inside the given 3D box */ Box = (function(_super) { __extends(Box, _super); function Box(min, max) { this.min = min != null ? min : new Vec3(); this.max = max != null ? max : new Vec3(100, 100, 100); } Box.prototype.apply = function(particle) { var pos; pos = particle.position; if (pos.x < this.min.x) { pos.x = this.min.x; } if (pos.y < this.min.y) { pos.y = this.min.y; } if (pos.z < this.min.z) { pos.z = this.min.z; } if (pos.x > this.max.x) { pos.x = this.max.x; } if (pos.y > this.max.y) { pos.y = this.max.y; } if (pos.z > this.max.z) { return pos.z = this.max.z; } }; Box.prototype.toString = function() { return "Box(" + this.min + ", " + this.max + ")"; }; return Box; })(Constraint); /* 2D version of Box. */ Area = (function(_super) { __extends(Area, _super); function Area(min, max) { this.min = min != null ? min : new Vec2(); this.max = max != null ? max : new Vec2(100, 100); } Area.prototype.apply = function(particle) { var pos; pos = particle.position; if (pos.x < this.min.x) { pos.x = this.min.x; } if (pos.y < this.min.y) { pos.y = this.min.y; } if (pos.x > this.max.x) { pos.x = this.max.x; } if (pos.y > this.max.y) { return pos.y = this.max.y; } }; Area.prototype.toString = function() { return "Area(" + this.min + ", " + this.max + ")"; }; return Area; })(Constraint); /* Keeps a particle within a certain 2D region by wrapping it around a given area. */ Wrap2 = (function(_super) { var delta; __extends(Wrap2, _super); delta = new Vec2(); function Wrap2(min, max) { this.min = min != null ? min : new Vec2(); this.max = max != null ? max : new Vec2(100, 100); } Wrap2.prototype.prepare = function() { return delta.set(this.max).sub(this.min); }; Wrap2.prototype.apply = function(particle) { var pos, prev; pos = particle.position; prev = particle.prev; if (pos.x < this.min.x) { pos.x += delta.x; prev.x += delta.x; } if (pos.y < this.min.y) { pos.y += delta.y; prev.y += delta.y; } if (pos.x > this.max.x) { pos.x -= delta.x; prev.x -= delta.x; } if (pos.y > this.max.y) { pos.y -= delta.y; return prev.y -= delta.y; } }; Wrap2.prototype.toString = function() { return "Wrap2(" + this.min + ", " + this.max + ")"; }; return Wrap2; })(Constraint); /* Keeps a particle within a certain 2D region by wrapping it around a given area. */ Wrap3 = (function(_super) { var delta; __extends(Wrap3, _super); delta = new Vec3(); function Wrap3(min, max) { this.min = min != null ? min : new Vec3(); this.max = max != null ? max : new Vec3(100, 100, 100); } Wrap3.prototype.prepare = function() { return delta.set(this.max).sub(this.min); }; Wrap3.prototype.apply = function(particle) { var pos, prev; pos = particle.position; prev = particle.prev; if (pos.x < this.min.x) { pos.x += delta.x; prev.x += delta.x; } if (pos.y < this.min.y) { pos.y += delta.y; prev.y += delta.y; } if (pos.z < this.min.z) { pos.z += delta.z; prev.z += delta.z; } if (pos.x > this.max.x) { pos.x -= delta.x; prev.x -= delta.x; } if (pos.y > this.max.y) { pos.y -= delta.y; prev.y -= delta.y; } if (pos.z > this.max.z) { pos.z -= delta.z; return prev.z -= delta.z; } }; Wrap3.prototype.toString = function() { return "Wrap3(" + this.min + ", " + this.max + ")"; }; return Wrap3; })(Constraint); /* Stops particles from colliding with each other. */ Collision = (function(_super) { __extends(Collision, _super); Collision.prototype.physics = null; function Collision(physics, searchRadius) { this.physics = physics; this.searchRadius = searchRadius != null ? searchRadius : 100; } Collision.prototype.apply = function(particle) { var delta, dist, distSq, neighbour, neighbours, position, radius, radiusSq, _i, _len; position = particle.position; delta = position.clone(); neighbours = this.physics.space.search(position, this.searchRadius); for (_i = 0, _len = neighbours.length; _i < _len; _i++) { neighbour = neighbours[_i]; if (neighbour === particle) { continue; } delta.set(position).sub(neighbour.position); distSq = delta.lengthSquared(); radius = particle.size + neighbour.size; radiusSq = radius * radius; if (distSq < radiusSq) { dist = Math.sqrt(distSq); delta.scale((dist - radius) / radius * 0.5); particle.position.sub(delta); neighbour.position.add(delta); } void 0; } return void 0; }; Collision.prototype.toString = function() { return "Collision(" + this.searchRadius + ")"; }; return Collision; })(Constraint); module.exports = { Box: Box, Area: Area, Wrap2: Wrap2, Wrap3: Wrap3, Collision: Collision }; }).call(this);