Skip to content
This repository has been archived by the owner on Dec 15, 2019. It is now read-only.

Commit

Permalink
Merge branch '1.0.0-rc1'
Browse files Browse the repository at this point in the history
  • Loading branch information
wellcaffeinated committed Apr 22, 2014
2 parents 7892343 + b96fc61 commit eefc5ac
Show file tree
Hide file tree
Showing 160 changed files with 87,433 additions and 21,768 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.grunt
.DS_Store
_SpecRunner.html
_working
_working
.sass-cache
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A modular, extendable, and easy-to-use physics engine for javascript.

Latest version: 0.5.4 (alpha)
Latest version: 0.6.0 (beta)

## Usage

Expand All @@ -28,7 +28,7 @@ then run grunt
$ grunt

The default grunt task will create a `_working/` directory with the
PhysicsJS development build. You can play around with that.
PhysicsJS development build. You can play around with that.
**NOTE**: the `_working/` directory won't be committed
(it is in .gitignore).

Expand Down
34 changes: 34 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "PhysicsJS",
"version": "0.6.0",
"homepage": "http://wellcaffeinated.net/PhysicsJS",
"authors": [
"Jasper Palfree <[email protected]>"
],
"description": "A modular, extendable, and easy-to-use physics engine for javascript",
"main": "dist/physicsjs-full-0.6.0.js",
"moduleType": [
"amd",
"globals",
"node"
],
"keywords": [
"physics",
"engine",
"simulation"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests",
"editor",
"lib",
"src",
"jshint.json",
"gruntfile.js",
"package.json"
]
}
126 changes: 126 additions & 0 deletions dist/behaviors/attractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* PhysicsJS v0.6.0 - 2014-04-22
* A modular, extendable, and easy-to-use physics engine for javascript
* http://wellcaffeinated.net/PhysicsJS
*
* Copyright (c) 2014 Jasper Palfree <[email protected]>
* Licensed MIT
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['physicsjs'], factory);
} else if (typeof exports === 'object') {
module.exports = factory.apply(root, ['physicsjs'].map(require));
} else {
factory.call(root, root.Physics);
}
}(this, function (Physics) {
'use strict';
/**
* class AttractorBehavior < Behavior
*
* `Physics.behavior('attractor')`.
*
* Attractor behavior attracts bodies to a specific point.
*
* Additional options include:
* - pos: The position of the attraction point
* - strength: How strong the attraction is (default: `1`)
* - order: The power of the inverse distance (default: `2` because that is newtonian gravity... inverse square)
* - max: The maximum distance in which to apply the attraction (default: Infinity)
* - min: The minimum distance above which to apply the attraction (default: very small non-zero)
**/
Physics.behavior('attractor', function( parent ){

var defaults = {

pos: null, // default to (0, 0)
// how strong the attraction is
strength: 1,
// power of the inverse distance (2 is inverse square)
order: 2,
// max distance to apply it to
max: false, // infinite
// min distance to apply it to
min: false // auto calc
};

return {

// extended
init: function( options ){

var self = this;
this._pos = new Physics.vector();
// call parent init method
parent.init.call( this );
this.options.defaults( defaults );
this.options.onChange(function( opts ){
self._maxDist = opts.max === false ? Infinity : opts.max;
self._minDist = opts.min ? opts.min : 10;
self.position( opts.pos );
});
this.options( options );
},

/**
* AttractorBehavior#position( [pos] ) -> this|Object
* - pos (Vectorish): The position to set
* + (Object): Returns the [[Vectorish]] position if no arguments provided
* + (this): For chaining
*
* Get or set the position of the attractor.
**/
position: function( pos ){

var self = this;

if ( pos ){
this._pos.clone( pos );
return self;
}

return this._pos.values();
},

// extended
behave: function( data ){

var bodies = this.getTargets()
,body
,order = this.options.order
,strength = this.options.strength
,minDist = this._minDist
,maxDist = this._maxDist
,scratch = Physics.scratchpad()
,acc = scratch.vector()
,norm
,g
;

for ( var j = 0, l = bodies.length; j < l; j++ ){

body = bodies[ j ];

// clone the position
acc.clone( this._pos );
acc.vsub( body.state.pos );
// get the distance
norm = acc.norm();

if (norm > minDist && norm < maxDist){

g = strength / Math.pow(norm, order);

body.accelerate( acc.normalize().mult( g ) );
}
}

scratch.done();
}
};
});

// end module: behaviors/attractor.js
return Physics;
}));// UMD
Loading

0 comments on commit eefc5ac

Please sign in to comment.