Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable #366

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Temp files
*~
*#

# Node Modules
node_modules/*

# File system
.DS_Store

# Sublime Text
*.sublime-project
*.sublime-workspace
# Temp files
*~
*#

# Node Modules
node_modules/*

# File system
.DS_Store

# Sublime Text
*.sublime-project
*.sublime-workspace
/nbproject/private/
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"strict": false,
"trailing" : true,
"undef": true,
"unused": true,
"indent": 4,
"shadow" : true,
"laxcomma": true,
Expand Down
42 changes: 18 additions & 24 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,31 @@ module.exports = function(grunt) {
options: {
nospawn: false
},
files: [
'src/**/*',
'demos/js/*Renderer.js',
'test/**/*'
],
tasks: [
'dev'
]
source: {
files: 'src/**/*',
tasks: [
'default'
]
},
renderer: {
files: 'demos/js/*Renderer.js',
tasks: [
'concat:renderer'
]
},
test: {
files: ['src/**/*', 'test/**/*'],
tasks: [
'test'
]
},
},

concat: {
renderer: {
src: ['demos/js/pixi.js', 'demos/js/dat.gui.js', 'demos/js/Renderer.js', 'demos/js/WebGLRenderer.js'],
dest: 'build/p2.renderer.js',
}
},

yuidoc: {
compile: {
name: 'p2.js',
description: '<%= pkg.description %>',
version: '<%= pkg.version %>',
url: '<%= pkg.homepage %>',
options: {
outdir : "docs",
paths : ["./src/"],
exclude: ".DS_Store,.svn,CVS,.git,build_rollup_tmp,build_tmp,gl-matrix"
}
}
}
});

Expand All @@ -82,10 +78,8 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-yuidoc');

grunt.registerTask('default', ['test','jshint','browserify','concat','uglify','addLicense','requireJsFix']);
grunt.registerTask('dev', ['test','jshint','browserify','concat']);
grunt.registerTask('test', ['nodeunit']);

// Not sure what flag Browserify needs to do this. Fixing it manually for now.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 p2.js authors
* Copyright (c) 2015 p2.js authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
77 changes: 24 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,55 +74,38 @@ var circleBody = new p2.Body({
position: [0, 10]
});

// Add a circle shape to the body
// Add a circle shape to the body.
var circleShape = new p2.Circle({ radius: 1 });
circleBody.addShape(circleShape);

// ...and add the body to the world.
// If we don't add it to the world, it won't be simulated.
world.addBody(circleBody);

// Create an infinite ground plane body
// Create an infinite ground plane.
var groundBody = new p2.Body({
mass: 0 // Setting mass to 0 makes it static
mass: 0 // Setting mass to 0 makes the body static
});
var groundShape = new p2.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);

// To animate the bodies, we must step the world forward in time, using a fixed time step size.
// The World will run substeps and interpolate automatically for us, to get smooth animation.
var fixedTimeStep = 1 / 60; // seconds
var maxSubSteps = 10; // Max sub steps to catch up with the wall clock
var lastTime;
// To get the trajectories of the bodies,
// we must step the world forward in time.
// This is done using a fixed time step size.
var timeStep = 1 / 60; // seconds

