Skip to content

Commit 3b496b8

Browse files
committed
migrate examples to Vec2Value
1 parent 0b31620 commit 3b496b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+922
-728
lines changed

example/8-Ball.ts

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Vec2, World, Circle, Settings, Polygon, Testbed, Vec2Value, Contact, Body } from "planck";
1+
import { World, Circle, Settings, Polygon, Testbed, Vec2Value, Contact, Body } from "planck";
22

33
const POCKET = "pocket";
44
const BALL = "ball";
@@ -115,37 +115,52 @@ class BilliardPhysics {
115115
const SPI4 = Math.sin(Math.PI / 4);
116116

117117
const topLeftRail = [
118-
new Vec2(POCKET_RADIUS, TABLE_HEIGHT * 0.5),
119-
new Vec2(POCKET_RADIUS, TABLE_HEIGHT * 0.5 + POCKET_RADIUS),
120-
new Vec2(
121-
TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
122-
TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
123-
),
124-
new Vec2(TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4, TABLE_HEIGHT * 0.5),
118+
{
119+
x: POCKET_RADIUS,
120+
y: TABLE_HEIGHT * 0.5,
121+
},
122+
{
123+
x: POCKET_RADIUS,
124+
y: TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
125+
},
126+
{
127+
x: TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
128+
y: TABLE_HEIGHT * 0.5 + POCKET_RADIUS,
129+
},
130+
{
131+
x: TABLE_WIDTH * 0.5 - POCKET_RADIUS / SPI4,
132+
y: TABLE_HEIGHT * 0.5,
133+
},
125134
];
126135

127136
const leftRail = [
128-
new Vec2(TABLE_WIDTH * 0.5, -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4)),
129-
new Vec2(
130-
TABLE_WIDTH * 0.5 + POCKET_RADIUS,
131-
-(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS),
132-
),
133-
new Vec2(
134-
TABLE_WIDTH * 0.5 + POCKET_RADIUS,
135-
TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
136-
),
137-
new Vec2(TABLE_WIDTH * 0.5, TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4),
137+
{
138+
x: TABLE_WIDTH * 0.5,
139+
y: -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4),
140+
},
141+
{
142+
x: TABLE_WIDTH * 0.5 + POCKET_RADIUS,
143+
y: -(TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS),
144+
},
145+
{
146+
x: TABLE_WIDTH * 0.5 + POCKET_RADIUS,
147+
y: TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4 + POCKET_RADIUS,
148+
},
149+
{
150+
x: TABLE_WIDTH * 0.5,
151+
y: TABLE_HEIGHT * 0.5 - POCKET_RADIUS / SPI4,
152+
},
138153
];
139154

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

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

145160
rails.push(topLeftRail);
146-
rails.push(topLeftRail.map((v) => new Vec2(-v.x, +v.y)));
147-
rails.push(topLeftRail.map((v) => new Vec2(+v.x, -v.y)));
148-
rails.push(topLeftRail.map((v) => new Vec2(-v.x, -v.y)));
161+
rails.push(topLeftRail.map((v) => ({ x: -v.x, y: +v.y })));
162+
rails.push(topLeftRail.map((v) => ({ x: +v.x, y: -v.y })));
163+
rails.push(topLeftRail.map((v) => ({ x: -v.x, y: -v.y })));
149164

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

160175
const pockets: Vec2Value[] = [];
161-
pockets.push(new Vec2(0, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 1.5));
162-
pockets.push(new Vec2(0, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 1.5));
163-
pockets.push(
164-
new Vec2(+TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7),
165-
);
166-
pockets.push(
167-
new Vec2(-TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7, +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7),
168-
);
169-
pockets.push(
170-
new Vec2(+TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7),
171-
);
172-
pockets.push(
173-
new Vec2(-TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7, -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7),
174-
);
176+
pockets.push({
177+
x: 0,
178+
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 1.5,
179+
});
180+
pockets.push({
181+
x: 0,
182+
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 1.5,
183+
});
184+
pockets.push({
185+
x: +TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7,
186+
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7,
187+
});
188+
pockets.push({
189+
x: -TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7,
190+
y: +TABLE_HEIGHT * 0.5 + POCKET_RADIUS * 0.7,
191+
});
192+
pockets.push({
193+
x: +TABLE_WIDTH * 0.5 + POCKET_RADIUS * 0.7,
194+
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7,
195+
});
196+
pockets.push({
197+
x: -TABLE_WIDTH * 0.5 - POCKET_RADIUS * 0.7,
198+
y: -TABLE_HEIGHT * 0.5 - POCKET_RADIUS * 0.7,
199+
});
175200

