Skip to content

Commit

Permalink
migrate examples to Vec2Value
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 23, 2024
1 parent 0b31620 commit 3b496b8
Show file tree
Hide file tree
Showing 58 changed files with 922 additions and 728 deletions.
97 changes: 61 additions & 36 deletions example/8-Ball.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vec2, World, Circle, Settings, Polygon, Testbed, Vec2Value, Contact, Body } from "planck";
import { World, Circle, Settings, Polygon, Testbed, Vec2Value, Contact, Body } from "planck";

const POCKET = "pocket";
const BALL = "ball";
Expand Down Expand Up @@ -115,37 +115,52 @@ class BilliardPhysics {
const SPI4 = Math.sin(Math.PI / 4);

const topLeftRail = [
new Vec2(POCKET_RADIUS, TABLE_HEIGHT * 0.5),
new Vec2(POCKET_RADIUS, TABLE_HEIGHT * 0.5 + POCKET_RADIUS),
new Vec2(
TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
),
new Vec2(TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4, TABLE_HEIGHT * 0.5),
{
x: POCKET_RADIUS,
y: TABLE_HEIGHT * 0.5,
},
{
x: POCKET_RADIUS,
y: TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
},
{
x: TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
y: TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
},
{
x: TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4,
y: TABLE_HEIGHT * 0.5,
},
];

const leftRail = [
new Vec2(TABLE_WIDTH * 0.5, -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4)),
new Vec2(
TABLE_WIDTH * 0.5 + POCKET_RADIUS,
-(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS),
),
new Vec2(
TABLE_WIDTH * 0.5 + POCKET_RADIUS,
TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
),
new Vec2(TABLE_WIDTH * 0.5, TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4),
{
x: TABLE_WIDTH * 0.5,
y: -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4),
},
{
x: TABLE_WIDTH * 0.5 + POCKET_RADIUS,
y: -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS),
},
{
x: TABLE_WIDTH * 0.5 + POCKET_RADIUS,
y: TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
},
{
x: TABLE_WIDTH * 0.5,
y: TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4,
},
];

const rails: Vec2Value[][] = [];

rails.push(leftRail);
rails.push(leftRail.map((v) => new Vec2(-v.x, +v.y)));
rails.push(leftRail.map((v) => ({ x: -v.x, y: +v.y })));

rails.push(topLeftRail);
rails.push(topLeftRail.map((v) => new Vec2(-v.x, +v.y)));
rails.push(topLeftRail.map((v) => new Vec2(+v.x, -v.y)));
rails.push(topLeftRail.map((v) => new Vec2(-v.x, -v.y)));
rails.push(topLeftRail.map((v) => ({ x: -v.x, y: +v.y })));
rails.push(topLeftRail.map((v) => ({ x: +v.x, y: -v.y })));
rails.push(topLeftRail.map((v) => ({ x: -v.x, y: -v.y })));

