Skip to content

Commit

Permalink
migrate examples to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 18, 2024
1 parent 9604296 commit 22541bc
Show file tree
Hide file tree
Showing 58 changed files with 2,339 additions and 1,910 deletions.
438 changes: 274 additions & 164 deletions example/8-Ball.ts

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions example/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
* Licensed under the MIT license
*/

import { Vec2, World, Circle, Box, Math, Testbed } from "planck";
import { Vec2, World, Circle, Box, Testbed } from "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
28 changes: 14 additions & 14 deletions example/ApplyForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import { Vec2, Transform, Polygon, Box, FrictionJoint, World, Edge, Testbed } from "planck";

let world = new World();
const world = new World();

const testbed = Testbed.mount();
testbed.y = -20;
testbed.start(world);

let ground = world.createBody(new Vec2(0.0, 20.0));
const ground = world.createBody(new Vec2(0.0, 20.0));

let wallFD = {
const wallFD = {
density: 0.0,
restitution: 0.4,
};
Expand All @@ -34,19 +34,19 @@ const xf1 = new Transform();
xf1.q.set(0.3524 * Math.PI);
xf1.p.set(xf1.q.getXAxis());

let poly1 = new Polygon(
const poly1 = new Polygon(
[new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 0.5)].map((v) => Transform.mul(xf1, v)),
);

const xf2 = new Transform();
xf2.q.set(-0.3524 * Math.PI);
xf2.p.set(Vec2.neg(xf2.q.getXAxis()));

let poly2 = new Polygon(
const poly2 = new Polygon(
[new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 0.5)].map((v) => Transform.mul(xf2, v)),
);

let jet = world.createBody({
const jet = world.createBody({
type: "dynamic",
angularDamping: 2.0,
linearDamping: 0.5,
Expand All @@ -58,22 +58,22 @@ let jet = world.createBody({
jet.createFixture(poly1, 2.0);
jet.createFixture(poly2, 2.0);

let boxFD = {
const boxFD = {
density: 1.0,
friction: 0.3,
};

for (let i = 0; i < 10; ++i) {
let box = world.createDynamicBody(new Vec2(0.0, 5.0 + 1.54 * i));
const box = world.createDynamicBody(new Vec2(0.0, 5.0 + 1.54 * i));

box.createFixture(new Box(0.5, 0.5), boxFD);

let gravity = 10.0;
let I = box.getInertia();
let mass = box.getMass();
const gravity = 10.0;
const I = box.getInertia();
const mass = box.getMass();

// For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
let radius = Math.sqrt((2.0 * I) / mass);
const radius = Math.sqrt((2.0 * I) / mass);

world.createJoint(
new FrictionJoint(
Expand All @@ -96,8 +96,8 @@ testbed.step = function () {
}

if (testbed.activeKeys.up) {
let f = jet.getWorldVector(new Vec2(0.0, -1.0));
let p = jet.getWorldPoint(new Vec2(0.0, 2.0));
const f = jet.getWorldVector(new Vec2(0.0, -1.0));
const p = jet.getWorldPoint(new Vec2(0.0, 2.0));
jet.applyLinearImpulse(f, p, true);
}
};
Loading

0 comments on commit 22541bc

Please sign in to comment.