UNPKG

cannon

Version:

A lightweight 3D physics engine written in JavaScript.

78 lines (59 loc) 2.09 kB
module.exports = ConeEquation; var Vec3 = require('../math/Vec3'); var Mat3 = require('../math/Mat3'); var Equation = require('./Equation'); /** * Cone equation. Works to keep the given body world vectors aligned, or tilted within a given angle from each other. * @class ConeEquation * @constructor * @author schteppe * @param {Body} bodyA * @param {Body} bodyB * @param {Vec3} [options.axisA] Local axis in A * @param {Vec3} [options.axisB] Local axis in B * @param {Vec3} [options.angle] The "cone angle" to keep * @param {number} [options.maxForce=1e6] * @extends Equation */ function ConeEquation(bodyA, bodyB, options){ options = options || {}; var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6; Equation.call(this,bodyA,bodyB,-maxForce, maxForce); this.axisA = options.axisA ? options.axisA.clone() : new Vec3(1, 0, 0); this.axisB = options.axisB ? options.axisB.clone() : new Vec3(0, 1, 0); /** * The cone angle to keep * @property {number} angle */ this.angle = typeof(options.angle) !== 'undefined' ? options.angle : 0; } ConeEquation.prototype = new Equation(); ConeEquation.prototype.constructor = ConeEquation; var tmpVec1 = new Vec3(); var tmpVec2 = new Vec3(); ConeEquation.prototype.computeB = function(h){ var a = this.a, b = this.b, ni = this.axisA, nj = this.axisB, nixnj = tmpVec1, njxni = tmpVec2, GA = this.jacobianElementA, GB = this.jacobianElementB; // Caluclate cross products ni.cross(nj, nixnj); nj.cross(ni, njxni); // The angle between two vector is: // cos(theta) = a * b / (length(a) * length(b) = { len(a) = len(b) = 1 } = a * b // g = a * b // gdot = (b x a) * wi + (a x b) * wj // G = [0 bxa 0 axb] // W = [vi wi vj wj] GA.rotational.copy(njxni); GB.rotational.copy(nixnj); var g = Math.cos(this.angle) - ni.dot(nj), GW = this.computeGW(), GiMf = this.computeGiMf(); var B = - g * a - GW * b - h * GiMf; return B; };