for (let i = 0; i < rails.length; i++) {
const body = this.world.createBody();
Expand All @@ -158,20 +173,30 @@ class BilliardPhysics {
}

const pockets: Vec2Value[] = [];
pockets.push(new Vec2(0, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 1.5));
pockets.push(new Vec2(0, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 1.5));
pockets.push(
new Vec2(+TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7),
);
pockets.push(
new Vec2(-TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7),
);
pockets.push(
new Vec2(+TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7),
);
pockets.push(
new Vec2(-TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7),
);
pockets.push({
x: 0,
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 1.5,
});
pockets.push({
x: 0,
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 1.5,
});
pockets.push({
x: +TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7,
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7,
});
pockets.push({
x: -TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7,
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7,
});
pockets.push({
x: +TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7,
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7,
});
pockets.push({
x: -TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7,
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7,
});

for (let i = 0; i < pockets.length; i++) {
const body = this.world.createBody({
Expand Down
13 changes: 8 additions & 5 deletions example/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Licensed under the MIT license
*/

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

const world = new World(new Vec2(0, 0));
const world = new World({ x: 0, y: 0 });

const testbed = Testbed.mount();
testbed.y = 0;
Expand All @@ -18,16 +18,19 @@ const circle = new Circle(0.1);
for (let i = 0; i < 50; ++i) {
const b = world.createBody({
type: "dynamic",
position: new Vec2(Math.random() * -6, Math.random() * 2 - 1),
position: {
x: Math.random() * -6,
y: Math.random() * 2 - 1,
},
});
b.createFixture(circle, 0.01);
}

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

box.createFixture(new Box(1.5, 1.5), 1.0);
box.setLinearVelocity(new Vec2(100.0, 0.0));
box.setLinearVelocity({ x: 100.0, y: 0.0 });
30 changes: 19 additions & 11 deletions example/ApplyForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,54 @@ const testbed = Testbed.mount();
testbed.y = -20;
testbed.start(world);

const ground = world.createBody(new Vec2(0.0, 20.0));
const ground = world.createBody({ x: 0.0, y: 20.0 });

const wallFD = {
density: 0.0,
restitution: 0.4,
};

// Left vertical
ground.createFixture(new Edge(new Vec2(-20.0, -20.0), new Vec2(-20.0, 20.0)), wallFD);
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: -20.0, y: 20.0 }), wallFD);

// Right vertical
ground.createFixture(new Edge(new Vec2(20.0, -20.0), new Vec2(20.0, 20.0)), wallFD);
ground.createFixture(new Edge({ x: 20.0, y: -20.0 }, { x: 20.0, y: 20.0 }), wallFD);

// Top horizontal
ground.createFixture(new Edge(new Vec2(-20.0, 20.0), new Vec2(20.0, 20.0)), wallFD);
ground.createFixture(new Edge({ x: -20.0, y: 20.0 }, { x: 20.0, y: 20.0 }), wallFD);

// Bottom horizontal
ground.createFixture(new Edge(new Vec2(-20.0, -20.0), new Vec2(20.0, -20.0)), wallFD);
ground.createFixture(new Edge({ x: -20.0, y: -20.0 }, { x: 20.0, y: -20.0 }), wallFD);

const xf1 = new Transform();
xf1.q.set(0.3524 * Math.PI);
xf1.p.set(xf1.q.getXAxis());

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)),
[
{ x: -1.0, y: 0.0 },
{ x: 1.0, y: 0.0 },
{ x: 0.0, y: 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()));

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)),
[
{ x: -1.0, y: 0.0 },
{ x: 1.0, y: 0.0 },
{ x: 0.0, y: 0.5 },
].map((v) => Transform.mul(xf2, v)),
);

const jet = world.createBody({
type: "dynamic",
angularDamping: 2.0,
linearDamping: 0.5,
position: new Vec2(0.0, 2.0),
position: { x: 0.0, y: 2.0 },
angle: Math.PI,
allowSleep: false,
});
Expand All @@ -64,7 +72,7 @@ const boxFD = {
};

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

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

Expand Down Expand Up @@ -96,8 +104,8 @@ testbed.step = function () {
}

if (testbed.activeKeys.up) {
const f = jet.getWorldVector(new Vec2(0.0, -1.0));
const p = jet.getWorldPoint(new Vec2(0.0, 2.0));
const f = jet.getWorldVector({ x: 0.0, y: -1.0 });
const p = jet.getWorldPoint({ x: 0.0, y: 2.0 });
jet.applyLinearImpulse(f, p, true);
}
};
31 changes: 17 additions & 14 deletions example/Asteroid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { World, Vec2, Circle, Polygon, Testbed, Body, Contact, Vec2Value } from "planck";
import { World, Circle, Polygon, Testbed, Body, Contact, Vec2Value } from "planck";

const SPACE_WIDTH = 16;
const SPACE_HEIGHT = 9;
Expand Down Expand Up @@ -69,18 +69,18 @@ class AsteroidPhysics {
type: "dynamic",
angularDamping: 2.0,
linearDamping: 0.5,
position: new Vec2(),
position: { x: 0, y: 0 },
userData: {
type: "ship",
},
});