176201
for (let i = 0; i < pockets.length; i++) {
177202
const body = this.world.createBody({

example/AddPair.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Licensed under the MIT license
44
*/
55

6-
import { Vec2, World, Circle, Box, Testbed } from "planck";
6+
import { World, Circle, Box, Testbed } from "planck";
77

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

1010
const testbed = Testbed.mount();
1111
testbed.y = 0;
@@ -18,16 +18,19 @@ const circle = new Circle(0.1);
1818
for (let i = 0; i < 50; ++i) {
1919
const b = world.createBody({
2020
type: "dynamic",
21-
position: new Vec2(Math.random() * -6, Math.random() * 2 - 1),
21+
position: {
22+
x: Math.random() * -6,
23+
y: Math.random() * 2 - 1,
24+
},
2225
});
2326
b.createFixture(circle, 0.01);
2427
}
2528

2629
const box = world.createBody({
2730
type: "dynamic",
28-
position: new Vec2(-40.0, 0.0),
31+
position: { x: -40.0, y: 0.0 },
2932
bullet: true,
3033
});
3134

3235
box.createFixture(new Box(1.5, 1.5), 1.0);
33-
box.setLinearVelocity(new Vec2(100.0, 0.0));
36+
box.setLinearVelocity({ x: 100.0, y: 0.0 });

example/ApplyForce.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,54 @@ const testbed = Testbed.mount();
1111
testbed.y = -20;
1212
testbed.start(world);
1313

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

1616
const wallFD = {
1717
density: 0.0,
1818
restitution: 0.4,
1919
};
2020

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

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

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

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

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

3737
const poly1 = new Polygon(
38-
[new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 0.5)].map((v) => Transform.mul(xf1, v)),
38+
[
39+
{ x: -1.0, y: 0.0 },
40+
{ x: 1.0, y: 0.0 },
41+
{ x: 0.0, y: 0.5 },
42+
].map((v) => Transform.mul(xf1, v)),
3943
);
4044

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

4549
const poly2 = new Polygon(
46-
[new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 0.5)].map((v) => Transform.mul(xf2, v)),
50+
[
51+
{ x: -1.0, y: 0.0 },
52+
{ x: 1.0, y: 0.0 },
53+
{ x: 0.0, y: 0.5 },
54+
].map((v) => Transform.mul(xf2, v)),
4755
);
4856

4957
const jet = world.createBody({
5058
type: "dynamic",
5159
angularDamping: 2.0,
5260
linearDamping: 0.5,
53-
position: new Vec2(0.0, 2.0),
61+
position: { x: 0.0, y: 2.0 },
5462
angle: Math.PI,
5563
allowSleep: false,
5664
});
@@ -64,7 +72,7 @@ const boxFD = {
6472
};
6573

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

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

@@ -96,8 +104,8 @@ testbed.step = function () {
96104
}
97105

98106
if (testbed.activeKeys.up) {
99-
const f = jet.getWorldVector(new Vec2(0.0, -1.0));
100-
const p = jet.getWorldPoint(new Vec2(0.0, 2.0));
107+
const f = jet.getWorldVector({ x: 0.0, y: -1.0 });
108+
const p = jet.getWorldPoint({ x: 0.0, y: 2.0 });
101109
jet.applyLinearImpulse(f, p, true);
102110
}
103111
};

example/Asteroid.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { World, Vec2, Circle, Polygon, Testbed, Body, Contact, Vec2Value } from "planck";
1+
import { World, Circle, Polygon, Testbed, Body, Contact, Vec2Value } from "planck";
22

33
const SPACE_WIDTH = 16;
44
const SPACE_HEIGHT = 9;
@@ -69,18 +69,18 @@ class AsteroidPhysics {
6969
type: "dynamic",
7070
angularDamping: 2.0,
7171
linearDamping: 0.5,
72-
position: new Vec2(),
72+
position: { x: 0, y: 0 },
7373
userData: {
7474
type: "ship",
7575
},
7676
});
7777

