Skip to content

Commit

Permalink
examples: use vite-alias planck, and lint pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 15, 2024
1 parent 71f4e5c commit 92ce0d1
Show file tree
Hide file tree
Showing 68 changed files with 1,750 additions and 1,568 deletions.
9 changes: 3 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"extends": [
"plugin:@typescript-eslint/recommended"
],
"extends": ["plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"rules": {
"no-var": "off",
"prefer-rest-params": "off",
"prefer-spread": "off",
"quotes": ["warn", "double"],
"semi": ["warn", "always"],
"one-var-declaration-per-line": "warn",

"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-types": "off",
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist/
/docs/api/
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 100
}
45 changes: 20 additions & 25 deletions HelloWorld.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*
* Planck.js
*
* Copyright (c) Erin Catto, Ali Shakiba
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* Copyright (c) Erin Catto
* Licensed under the MIT license
*/

/*
Expand All @@ -17,19 +13,19 @@
* To run this example simply run `node HelloWorld.js` from command line.
*/

import { Box, World, } from "./src";
import { Box, World } from "planck";

// Define the gravity vector.
var gravity = {x: 0.0, y: -10.0};
var gravity = { x: 0.0, y: -10.0 };

// Construct a world object, which will hold and simulate the rigid bodies.
var world = new World({
gravity: gravity
gravity: gravity,
});

// Define the ground body.
var groundBodyDef = {
position: {x: 0.0, y: -10.0}
position: { x: 0.0, y: -10.0 },
};

// Call the body factory which allocates memory for the ground body
Expand All @@ -46,20 +42,20 @@ groundBody.createFixture(groundBox, 0.0);

// Define the dynamic body. We set its position and call the body factory.
var body = world.createBody({
type: "dynamic",
position: {x:0.0, y: 4.0},
type: "dynamic",
position: { x: 0.0, y: 4.0 },
});

// Define another box shape for our dynamic body.
var dynamicBox = new Box(1.0, 1.0);

// Define the dynamic body fixture.
var fixtureDef = {
shape: dynamicBox,
// Set the box density to be non-zero, so it will be dynamic.
density: 1.0,
// Override the default friction.
friction: 0.3,
shape: dynamicBox,
// Set the box density to be non-zero, so it will be dynamic.
density: 1.0,
// Override the default friction.
friction: 0.3,
};

// Add the shape to the body.
Expand All @@ -72,18 +68,17 @@ var timeStep = 1.0 / 60.0;
var velocityIterations = 6;
var positionIterations = 2;


// This is our little game loop.
for (var i = 0; i < 60; ++i) {
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.step(timeStep, velocityIterations, positionIterations);
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.step(timeStep, velocityIterations, positionIterations);

// Now print the position and angle of the body.
var position = body.getPosition();
var angle = body.getAngle();
// Now print the position and angle of the body.
var position = body.getPosition();
var angle = body.getAngle();

console.log(position.x.toFixed(2), position.y.toFixed(2), angle.toFixed(2));
console.log(position.x.toFixed(2), position.y.toFixed(2), angle.toFixed(2));
}

console.log(Math.abs(position.x) < 0.01);
Expand Down
131 changes: 82 additions & 49 deletions example/8-Ball.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
const { Vec2, World, Circle, Settings, Polygon, Testbed } = planck;
import { Vec2, World, Circle, Settings, Polygon, Testbed } from "planck";

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

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

let width = 8.00, height = 4.00;
let width = 8.0;
let height = 4.0;

let BALL_R = 0.12;
let POCKET_R = 0.2;
Expand All @@ -40,40 +42,40 @@ testbed.mouseForce = -20;
testbed.start(world);

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

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

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

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

world.createBody().createFixture(new Polygon(railV), railFixDef);
Expand All @@ -84,18 +86,42 @@ 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 Vec2(0, -height * .5 - POCKET_R * 1.5), POCKET_R), pocketFixDef);
world.createBody().createFixture(new Circle(new Vec2(0, +height * .5 + POCKET_R * 1.5), POCKET_R), pocketFixDef);

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

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

world
.createBody()
.createFixture(
new Circle(new Vec2(+width * 0.5 + POCKET_R * 0.7, +height * 0.5 + POCKET_R * 0.7), POCKET_R),
pocketFixDef,
);
world
.createBody()
.createFixture(
new Circle(new Vec2(-width * 0.5 - POCKET_R * 0.7, +height * 0.5 + POCKET_R * 0.7), POCKET_R),
pocketFixDef,
);

world
.createBody()
.createFixture(
new Circle(new Vec2(+width * 0.5 + POCKET_R * 0.7, -height * 0.5 - POCKET_R * 0.7), POCKET_R),
pocketFixDef,
);
world
.createBody()
.createFixture(
new Circle(new Vec2(-width * 0.5 - POCKET_R * 0.7, -height * 0.5 - POCKET_R * 0.7), POCKET_R),
pocketFixDef,
);

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

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

if (COLORED) {
shuffleArray(COLORS);
Expand All @@ -115,15 +141,21 @@ for (let i = 0; i < balls.length; i++) {
ball.style = balls[i].style;
}

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

let pocket = fA.getUserData() === pocketFixDef.userData && bA || fB.getUserData() === pocketFixDef.userData && bB;
let ball = fA.getUserData() === ballFixDef.userData && bA || fB.getUserData() === ballFixDef.userData && bB;
let pocket =
(fA.getUserData() === pocketFixDef.userData && bA) ||
(fB.getUserData() === pocketFixDef.userData && bB);
let ball =
(fA.getUserData() === ballFixDef.userData && bA) ||
(fB.getUserData() === ballFixDef.userData && bB);

// do not change world immediately
setTimeout(function() {
setTimeout(function () {
if (ball && pocket) {
world.destroyBody(ball);
}
Expand All @@ -133,12 +165,13 @@ world.on('post-solve', function(contact) {
function rack(r, cx, cy) {
let n = 5;
let balls = [];
let d = r * 2, l = SPI3 * d;
let d = r * 2;
let 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,
y: cy + (j - i * 0.5) * d + Math.random() * r * 0.02,
});
}
}
Expand Down
15 changes: 7 additions & 8 deletions example/AddPair.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
* Copyright (c) Erin Catto
*
* This source code is licensed under the MIT license.
* Licensed under the MIT license
*/

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

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

Expand All @@ -18,16 +17,16 @@ let circle = new Circle(0.1);

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

let box = world.createBody({
type : 'dynamic',
position : new Vec2(-40.0, 0.0),
bullet : true
type: "dynamic",
position: new Vec2(-40.0, 0.0),
bullet: true,
});

box.createFixture(new Box(1.5, 1.5), 1.0);
Expand Down
Loading

0 comments on commit 92ce0d1

Please sign in to comment.