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

Convert examples to typescript #283

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a4a529a
perf: 8ball example converted to typescript
Arsham-dev Jul 22, 2024
b066fd4
perf: addpair example converted to typescript
Arsham-dev Jul 22, 2024
ca65110
perf: applyforce example converted to typescript
Arsham-dev Jul 22, 2024
53b9f22
perf: astroid code converted to typescript
Arsham-dev Jul 22, 2024
3d5d9b2
perf: code of basic slider crank converted to typescript
Arsham-dev Jul 22, 2024
b46ec42
perf: code of body types converted to typescript
Arsham-dev Jul 22, 2024
8161cf7
perf: code of boxes converted to typescript
Arsham-dev Jul 22, 2024
8a2d1cb
perf: code of breakable converted to typescript
Arsham-dev Jul 22, 2024
5b59421
perf: code of bridge converted to typescript
Arsham-dev Jul 22, 2024
5bfeb79
perf: code of bullettest converted to typescript
Arsham-dev Jul 22, 2024
f971a4f
perf: code of cantilevel converted to typescript
Arsham-dev Jul 22, 2024
f4aaa2c
perf: code of car converted to typescript
Arsham-dev Jul 22, 2024
ab65537
perf: code of chain converted to typescript
Arsham-dev Jul 22, 2024
57516f1
perf: code of charactercollision converted to typescript
Arsham-dev Jul 22, 2024
31796d9
perf: code of collisionprocessing converted to typescript
Arsham-dev Jul 22, 2024
a422960
perf: code of compundshapes converted to typescript
Arsham-dev Jul 22, 2024
a0b9c45
perf: code of confined converted to typescript
Arsham-dev Jul 22, 2024
50bd6bc
perf: code of continuoustest converted to typescript
Arsham-dev Jul 22, 2024
78a6d2b
perf: code of convexhull converted to typescript
Arsham-dev Jul 22, 2024
14ff0fb
perf: code of conveyorbelt converted to typescript
Arsham-dev Jul 22, 2024
f9817a8
perf: code of dominos converted to typescript
Arsham-dev Jul 22, 2024
9d745b5
perf: code of edgeshapes converted to typescript
Arsham-dev Jul 22, 2024
a919183
perf: code of edgetest converted to typescript
Arsham-dev Jul 22, 2024
63c1c06
perf: code of gears converted to typescript
Arsham-dev Jul 22, 2024
8a1cfb1
perf: code of heavyonlight converted to typescript
Arsham-dev Jul 22, 2024
4b02171
perf: code of heavyonlighttwo converted to typescript
Arsham-dev Jul 22, 2024
8a35d93
perf: code of mixer converted to typescript
Arsham-dev Jul 22, 2024
ae28ac9
perf: code of motorjoint converted to typescript
Arsham-dev Jul 22, 2024
f47efb9
perf: code of pinball converted to typescript
Arsham-dev Jul 22, 2024
5bcff14
perf: code of polycollision converted to typescript
Arsham-dev Jul 22, 2024
ef9c8bd
perf: code of prismatic converted to typescript
Arsham-dev Jul 22, 2024
fe4d9c6
perf: code of pulleys converted to typescript
Arsham-dev Jul 22, 2024
50c3a29
perf: code of pyramid converted to typescript
Arsham-dev Jul 22, 2024
edb991a
perf: code of revolute converted to typescript
Arsham-dev Jul 22, 2024
f1344e4
perf: code of sensortest converted to type
Arsham-dev Jul 23, 2024
ea99e65
perf: code of shapecast converted to typescript
Arsham-dev Jul 23, 2024
ef5a7e5
perf: code of shapeediting converted to typescript
Arsham-dev Jul 23, 2024
7a2d874
perf: code of shuffle converted to typescript
Arsham-dev Jul 23, 2024
6cd1c39
perf: code of slidercrank converted to typescript
Arsham-dev Jul 23, 2024
bccdc2a
perf: code of soccer converted to typescript
Arsham-dev Jul 23, 2024
0bebb87
perf: code of spherestack converted to typescript
Arsham-dev Jul 23, 2024
13a9e2d
perf: code of theojansen converted to typescript
Arsham-dev Jul 23, 2024
1cd7008
perf: code of tiles converted to typescript
Arsham-dev Jul 23, 2024
f874d71
perf: code of timeofimpact converted to typescript
Arsham-dev Jul 23, 2024
1067387
perf: code of tumbler converted to typescript
Arsham-dev Jul 23, 2024
d81cd48
perf: code of varyingfriction converted to typescript
Arsham-dev Jul 23, 2024
2b63fa8
perf: code of varyingrestitutiin converted to typescript
Arsham-dev Jul 23, 2024
702261f
perf: code of verticalstack converted to typescript
Arsham-dev Jul 23, 2024
b57e1cb
perf: code of web converted to typescript
Arsham-dev Jul 23, 2024
7e151cb
perf: code of web distancetest and collisionfiltering to typescript
Arsham-dev Jul 23, 2024
95ea9e9
perf: code of dynamictreetest converted to typescript
Arsham-dev Jul 23, 2024
be7e922
perf: code of mobile converted to typescript
Arsham-dev Jul 23, 2024
5623e92
perf: code of polyshapes converted to typescript
Arsham-dev Jul 23, 2024
a4ef244
perf: code of raycast converted to typescript
Arsham-dev Jul 23, 2024
154974e
perf: code of ropejoint converted to typescript
Arsham-dev Jul 23, 2024
260185b
fix: distance test bug fixed
Arsham-dev Jul 23, 2024
131dd9f
fix: dynamictreetest bug fixed
Arsham-dev Jul 23, 2024
330c455
fix: some error type fixeD
Arsham-dev Jul 23, 2024
3d864ab
refactor: breakout code converted to typescript
Arsham-dev Jul 23, 2024
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
157 changes: 0 additions & 157 deletions example/8-Ball.js