7878
this.ship.createFixture(
7979
new Polygon([
80-
new Vec2(-0.15, -0.15),
81-
new Vec2(0, -0.1),
82-
new Vec2(0.15, -0.15),
83-
new Vec2(0, 0.2),
80+
{ x: -0.15, y: -0.15 },
81+
{ x: 0, y: -0.1 },
82+
{ x: 0.15, y: -0.15 },
83+
{ x: 0, y: 0.2 },
8484
]),
8585
{
8686
density: 1000,
@@ -104,8 +104,8 @@ class AsteroidPhysics {
104104

105105
thrustForward() {
106106
if (!this.ship) return false;
107-
const f = this.ship.getWorldVector(new Vec2(0.0, 1.0));
108-
const p = this.ship.getWorldPoint(new Vec2(0.0, 2.0));
107+
const f = this.ship.getWorldVector({ x: 0.0, y: 1.0 });
108+
const p = this.ship.getWorldPoint({ x: 0.0, y: 2.0 });
109109
this.ship.applyLinearImpulse(f, p, true);
110110
return true;
111111
}
@@ -116,8 +116,8 @@ class AsteroidPhysics {
116116
const body = this.world.createBody({
117117
type: "dynamic",
118118
// mass : 0.05,
119-
position: this.ship.getWorldPoint(new Vec2(0, 0)),
120-
linearVelocity: this.ship.getWorldVector(new Vec2(0, speed)),
119+
position: this.ship.getWorldPoint({ x: 0, y: 0 }),
120+
linearVelocity: this.ship.getWorldVector({ x: 0, y: speed }),
121121
bullet: true,
122122
userData: {
123123
type: "bullet",
@@ -160,16 +160,16 @@ class AsteroidPhysics {
160160
const a = (i * 2 * Math.PI) / n;
161161
const x = radius * (Math.sin(a) + Calc.random(0.3));
162162
const y = radius * (Math.cos(a) + Calc.random(0.3));
163-
path.push(new Vec2(x, y));
163+
path.push({ x: x, y: y });
164164
}
165165

166166
const shape = new Polygon(path);
167167

168168
const asteroidBody = this.world.createBody({
169169
// mass : 10,
170170
type: "kinematic",
171-
position: new Vec2(x, y),
172-
linearVelocity: new Vec2(vx, vy),
171+
position: { x: x, y: y },
172+
linearVelocity: { x: vx, y: vy },
173173
angularVelocity: va,
174174
userData: {
175175
type: "asteroid",
@@ -199,7 +199,10 @@ class AsteroidPhysics {
199199
const angleDisturb = (Math.PI / 2) * Math.random();
200200
for (let i = 0; i < 4; i++) {
201201
const angle = (Math.PI / 2) * i + angleDisturb;
202-
const d = new Vec2(radius * Math.cos(angle), radius * Math.sin(angle));
202+
const d = {
203+
x: radius * Math.cos(angle),
204+
y: radius * Math.sin(angle),
205+
};
203206
const sp = parent.getWorldPoint(d);
204207

205208
const vx = Calc.random(ASTEROID_SPEED);

example/BasicSliderCrank.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55

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

8-
import { Vec2, World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
8+
import { World, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck";
99

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

1212
const testbed = Testbed.mount();
1313
testbed.y = -15;
1414
testbed.start(world);
1515

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

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

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

2828
// Define piston
2929
const piston = world.createDynamicBody({
3030
fixedRotation: true,
31-
position: new Vec2(12.0, 20.0),
31+
position: { x: 12.0, y: 20.0 },
3232
});
3333
piston.createFixture(new Box(3.0, 3.0), 2.0);
34-
world.createJoint(new RevoluteJoint({}, rod, piston, new Vec2(12.0, 20.0)));
35-
world.createJoint(new PrismaticJoint({}, ground, piston, new Vec2(12.0, 17.0), new Vec2(1.0, 0.0)));
34+
world.createJoint(new RevoluteJoint({}, rod, piston, { x: 12.0, y: 20.0 }));
35+
world.createJoint(new PrismaticJoint({}, ground, piston, { x: 12.0, y: 17.0 }, { x: 1.0, y: 0.0 }));

0 commit comments

Comments
 (0)