// Animation loop
function animate(time){
requestAnimationFrame(animate);
// The "Game loop". Could be replaced by, for example, requestAnimationFrame.
setInterval(function(){

// Compute elapsed time since last render frame
var deltaTime = lastTime ? (time - lastTime) / 1000 : 0;
// The step method moves the bodies forward in time.
world.step(timeStep);

// Move bodies forward in time
world.step(fixedTimeStep, deltaTime, maxSubSteps);
// Print the circle position to console.
// Could be replaced by a render call.
console.log("Circle y position: " + circleBody.position[1]);

// Render the circle at the current interpolated position
renderCircleAtPosition(circleBody.interpolatedPosition);

lastTime = time;
}

// Start the animation loop
requestAnimationFrame(animate);
```

To interact with bodies, you need to do it *after each internal step*. Simply attach a *"postStep"* listener to the world, and make sure to use ```body.position``` here - ```body.interpolatedPosition``` is only for rendering.

```js
world.on('postStep', function(event){
// Add horizontal spring force
circleBody.force[0] -= 100 * circleBody.position[0];
});
}, 1000 * timeStep);
```

### Install
Expand Down Expand Up @@ -163,28 +146,16 @@ var p2 = require('p2');

Note that concave polygon shapes can be created using [Body.fromPolygon](http://schteppe.github.io/p2.js/docs/classes/Body.html#method_fromPolygon).

### Install
### Unit testing
Tests are written for [Nodeunit](https://github.com/caolan/nodeunit). Run the tests with the command ```grunt test```.

### Contribute
Make sure you have git, [Node.js](http://nodejs.org), NPM and [grunt](http://gruntjs.com/) installed.
```
git clone https://github.com/schteppe/p2.js.git;
git clone https://github.com/schteppe/p2.js.git; # Clone the repo
cd p2.js;
npm install; # Install dependencies
grunt;
```

### Grunt tasks
List all tasks using ```grunt --help```.
```
grunt # Run tests, build, minify
grunt dev # Run tests, build
grunt test # Run tests
grunt yuidoc # Build docs
grunt watch # Watch for changes and run the "dev" task
npm install; # Install dependencies
# (make changes to source)
grunt; # Builds build/p2.js and build/p2.min.js
```

### Release process
1. Bump version number.
2. Build and commit files in ```build/``` and ```docs/```.
3. Tag the commit with the version number e.g. vX.Y.Z
4. Add relase notes to github
5. Publish to NPM
The most recent commits are currently pushed to the ```master``` branch. Thanks for contributing!
4 changes: 2 additions & 2 deletions demos/car.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
world.addConstraint(revoluteFront);

// Enable the constraint motor for the back wheel
revoluteBack.motorEnabled = true;
revoluteBack.motorSpeed = 10; // Rotational speed in radians per second
revoluteBack.enableMotor();
revoluteBack.setMotorSpeed(10); // Rotational speed in radians per second

this.frame(0, 0, 8, 6);
});
Expand Down
2 changes: 2 additions & 0 deletions demos/circles.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

// Pre-fill object pools. Completely optional but good for performance!
world.overlapKeeper.recordPool.resize(16);
world.islandManager.islandPool.resize(128);
world.islandManager.nodePool.resize(1024);
world.narrowphase.contactEquationPool.resize(1024);
world.narrowphase.frictionEquationPool.resize(1024);

Expand Down
12 changes: 2 additions & 10 deletions demos/compound.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,25 @@
position: [M*r*d, N*r*d*2],
angularVelocity : 1
});
body3 = new p2.Body({
mass: 1,
position: [-2*M*r*d, N*r*d*2],
angularVelocity : 1
});
for(var i=0; i<N; i++){
for(var j=0; j<M; j++){
var x = (i-N/2+1/2)*2*r*d;
var y = (j-M/2+1/2)*2*r*d;
var angle = (j + i) * Math.PI / 8;
body1.addShape(new p2.Particle(), [x,y], 0);
body2.addShape(new p2.Circle({ radius: r }), [x,y], 0);
body3.addShape(new p2.Capsule({ radius: r/2, length: r }), [x,y], angle);
}
}
world.addBody(body1);
world.addBody(body2);
world.addBody(body3);

// Create boxes
box = new p2.Body({
position:[3,2],
mass : 1,
angularVelocity : -0.2
});
box.addShape(new p2.Box({ width: 1, height: 1 }), [0,0.5], Math.PI/4);
box.addShape(new p2.Box({ width: 1, height: 1 }), [0,-0.5], 0);
box.addShape(new p2.Box({ width: 1, height: 1 }), [0,0.5]);
box.addShape(new p2.Box({ width: 1, height: 1 }), [0,-0.5]);
world.addBody(box);

// Create circle
Expand Down
4 changes: 3 additions & 1 deletion demos/constraints.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
position: [x,y],
});
p.addShape(new p2.Circle({ radius: r }));
world.addBody(p);
if(lastBody){
var c = new p2.DistanceConstraint(p, lastBody);
world.addConstraint(c);
} else {
p.velocity[0] = 10;
}
lastBody = p;
world.addBody(p);
}

// Create RevoluteConstraint
Expand All @@ -79,6 +79,8 @@
});
bodyB.addShape(new p2.Circle({ radius: 1 }));
world.addBody(bodyB);
var pivotA = [0,2];
var pivotB = [0,-2];
var cr = new p2.RevoluteConstraint(bodyA, bodyB, {
worldPivot: [3, 2]
});
Expand Down
Loading