This file was deleted.

159 changes: 159 additions & 0 deletions example/8-Ball.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import planck from "../src/main";

const { World, Circle, Settings, Polygon , Testbed} = planck

const SPI4 = Math.sin(Math.PI / 4), SPI3 = Math.sin(Math.PI / 3);

const COLORED = true;
const BLACK = {fill: 'black', stroke: 'white'};
const WHITE = {fill: 'white', stroke: 'black'};
const 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'}
];

const width = 8.00, height = 4.00;

const BALL_R = 0.12;
const POCKET_R = 0.2;

Settings.velocityThreshold = 0;

const world = new World();

const testbed = Testbed.mount();
testbed.x = 0;
testbed.y = 0;
testbed.width = width * 1.2;
testbed.height = height * 1.2;
testbed.mouseForce = -20;
testbed.start(world);

const railH = [
new planck.Vec2(POCKET_R, height * .5),
new planck.Vec2(POCKET_R, height * .5 + POCKET_R),
new planck.Vec2(width * .5 - POCKET_R / SPI4 + POCKET_R, height * .5 + POCKET_R),
new planck.Vec2(width * .5 - POCKET_R / SPI4, height * .5)
];

const railV = [
new planck.Vec2(width * .5, -(height * .5 - POCKET_R / SPI4)),
new planck.Vec2(width * .5 + POCKET_R, -(height * .5 - POCKET_R / SPI4 + POCKET_R)),
new planck.Vec2(width * .5 + POCKET_R, height * .5 - POCKET_R / SPI4 + POCKET_R),
new planck.Vec2(width * .5, height * .5 - POCKET_R / SPI4)
];

const railFixDef = {
friction: 0.1,
restitution: 0.9,
userData: 'rail'
};
const pocketFixDef = {
userData: 'pocket'
};
const ballFixDef = {
friction: 0.1,
restitution: 0.99,
density: 1,
userData: 'ball'
};
const ballBodyDef = {
linearDamping: 1.5,
angularDamping: 1
};

