File: src\classes\ForceGenerators\DragForce.js
/**
* adds a drag force to associated objects
*
* @class DragForce
* @extends ForceGenerator
* @constructor
*/
Goblin.DragForce = function( drag_coefficient, squared_drag_coefficient ) {
/**
* drag coefficient
*
* @property drag_coefficient
* @type {Number}
* @default 0
*/
this.drag_coefficient = drag_coefficient || 0;
/**
* drag coefficient
*
* @property drag_coefficient
* @type {Number}
* @default 0
*/
this.squared_drag_coefficient = squared_drag_coefficient || 0;
/**
* whether or not the force generator is enabled
*
* @property enabled
* @type {Boolean}
* @default true
*/
this.enabled = true;
/**
* array of objects affected by the generator
*
* @property affected
* @type {Array}
* @default []
* @private
*/
this.affected = [];
};
Goblin.DragForce.prototype.enable = Goblin.ForceGenerator.prototype.enable;
Goblin.DragForce.prototype.disable = Goblin.ForceGenerator.prototype.disable;
Goblin.DragForce.prototype.affect = Goblin.ForceGenerator.prototype.affect;
Goblin.DragForce.prototype.unaffect = Goblin.ForceGenerator.prototype.unaffect;
/**
* applies force to the associated objects
*
* @method applyForce
*/
Goblin.DragForce.prototype.applyForce = function() {
if ( !this.enabled ) {
return;
}
var i, affected_count, object, drag,
force = _tmp_vec3_1;
for ( i = 0, affected_count = this.affected.length; i < affected_count; i++ ) {
object = this.affected[i];
force.copy( object.linear_velocity );
// Calculate the total drag coefficient.
drag = force.length();
drag = ( this.drag_coefficient * drag ) + ( this.squared_drag_coefficient * drag * drag );
// Calculate the final force and apply it.
force.normalize();
force.scale( -drag );
object.applyForce( force );
}
};