code
stringlengths 10
343k
| docstring
stringlengths 36
21.9k
| func_name
stringlengths 1
3.35k
| language
stringclasses 1
value | repo
stringlengths 7
58
| path
stringlengths 4
131
| url
stringlengths 44
195
| license
stringclasses 5
values |
---|---|---|---|---|---|---|---|
Particle.prototype.setRestrictions = function setRestrictions(transRestrictions, rotRestrictions) {
transRestrictions = transRestrictions || '';
rotRestrictions = rotRestrictions || '';
this.restrictions = 0;
if (transRestrictions.indexOf('x') > -1) this.restrictions |= 32;
if (transRestrictions.indexOf('y') > -1) this.restrictions |= 16;
if (transRestrictions.indexOf('z') > -1) this.restrictions |= 8;
if (rotRestrictions.indexOf('x') > -1) this.restrictions |= 4;
if (rotRestrictions.indexOf('y') > -1) this.restrictions |= 2;
if (rotRestrictions.indexOf('z') > -1) this.restrictions |= 1;
return this;
}; | Setter for the particle restriction bitmask.
@method
@param {String} transRestrictions The restrictions to linear motion.
@param {String} rotRestrictions The restrictions to rotational motion.
@return {Particle} this | setRestrictions ( transRestrictions , rotRestrictions ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getMass = function getMass() {
return this.mass;
}; | Getter for mass
@method
@return {Number} mass | getMass ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setMass = function setMass(mass) {
this.mass = mass;
this.inverseMass = 1 / mass;
return this;
}; | Set the mass of the Particle.
@method
@param {Number} mass The mass.
@return {Particle} this | setMass ( mass ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getInverseMass = function() {
return this.inverseMass;
}; | Getter for inverse mass
@method
@return {Number} inverse mass | Particle.prototype.getInverseMass ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.updateLocalInertia = function updateLocalInertia() {
this.localInertia.set([0,0,0,0,0,0,0,0,0]);
this.localInverseInertia.set([0,0,0,0,0,0,0,0,0]);
return this;
}; | Resets the inertia tensor and its inverse to reflect the current shape.
@method
@return {Particle} this | updateLocalInertia ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.updateInertia = function updateInertia() {
var localInvI = this.localInverseInertia;
var q = this.orientation;
if ((localInvI[0] === localInvI[4] && localInvI[4] === localInvI[8]) || q.w === 1) return this;
var R = q.toMatrix(MAT1_REGISTER);
Mat33.multiply(R, this.inverseInertia, this.inverseInertia);
Mat33.multiply(this.localInverseInertia, R.transpose(), this.inverseInertia);
return this;
}; | Updates the world inverse inertia tensor.
@method
@return {Particle} this | updateInertia ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getPosition = function getPosition() {
return this.position;
}; | Getter for position
@method
@return {Vec3} position | getPosition ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setPosition = function setPosition(x, y, z) {
this.position.set(x, y, z);
return this;
}; | Setter for position
@method
@param {Number} x the x coordinate for position
@param {Number} y the y coordinate for position
@param {Number} z the z coordinate for position
@return {Particle} this
@return {Particle} this | setPosition ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getVelocity = function getVelocity() {
return this.velocity;
}; | Getter for velocity
@method
@return {Vec3} velocity | getVelocity ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setVelocity = function setVelocity(x, y, z) {
this.velocity.set(x, y, z);
Vec3.scale(this.velocity, this.mass, this.momentum);
return this;
}; | Setter for velocity
@method
@param {Number} x the x coordinate for velocity
@param {Number} y the y coordinate for velocity
@param {Number} z the z coordinate for velocity
@return {Particle} this | setVelocity ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getMomentum = function getMomentum() {
return this.momentum;
}; | Getter for momenutm
@method
@return {Vec3} momentum | getMomentum ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setMomentum = function setMomentum(x, y, z) {
this.momentum.set(x, y, z);
Vec3.scale(this.momentum, this.inverseMass, this.velocity);
return this;
}; | Setter for momentum
@method
@param {Number} x the x coordinate for momentum
@param {Number} y the y coordinate for momentum
@param {Number} z the z coordinate for momentum
@return {Particle} this | setMomentum ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getOrientation = function getOrientation() {
return this.orientation;
}; | Getter for orientation
@method
@return {Quaternion} orientation | getOrientation ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setOrientation = function setOrientation(w,x,y,z) {
this.orientation.set(w,x,y,z).normalize();
this.updateInertia();
return this;
}; | Setter for orientation
@method
@param {Number} w The w component.
@param {Number} x The x component.
@param {Number} y The y component.
@param {Number} z The z component.
@return {Particle} this | setOrientation ( w , x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getAngularVelocity = function getAngularVelocity() {
return this.angularVelocity;
}; | Getter for angular velocity
@method
@return {Vec3} angularVelocity | getAngularVelocity ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setAngularVelocity = function setAngularVelocity(x,y,z) {
this.angularVelocity.set(x,y,z);
var I = Mat33.inverse(this.inverseInertia, MAT1_REGISTER);
if (I) I.vectorMultiply(this.angularVelocity, this.angularMomentum);
else this.angularMomentum.clear();
return this;
}; | Setter for angular velocity
@method
@param {Number} x The x component.
@param {Number} y The y component.
@param {Number} z The z component.
@return {Particle} this | setAngularVelocity ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getAngularMomentum = function getAngularMomentum() {
return this.angularMomentum;
}; | Getter for angular momentum
@method
@return {Vec3} angular momentum | getAngularMomentum ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setAngularMomentum = function setAngularMomentum(x,y,z) {
this.angularMomentum.set(x,y,z);
this.inverseInertia.vectorMultiply(this.angularMomentum, this.angularVelocity);
return this;
}; | Setter for angular momentum
@method
@param {Number} x The x component.
@param {Number} y The y component.
@param {Number} z The z component.
@return {Particle} this | setAngularMomentum ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getForce = function getForce() {
return this.force;
}; | Getter for the force on the Particle
@method
@return {Vec3} force | getForce ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setForce = function setForce(x, y, z) {
this.force.set(x, y, z);
return this;
}; | Setter for the force on the Particle
@method
@param {Number} x The x component.
@param {Number} y The y component.
@param {Number} z The z component.
@return {Particle} this | setForce ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.getTorque = function getTorque() {
return this.torque;
}; | Getter for torque.
@method
@return {Vec3} torque | getTorque ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.setTorque = function setTorque(x, y, z) {
this.torque.set(x, y, z);
return this;
}; | Setter for torque.
@method
@param {Number} x The x component.
@param {Number} y The y component.
@param {Number} z The z component.
@return {Particle} this | setTorque ( x , y , z ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.applyForce = function applyForce(force) {
this.force.add(force);
return this;
}; | Extends Particle.applyForce with an optional argument
to apply the force at an off-centered location, resulting in a torque.
@method
@param {Vec3} force Force to apply.
@return {Particle} this | applyForce ( force ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.applyTorque = function applyTorque(torque) {
this.torque.add(torque);
return this;
}; | Applied a torque force to a Particle, inducing a rotation.
@method
@param {Vec3} torque Torque to apply.
@return {Particle} this | applyTorque ( torque ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.applyImpulse = function applyImpulse(impulse) {
this.momentum.add(impulse);
Vec3.scale(this.momentum, this.inverseMass, this.velocity);
return this;
}; | Applies an impulse to momentum and updates velocity.
@method
@param {Vec3} impulse Impulse to apply.
@return {Particle} this | applyImpulse ( impulse ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.applyAngularImpulse = function applyAngularImpulse(angularImpulse) {
this.angularMomentum.add(angularImpulse);
this.inverseInertia.vectorMultiply(this.angularMomentum, this.angularVelocity);
return this;
}; | Applies an angular impulse to angular momentum and updates angular velocity.
@method
@param {Vec3} angularImpulse Angular impulse to apply.
@return {Particle} this | applyAngularImpulse ( angularImpulse ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.support = function support() {
return ZERO_VECTOR;
}; | Used in collision detection. The support function should accept a Vec3 direction
and return the point on the body's shape furthest in that direction. For point particles,
this returns the zero vector.
@method
@return {Vec3} The zero vector. | support ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
Particle.prototype.updateShape = function updateShape() {}; | Update the body's shape to reflect current orientation. Called in Collision.
Noop for point particles.
@method
@return {undefined} undefined | updateShape ( ) | javascript | Famous/engine | physics/bodies/Particle.js | https://github.com/Famous/engine/blob/master/physics/bodies/Particle.js | MIT |
function ConvexBody(options) {
Particle.call(this, options);
var originalSize = hull.polyhedralProperties.size;
var size = options.size || originalSize;
var scaleX = size[0] / originalSize[0];
var scaleY = size[1] / originalSize[1];
var scaleZ = size[2] / originalSize[2];
this._scale = [scaleX, scaleY, scaleZ];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
this.hull = hull;
this.vertices = [];
for (var i = 0, len = hull.vertices.length; i < len; i++) {
this.vertices.push(T.vectorMultiply(hull.vertices[i], new Vec3()));
}
_computeInertiaProperties.call(this, T);
this.inverseInertia.copy(this.localInverseInertia);
this.updateInertia();
var w = options.angularVelocity;
if (w) this.setAngularVelocity(w.x, w.y, w.z);
} | The body class with inertia and vertices inferred from the input ConvexHull or Vec3 array.
@class ConvexBody
@param {Object} options The options hash. | ConvexBody ( options ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
ConvexBody.prototype.setSize = function setSize(x,y,z) {
var originalSize = hull.polyhedralProperties.size;
this.size[0] = x;
this.size[1] = y;
this.size[2] = z;
var scaleX = x / originalSize[0];
var scaleY = y / originalSize[1];
var scaleZ = z / originalSize[2];
this._scale = [scaleX, scaleY, scaleZ];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
var vertices = this.vertices;
for (var i = 0, len = hull.vertices.length; i < len; i++) {
T.vectorMultiply(hull.vertices[i], vertices[i]);
}
return this;
}; | Set the size and recalculate
@method
@chainable
@param {Number} x The x span.
@param {Number} y The y span.
@param {Number} z The z span.
@return {ConvexBody} this | setSize ( x , y , z ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
ConvexBody.prototype.updateLocalInertia = function updateInertia() {
var scaleX = this._scale[0];
var scaleY = this._scale[1];
var scaleZ = this._scale[2];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
_computeInertiaProperties.call(this, T);
return this;
}; | Update the local inertia and inverse inertia to reflect the current size.
@method
@return {ConvexBody} this | updateInertia ( ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
ConvexBody.prototype.support = function support(direction) {
var vertices = this.vertices;
var vertex, dot, furthest;
var max = -Infinity;
for (var i = 0, len = vertices.length; i < len; i++) {
vertex = vertices[i];
dot = Vec3.dot(vertex,direction);
if (dot > max) {
furthest = vertex;
max = dot;
}
}
return furthest;
}; | Retrieve the vertex furthest in a direction. Used internally for collision detection.
@method
@param {Vec3} direction The direction in which to search.
@return {Vec3} The furthest vertex. | support ( direction ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
ConvexBody.prototype.updateShape = function updateShape() {
var vertices = this.vertices;
var q = this.orientation;
var modelVertices = this.hull.vertices;
var scaleX = this._scale[0];
var scaleY = this._scale[1];
var scaleZ = this._scale[2];
var t = TEMP_REGISTER;
for (var i = 0, len = vertices.length; i < len; i++) {
t.copy(modelVertices[i]);
t.x *= scaleX;
t.y *= scaleY;
t.z *= scaleZ;
Vec3.applyRotation(t, q, vertices[i]);
}
return this;
}; | Update vertices to reflect current orientation.
@method
@return {ConvexBody} this | updateShape ( ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
function convexBodyFactory(hull) {
if (!(hull instanceof ConvexHull)) {
if (!(hull instanceof Array)) throw new Error('convexBodyFactory requires a ConvexHull object or an array of Vec3\'s as input.');
else hull = new ConvexHull(hull);
}
/**
* The body class with inertia and vertices inferred from the input ConvexHull or Vec3 array.
*
* @class ConvexBody
* @param {Object} options The options hash.
*/
function ConvexBody(options) {
Particle.call(this, options);
var originalSize = hull.polyhedralProperties.size;
var size = options.size || originalSize;
var scaleX = size[0] / originalSize[0];
var scaleY = size[1] / originalSize[1];
var scaleZ = size[2] / originalSize[2];
this._scale = [scaleX, scaleY, scaleZ];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
this.hull = hull;
this.vertices = [];
for (var i = 0, len = hull.vertices.length; i < len; i++) {
this.vertices.push(T.vectorMultiply(hull.vertices[i], new Vec3()));
}
_computeInertiaProperties.call(this, T);
this.inverseInertia.copy(this.localInverseInertia);
this.updateInertia();
var w = options.angularVelocity;
if (w) this.setAngularVelocity(w.x, w.y, w.z);
}
ConvexBody.prototype = Object.create(Particle.prototype);
ConvexBody.prototype.constructor = ConvexBody;
/**
* Set the size and recalculate
*
* @method
* @chainable
* @param {Number} x The x span.
* @param {Number} y The y span.
* @param {Number} z The z span.
* @return {ConvexBody} this
*/
ConvexBody.prototype.setSize = function setSize(x,y,z) {
var originalSize = hull.polyhedralProperties.size;
this.size[0] = x;
this.size[1] = y;
this.size[2] = z;
var scaleX = x / originalSize[0];
var scaleY = y / originalSize[1];
var scaleZ = z / originalSize[2];
this._scale = [scaleX, scaleY, scaleZ];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
var vertices = this.vertices;
for (var i = 0, len = hull.vertices.length; i < len; i++) {
T.vectorMultiply(hull.vertices[i], vertices[i]);
}
return this;
};
/**
* Update the local inertia and inverse inertia to reflect the current size.
*
* @method
* @return {ConvexBody} this
*/
ConvexBody.prototype.updateLocalInertia = function updateInertia() {
var scaleX = this._scale[0];
var scaleY = this._scale[1];
var scaleZ = this._scale[2];
var T = new Mat33([scaleX, 0, 0, 0, scaleY, 0, 0, 0, scaleZ]);
_computeInertiaProperties.call(this, T);
return this;
};
/**
* Retrieve the vertex furthest in a direction. Used internally for collision detection.
*
* @method
* @param {Vec3} direction The direction in which to search.
* @return {Vec3} The furthest vertex.
*/
ConvexBody.prototype.support = function support(direction) {
var vertices = this.vertices;
var vertex, dot, furthest;
var max = -Infinity;
for (var i = 0, len = vertices.length; i < len; i++) {
vertex = vertices[i];
dot = Vec3.dot(vertex,direction);
if (dot > max) {
furthest = vertex;
max = dot;
}
}
return furthest;
};
/**
* Update vertices to reflect current orientation.
*
* @method
* @return {ConvexBody} this
*/
ConvexBody.prototype.updateShape = function updateShape() {
var vertices = this.vertices;
var q = this.orientation;
var modelVertices = this.hull.vertices;
var scaleX = this._scale[0];
var scaleY = this._scale[1];
var scaleZ = this._scale[2];
var t = TEMP_REGISTER;
for (var i = 0, len = vertices.length; i < len; i++) {
t.copy(modelVertices[i]);
t.x *= scaleX;
t.y *= scaleY;
t.z *= scaleZ;
Vec3.applyRotation(t, q, vertices[i]);
}
return this;
};
return ConvexBody;
} | Returns a constructor for a physical body reflecting the shape defined by input ConvexHull or Vec3 array.
@method
@param {ConvexHull | Vec3[]} hull ConvexHull instance or Vec3 array.
@return {Function} The constructor for the custom convex body type. | convexBodyFactory ( hull ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
function _computeInertiaProperties(T) {
var polyhedralProperties = this.hull.polyhedralProperties;
var T_values = T.get();
var detT = T_values[0] * T_values[4] * T_values[8];
var E_o = polyhedralProperties.eulerTensor;
var E = new Mat33();
Mat33.multiply(T, E_o, E);
Mat33.multiply(E, T, E);
var E_values = E.get();
var Exx = E_values[0];
var Eyy = E_values[4];
var Ezz = E_values[8];
var Exy = E_values[1];
var Eyz = E_values[7];
var Exz = E_values[2];
var newVolume = polyhedralProperties.volume * detT;
var mass = this.mass;
var density = mass / newVolume;
var Ixx = Eyy + Ezz;
var Iyy = Exx + Ezz;
var Izz = Exx + Eyy;
var Ixy = -Exy;
var Iyz = -Eyz;
var Ixz = -Exz;
var centroid = polyhedralProperties.centroid;
Ixx -= newVolume * (centroid.y * centroid.y + centroid.z * centroid.z);
Iyy -= newVolume * (centroid.z * centroid.z + centroid.x * centroid.x);
Izz -= newVolume * (centroid.x * centroid.x + centroid.y * centroid.y);
Ixy += newVolume * centroid.x * centroid.y;
Iyz += newVolume * centroid.y * centroid.z;
Ixz += newVolume * centroid.z * centroid.x;
Ixx *= density * detT;
Iyy *= density * detT;
Izz *= density * detT;
Ixy *= density * detT;
Iyz *= density * detT;
Ixz *= density * detT;
var inertia = [
Ixx, Ixy, Ixz,
Ixy, Iyy, Iyz,
Ixz, Iyz, Izz
];
this.localInertia.set(inertia);
Mat33.inverse(this.localInertia, this.localInverseInertia);
} | Determines mass and inertia tensor based off the density, size, and facet information of the polyhedron.
@method
@private
@param {Mat33} T The matrix transforming the intial set of vertices to a set reflecting the body size.
@return {undefined} undefined | _computeInertiaProperties ( T ) | javascript | Famous/engine | physics/bodies/convexBodyFactory.js | https://github.com/Famous/engine/blob/master/physics/bodies/convexBodyFactory.js | MIT |
function Box(options) {
_Box.call(this, options);
this.normals = [
// Order: top, right, front
new Vec3(0, 1, 0),
new Vec3(1, 0, 0),
new Vec3(0, 0, 1)
];
this.type = 1 << 1;
} | @class Box
@extends Particle
@param {Object} options Initial state of the body. | Box ( options ) | javascript | Famous/engine | physics/bodies/Box.js | https://github.com/Famous/engine/blob/master/physics/bodies/Box.js | MIT |
function Curve(targets, options) {
if (targets) {
if (targets instanceof Array) this.targets = targets;
else this.targets = [targets];
}
else this.targets = [];
Constraint.call(this, options);
this.impulses = {};
this.normals = {};
this.velocityBiases = {};
this.divisors = {};
} | A constraint that keeps a physics body on a given implicit curve.
@class Curve
@extends Constraint
@param {Particle[]} targets The bodies to track.
@param {Object} options The options hash. | Curve ( targets , options ) | javascript | Famous/engine | physics/constraints/Curve.js | https://github.com/Famous/engine/blob/master/physics/constraints/Curve.js | MIT |
Curve.prototype.init = function() {
this.equation1 = this.equation1 || function() {
return 0;
};
this.equation2 = this.equation2 || function(x, y, z) {
return z;
};
this.period = this.period || 1;
this.dampingRatio = this.dampingRatio || 0.5;
this.stiffness = 4 * PI * PI / (this.period * this.period);
this.damping = 4 * PI * this.dampingRatio / this.period;
}; | Initialize the Curve. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | Curve.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Curve.js | https://github.com/Famous/engine/blob/master/physics/constraints/Curve.js | MIT |
Curve.prototype.update = function update(time, dt) {
var targets = this.targets;
var normals = this.normals;
var velocityBiases = this.velocityBiases;
var divisors = this.divisors;
var impulses = this.impulses;
var impulse = IMPULSE_REGISTER;
var n = NORMAL_REGISTER;
var f = this.equation1;
var g = this.equation2;
var _c = this.damping;
var _k = this.stiffness;
for (var i = 0, len = targets.length; i < len; i++) {
var body = targets[i];
var ID = body._ID;
if (body.immune) continue;
var p = body.position;
var m = body.mass;
var gamma;
var beta;
if (this.period === 0) {
gamma = 0;
beta = 1;
}
else {
var c = _c * m;
var k = _k * m;
gamma = 1 / (dt*(c + dt*k));
beta = dt*k / (c + dt*k);
}
var x = p.x;
var y = p.y;
var z = p.z;
var f0 = f(x, y, z);
var dfx = (f(x + EPSILSON, y, z) - f0) / EPSILSON;
var dfy = (f(x, y + EPSILSON, z) - f0) / EPSILSON;
var dfz = (f(x, y, z + EPSILSON) - f0) / EPSILSON;
var g0 = g(x, y, z);
var dgx = (g(x + EPSILSON, y, z) - g0) / EPSILSON;
var dgy = (g(x, y + EPSILSON, z) - g0) / EPSILSON;
var dgz = (g(x, y, z + EPSILSON) - g0) / EPSILSON;
n.set(dfx + dgx, dfy + dgy, dfz + dgz);
n.normalize();
var baumgarte = beta * (f0 + g0) / dt;
var divisor = gamma + 1 / m;
var lambda = impulses[ID] || 0;
Vec3.scale(n, lambda, impulse);
body.applyImpulse(impulse);
normals[ID] = normals[ID] || new Vec3();
normals[ID].copy(n);
velocityBiases[ID] = baumgarte;
divisors[ID] = divisor;
impulses[ID] = 0;
}
}; | Warmstart the constraint and prepare calculations used in the .resolve step.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | update ( time , dt ) | javascript | Famous/engine | physics/constraints/Curve.js | https://github.com/Famous/engine/blob/master/physics/constraints/Curve.js | MIT |
Curve.prototype.resolve = function resolve() {
var targets = this.targets;
var normals = this.normals;
var velocityBiases = this.velocityBiases;
var divisors = this.divisors;
var impulses = this.impulses;
var impulse = IMPULSE_REGISTER;
for (var i = 0, len = targets.length; i < len; i++) {
var body = targets[i];
var ID = body._ID;
if (body.immune) continue;
var v = body.velocity;
var n = normals[ID];
var lambda = -(Vec3.dot(n, v) + velocityBiases[ID]) / divisors[ID];
Vec3.scale(n, lambda, impulse);
body.applyImpulse(impulse);
impulses[ID] += lambda;
}
}; | Adds a curve impulse to a physics body.
@method
@return {undefined} undefined | resolve ( ) | javascript | Famous/engine | physics/constraints/Curve.js | https://github.com/Famous/engine/blob/master/physics/constraints/Curve.js | MIT |
function clamp(value, lower, upper) {
return value < lower ? lower : value > upper ? upper : value;
} | Helper function to clamp a value to a given range.
@method
@private
@param {Number} value The value to clamp.
@param {Number} lower The lower end.
@param {Number} upper The upper end.
@return {Number} The clamped value. | clamp ( value , lower , upper ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function CollisionData(penetration, normal, worldContactA, worldContactB, localContactA, localContactB) {
this.penetration = penetration;
this.normal = normal;
this.worldContactA = worldContactA;
this.worldContactB = worldContactB;
this.localContactA = localContactA;
this.localContactB = localContactB;
} | Object maintaining various figures of a collision. Registered in ObjectManager.
@class CollisionData
@param {Number} penetration The degree of penetration.
@param {Vec3} normal The normal for the collision.
@param {Vec3} worldContactA The contact for A in world coordinates.
@param {Vec3} worldContactB The contact for B in world coordinates.
@param {Vec3} localContactA The contact for A in local coordinates.
@param {Vec3} localContactB The contact for B in local coordinates. | CollisionData ( penetration , normal , worldContactA , worldContactB , localContactA , localContactB ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
CollisionData.prototype.reset = function reset(penetration, normal, worldContactA, worldContactB, localContactA, localContactB) {
this.penetration = penetration;
this.normal = normal;
this.worldContactA = worldContactA;
this.worldContactB = worldContactB;
this.localContactA = localContactA;
this.localContactB = localContactB;
return this;
}; | Used by ObjectManager to reset the object with different data.
@method
@param {Number} penetration The degree of penetration.
@param {Vec3} normal The normal for the collision.
@param {Vec3} worldContactA The contact for A in world coordinates.
@param {Vec3} worldContactB The contact for B in world coordinates.
@param {Vec3} localContactA The contact for A in local coordinates.
@param {Vec3} localContactB The contact for B in local coordinates.
@return {CollisionData} this | reset ( penetration , normal , worldContactA , worldContactB , localContactA , localContactB ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function Collision(targets, options) {
this.targets = [];
if (targets) this.targets = this.targets.concat(targets);
Constraint.call(this, options);
} | Ridid body Elastic Collision
@class Collision
@extends Constraint
@param {Particle[]} targets The bodies to track.
@param {Object} options The options hash. | Collision ( targets , options ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.init = function() {
if (this.broadPhase) {
var BroadPhase = this.broadphase;
if (BroadPhase instanceof Function) this.broadPhase = new BroadPhase(this.targets);
}
else this.broadPhase = new SweepAndPrune(this.targets);
this.contactManifoldTable = this.contactManifoldTable || new ContactManifoldTable();
}; | Initialize the Collision tracker. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | Collision.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.update = function update(time, dt) {
this.contactManifoldTable.update(dt);
if (this.targets.length === 0) return;
var i, len;
for (i = 0, len = this.targets.length; i < len; i++) {
this.targets[i].updateShape();
}
var potentialCollisions = this.broadPhase.update();
var pair;
for (i = 0, len = potentialCollisions.length; i < len; i++) {
pair = potentialCollisions[i];
if (pair) this.applyNarrowPhase(pair);
}
this.contactManifoldTable.prepContacts(dt);
}; | Collison detection. Updates the existing contact manifolds, runs the broadphase, and performs narrowphase
collision detection. Warm starts the contacts based on the results of the previous physics frame
and prepares necesssary calculations for the resolution.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | update ( time , dt ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.resolve = function resolve(time, dt) {
this.contactManifoldTable.resolveManifolds(dt);
}; | Apply impulses to resolve all Contact constraints.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | resolve ( time , dt ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.addTarget = function addTarget(target) {
this.targets.push(target);
this.broadPhase.add(target);
}; | Add a target or targets to the collision system.
@method
@param {Particle} target The body to begin tracking.
@return {undefined} undefined | addTarget ( target ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.removeTarget = function removeTarget(target) {
var index = this.targets.indexOf(target);
if (index < 0) return;
this.targets.splice(index, 1);
this.broadPhase.remove(target);
}; | Remove a target or targets from the collision system.
@method
@param {Particle} target The body to remove.
@return {undefined} undefined | removeTarget ( target ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
Collision.prototype.applyNarrowPhase = function applyNarrowPhase(targets) {
for (var i = 0, len = targets.length; i < len; i++) {
for (var j = i + 1; j < len; j++) {
var a = targets[i];
var b = targets[j];
if ((a.collisionMask & b.collisionGroup && a.collisionGroup & b.collisionMask) === 0) continue;
var collisionType = a.type | b.type;
if (dispatch[collisionType]) dispatch[collisionType](this, a, b);
}
}
}; | Narrowphase collision detection,
registers the Contact constraints for colliding bodies.
Will detect the type of bodies in the collision.
@method
@param {Particle[]} targets The targets.
@return {undefined} undefined | applyNarrowPhase ( targets ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function sphereIntersectSphere(context, sphere1, sphere2) {
var p1 = sphere1.position;
var p2 = sphere2.position;
var relativePosition = Vec3.subtract(p2, p1, new Vec3());
var distance = relativePosition.length();
var sumRadii = sphere1.radius + sphere2.radius;
var n = relativePosition.scale(1/distance);
var overlap = sumRadii - distance;
// Distance check
if (overlap < 0) return;
var rSphere1 = Vec3.scale(n, sphere1.radius, new Vec3());
var rSphere2 = Vec3.scale(n, -sphere2.radius, new Vec3());
var wSphere1 = Vec3.add(p1, rSphere1, new Vec3());
var wSphere2 = Vec3.add(p2, rSphere2, new Vec3());
var collisionData = oMRequestCollisionData().reset(overlap, n, wSphere1, wSphere2, rSphere1, rSphere2);
context.contactManifoldTable.registerContact(sphere1, sphere2, collisionData);
} | Detects sphere-sphere collisions and registers the Contact.
@private
@method
@param {Object} context The Collision instance.
@param {Sphere} sphere1 One sphere collider.
@param {Sphere} sphere2 The other sphere collider.
@return {undefined} undefined | sphereIntersectSphere ( context , sphere1 , sphere2 ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function boxIntersectSphere(context, box, sphere) {
if (box.type === SPHERE) {
var temp = sphere;
sphere = box;
box = temp;
}
var pb = box.position;
var ps = sphere.position;
var relativePosition = Vec3.subtract(ps, pb, VEC_REGISTER);
var q = box.orientation;
var r = sphere.radius;
var bsize = box.size;
var halfWidth = bsize[0]*0.5;
var halfHeight = bsize[1]*0.5;
var halfDepth = bsize[2]*0.5;
// x, y, z
var bnormals = box.normals;
var n1 = q.rotateVector(bnormals[1], new Vec3());
var n2 = q.rotateVector(bnormals[0], new Vec3());
var n3 = q.rotateVector(bnormals[2], new Vec3());
// Find the point on the cube closest to the center of the sphere
var closestPoint = new Vec3();
closestPoint.x = clamp(Vec3.dot(relativePosition,n1), -halfWidth, halfWidth);
closestPoint.y = clamp(Vec3.dot(relativePosition,n2), -halfHeight, halfHeight);
closestPoint.z = clamp(Vec3.dot(relativePosition,n3), -halfDepth, halfDepth);
// The vector found is relative to the center of the unrotated box -- rotate it
// to find the point w.r.t. to current orientation
closestPoint.applyRotation(q);
// The impact point in world space
var impactPoint = Vec3.add(pb, closestPoint, new Vec3());
var sphereToImpact = Vec3.subtract(impactPoint, ps, impactPoint);
var distanceToSphere = sphereToImpact.length();
// If impact point is not closer to the sphere's center than its radius -> no collision
var overlap = r - distanceToSphere;
if (overlap < 0) return;
var n = Vec3.scale(sphereToImpact, -1 / distanceToSphere, new Vec3());
var rBox = closestPoint;
var rSphere = sphereToImpact;
var wBox = Vec3.add(pb, rBox, new Vec3());
var wSphere = Vec3.add(ps, rSphere, new Vec3());
var collisionData = oMRequestCollisionData().reset(overlap, n, wBox, wSphere, rBox, rSphere);
context.contactManifoldTable.registerContact(box, sphere, collisionData);
} | Detects box-sphere collisions and registers the Contact.
@param {Object} context The Collision instance.
@param {Box} box The box collider.
@param {Sphere} sphere The sphere collider.
@return {undefined} undefined | boxIntersectSphere ( context , box , sphere ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function convexIntersectConvex(context, convex1, convex2) {
var glkSimplex = gjk(convex1, convex2);
// No simplex -> no collision
if (!glkSimplex) return;
var collisionData = epa(convex1, convex2, glkSimplex);
if (collisionData !== null) context.contactManifoldTable.registerContact(convex1, convex2, collisionData);
} | Detects convex-convex collisions and registers the Contact. Uses GJK to determine overlap and then
EPA to determine the actual collision data.
@param {Object} context The Collision instance.
@param {Particle} convex1 One convex body collider.
@param {Particle} convex2 The other convex body collider.
@return {undefined} undefined | convexIntersectConvex ( context , convex1 , convex2 ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function convexIntersectWall(context, convex, wall) {
if (convex.type === WALL) {
var temp = wall;
wall = convex;
convex = temp;
}
var convexPos = convex.position;
var wallPos = wall.position;
var n = wall.normal;
var invN = wall.invNormal;
var rConvex = convex.support(invN);
var wConvex = Vec3.add(convexPos, rConvex, new Vec3());
var diff = Vec3.subtract(wConvex, wallPos, VEC_REGISTER);
var penetration = Vec3.dot(diff, invN);
if (penetration < 0) return;
var wWall = Vec3.scale(n, penetration, new Vec3()).add(wConvex);
var rWall = Vec3.subtract(wWall, wall.position, new Vec3());
var collisionData = oMRequestCollisionData().reset(penetration, invN, wConvex, wWall, rConvex, rWall);
context.contactManifoldTable.registerContact(convex, wall, collisionData);
} | Detects convex-wall collisions and registers the Contact.
@param {Object} context The Collision instance.
@param {Particle} convex The convex body collider.
@param {Wall} wall The wall collider.
@return {undefined} undefined | convexIntersectWall ( context , convex , wall ) | javascript | Famous/engine | physics/constraints/Collision.js | https://github.com/Famous/engine/blob/master/physics/constraints/Collision.js | MIT |
function BallAndSocket(a, b, options) {
this.a = a;
this.b = b;
Constraint.call(this, options);
this.impulse = new Vec3();
this.angImpulseA = new Vec3();
this.angImpulseB = new Vec3();
this.error = new Vec3();
this.effMassMatrix = new Mat33();
} | A constraint that maintains positions and orientations with respect to a specific anchor point.
@class BallAndSocket
@extends Constraint
@param {Particle} a One of the bodies.
@param {Particle} b The other body.
@param {Options} options An object of configurable options. | BallAndSocket ( a , b , options ) | javascript | Famous/engine | physics/constraints/BallAndSocket.js | https://github.com/Famous/engine/blob/master/physics/constraints/BallAndSocket.js | MIT |
BallAndSocket.prototype.init = function() {
var w = this.anchor;
var a = this.a;
var b = this.b;
var q1t = Quaternion.conjugate(a.orientation, new Quaternion());
var q2t = Quaternion.conjugate(b.orientation, new Quaternion());
this.rA = Vec3.subtract(w, a.position, new Vec3());
this.rB = Vec3.subtract(w, b.position, new Vec3());
this.bodyRA = q1t.rotateVector(this.rA, new Vec3());
this.bodyRB = q2t.rotateVector(this.rB, new Vec3());
}; | Initialize the BallAndSocket. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | BallAndSocket.prototype.init ( ) | javascript | Famous/engine | physics/constraints/BallAndSocket.js | https://github.com/Famous/engine/blob/master/physics/constraints/BallAndSocket.js | MIT |
BallAndSocket.prototype.update = function(time, dt) {
var a = this.a;
var b = this.b;
var rA = a.orientation.rotateVector(this.bodyRA, this.rA);
var rB = b.orientation.rotateVector(this.bodyRB, this.rB);
var xRA = new Mat33([0,rA.z,-rA.y,-rA.z,0,rA.x,rA.y,-rA.x,0]);
var xRB = new Mat33([0,rB.z,-rB.y,-rB.z,0,rB.x,rB.y,-rB.x,0]);
var RIaRt = Mat33.multiply(xRA, a.inverseInertia, new Mat33()).multiply(xRA.transpose());
var RIbRt = Mat33.multiply(xRB, b.inverseInertia, new Mat33()).multiply(xRB.transpose());
var invEffInertia = Mat33.add(RIaRt, RIbRt, RIaRt);
var worldA = Vec3.add(a.position, this.rA, this.anchor);
var worldB = Vec3.add(b.position, this.rB, VEC2_REGISTER);
Vec3.subtract(worldB, worldA, this.error);
this.error.scale(0.2/dt);
var imA = a.inverseMass;
var imB = b.inverseMass;
var invEffMass = new Mat33([imA + imB,0,0,0,imA + imB,0,0,0,imA + imB]);
Mat33.add(invEffInertia, invEffMass, this.effMassMatrix);
this.effMassMatrix.inverse();
var impulse = this.impulse;
var angImpulseA = this.angImpulseA;
var angImpulseB = this.angImpulseB;
b.applyImpulse(impulse);
b.applyAngularImpulse(angImpulseB);
impulse.invert();
a.applyImpulse(impulse);
a.applyAngularImpulse(angImpulseA);
impulse.clear();
angImpulseA.clear();
angImpulseB.clear();
}; | Detect violations of the constraint. Warm start the constraint, if possible.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | BallAndSocket.prototype.update ( time , dt ) | javascript | Famous/engine | physics/constraints/BallAndSocket.js | https://github.com/Famous/engine/blob/master/physics/constraints/BallAndSocket.js | MIT |
BallAndSocket.prototype.resolve = function resolve() {
var a = this.a;
var b = this.b;
var rA = this.rA;
var rB = this.rB;
var v1 = Vec3.add(a.velocity, Vec3.cross(a.angularVelocity, rA, WxR_REGISTER), VB1_REGISTER);
var v2 = Vec3.add(b.velocity, Vec3.cross(b.angularVelocity, rB, WxR_REGISTER), VB2_REGISTER);
var impulse = v1.subtract(v2).subtract(this.error).applyMatrix(this.effMassMatrix);
var angImpulseB = Vec3.cross(rB, impulse, VEC1_REGISTER);
var angImpulseA = Vec3.cross(rA, impulse, VEC2_REGISTER).invert();
b.applyImpulse(impulse);
b.applyAngularImpulse(angImpulseB);
impulse.invert();
a.applyImpulse(impulse);
a.applyAngularImpulse(angImpulseA);
impulse.invert();
this.impulse.add(impulse);
this.angImpulseA.add(angImpulseA);
this.angImpulseB.add(angImpulseB);
}; | Apply impulses to resolve the constraint.
@method
@return {undefined} undefined | resolve ( ) | javascript | Famous/engine | physics/constraints/BallAndSocket.js | https://github.com/Famous/engine/blob/master/physics/constraints/BallAndSocket.js | MIT |
function Direction(a, b, options) {
this.a = a;
this.b = b;
Constraint.call(this, options);
this.impulse = 0;
this.distance = 0;
this.normal = new Vec3();
this.velocityBias = 0;
this.divisor = 0;
} | A constraint that maintains the direction of one body from another.
@class Direction
@extends Constraint
@param {Particle} a One of the bodies.
@param {Particle} b The other body.
@param {Object} options An object of configurable options. | Direction ( a , b , options ) | javascript | Famous/engine | physics/constraints/Direction.js | https://github.com/Famous/engine/blob/master/physics/constraints/Direction.js | MIT |
Direction.prototype.init = function() {
this.direction = this.direction || Vec3.subtract(this.b.position, this.a.position, new Vec3());
this.direction.normalize();
this.minLength = this.minLength || 0;
this.period = this.period || 0.2;
this.dampingRatio = this.dampingRatio || 0.5;
this.stiffness = 4 * PI * PI / (this.period * this.period);
this.damping = 4 * PI * this.dampingRatio / this.period;
}; | Initialize the Direction. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | Direction.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Direction.js | https://github.com/Famous/engine/blob/master/physics/constraints/Direction.js | MIT |
Direction.prototype.update = function update(time, dt) {
var a = this.a;
var b = this.b;
var n = NORMAL_REGISTER;
var diffP = P_REGISTER;
var impulse = IMPULSE_REGISTER;
var directionVector = DIRECTION_REGISTER;
var p1 = a.position;
var w1 = a.inverseMass;
var p2 = b.position;
var w2 = b.inverseMass;
var direction = this.direction;
Vec3.subtract(p2, p1, diffP);
Vec3.scale(direction, Vec3.dot(direction, diffP), directionVector);
var goal = directionVector.add(p1);
Vec3.subtract(p2, goal, n);
var dist = n.length();
n.normalize();
var invEffectiveMass = w1 + w2;
var effectiveMass = 1 / invEffectiveMass;
var gamma;
var beta;
if (this.period === 0) {
gamma = 0;
beta = 1;
}
else {
var c = this.damping * effectiveMass;
var k = this.stiffness * effectiveMass;
gamma = 1 / (dt*(c + dt*k));
beta = dt*k / (c + dt*k);
}
var baumgarte = beta * dist / dt;
var divisor = gamma + invEffectiveMass;
var lambda = this.impulse;
Vec3.scale(n, lambda, impulse);
b.applyImpulse(impulse);
a.applyImpulse(impulse.invert());
this.normal.copy(n);
this.distance = dist;
this.velocityBias = baumgarte;
this.divisor = divisor;
this.impulse = 0;
}; | Warmstart the constraint and prepare calculations used in .resolve.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | update ( time , dt ) | javascript | Famous/engine | physics/constraints/Direction.js | https://github.com/Famous/engine/blob/master/physics/constraints/Direction.js | MIT |
Direction.prototype.resolve = function update() {
var a = this.a;
var b = this.b;
var impulse = IMPULSE_REGISTER;
var diffV = V_REGISTER;
var minLength = this.minLength;
var dist = this.distance;
if (Math.abs(dist) < minLength) return;
var v1 = a.velocity;
var v2 = b.velocity;
var n = this.normal;
Vec3.subtract(v2, v1, diffV);
var lambda = -(Vec3.dot(n, diffV) + this.velocityBias) / this.divisor;
Vec3.scale(n, lambda, impulse);
b.applyImpulse(impulse);
a.applyImpulse(impulse.invert());
this.impulse += lambda;
}; | Adds an impulse to a physics body's velocity due to the constraint
@method
@return {undefined} undefined | update ( ) | javascript | Famous/engine | physics/constraints/Direction.js | https://github.com/Famous/engine/blob/master/physics/constraints/Direction.js | MIT |
function Constraint(options) {
options = options || {};
this.setOptions(options);
this._ID = _ID++;
} | Base Constraint class to be used in the Physics
Subclass this class to implement a constraint
@virtual
@class Constraint
@param {Object} options The options hash. | Constraint ( options ) | javascript | Famous/engine | physics/constraints/Constraint.js | https://github.com/Famous/engine/blob/master/physics/constraints/Constraint.js | MIT |
Constraint.prototype.setOptions = function setOptions(options) {
for (var key in options) this[key] = options[key];
this.init(options);
}; | Decorates the Constraint with the options object.
@method
@param {Object} options The options hash.
@return {undefined} undefined | setOptions ( options ) | javascript | Famous/engine | physics/constraints/Constraint.js | https://github.com/Famous/engine/blob/master/physics/constraints/Constraint.js | MIT |
Constraint.prototype.init = function init(options) {}; | Method invoked upon instantiation and the setting of options.
@method
@param {Object} options The options hash.
@return {undefined} undefined | init ( options ) | javascript | Famous/engine | physics/constraints/Constraint.js | https://github.com/Famous/engine/blob/master/physics/constraints/Constraint.js | MIT |
Constraint.prototype.resolve = function resolve(time, dt) {}; | Apply impulses to resolve the constraint.
@method
@param {Number} time The current time in the physics engine.
@param {Number} dt The physics engine frame delta.
@return {undefined} undefined | resolve ( time , dt ) | javascript | Famous/engine | physics/constraints/Constraint.js | https://github.com/Famous/engine/blob/master/physics/constraints/Constraint.js | MIT |
function Angle(a, b, options) {
this.a = a;
this.b = b;
Constraint.call(this, options);
this.effectiveInertia = new Mat33();
this.angularImpulse = new Vec3();
this.error = 0;
} | A constraint that keeps a physics body a given direction away from a given
anchor, or another attached body.
@class Angle
@extends Constraint
@param {Particle} a One of the bodies.
@param {Particle} b The other body.
@param {Object} options An object of configurable options. | Angle ( a , b , options ) | javascript | Famous/engine | physics/constraints/Angle.js | https://github.com/Famous/engine/blob/master/physics/constraints/Angle.js | MIT |
Angle.prototype.init = function() {
this.cosAngle = this.cosAngle || this.a.orientation.dot(this.b.orientation);
}; | Initialize the Angle. Sets defaults if a property was not already set.
@method
@param {Object} options The options hash.
@return {undefined} undefined | Angle.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Angle.js | https://github.com/Famous/engine/blob/master/physics/constraints/Angle.js | MIT |
Angle.prototype.update = function update() {
var a = this.a;
var b = this.b;
var q1 = a.orientation;
var q2 = b.orientation;
var cosTheta = q1.dot(q2);
var diff = 2*(cosTheta - this.cosAngle);
this.error = diff;
var angularImpulse = this.angularImpulse;
b.applyAngularImpulse(angularImpulse);
a.applyAngularImpulse(angularImpulse.invert());
Mat33.add(a.inverseInertia, b.inverseInertia, this.effectiveInertia);
this.effectiveInertia.inverse();
angularImpulse.clear();
}; | Warmstart the constraint and prepare calculations used in .resolve.
@method
@return {undefined} undefined | update ( ) | javascript | Famous/engine | physics/constraints/Angle.js | https://github.com/Famous/engine/blob/master/physics/constraints/Angle.js | MIT |
Angle.prototype.resolve = function update() {
var a = this.a;
var b = this.b;
var diffW = DELTA_REGISTER;
var w1 = a.angularVelocity;
var w2 = b.angularVelocity;
Vec3.subtract(w1, w2, diffW);
diffW.scale(1 + this.error);
var angularImpulse = diffW.applyMatrix(this.effectiveInertia);
b.applyAngularImpulse(angularImpulse);
a.applyAngularImpulse(angularImpulse.invert());
angularImpulse.invert();
this.angularImpulse.add(angularImpulse);
}; | Adds an angular impulse to a physics body's angular velocity.
@method
@return {undefined} undefined | update ( ) | javascript | Famous/engine | physics/constraints/Angle.js | https://github.com/Famous/engine/blob/master/physics/constraints/Angle.js | MIT |
function Hinge(a, b, options) {
this.a = a;
this.b = b;
Constraint.call(this, options);
this.impulse = new Vec3();
this.angImpulseA = new Vec3();
this.angImpulseB = new Vec3();
this.error = new Vec3();
this.errorRot = [0,0];
this.effMassMatrix = new Mat33();
this.effMassMatrixRot = [];
} | A constraint that confines two bodies to the plane defined by the axis of the hinge.
@class Hinge
@extends Constraint
@param {Particle} a One of the bodies.
@param {Particle} b The other body.
@param {Options} options The options hash. | Hinge ( a , b , options ) | javascript | Famous/engine | physics/constraints/Hinge.js | https://github.com/Famous/engine/blob/master/physics/constraints/Hinge.js | MIT |
Hinge.prototype.init = function() {
var w = this.anchor;
var u = this.axis.normalize();
var a = this.a;
var b = this.b;
var q1t = Quaternion.conjugate(a.orientation, new Quaternion());
var q2t = Quaternion.conjugate(b.orientation, new Quaternion());
this.rA = Vec3.subtract(w, a.position, new Vec3());
this.rB = Vec3.subtract(w, b.position, new Vec3());
this.bodyRA = q1t.rotateVector(this.rA, new Vec3());
this.bodyRB = q2t.rotateVector(this.rB, new Vec3());
this.axisA = Vec3.clone(u);
this.axisB = Vec3.clone(u);
this.axisBTangent1 = new Vec3();
this.axisBTangent2 = new Vec3();
this.t1xA = new Vec3();
this.t2xA = new Vec3();
this.bodyAxisA = q1t.rotateVector(u, new Vec3());
this.bodyAxisB = q2t.rotateVector(u, new Vec3());
}; | Initialize the Hinge. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | Hinge.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Hinge.js | https://github.com/Famous/engine/blob/master/physics/constraints/Hinge.js | MIT |
function Distance(a, b, options) {
this.a = a;
this.b = b;
Constraint.call(this, options);
this.impulse = 0;
this.distance = 0;
this.normal = new Vec3();
this.velocityBias = 0;
this.divisor = 0;
} | A constraint that keeps two bodies within a certain distance.
@class Distance
@extends Constraint
@param {Particle} a One of the bodies.
@param {Particle} b The other body.
@param {Object} options An object of configurable options. | Distance ( a , b , options ) | javascript | Famous/engine | physics/constraints/Distance.js | https://github.com/Famous/engine/blob/master/physics/constraints/Distance.js | MIT |
Distance.prototype.init = function() {
this.length = this.length || Vec3.subtract(this.b.position, this.a.position, P_REGISTER).length();
this.minLength = this.minLength || 0;
this.period = this.period || 0.2;
this.dampingRatio = this.dampingRatio || 0.5;
this.stiffness = 4 * PI * PI / (this.period * this.period);
this.damping = 4 * PI * this.dampingRatio / this.period;
}; | Initialize the Distance. Sets defaults if a property was not already set.
@method
@return {undefined} undefined | Distance.prototype.init ( ) | javascript | Famous/engine | physics/constraints/Distance.js | https://github.com/Famous/engine/blob/master/physics/constraints/Distance.js | MIT |
function GJK_EPASupportPoint(vertex, worldVertexA, worldVertexB) {
this.vertex = vertex;
this.worldVertexA = worldVertexA;
this.worldVertexB = worldVertexB;
} | Support point to be added to the DynamicGeometry. The point in Minkowski space as well as the
original pair.
@class GJK_EPASupportPoint
@param {Vec3} vertex The point in Minkowski space.
@param {Vec3} worldVertexA The one vertex.
@param {Vec3} worldVertexB The other vertex. | GJK_EPASupportPoint ( vertex , worldVertexA , worldVertexB ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
GJK_EPASupportPoint.prototype.reset = function reset(vertex, worldVertexA, worldVertexB) {
this.vertex = vertex;
this.worldVertexA = worldVertexA;
this.worldVertexB = worldVertexB;
return this;
}; | Used by ObjectManager to reset the object with different data.
@method
@param {Vec3} vertex The point in Minkowski space.
@param {Vec3} worldVertexA The one vertex.
@param {Vec3} worldVertexB The other vertex.
@return {GJK_EPASupportPoint} this | reset ( vertex , worldVertexA , worldVertexB ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
function freeGJK_EPADynamicGeometry(geometry) {
var vertices = geometry.vertices;
var i;
i = vertices.length;
while (i--) {
var v = vertices.pop();
if (v !== null) oMFreeGJK_EPASupportPoint(v);
}
geometry.numVertices = 0;
var features = geometry.features;
i = features.length;
while (i--) {
var f = features.pop();
if (f !== null) oMFreeDynamicGeometryFeature(f);
}
geometry.numFeatures = 0;
oMFreeDynamicGeometry(geometry);
} | Free the DynamicGeomtetry and associate vertices and features for later reuse.
@method
@param {DynamicGeometry} geometry The geometry to release to the pool.
@return {undefined} undefined | freeGJK_EPADynamicGeometry ( geometry ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
function minkowskiSupport(body1, body2, direction) {
var inverseDirection = Vec3.scale(direction, -1, INVDIRECTION_REGISTER);
var w1 = Vec3.add(body1.support(direction), body1.position, new Vec3());
var w2 = Vec3.add(body2.support(inverseDirection), body2.position, new Vec3());
// The vertex in Minkowski space as well as the original pair in world space
return oMRequestGJK_EPASupportPoint().reset(Vec3.subtract(w1, w2, new Vec3()), w1, w2);
} | Find the point in Minkowski space furthest in a given direction for two convex Bodies.
@method
@param {Particle} body1 The one body.
@param {Particle} body2 The other body.
@param {Vec3} direction The search direction.
@return {GJK_EPASupportPoint} The result. | minkowskiSupport ( body1 , body2 , direction ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
function gjk(body1, body2) {
var support = minkowskiSupport;
// Use p2 - p1 to seed the initial choice of direction
var direction = Vec3.subtract(body2.position, body1.position, DIRECTION_REGISTER).normalize();
var simplex = oMRequestDynamicGeometry();
simplex.addVertex(support(body1, body2, direction));
direction.invert();
var i = 0;
var maxIterations = 1e3;
while(i++ < maxIterations) {
if (direction.x === 0 && direction.y === 0 && direction.z === 0) break;
simplex.addVertex(support(body1, body2, direction));
if (Vec3.dot(simplex.getLastVertex().vertex, direction) < 0) break;
// If simplex contains origin, return for use in EPA
if (simplex.simplexContainsOrigin(direction, oMFreeGJK_EPASupportPoint)) return simplex;
}
freeGJK_EPADynamicGeometry(simplex);
return false;
} | Gilbert-Johnson-Keerthi collision detection. Returns a DynamicGeometry simplex if the bodies are found
to have collided, or false for no collsion.
@method
@param {Particle} body1 The one body.
@param {Particle} body2 The other body.
@return {DynamicGeometry|Boolean} Result of the GJK query. | gjk ( body1 , body2 ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
function epa(body1, body2, polytope) {
var support = minkowskiSupport;
var depthEstimate = Infinity;
var i = 0;
var maxIterations = 1e3;
while(i++ < maxIterations) {
var closest = polytope.getFeatureClosestToOrigin();
if (closest === null) return null;
var direction = closest.normal;
var point = support(body1, body2, direction);
depthEstimate = Math.min(depthEstimate, Vec3.dot(point.vertex, direction));
if (depthEstimate - closest.distance <= 0.01) {
var supportA = polytope.vertices[closest.vertexIndices[0]];
var supportB = polytope.vertices[closest.vertexIndices[1]];
var supportC = polytope.vertices[closest.vertexIndices[2]];
var A = supportA.vertex;
var B = supportB.vertex;
var C = supportC.vertex;
var P = Vec3.scale(direction, closest.distance, P_REGISTER);
var V0 = Vec3.subtract(B, A, V0_REGISTER);
var V1 = Vec3.subtract(C, A, V1_REGISTER);
var V2 = Vec3.subtract(P, A, V2_REGISTER);
var d00 = Vec3.dot(V0, V0);
var d01 = Vec3.dot(V0, V1);
var d11 = Vec3.dot(V1, V1);
var d20 = Vec3.dot(V2, V0);
var d21 = Vec3.dot(V2, V1);
var denom = d00*d11 - d01*d01;
var v = (d11*d20 - d01*d21) / denom;
var w = (d00*d21 - d01*d20) / denom;
var u = 1.0 - v - w;
var body1Contact = supportA.worldVertexA.scale(u)
.add(supportB.worldVertexA.scale(v))
.add(supportC.worldVertexA.scale(w));
var body2Contact = supportA.worldVertexB.scale(u)
.add(supportB.worldVertexB.scale(v))
.add(supportC.worldVertexB.scale(w));
var localBody1Contact = Vec3.subtract(body1Contact, body1.position, new Vec3());
var localBody2Contact = Vec3.subtract(body2Contact, body2.position, new Vec3());
freeGJK_EPADynamicGeometry(polytope);
oMFreeGJK_EPASupportPoint(point);
return ObjectManager.requestCollisionData().reset(closest.distance, direction, body1Contact, body2Contact, localBody1Contact, localBody2Contact);
}
else {
polytope.addVertex(point);
polytope.reshape();
}
}
throw new Error('EPA failed to terminate in allotted iterations.');
} | Expanding Polytope Algorithm--penetration depth, collision normal, and contact points.
Returns a CollisonData object.
@method
@param {Body} body1 The one body.
@param {Body} body2 The other body.
@param {DynamicGeometry} polytope The seed simplex from GJK.
@return {CollisionData} The collision data. | epa ( body1 , body2 , polytope ) | javascript | Famous/engine | physics/constraints/collision/ConvexCollisionDetection.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ConvexCollisionDetection.js | MIT |
function AABB(body) {
this._body = body;
this._ID = body._ID;
this.position = null;
this.vertices = {
x: [],
y: [],
z: []
};
this.update();
} | Axis-aligned bounding box. Used in collision broadphases.
@class AABB
@param {Particle} body The body around which to track a bounding box. | AABB ( body ) | javascript | Famous/engine | physics/constraints/collision/AABB.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/AABB.js | MIT |
AABB.prototype.update = function() {
var body = this._body;
var pos = this.position = body.position;
var minX = Infinity, maxX = -Infinity;
var minY = Infinity, maxY = -Infinity;
var minZ = Infinity, maxZ = -Infinity;
var type = body.type;
if (type === SPHERE) {
maxX = maxY = maxZ = body.radius;
minX = minY = minZ = -body.radius;
}
else if (type === WALL) {
var d = body.direction;
maxX = maxY = maxZ = 1e6;
minX = minY = minZ = -1e6;
switch (d) {
case DOWN:
maxY = 25;
minY = -1e3;
break;
case UP:
maxY = 1e3;
minY = -25;
break;
case LEFT:
maxX = 25;
minX = -1e3;
break;
case RIGHT:
maxX = 1e3;
minX = -25;
break;
case FORWARD:
maxZ = 25;
minZ = -1e3;
break;
case BACKWARD:
maxZ = 1e3;
minZ = -25;
break;
default:
break;
}
}
else if (body.vertices) {
// ConvexBody
var bodyVertices = body.vertices;
for (var i = 0, len = bodyVertices.length; i < len; i++) {
var vertex = bodyVertices[i];
if (vertex.x < minX) minX = vertex.x;
if (vertex.x > maxX) maxX = vertex.x;
if (vertex.y < minY) minY = vertex.y;
if (vertex.y > maxY) maxY = vertex.y;
if (vertex.z < minZ) minZ = vertex.z;
if (vertex.z > maxZ) maxZ = vertex.z;
}
}
else {
// Particle
maxX = maxY = maxZ = 25;
minX = minY = minZ = -25;
}
var vertices = this.vertices;
vertices.x[0] = minX + pos.x;
vertices.x[1] = maxX + pos.x;
vertices.y[0] = minY + pos.y;
vertices.y[1] = maxY + pos.y;
vertices.z[0] = minZ + pos.z;
vertices.z[1] = maxZ + pos.z;
}; | Update the bounds to reflect the current orientation and position of the parent Body.
@method
@return {undefined} undefined | AABB.prototype.update ( ) | javascript | Famous/engine | physics/constraints/collision/AABB.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/AABB.js | MIT |
AABB.checkOverlap = function(aabb1, aabb2) {
var vertices1 = aabb1.vertices;
var vertices2 = aabb2.vertices;
var x10 = vertices1.x[0];
var x11 = vertices1.x[1];
var x20 = vertices2.x[0];
var x21 = vertices2.x[1];
if ((x20 <= x10 && x10 <= x21) || (x10 <= x20 && x20 <= x11)) {
var y10 = vertices1.y[0];
var y11 = vertices1.y[1];
var y20 = vertices2.y[0];
var y21 = vertices2.y[1];
if ((y20 <= y10 && y10 <= y21) || (y10 <= y20 && y20 <= y11)) {
var z10 = vertices1.z[0];
var z11 = vertices1.z[1];
var z20 = vertices2.z[0];
var z21 = vertices2.z[1];
if ((z20 <= z10 && z10 <= z21) || (z10 <= z20 && z20 <= z11)) {
return true;
}
}
}
return false;
}; | Check for overlap between two AABB's.
@method
@param {AABB} aabb1 The first bounding box.
@param {AABB} aabb2 The second bounding box.
@return {undefined} undefined | AABB.checkOverlap ( aabb1 , aabb2 ) | javascript | Famous/engine | physics/constraints/collision/AABB.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/AABB.js | MIT |
function SweepAndPrune(targets) {
this._sweepVolumes = [];
this._entityRegistry = {};
this._boundingVolumeRegistry = {};
this.endpoints = {x: [], y: [], z: []};
this.overlaps = [];
this.overlapsMatrix = {};
this._IDPool = [];
targets = targets || [];
for (var i = 0; i < targets.length; i++) {
this.add(targets[i]);
}
} | Persistant object maintaining sorted lists of AABB endpoints used in a sweep-and-prune broadphase.
Used to accelerate collision detection.
http://en.wikipedia.org/wiki/Sweep_and_prune
@class SweepAndPrune
@param {Particle[]} targets The bodies to track. | SweepAndPrune ( targets ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
SweepAndPrune.prototype.add = function(body) {
var boundingVolume = new AABB(body);
var sweepVolume = new SweepVolume(boundingVolume);
this._entityRegistry[body._ID] = body;
this._boundingVolumeRegistry[body._ID] = boundingVolume;
this._sweepVolumes.push(sweepVolume);
for (var i = 0; i < 3; i++) {
var axis = AXES[i];
this.endpoints[axis].push(sweepVolume.points[axis][0]);
this.endpoints[axis].push(sweepVolume.points[axis][1]);
}
}; | Start tracking a body in the broad-phase.
@method
@param {Body} body The body to track.
@return {undefined} undefined | SweepAndPrune.prototype.add ( body ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
SweepAndPrune.prototype.remove = function remove(body) {
this._entityRegistry[body._ID] = null;
this._boundingVolumeRegistry[body._ID] = null;
var i, len;
var index;
for (i = 0, len = this._sweepVolumes.length; i < len; i++) {
if (this._sweepVolumes[i]._ID === body._ID) {
index = i;
break;
}
}
this._sweepVolumes.splice(index, 1);
var endpoints = this.endpoints;
var point;
var xs = [];
for (i = 0, len = endpoints.x.length; i < len; i++) {
point = endpoints.x[i];
if (point._ID !== body._ID) xs.push(point);
}
var ys = [];
for (i = 0, len = endpoints.y.length; i < len; i++) {
point = endpoints.y[i];
if (point._ID !== body._ID) ys.push(point);
}
var zs = [];
for (i = 0, len = endpoints.z.length; i < len; i++) {
point = endpoints.z[i];
if (point._ID !== body._ID) zs.push(point);
}
endpoints.x = xs;
endpoints.y = ys;
endpoints.z = zs;
}; | Stop tracking a body in the broad-phase.
@method
@param {Body} body The body to cease tracking.
@return {undefined} undefined | remove ( body ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
SweepAndPrune.prototype.update = function() {
var _sweepVolumes = this._sweepVolumes;
var _entityRegistry = this._entityRegistry;
var _boundingVolumeRegistry = this._boundingVolumeRegistry;
var i, j, k, len;
for (j = 0, len = _sweepVolumes.length; j < len; j++) {
_sweepVolumes[j].update();
}
var endpoints = this.endpoints;
var overlaps = this.overlaps;
var overlapsMatrix = this.overlapsMatrix;
var _IDPool = this._IDPool;
for (k = 0; k < 3; k++) {
var axis = AXES[k];
// Insertion sort:
var endpointAxis = endpoints[axis];
for (j = 1, len = endpointAxis.length; j < len; j++) {
var current = endpointAxis[j];
var val = current.value;
var swap;
var row;
var index;
var lowID;
var highID;
var cID;
var sID;
i = j - 1;
while (i >= 0 && (swap = endpointAxis[i]).value > val) {
// A swap occurence indicates that current and swap either just started or just stopped overlapping
cID = current._ID;
sID = swap._ID;
if (cID < sID) {
lowID = cID;
highID = sID;
}
else {
lowID = sID;
highID = cID;
}
// If, for this axis, min point of current and max point of swap
if (~current.side & swap.side) {
// Now overlapping on this axis -> possible overlap, do full AABB check
if (AABB.checkOverlap(_boundingVolumeRegistry[cID], _boundingVolumeRegistry[sID])) {
row = overlapsMatrix[lowID] = overlapsMatrix[lowID] || {};
index = row[highID] = _IDPool.length ? _IDPool.pop() : overlaps.length;
overlaps[index] = [_entityRegistry[lowID], _entityRegistry[highID]];
}
// Else if, for this axis, max point of current and min point of swap
}
else if (current.side & ~swap.side) {
// Now not overlapping on this axis -> definitely not overlapping
if ((row = overlapsMatrix[lowID]) && row[highID] != null) {
index = row[highID];
overlaps[index] = null;
row[highID] = null;
_IDPool.push(index);
}
}
// Else if max of both or min of both, still overlapping
endpointAxis[i + 1] = swap;
i--;
}
endpointAxis[i + 1] = current;
}
}
return overlaps;
}; | Update the endpoints of the tracked AABB's and resort the endpoint lists accordingly. Uses an insertion sort,
where swaps during the sort are taken to signify a potential change in overlap status for the two
relevant AABB's. Returns pairs of overlapping AABB's.
@method
@return {Array.<Particle[]>} The result of the broadphase. | SweepAndPrune.prototype.update ( ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
function SweepVolume(boundingVolume) {
this._boundingVolume = boundingVolume;
this._ID = boundingVolume._ID;
this.points = {
x: [{_ID: boundingVolume._ID, side: 0, value: null}, {_ID: boundingVolume._ID, side: 1, value: null}],
y: [{_ID: boundingVolume._ID, side: 0, value: null}, {_ID: boundingVolume._ID, side: 1, value: null}],
z: [{_ID: boundingVolume._ID, side: 0, value: null}, {_ID: boundingVolume._ID, side: 1, value: null}]
};
this.update();
} | Object used to associate an AABB with its endpoints in the sorted lists.
@class SweepVolume
@param {AABB} boundingVolume The bounding volume to track. | SweepVolume ( boundingVolume ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
SweepVolume.prototype.update = function() {
var boundingVolume = this._boundingVolume;
boundingVolume.update();
var points = this.points;
for (var i = 0; i < 3; i++) {
var axis = AXES[i];
points[axis][0].value = boundingVolume.vertices[axis][0];
points[axis][1].value = boundingVolume.vertices[axis][1];
}
}; | Update the endpoints to reflect the current location of the AABB.
@method
@return {undefined} undefined | SweepVolume.prototype.update ( ) | javascript | Famous/engine | physics/constraints/collision/SweepAndPrune.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/SweepAndPrune.js | MIT |
function ContactManifoldTable() {
this.manifolds = [];
this.collisionMatrix = {};
this._IDPool = [];
} | Table maintaining and managing current contact manifolds.
@class ContactManifoldTable | ContactManifoldTable ( ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.addManifold = function addManifold(lowID, highID, bodyA, bodyB) {
var collisionMatrix = this.collisionMatrix;
collisionMatrix[lowID] = collisionMatrix[lowID] || {};
var index = this._IDPool.length ? this._IDPool.pop() : this.manifolds.length;
this.collisionMatrix[lowID][highID] = index;
var manifold = oMRequestManifold().reset(lowID, highID, bodyA, bodyB);
this.manifolds[index] = manifold;
return manifold;
}; | Create a new contact manifold. Tracked by the collisionMatrix according to
its low-high ordered ID pair.
@method
@param {Number} lowID The lower id of the pair of bodies.
@param {Number} highID The higher id of the pair of bodies.
@param {Particle} bodyA The first body.
@param {Particle} bodyB The second body.
@return {ContactManifold} The new manifold. | addManifold ( lowID , highID , bodyA , bodyB ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.removeManifold = function removeManifold(manifold, index) {
var collisionMatrix = this.collisionMatrix;
this.manifolds[index] = null;
collisionMatrix[manifold.lowID][manifold.highID] = null;
this._IDPool.push(index);
oMFreeManifold(manifold);
}; | Remove a manifold and free it for later reuse.
@method
@param {ContactManifold} manifold The manifold to remove.
@param {Number} index The index of the manifold.
@return {undefined} undefined | removeManifold ( manifold , index ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.update = function update(dt) {
var manifolds = this.manifolds;
for (var i = 0, len = manifolds.length; i < len; i++) {
var manifold = manifolds[i];
if (!manifold) continue;
var persists = manifold.update(dt);
if (!persists) {
this.removeManifold(manifold, i);
manifold.bodyA.events.trigger('collision:end', manifold);
manifold.bodyB.events.trigger('collision:end', manifold);
}
}
}; | Update each of the manifolds, removing those that no longer contain contact points.
@method
@param {Number} dt Delta time.
@return {undefined} undefined | update ( dt ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.prepContacts = function prepContacts(dt) {
var manifolds = this.manifolds;
for (var i = 0, len = manifolds.length; i < len; i++) {
var manifold = manifolds[i];
if (!manifold) continue;
var contacts = manifold.contacts;
for (var j = 0, lenj = contacts.length; j < lenj; j++) {
var contact = contacts[j];
if (!contact) continue;
contact.update(dt);
}
}
}; | Warm start all Contacts, and perform precalculations needed in the iterative solver.
@method
@param {Number} dt Delta time.
@return {undefined} undefined | prepContacts ( dt ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.resolveManifolds = function resolveManifolds() {
var manifolds = this.manifolds;
for (var i = 0, len = manifolds.length; i < len; i++) {
var manifold = manifolds[i];
if (!manifold) continue;
manifold.resolveContacts();
}
}; | Resolve all contact manifolds.
@method
@return {undefined} undefined | resolveManifolds ( ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
ContactManifoldTable.prototype.registerContact = function registerContact(bodyA, bodyB, collisionData) {
var lowID;
var highID;
if (bodyA._ID < bodyB._ID) {
lowID = bodyA._ID;
highID = bodyB._ID;
}
else {
lowID = bodyB._ID;
highID = bodyA._ID;
}
var manifolds = this.manifolds;
var collisionMatrix = this.collisionMatrix;
var manifold;
if (!collisionMatrix[lowID] || collisionMatrix[lowID][highID] == null) {
manifold = this.addManifold(lowID, highID, bodyA, bodyB);
manifold.addContact(bodyA, bodyB, collisionData);
bodyA.events.trigger('collision:start', manifold);
bodyB.events.trigger('collision:start', manifold);
}
else {
manifold = manifolds[ collisionMatrix[lowID][highID] ];
manifold.contains(collisionData);
manifold.addContact(bodyA, bodyB, collisionData);
}
}; | Create a new Contact, also creating a new Manifold if one does not already exist for that pair.
@method
@param {Body} bodyA The first body.
@param {Body} bodyB The second body.
@param {CollisionData} collisionData The data for the collision.
@return {undefined} undefined | registerContact ( bodyA , bodyB , collisionData ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
function Manifold(lowID, highID, bodyA, bodyB) {
this.lowID = lowID;
this.highID = highID;
this.contacts = [];
this.numContacts = 0;
this.bodyA = bodyA;
this.bodyB = bodyB;
this.lru = 0;
} | Class to keep track of Contact points.
@class manifold
@param {Number} lowID The lower id of the pair of bodies.
@param {Number} highID The higher id of the pair of bodies.
@param {Particle} bodyA The first body.
@param {Particle} bodyB The second body. | Manifold ( lowID , highID , bodyA , bodyB ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
Manifold.prototype.reset = function reset(lowID, highID, bodyA, bodyB) {
this.lowID = lowID;
this.highID = highID;
this.contacts = [];
this.numContacts = 0;
this.bodyA = bodyA;
this.bodyB = bodyB;
this.lru = 0;
return this;
}; | Used by ObjectManager to reset the object with different data.
@method
@param {Number} lowID The lower id of the pair of bodies.
@param {Number} highID The higher id of the pair of bodies.
@param {Particle} bodyA The first body.
@param {Particle} bodyB The second body.
@return {Manifold} this | reset ( lowID , highID , bodyA , bodyB ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
Manifold.prototype.addContact = function addContact(bodyA, bodyB, collisionData) {
var index = this.lru;
if (this.contacts[index]) this.removeContact(this.contacts[index], index);
this.contacts[index] = oMRequestContact().reset(bodyA, bodyB, collisionData);
this.lru = (this.lru + 1) % 4;
this.numContacts++;
}; | Create a new Contact point and add it to the Manifold.
@method
@param {Body} bodyA The first body.
@param {Body} bodyB The second body.
@param {CollisionData} collisionData The data for the collision.
@return {undefined} undefined | addContact ( bodyA , bodyB , collisionData ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
Manifold.prototype.removeContact = function removeContact(contact, index) {
this.contacts[index] = null;
this.numContacts--;
ObjectManager.freeCollisionData(contact.data);
contact.data = null;
oMFreeContact(contact);
}; | Remove and free a Contact for later reuse.
@method
@param {Contact} contact The Contact to remove.
@param {Number} index The index of the Contact.
@return {undefined} undefined | removeContact ( contact , index ) | javascript | Famous/engine | physics/constraints/collision/ContactManifold.js | https://github.com/Famous/engine/blob/master/physics/constraints/collision/ContactManifold.js | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.