function mirror(vertices:planck.Vec2[], x:number, y:number) {
return vertices.map((v:planck.Vec2) => new planck.Vec2(x * v.x, y * v.y));
}

world.createBody().createFixture(new Polygon(railV), railFixDef);
world.createBody().createFixture(new Polygon(mirror(railV, -1, +1)), railFixDef);

world.createBody().createFixture(new Polygon(railH), railFixDef);
world.createBody().createFixture(new Polygon(mirror(railH, -1, +1)), railFixDef);
world.createBody().createFixture(new Polygon(mirror(railH, +1, -1)), railFixDef);
world.createBody().createFixture(new Polygon(mirror(railH, -1, -1)), railFixDef);

world.createBody().createFixture(new Circle(new planck.Vec2(0, -height * .5 - POCKET_R * 1.5), POCKET_R), pocketFixDef);
world.createBody().createFixture(new Circle(new planck.Vec2(0, +height * .5 + POCKET_R * 1.5), POCKET_R), pocketFixDef);

world.createBody().createFixture(new Circle(new planck.Vec2(+width * .5 + POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef);
world.createBody().createFixture(new Circle(new planck.Vec2(-width * .5 - POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef);

world.createBody().createFixture(new Circle(new planck.Vec2(+width * .5 + POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef);
world.createBody().createFixture(new Circle(new planck.Vec2(-width * .5 - POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef);

const balls = rack(BALL_R, width / 4, 0);

balls.push({x: -width / 4, y: 0});

if (COLORED) {
shuffleArray(COLORS);
for (let i = 0; i < COLORS.length; i++) {
balls[i].style = COLORS[i];
}
balls[14].style = balls[4].style;
balls[4].style = BLACK;
balls[balls.length - 1].style = WHITE;
}

for (let i = 0; i < balls.length; i++) {
const ball = world.createDynamicBody(ballBodyDef);
ball.setBullet(true)
ball.setPosition(balls[i]);
ball.createFixture(new Circle(BALL_R), ballFixDef);
ball.style = balls[i].style;
}

world.on('post-solve', function(contact) {
const fA = contact.getFixtureA(), bA = fA.getBody();
const fB = contact.getFixtureB(), bB = fB.getBody();

const pocket = fA.getUserData() === pocketFixDef.userData && bA || fB.getUserData() === pocketFixDef.userData && bB;
const 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);
});

function rack(r, cx, cy) {
const n = 5;
const balls = [];
const d = r * 2, l = SPI3 * d;
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
balls.push({
x: cx + i * l /*- (n - 1) * 0.5 * l*/ + Math.random() * r * 0.02,
y: cy + (j - i * 0.5 ) * d + Math.random() * r * 0.02,
});
}
}
return balls;
}

function shuffleArray(array) {
// http://stackoverflow.com/a/12646864/483728
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
11 changes: 6 additions & 5 deletions example/AddPair.js → example/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,29 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import planck from "../src/main";

const { Vec2, World, Circle, Box, Math, Testbed } = planck;
const { Vec2, World, Circle, Box, Testbed } = planck;

let world = new World(new Vec2(0, 0));
const world = new World(new Vec2(0, 0));

const testbed = Testbed.mount();
testbed.y = 0;
testbed.hz = 60;
testbed.speed = 1;
testbed.start(world);

let circle = new Circle(0.1);
const circle = new Circle(0.1);

for (let i = 0; i < 50; ++i) {
let b = world.createBody({
const b = world.createBody({
type : 'dynamic',
position : new Vec2(Math.random() * -6, Math.random() * 2 - 1),
});
b.createFixture(circle, 0.01);
}

let box = world.createBody({
const box = world.createBody({
type : 'dynamic',
position : new Vec2(-40.0, 0.0),
bullet : true
Expand Down
Loading
Loading