this.ship.createFixture(
new Polygon([
new Vec2(-0.15, -0.15),
new Vec2(0, -0.1),
new Vec2(0.15, -0.15),
new Vec2(0, 0.2),
{ x: -0.15, y: -0.15 },
{ x: 0, y: -0.1 },
{ x: 0.15, y: -0.15 },
{ x: 0, y: 0.2 },
]),
{
density: 1000,
Expand All @@ -104,8 +104,8 @@ class AsteroidPhysics {

thrustForward() {
if (!this.ship) return false;
const f = this.ship.getWorldVector(new Vec2(0.0, 1.0));
const p = this.ship.getWorldPoint(new Vec2(0.0, 2.0));
const f = this.ship.getWorldVector({ x: 0.0, y: 1.0 });
const p = this.ship.getWorldPoint({ x: 0.0, y: 2.0 });
this.ship.applyLinearImpulse(f, p, true);
return true;
}
Expand All @@ -116,8 +116,8 @@ class AsteroidPhysics {
const body = this.world.createBody({
type: "dynamic",
// mass : 0.05,
position: this.ship.getWorldPoint(new Vec2(0, 0)),
linearVelocity: this.ship.getWorldVector(new Vec2(0, speed)),
position: this.ship.getWorldPoint({ x: 0, y: 0 }),
linearVelocity: this.ship.getWorldVector({ x: 0, y: speed }),
bullet: true,
userData: {
type: "bullet",
Expand Down Expand Up @@ -160,16 +160,16 @@ class AsteroidPhysics {
const a = (i * 2 * Math.PI) / n;
const x = radius * (Math.sin(a) + Calc.random(0.3));
const y = radius * (Math.cos(a) + Calc.random(0.3));
path.push(new Vec2(x, y));
path.push({ x: x, y: y });
}

const shape = new Polygon(path);

const asteroidBody = this.world.createBody({
// mass : 10,
type: "kinematic",
position: new Vec2(x, y),
linearVelocity: new Vec2(vx, vy),
position: { x: x, y: y },
linearVelocity: { x: vx, y: vy },
angularVelocity: va,
userData: {
type: "asteroid",
Expand Down Expand Up @@ -199,7 +199,10 @@ class AsteroidPhysics {
const angleDisturb = (Math.PI / 2) * Math.random();
for (let i = 0; i < 4; i++) {
const angle = (Math.PI / 2) * i + angleDisturb;
const d = new Vec2(radius * Math.cos(angle), radius * Math.sin(angle));
const d = {
x: radius * Math.cos(angle),
y: radius * Math.sin(angle),
};
const sp = parent.getWorldPoint(d);

const vx = Calc.random(ASTEROID_SPEED);
Expand Down
20 changes: 10 additions & 10 deletions example/BasicSliderCrank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

// A basic slider crank created for GDC tutorial: Understanding Constraints

import { Vec2, World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
import { World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";

const world = new World(new Vec2(0, -10));
const world = new World({ x: 0, y: -10 });

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

const ground = world.createBody(new Vec2(0.0, 17.0));
const ground = world.createBody({ x: 0.0, y: 17.0 });

// Define crank.
const crank = world.createDynamicBody(new Vec2(-8.0, 20.0));
const crank = world.createDynamicBody({ x: -8.0, y: 20.0 });
crank.createFixture(new Box(4.0, 1.0), 2.0);
world.createJoint(new RevoluteJoint({}, ground, crank, new Vec2(-12.0, 20.0)));
world.createJoint(new RevoluteJoint({}, ground, crank, { x: -12.0, y: 20.0 }));

// Define connecting rod
const rod = world.createDynamicBody(new Vec2(4.0, 20.0));
const rod = world.createDynamicBody({ x: 4.0, y: 20.0 });
rod.createFixture(new Box(8.0, 1.0), 2.0);
world.createJoint(new RevoluteJoint({}, crank, rod, new Vec2(-4.0, 20.0)));
world.createJoint(new RevoluteJoint({}, crank, rod, { x: -4.0, y: 20.0 }));

// Define piston
const piston = world.createDynamicBody({
fixedRotation: true,
position: new Vec2(12.0, 20.0),
position: { x: 12.0, y: 20.0 },
});
piston.createFixture(new Box(3.0, 3.0), 2.0);
world.createJoint(new RevoluteJoint({}, rod, piston, new Vec2(12.0, 20.0)));
world.createJoint(new PrismaticJoint({}, ground, piston, new Vec2(12.0, 17.0), new Vec2(1.0, 0.0)));
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));
world.createJoint(new PrismaticJoint({}, ground, piston, { x: 12.0, y: 17.0 }, { x: 1.0, y: 0.0 }));
Loading

0 comments on commit 3b496b8

Please sign in to comment.