-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1112f5f
commit 71de2c3
Showing
121 changed files
with
25,170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
planck.testbed('8 Ball', function(testbed) { | ||
var pl = planck, Vec2 = pl.Vec2, Math = pl.Math; | ||
|
||
var SPI4 = Math.sin(Math.PI / 4), SPI3 = Math.sin(Math.PI / 3); | ||
|
||
var COLORED = true; | ||
var BLACK = {fill: 'black', stroke: 'white'}; | ||
var WHITE = {fill: 'white', stroke: 'black'}; | ||
var COLORS = [ | ||
{fill: '#ffdd00', stroke: '#000000'}, | ||
{fill: '#ffdd00', stroke: '#ffffff'}, | ||
{fill: '#ff3300', stroke: '#000000'}, | ||
{fill: '#ff3300', stroke: '#ffffff'}, | ||
{fill: '#662200', stroke: '#000000'}, | ||
{fill: '#662200', stroke: '#ffffff'}, | ||
{fill: '#ff8800', stroke: '#000000'}, | ||
{fill: '#ff8800', stroke: '#ffffff'}, | ||
{fill: '#00bb11', stroke: '#000000'}, | ||
{fill: '#00bb11', stroke: '#ffffff'}, | ||
{fill: '#9900ff', stroke: '#000000'}, | ||
{fill: '#9900ff', stroke: '#ffffff'}, | ||
{fill: '#0077ff', stroke: '#000000'}, | ||
{fill: '#0077ff', stroke: '#ffffff'} | ||
]; | ||
|
||
var width = 8.00, height = 4.00; | ||
|
||
var BALL_R = 0.12; | ||
var POCKET_R = 0.2; | ||
|
||
testbed.x = 0; | ||
testbed.y = 0; | ||
testbed.width = width * 1.2; | ||
testbed.height = height * 1.2; | ||
testbed.ratio = 100; | ||
testbed.mouseForce = -30; | ||
|
||
pl.internal.Settings.velocityThreshold = 0; | ||
|
||
var world = pl.World({}); | ||
|
||
var railH = [ | ||
Vec2(POCKET_R, height * .5), | ||
Vec2(POCKET_R, height * .5 + POCKET_R), | ||
Vec2(width * .5 - POCKET_R / SPI4 + POCKET_R, height * .5 + POCKET_R), | ||
Vec2(width * .5 - POCKET_R / SPI4, height * .5) | ||
]; | ||
|
||
var railV = [ | ||
Vec2(width * .5, -(height * .5 - POCKET_R / SPI4)), | ||
Vec2(width * .5 + POCKET_R, -(height * .5 - POCKET_R / SPI4 + POCKET_R)), | ||
Vec2(width * .5 + POCKET_R, height * .5 - POCKET_R / SPI4 + POCKET_R), | ||
Vec2(width * .5, height * .5 - POCKET_R / SPI4) | ||
]; | ||
|
||
var railFixDef = { | ||
friction: 0.1, | ||
restitution: 0.9, | ||
userData: 'rail' | ||
}; | ||
var pocketFixDef = { | ||
userData: 'pocket' | ||
}; | ||
var ballFixDef = { | ||
friction: 0.1, | ||
restitution: 0.99, | ||
density: 1, | ||
userData: 'ball' | ||
}; | ||
var ballBodyDef = { | ||
linearDamping: 1.5, | ||
angularDamping: 1 | ||
}; | ||
|
||
world.createBody().createFixture(pl.Polygon(railV.map(scale(+1, +1))), railFixDef); | ||
world.createBody().createFixture(pl.Polygon(railV.map(scale(-1, +1))), railFixDef); | ||
|
||
world.createBody().createFixture(pl.Polygon(railH.map(scale(+1, +1))), railFixDef); | ||
world.createBody().createFixture(pl.Polygon(railH.map(scale(-1, +1))), railFixDef); | ||
world.createBody().createFixture(pl.Polygon(railH.map(scale(+1, -1))), railFixDef); | ||
world.createBody().createFixture(pl.Polygon(railH.map(scale(-1, -1))), railFixDef); | ||
|
||
world.createBody().createFixture(pl.Circle(Vec2(0, -height * .5 - POCKET_R * 1.5), POCKET_R), pocketFixDef); | ||
world.createBody().createFixture(pl.Circle(Vec2(0, +height * .5 + POCKET_R * 1.5), POCKET_R), pocketFixDef); | ||
|
||
world.createBody().createFixture(pl.Circle(Vec2(+width * .5 + POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef); | ||
world.createBody().createFixture(pl.Circle(Vec2(-width * .5 - POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef); | ||
|
||
world.createBody().createFixture(pl.Circle(Vec2(+width * .5 + POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef); | ||
world.createBody().createFixture(pl.Circle(Vec2(-width * .5 - POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef); | ||
|
||
var balls = rack(BALL_R).map(translate(width / 4, 0)); | ||
|
||
balls.push({x: -width / 4, y: 0}); | ||
|
||
if (COLORED) { | ||
shuffleArray(COLORS); | ||
for (var i = 0; i < COLORS.length; i++) { | ||
balls[i].render = COLORS[i]; | ||
} | ||
balls[14].render = balls[4].render; | ||
balls[4].render = BLACK; | ||
balls[balls.length - 1].render = WHITE; | ||
} | ||
|
||
for (i = 0; i < balls.length; i++) { | ||
var ball = world.createDynamicBody(ballBodyDef); | ||
ball.setBullet(true); | ||
ball.setPosition(balls[i]); | ||
ball.createFixture(pl.Circle(BALL_R), ballFixDef); | ||
ball.render = balls[i].render; | ||
} | ||
|
||
world.on('post-solve', function(contact) { | ||
var fA = contact.getFixtureA(), bA = fA.getBody(); | ||
var fB = contact.getFixtureB(), bB = fB.getBody(); | ||
|
||
var pocket = fA.getUserData() == pocketFixDef.userData && bA || fB.getUserData() == pocketFixDef.userData && bB; | ||
var ball = fA.getUserData() == ballFixDef.userData && bA || fB.getUserData() == ballFixDef.userData && bB; | ||
|
||
// do not change world immediately | ||
setTimeout(function() { | ||
if (ball && pocket) { | ||
world.destroyBody(ball); | ||
} | ||
}, 1); | ||
}); | ||
|
||
return world; | ||
|
||
function rack(r) { | ||
var n = 5; | ||
var balls = []; | ||
var d = r * 2, l = SPI3 * d; | ||
for (var i = 0; i < n; i++) { | ||
for (var j = 0; j <= i; j++) { | ||
balls.push({ | ||
x: i * l /*- (n - 1) * 0.5 * l*/ + Math.random(r * 0.02), | ||
y: (j - i * 0.5 ) * d + Math.random(r * 0.02), | ||
}); | ||
} | ||
} | ||
return balls; | ||
} | ||
|
||
function scale(x, y) { | ||
return function(v) { | ||
return pl.Vec2(v.x * x, v.y * y); | ||
}; | ||
} | ||
|
||
function translate(x, y) { | ||
return function(v) { | ||
return pl.Vec2(v.x + x, v.y + y); | ||
}; | ||
} | ||
|
||
function shuffleArray(array) { | ||
// http://stackoverflow.com/a/12646864/483728 | ||
for (var i = array.length - 1; i > 0; i--) { | ||
var j = Math.floor(Math.random() * (i + 1)); | ||
var temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
return array; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2016-2017 Ali Shakiba http://shakiba.me/planck.js | ||
* Copyright (c) 2006-2012 Erin Catto http://www.box2d.org | ||
* | ||
* This software is provided 'as-is', without any express or implied | ||
* warranty. In no event will the authors be held liable for any damages | ||
* arising from the use of this software. | ||
* Permission is granted to anyone to use this software for any purpose, | ||
* including commercial applications, and to alter it and redistribute it | ||
* freely, subject to the following restrictions: | ||
* 1. The origin of this software must not be misrepresented; you must not | ||
* claim that you wrote the original software. If you use this software | ||
* in a product, an acknowledgment in the product documentation would be | ||
* appreciated but is not required. | ||
* 2. Altered source versions must be plainly marked as such, and must not be | ||
* misrepresented as being the original software. | ||
* 3. This notice may not be removed or altered from any source distribution. | ||
*/ | ||
|
||
planck.testbed('AddPair', function(testbed) { | ||
var pl = planck, Vec2 = pl.Vec2; | ||
var world = new pl.World(Vec2(0, 0)); | ||
|
||
var shape = pl.Circle(0.1); | ||
|
||
testbed.y = 0; | ||
testbed.hz = 60; | ||
testbed.speed = 0.5; | ||
|
||
for (var i = 0; i < 50; ++i) { | ||
var body = world.createBody({ | ||
type : 'dynamic', | ||
position : Vec2(pl.Math.random(0.0, -6.0), pl.Math.random(-1.0, 1.0)) | ||
}); | ||
body.createFixture(shape, 0.01); | ||
} | ||
|
||
var box = world.createBody({ | ||
type : 'dynamic', | ||
position : Vec2(-40.0, 0.0), | ||
bullet : true | ||
}); | ||
|
||
box.createFixture(pl.Box(1.5, 1.5), 1.0); | ||
box.setLinearVelocity(Vec2(100.0, 0.0)); | ||
|
||
return world; | ||
}); |
Oops, something went wrong.