From 3b496b846c19047a9fed1b1fbf7ea6dc45467716 Mon Sep 17 00:00:00 2001 From: Ali Shakiba Date: Mon, 23 Dec 2024 18:24:21 +0330 Subject: [PATCH] migrate examples to Vec2Value --- example/8-Ball.ts | 97 +++++++++++++++++++++------------- example/AddPair.ts | 13 +++-- example/ApplyForce.ts | 30 +++++++---- example/Asteroid.ts | 31 ++++++----- example/BasicSliderCrank.ts | 20 +++---- example/BodyTypes.ts | 22 ++++---- example/Boxes.ts | 10 ++-- example/Breakable.ts | 10 ++-- example/Breakout.ts | 86 +++++++++++++++--------------- example/Bridge.ts | 22 ++++---- example/BulletTest.ts | 22 ++++---- example/Cantilever.ts | 35 ++++++------ example/Car.ts | 84 ++++++++++++++--------------- example/Chain.ts | 12 +++-- example/CharacterCollision.ts | 70 +++++++++++++----------- example/CollisionFiltering.ts | 38 +++++++------ example/CollisionProcessing.ts | 60 +++++++++++++-------- example/CompoundShapes.ts | 45 ++++++++++------ example/Confined.ts | 26 +++++---- example/ContinuousTest.ts | 24 ++++----- example/ConvexHull.ts | 8 +-- example/ConveyorBelt.ts | 10 ++-- example/DistanceTest.ts | 6 +-- example/Dominos.ts | 44 +++++++-------- example/DynamicTreeTest.ts | 25 ++++++--- example/EdgeShapes.ts | 44 +++++++++------ example/EdgeTest.ts | 22 ++++---- example/Gears.ts | 19 ++++--- example/HeavyOnLight.ts | 10 ++-- example/HeavyOnLightTwo.ts | 12 ++--- example/Mixer.ts | 33 ++++++++---- example/Mobile.ts | 16 +++--- example/MobileBalanced.ts | 18 +++---- example/MotorJoint.ts | 13 +++-- example/OneSidedPlatform.ts | 12 ++--- example/Pinball.ts | 24 ++++----- example/PolyCollision.ts | 8 +-- example/PolyShapes.ts | 37 +++++++------ example/Prismatic.ts | 13 +++-- example/Pulleys.ts | 22 ++++---- example/Pyramid.ts | 23 ++++---- example/RayCast.ts | 44 ++++++++------- example/Revolute.ts | 28 +++++----- example/RopeJoint.ts | 14 ++--- example/SensorTest.ts | 10 ++-- example/ShapeCast.ts | 62 +++++++++++++--------- example/ShapeEditing.ts | 12 ++--- example/Shuffle.ts | 26 +++++---- example/SliderCrank.ts | 24 ++++----- example/Soccer.ts | 72 +++++++++++++++---------- example/SphereStack.ts | 10 ++-- example/TheoJansen.ts | 35 ++++++------ example/Tiles.ts | 27 +++++----- example/Tumbler.ts | 21 ++++---- example/VaryingFriction.ts | 18 +++---- example/VaryingRestitution.ts | 8 +-- example/VerticalStack.ts | 21 ++++---- example/Web.ts | 42 +++++++-------- 58 files changed, 922 insertions(+), 728 deletions(-) diff --git a/example/8-Ball.ts b/example/8-Ball.ts index e132b9b6..604d5807 100644 --- a/example/8-Ball.ts +++ b/example/8-Ball.ts @@ -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"; @@ -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(); @@ -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({ diff --git a/example/AddPair.ts b/example/AddPair.ts index 701499b9..e4bdebd5 100644 --- a/example/AddPair.ts +++ b/example/AddPair.ts @@ -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; @@ -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 }); diff --git a/example/ApplyForce.ts b/example/ApplyForce.ts index 57f520f6..c8b1467d 100644 --- a/example/ApplyForce.ts +++ b/example/ApplyForce.ts @@ -11,7 +11,7 @@ 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, @@ -19,23 +19,27 @@ const wallFD = { }; // 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(); @@ -43,14 +47,18 @@ 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, }); @@ -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); @@ -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); } }; diff --git a/example/Asteroid.ts b/example/Asteroid.ts index 8d4bd4ae..688311cd 100644 --- a/example/Asteroid.ts +++ b/example/Asteroid.ts @@ -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; @@ -69,7 +69,7 @@ class AsteroidPhysics { type: "dynamic", angularDamping: 2.0, linearDamping: 0.5, - position: new Vec2(), + position: { x: 0, y: 0 }, userData: { type: "ship", }, @@ -77,10 +77,10 @@ class AsteroidPhysics { 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, @@ -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; } @@ -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", @@ -160,7 +160,7 @@ 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); @@ -168,8 +168,8 @@ class AsteroidPhysics { 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", @@ -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); diff --git a/example/BasicSliderCrank.ts b/example/BasicSliderCrank.ts index adff837b..9347bdc5 100644 --- a/example/BasicSliderCrank.ts +++ b/example/BasicSliderCrank.ts @@ -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 })); diff --git a/example/BodyTypes.ts b/example/BodyTypes.ts index 784cc1c0..a8fe8fe4 100644 --- a/example/BodyTypes.ts +++ b/example/BodyTypes.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Box, RevoluteJoint, PrismaticJoint, Testbed } from "planck"; +import { World, Edge, 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.info("Z: Dynamic, X: Static, C: Kinematic"); @@ -14,16 +14,16 @@ testbed.start(world); const SPEED = 3.0; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0))); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 })); // Define attachment -const attachment = world.createDynamicBody(new Vec2(0.0, 3.0)); +const attachment = world.createDynamicBody({ x: 0.0, y: 3.0 }); attachment.createFixture(new Box(0.5, 2.0), 2.0); // Define platform -const platform = world.createDynamicBody(new Vec2(-4.0, 5.0)); +const platform = world.createDynamicBody({ x: -4.0, y: 5.0 }); -platform.createFixture(new Box(0.5, 4.0, new Vec2(4.0, 0.0), 0.5 * Math.PI), { +platform.createFixture(new Box(0.5, 4.0, { x: 4.0, y: 0.0 }, 0.5 * Math.PI), { friction: 0.6, density: 2.0, }); @@ -36,7 +36,7 @@ world.createJoint( }, attachment, platform, - new Vec2(0.0, 5.0), + { x: 0.0, y: 5.0 }, ), ); @@ -51,13 +51,13 @@ world.createJoint( }, ground, platform, - new Vec2(0.0, 5.0), - new Vec2(1.0, 0.0), + { x: 0.0, y: 5.0 }, + { x: 1.0, y: 0.0 }, ), ); // Create a payload -const payload = world.createDynamicBody(new Vec2(0.0, 8.0)); +const payload = world.createDynamicBody({ x: 0.0, y: 8.0 }); payload.createFixture(new Box(0.75, 0.75), { friction: 0.6, density: 2.0 }); testbed.keydown = function (code, char) { @@ -67,7 +67,7 @@ testbed.keydown = function (code, char) { platform.setStatic(); } else if (char === "C") { platform.setKinematic(); - platform.setLinearVelocity(new Vec2(-SPEED, 0.0)); + platform.setLinearVelocity({ x: -SPEED, y: 0.0 }); platform.setAngularVelocity(0.0); } }; diff --git a/example/Boxes.ts b/example/Boxes.ts index bbdc5d84..9acb097b 100644 --- a/example/Boxes.ts +++ b/example/Boxes.ts @@ -1,22 +1,22 @@ -import { Vec2, World, Edge, Box, Testbed } from "planck"; +import { World, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const bar = world.createBody(); -bar.createFixture(new Edge(new Vec2(-20, 5), new Vec2(20, 5))); +bar.createFixture(new Edge({ x: -20, y: 5 }, { x: 20, y: 5 })); bar.setAngle(0.2); for (let i = -2; i <= 2; i++) { for (let j = -2; j <= 2; j++) { const box = world.createBody().setDynamic(); box.createFixture(new Box(0.5, 0.5)); - box.setPosition(new Vec2(i * 1, -j * 1 + 20)); + box.setPosition({ x: i * 1, y: -j * 1 + 20 }); box.setMassData({ mass: 1, - center: new Vec2(), + center: { x: 0, y: 0 }, I: 1, }); } diff --git a/example/Breakable.ts b/example/Breakable.ts index 76919d50..2cfceb41 100644 --- a/example/Breakable.ts +++ b/example/Breakable.ts @@ -7,7 +7,7 @@ import { World, Vec2, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -19,15 +19,15 @@ let broke = false; // Ground body const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); // Breakable dynamic body -const body1 = world.createDynamicBody(new Vec2(0.0, 40.0), 0.25 * Math.PI); +const body1 = world.createDynamicBody({ x: 0.0, y: 40.0 }, 0.25 * Math.PI); -const shape1 = new Box(0.5, 0.5, new Vec2(-0.5, 0.0), 0.0); +const shape1 = new Box(0.5, 0.5, { x: -0.5, y: 0.0 }, 0.0); const piece1 = body1.createFixture(shape1, 1.0); -const shape2 = new Box(0.5, 0.5, new Vec2(0.5, 0.0), 0.0); +const shape2 = new Box(0.5, 0.5, { x: 0.5, y: 0.0 }, 0.0); let piece2 = body1.createFixture(shape2, 1.0); world.on("post-solve", function (contact, impulse) { diff --git a/example/Breakout.ts b/example/Breakout.ts index 9a716e5f..f5b13504 100644 --- a/example/Breakout.ts +++ b/example/Breakout.ts @@ -106,28 +106,28 @@ const ballShape = new CircleShape(0.5); const normalBrickShape = new BoxShape(1.9 / 2, 1.9 / 2); const smallBrickShape = new BoxShape(0.9 / 2, 0.9 / 2); const miniPaddleShape = new PolygonShape([ - new Vec2(1.7, -0.2), - new Vec2(1.8, -0.1), - new Vec2(1.8, 0.1), - new Vec2(1.7, 0.2), - new Vec2(1.2, 0.4), - new Vec2(0.4, 0.6), - new Vec2(-0.4, 0.6), - new Vec2(-1.2, 0.4), - new Vec2(-1.7, 0.2), - new Vec2(-1.8, 0.1), - new Vec2(-1.8, -0.1), - new Vec2(-1.7, -0.2), + { x: 1.7, y: -0.2 }, + { x: 1.8, y: -0.1 }, + { x: 1.8, y: 0.1 }, + { x: 1.7, y: 0.2 }, + { x: 1.2, y: 0.4 }, + { x: 0.4, y: 0.6 }, + { x: -0.4, y: 0.6 }, + { x: -1.2, y: 0.4 }, + { x: -1.7, y: 0.2 }, + { x: -1.8, y: 0.1 }, + { x: -1.8, y: -0.1 }, + { x: -1.7, y: -0.2 }, ]); const fullPaddleShape = new PolygonShape([ - new Vec2(1.2, -0.1), - new Vec2(1.2, 0.1), - new Vec2(0.9, 0.4), - new Vec2(0.2, 0.6), - new Vec2(-0.2, 0.6), - new Vec2(-0.9, 0.4), - new Vec2(-1.2, 0.1), - new Vec2(-1.2, -0.1), + { x: 1.2, y: -0.1 }, + { x: 1.2, y: 0.1 }, + { x: 0.9, y: 0.4 }, + { x: 0.2, y: 0.6 }, + { x: -0.2, y: 0.6 }, + { x: -0.9, y: 0.4 }, + { x: -1.2, y: 0.1 }, + { x: -1.2, y: -0.1 }, ]); class BreakoutPhysics { client?: BreakoutPhysicsClientInterface; @@ -172,7 +172,7 @@ class BreakoutPhysics { const ball = this.balls[0]; const a = Math.PI * Math.random() * 0.4 - 0.2; const speed = 10; - ball.setLinearVelocity(new Vec2(speed * Math.sin(a), speed * Math.cos(a))); + ball.setLinearVelocity({ x: speed * Math.sin(a), y: speed * Math.cos(a) }); } collidePhysics = (contact: Contact) => { @@ -215,28 +215,28 @@ class BreakoutPhysics { createBoardPhysics() { { - const wall = this.world.createBody(new Vec2(+9, -0.5)); - wall.createFixture(new EdgeShape(new Vec2(0, -12.5), new Vec2(0, +11.5)), wallFix); + const wall = this.world.createBody({ x: +9, y: -0.5 }); + wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix); } { - const wall = this.world.createBody(new Vec2(-9, -0.5)); - wall.createFixture(new EdgeShape(new Vec2(0, -12.5), new Vec2(0, +11.5)), wallFix); + const wall = this.world.createBody({ x: -9, y: -0.5 }); + wall.createFixture(new EdgeShape({ x: 0, y: -12.5 }, { x: 0, y: +11.5 }), wallFix); } { - const wall = this.world.createBody(new Vec2(0, +12)); - wall.createFixture(new EdgeShape(new Vec2(-8, 0), new Vec2(+8, 0)), wallFix); + const wall = this.world.createBody({ x: 0, y: +12 }); + wall.createFixture(new EdgeShape({ x: -8, y: 0 }, { x: +8, y: 0 }), wallFix); } { - const wall = this.world.createBody(new Vec2(9, 12)); - wall.createFixture(new EdgeShape(new Vec2(-1, 0), new Vec2(0, -1)), wallFix); + const wall = this.world.createBody({ x: 9, y: 12 }); + wall.createFixture(new EdgeShape({ x: -1, y: 0 }, { x: 0, y: -1 }), wallFix); } { - const wall = this.world.createBody(new Vec2(-9, 12)); - wall.createFixture(new EdgeShape(new Vec2(1, 0), new Vec2(0, -1)), wallFix); + const wall = this.world.createBody({ x: -9, y: 12 }); + wall.createFixture(new EdgeShape({ x: 1, y: 0 }, { x: 0, y: -1 }), wallFix); } { - const wall = this.world.createBody(new Vec2(0, -13)); - wall.createFixture(new EdgeShape(new Vec2(-9, 0), new Vec2(+9, 0)), wallFix); + const wall = this.world.createBody({ x: 0, y: -13 }); + wall.createFixture(new EdgeShape({ x: -9, y: 0 }, { x: +9, y: 0 }), wallFix); wall.setUserData(new WallData()); this.bottomWall = wall; @@ -254,7 +254,7 @@ class BreakoutPhysics { const body = this.world.createBody({ type: "kinematic", - position: new Vec2(0, -10.5), + position: { x: 0, y: -10.5 }, }); body.createFixture(shape, paddleFix); @@ -284,7 +284,7 @@ class BreakoutPhysics { body.setPosition(oldBall.getPosition()); body.setLinearVelocity(Vec2.neg(oldBall.getLinearVelocity())); } else { - body.setPosition(new Vec2(0, -5)); + body.setPosition({ x: 0, y: -5 }); } body.setUserData(data); @@ -300,7 +300,7 @@ class BreakoutPhysics { addBrickPhysics(data: BrickData) { const shape = data.subtype == "small" ? smallBrickShape : normalBrickShape; - const pos = new Vec2((data.i - 3) * 2, 9 - data.j * 2); + const pos = { x: (data.i - 3) * 2, y: 9 - data.j * 2 }; const body = this.world.createBody(pos); body.createFixture(shape, brickFix); @@ -311,7 +311,7 @@ class BreakoutPhysics { updateBrickPhysics(data: BrickData) { const body = data.body; - body.setPosition(new Vec2((data.i - 3) * 2, 9 - data.j * 2)); + body.setPosition({ x: (data.i - 3) * 2, y: 9 - data.j * 2 }); } removeBrickPhysics(data: BrickData) { @@ -330,8 +330,8 @@ class BreakoutPhysics { } else { body.createFixture(new CircleShape(0.3), dropFix); } - body.setPosition(new Vec2((drop.i - 3) * 2, 9 - drop.j * 2)); - body.setLinearVelocity(new Vec2(0, drop.speed)); + body.setPosition({ x: (drop.i - 3) * 2, y: 9 - drop.j * 2 }); + body.setLinearVelocity({ x: 0, y: drop.speed }); body.setUserData(drop); drop.body = body; @@ -345,13 +345,13 @@ class BreakoutPhysics { } movePaddlePhysics(dir: number) { - let p = this.paddle.getPosition(); - p = new Vec2(dir, 0).add(p); + const from = this.paddle.getPosition(); + const to = { x: dir + from.x, y: 0 + from.y }; const data = this.paddle.getUserData() as PaddleData; const paddleWidth = data.subtype == "mini" ? 2.4 : 3.6; const maxX = 9 - paddleWidth / 2; - p.x = Math.min(maxX, Math.max(-maxX, p.x)); - this.paddle.setPosition(p); + to.x = Math.min(maxX, Math.max(-maxX, to.x)); + this.paddle.setPosition(to); } } diff --git a/example/Bridge.ts b/example/Bridge.ts index 15b7d747..c1b08ee3 100644 --- a/example/Bridge.ts +++ b/example/Bridge.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Body, Edge, Box, Polygon, Circle, RevoluteJoint, Testbed } from "planck"; +import { World, Body, Edge, Box, Polygon, Circle, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -4)); +const world = new World({ x: 0, y: -4 }); const testbed = Testbed.mount(); testbed.start(world); @@ -15,7 +15,7 @@ const COUNT = 30; let middle: Body; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const bridgeRect = new Box(0.5, 0.125); @@ -26,10 +26,10 @@ const bridgeFD = { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody(new Vec2(-14.5 + 1.0 * i, 5.0)); + const body = world.createDynamicBody({ x: -14.5 + 1.0 * i, y: 5.0 }); body.createFixture(bridgeRect, bridgeFD); - const anchor = new Vec2(-15.0 + 1.0 * i, 5.0); + const anchor = { x: -15.0 + 1.0 * i, y: 5.0 }; world.createJoint(new RevoluteJoint({}, prevBody, body, anchor)); if (i * 2 === COUNT) { @@ -38,18 +38,22 @@ for (let i = 0; i < COUNT; ++i) { prevBody = body; } -const anchor = new Vec2(-15.0 + 1.0 * COUNT, 5.0); +const anchor = { x: -15.0 + 1.0 * COUNT, y: 5.0 }; world.createJoint(new RevoluteJoint({}, prevBody, ground, anchor)); for (let i = 0; i < 2; ++i) { - const body = world.createDynamicBody(new Vec2(-8.0 + 8.0 * i, 12.0)); + const body = world.createDynamicBody({ x: -8.0 + 8.0 * i, y: 12.0 }); - const vertices = [new Vec2(-0.5, 0.0), new Vec2(0.5, 0.0), new Vec2(0.0, 1.5)]; + const vertices = [ + { x: -0.5, y: 0.0 }, + { x: 0.5, y: 0.0 }, + { x: 0.0, y: 1.5 }, + ]; body.createFixture(new Polygon(vertices), 1.0); } const shape = new Circle(0.5); for (let i = 0; i < 3; ++i) { - const body = world.createDynamicBody(new Vec2(-6.0 + 6.0 * i, 10.0)); + const body = world.createDynamicBody({ x: -6.0 + 6.0 * i, y: 10.0 }); body.createFixture(shape, 1.0); } diff --git a/example/BulletTest.ts b/example/BulletTest.ts index 8df2a758..b15c5c2e 100644 --- a/example/BulletTest.ts +++ b/example/BulletTest.ts @@ -3,18 +3,18 @@ * Licensed under the MIT license */ -import { World, Vec2, Edge, Box, stats, Testbed } from "planck"; +import { World, Edge, Box, stats, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-10.0, 0.0), new Vec2(10.0, 0.0)), 0.0); -ground.createFixture(new Box(0.2, 1.0, new Vec2(0.5, 1.0), 0.0), 0.0); +ground.createFixture(new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }), 0.0); +ground.createFixture(new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0), 0.0); -const body = world.createDynamicBody(new Vec2(0.0, 4.0)); +const body = world.createDynamicBody({ x: 0.0, y: 4.0 }); body.createFixture(new Box(2.0, 0.1), 1.0); // x = Math.random(-1.0, 1.0); @@ -22,21 +22,21 @@ let x = 0.20352793; const bullet = world.createBody({ type: "dynamic", - position: new Vec2(x, 10.0), + position: { x: x, y: 10.0 }, bullet: true, }); bullet.createFixture(new Box(0.25, 0.25), 100.0); -bullet.setLinearVelocity(new Vec2(0.0, -50.0)); +bullet.setLinearVelocity({ x: 0.0, y: -50.0 }); function Launch() { - body.setTransform(new Vec2(0.0, 4.0), 0.0); - body.setLinearVelocity(new Vec2()); + body.setTransform({ x: 0.0, y: 4.0 }, 0.0); + body.setLinearVelocity({ x: 0, y: 0 }); body.setAngularVelocity(0.0); x = Math.random() * 2 - 1; - bullet.setTransform(new Vec2(x, 10.0), 0.0); - bullet.setLinearVelocity(new Vec2(0.0, -50.0)); + bullet.setTransform({ x: x, y: 10.0 }, 0.0); + bullet.setLinearVelocity({ x: 0.0, y: -50.0 }); bullet.setAngularVelocity(0.0); stats.gjkCalls = 0; diff --git a/example/Cantilever.ts b/example/Cantilever.ts index 763b80af..659eae35 100644 --- a/example/Cantilever.ts +++ b/example/Cantilever.ts @@ -8,9 +8,9 @@ // So why not go ahead and use soft weld joints? They behave like a revolute // joint with a rotational spring. -import { World, Vec2, Edge, Box, WeldJoint, Polygon, Circle, Testbed } from "planck"; +import { World, Edge, Box, WeldJoint, Polygon, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -18,14 +18,14 @@ testbed.start(world); const COUNT = 8; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody(new Vec2(-14.5 + 1.0 * i, 5.0)); + const body = world.createDynamicBody({ x: -14.5 + 1.0 * i, y: 5.0 }); body.createFixture(new Box(0.5, 0.125), 20.0); - const anchor = new Vec2(-15.0 + 1.0 * i, 5.0); + const anchor = { x: -15.0 + 1.0 * i, y: 5.0 }; world.createJoint(new WeldJoint({}, prevBody, body, anchor)); prevBody = body; @@ -34,10 +34,10 @@ ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); { let prevBody = ground; for (let i = 0; i < 3; ++i) { - const body = world.createDynamicBody(new Vec2(-14.0 + 2.0 * i, 15.0)); + const body = world.createDynamicBody({ x: -14.0 + 2.0 * i, y: 15.0 }); body.createFixture(new Box(1.0, 0.125), 20.0); - const anchor = new Vec2(-15.0 + 2.0 * i, 15.0); + const anchor = { x: -15.0 + 2.0 * i, y: 15.0 }; world.createJoint( new WeldJoint( { @@ -56,11 +56,11 @@ ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody(new Vec2(-4.5 + 1.0 * i, 5.0)); + const body = world.createDynamicBody({ x: -4.5 + 1.0 * i, y: 5.0 }); body.createFixture(new Box(0.5, 0.125), 20.0); if (i > 0) { - const anchor = new Vec2(-5.0 + 1.0 * i, 5.0); + const anchor = { x: -5.0 + 1.0 * i, y: 5.0 }; world.createJoint(new WeldJoint({}, prevBody, body, anchor)); } @@ -70,11 +70,11 @@ ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); { let prevBody = ground; for (let i = 0; i < COUNT; ++i) { - const body = world.createDynamicBody(new Vec2(5.5 + 1.0 * i, 10.0)); + const body = world.createDynamicBody({ x: 5.5 + 1.0 * i, y: 10.0 }); body.createFixture(new Box(0.5, 0.125), 20.0); if (i > 0) { - const anchor = new Vec2(5.0 + 1.0 * i, 10.0); + const anchor = { x: 5.0 + 1.0 * i, y: 10.0 }; world.createJoint( new WeldJoint( { @@ -93,17 +93,18 @@ ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); } { for (let i = 0; i < 2; ++i) { - const vertices: Vec2[] = []; - vertices[0] = new Vec2(-0.5, 0.0); - vertices[1] = new Vec2(0.5, 0.0); - vertices[2] = new Vec2(0.0, 1.5); + const vertices = [ + { x: -0.5, y: 0.0 }, + { x: 0.5, y: 0.0 }, + { x: 0.0, y: 1.5 }, + ]; - const body = world.createDynamicBody(new Vec2(-8.0 + 8.0 * i, 12.0)); + const body = world.createDynamicBody({ x: -8.0 + 8.0 * i, y: 12.0 }); body.createFixture(new Polygon(vertices), 1.0); } for (let i = 0; i < 2; ++i) { - const body = world.createDynamicBody(new Vec2(-6.0 + 6.0 * i, 10.0)); + const body = world.createDynamicBody({ x: -6.0 + 6.0 * i, y: 10.0 }); body.createFixture(new Circle(0.5), 1.0); } } diff --git a/example/Car.ts b/example/Car.ts index c359dd1e..8f805f77 100644 --- a/example/Car.ts +++ b/example/Car.ts @@ -5,12 +5,12 @@ // This is a fun demo that shows off the wheel joint -import { World, Vec2, Edge, Box, Circle, Polygon, RevoluteJoint, WheelJoint, Testbed } from "planck"; +import { World, Edge, Box, Circle, Polygon, RevoluteJoint, WheelJoint, Testbed } from "planck"; const testbed = Testbed.mount(); const world = new World({ - gravity: new Vec2(0, -10), + gravity: { x: 0, y: -10 }, }); testbed.speed = 1.3; @@ -30,7 +30,7 @@ const groundFD = { friction: 0.6, }; -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0)), groundFD); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), groundFD); const hs = [0.25, 1.0, 4.0, 0.0, 0.0, -1.0, -2.0, -2.0, -1.25, 0.0]; @@ -40,34 +40,34 @@ const dx = 5.0; for (let i = 0; i < 10; ++i) { const y2 = hs[i]; - ground.createFixture(new Edge(new Vec2(x, y1), new Vec2(x + dx, y2)), groundFD); + ground.createFixture(new Edge({ x: x, y: y1 }, { x: x + dx, y: y2 }), groundFD); y1 = y2; x += dx; } for (let i = 0; i < 10; ++i) { const y2 = hs[i]; - ground.createFixture(new Edge(new Vec2(x, y1), new Vec2(x + dx, y2)), groundFD); + ground.createFixture(new Edge({ x: x, y: y1 }, { x: x + dx, y: y2 }), groundFD); y1 = y2; x += dx; } -ground.createFixture(new Edge(new Vec2(x, 0.0), new Vec2(x + 40.0, 0.0)), groundFD); +ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x + 40.0, y: 0.0 }), groundFD); x += 80.0; -ground.createFixture(new Edge(new Vec2(x, 0.0), new Vec2(x + 40.0, 0.0)), groundFD); +ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x + 40.0, y: 0.0 }), groundFD); x += 40.0; -ground.createFixture(new Edge(new Vec2(x, 0.0), new Vec2(x + 10.0, 5.0)), groundFD); +ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x + 10.0, y: 5.0 }), groundFD); x += 20.0; -ground.createFixture(new Edge(new Vec2(x, 0.0), new Vec2(x + 40.0, 0.0)), groundFD); +ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x + 40.0, y: 0.0 }), groundFD); x += 40.0; -ground.createFixture(new Edge(new Vec2(x, 0.0), new Vec2(x, 20.0)), groundFD); +ground.createFixture(new Edge({ x: x, y: 0.0 }, { x: x, y: 20.0 }), groundFD); // Teeter -const teeter = world.createDynamicBody(new Vec2(140.0, 1.0)); +const teeter = world.createDynamicBody({ x: 140.0, y: 1.0 }); teeter.createFixture(new Box(10.0, 0.25), 1.0); world.createJoint( new RevoluteJoint( @@ -93,41 +93,41 @@ const bridgeFD = { let prevBody = ground; let i: number; for (i = 0; i < 20; ++i) { - const bridgeBlock = world.createDynamicBody(new Vec2(161.0 + 2.0 * i, -0.125)); + const bridgeBlock = world.createDynamicBody({ x: 161.0 + 2.0 * i, y: -0.125 }); bridgeBlock.createFixture(new Box(1.0, 0.125), bridgeFD); world.createJoint( - new RevoluteJoint({}, prevBody, bridgeBlock, new Vec2(160.0 + 2.0 * i, -0.125)), + new RevoluteJoint({}, prevBody, bridgeBlock, { x: 160.0 + 2.0 * i, y: -0.125 }), ); prevBody = bridgeBlock; } -world.createJoint(new RevoluteJoint({}, prevBody, ground, new Vec2(160.0 + 2.0 * i, -0.125))); +world.createJoint(new RevoluteJoint({}, prevBody, ground, { x: 160.0 + 2.0 * i, y: -0.125 })); // Boxes const box = new Box(0.5, 0.5); -world.createDynamicBody(new Vec2(230.0, 0.5)).createFixture(box, 0.5); +world.createDynamicBody({ x: 230.0, y: 0.5 }).createFixture(box, 0.5); -world.createDynamicBody(new Vec2(230.0, 1.5)).createFixture(box, 0.5); +world.createDynamicBody({ x: 230.0, y: 1.5 }).createFixture(box, 0.5); -world.createDynamicBody(new Vec2(230.0, 2.5)).createFixture(box, 0.5); +world.createDynamicBody({ x: 230.0, y: 2.5 }).createFixture(box, 0.5); -world.createDynamicBody(new Vec2(230.0, 3.5)).createFixture(box, 0.5); +world.createDynamicBody({ x: 230.0, y: 3.5 }).createFixture(box, 0.5); -world.createDynamicBody(new Vec2(230.0, 4.5)).createFixture(box, 0.5); +world.createDynamicBody({ x: 230.0, y: 4.5 }).createFixture(box, 0.5); // Car -const car = world.createDynamicBody(new Vec2(0.0, 1.0)); +const car = world.createDynamicBody({ x: 0.0, y: 1.0 }); car.createFixture( new Polygon([ - new Vec2(-1.5, -0.5), - new Vec2(1.5, -0.5), - new Vec2(1.5, 0.0), - new Vec2(0.0, 0.9), - new Vec2(-1.15, 0.9), - new Vec2(-1.5, 0.2), + { x: -1.5, y: -0.5 }, + { x: 1.5, y: -0.5 }, + { x: 1.5, y: 0.0 }, + { x: 0.0, y: 0.9 }, + { x: -1.15, y: 0.9 }, + { x: -1.5, y: 0.2 }, ]), 1.0, ); @@ -137,10 +137,10 @@ const wheelFD = { friction: 0.9, }; -const wheelBack = world.createDynamicBody(new Vec2(-1.0, 0.35)); +const wheelBack = world.createDynamicBody({ x: -1.0, y: 0.35 }); wheelBack.createFixture(new Circle(0.4), wheelFD); -const wheelFront = world.createDynamicBody(new Vec2(1.0, 0.4)); +const wheelFront = world.createDynamicBody({ x: 1.0, y: 0.4 }); wheelFront.createFixture(new Circle(0.4), wheelFD); const springBack = world.createJoint( @@ -155,7 +155,7 @@ const springBack = world.createJoint( car, wheelBack, wheelBack.getPosition(), - new Vec2(0.0, 1.0), + { x: 0.0, y: 1.0 }, ), ); @@ -171,35 +171,35 @@ const springFront = world.createJoint( car, wheelFront, wheelFront.getPosition(), - new Vec2(0.0, 1.0), + { x: 0.0, y: 1.0 }, ), ); testbed.keydown = function () { if (testbed.activeKeys.down) { HZ = Math.max(0.0, HZ - 1.0); - springBack.setSpringFrequencyHz(HZ); - springFront.setSpringFrequencyHz(HZ); + springBack?.setSpringFrequencyHz(HZ); + springFront?.setSpringFrequencyHz(HZ); } else if (testbed.activeKeys.up) { HZ += 1.0; - springBack.setSpringFrequencyHz(HZ); - springFront.setSpringFrequencyHz(HZ); + springBack?.setSpringFrequencyHz(HZ); + springFront?.setSpringFrequencyHz(HZ); } }; testbed.step = function () { if (testbed.activeKeys.right && testbed.activeKeys.left) { - springBack.setMotorSpeed(0); - springBack.enableMotor(true); + springBack?.setMotorSpeed(0); + springBack?.enableMotor(true); } else if (testbed.activeKeys.right) { - springBack.setMotorSpeed(-SPEED); - springBack.enableMotor(true); + springBack?.setMotorSpeed(-SPEED); + springBack?.enableMotor(true); } else if (testbed.activeKeys.left) { - springBack.setMotorSpeed(+SPEED); - springBack.enableMotor(true); + springBack?.setMotorSpeed(+SPEED); + springBack?.enableMotor(true); } else { - springBack.setMotorSpeed(0); - springBack.enableMotor(false); + springBack?.setMotorSpeed(0); + springBack?.enableMotor(false); } const cp = car.getPosition(); diff --git a/example/Chain.ts b/example/Chain.ts index 088efba8..32105024 100644 --- a/example/Chain.ts +++ b/example/Chain.ts @@ -3,28 +3,30 @@ * Licensed under the MIT license */ -import { World, Vec2, Edge, Box, RevoluteJoint, Testbed } from "planck"; +import { World, Edge, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const shape = new Box(0.6, 0.125); const y = 25.0; let prevBody = ground; for (let i = 0; i < 30; ++i) { - const body = world.createDynamicBody(new Vec2(0.5 + i, y)); + const body = world.createDynamicBody({ + position: { x: 0.5 + i, y: y }, + }); body.createFixture(shape, { density: 20.0, friction: 0.2, }); - const anchor = new Vec2(i, y); + const anchor = { x: i, y: y }; world.createJoint( new RevoluteJoint( { diff --git a/example/CharacterCollision.ts b/example/CharacterCollision.ts index 243504dd..a3d0f836 100644 --- a/example/CharacterCollision.ts +++ b/example/CharacterCollision.ts @@ -7,9 +7,9 @@ // show how you should implement a character in your application. // Instead this is used to test smooth collision on edge chains. -import { World, Vec2, Edge, Chain, Box, Polygon, Circle, Testbed } from "planck"; +import { World, Vec2Value, Edge, Chain, Box, Polygon, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.info(` @@ -21,20 +21,25 @@ testbed.start(world); // Ground body const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Collinear edges with no adjacency information. // This shows the problematic case where a box shape can hit // an internal vertex. const edge = world.createBody(); -edge.createFixture(new Edge(new Vec2(-8.0, 1.0), new Vec2(-6.0, 1.0)), 0.0); -edge.createFixture(new Edge(new Vec2(-6.0, 1.0), new Vec2(-4.0, 1.0)), 0.0); -edge.createFixture(new Edge(new Vec2(-4.0, 1.0), new Vec2(-2.0, 1.0)), 0.0); +edge.createFixture(new Edge({ x: -8.0, y: 1.0 }, { x: -6.0, y: 1.0 }), 0.0); +edge.createFixture(new Edge({ x: -6.0, y: 1.0 }, { x: -4.0, y: 1.0 }), 0.0); +edge.createFixture(new Edge({ x: -4.0, y: 1.0 }, { x: -2.0, y: 1.0 }), 0.0); // Chain shape -const chain = world.createBody(new Vec2(), 0.25 * Math.PI); +const chain = world.createBody({ x: 0, y: 0 }, 0.25 * Math.PI); chain.createFixture( - new Chain([new Vec2(5.0, 7.0), new Vec2(6.0, 8.0), new Vec2(7.0, 8.0), new Vec2(8.0, 7.0)]), + new Chain([ + { x: 5.0, y: 7.0 }, + { x: 6.0, y: 8.0 }, + { x: 7.0, y: 8.0 }, + { x: 8.0, y: 7.0 }, + ]), 0.0, ); @@ -42,35 +47,40 @@ chain.createFixture( // have non-smooth collision. There is no solution // to this problem. const tiles = world.createBody(); -tiles.createFixture(new Box(1.0, 1.0, new Vec2(4.0, 3.0), 0.0), 0.0); -tiles.createFixture(new Box(1.0, 1.0, new Vec2(6.0, 3.0), 0.0), 0.0); -tiles.createFixture(new Box(1.0, 1.0, new Vec2(8.0, 3.0), 0.0), 0.0); +tiles.createFixture(new Box(1.0, 1.0, { x: 4.0, y: 3.0 }, 0.0), 0.0); +tiles.createFixture(new Box(1.0, 1.0, { x: 6.0, y: 3.0 }, 0.0), 0.0); +tiles.createFixture(new Box(1.0, 1.0, { x: 8.0, y: 3.0 }, 0.0), 0.0); // Square made from an edge loop. Collision should be smooth. const square = world.createBody(); square.createFixture( new Chain( - [new Vec2(-1.0, 3.0), new Vec2(1.0, 3.0), new Vec2(1.0, 5.0), new Vec2(-1.0, 5.0)], + [ + { x: -1.0, y: 3.0 }, + { x: 1.0, y: 3.0 }, + { x: 1.0, y: 5.0 }, + { x: -1.0, y: 5.0 }, + ], true, ), 0.0, ); // Edge loop. Collision should be smooth. -const loop = world.createBody(new Vec2(-10.0, 4.0)); +const loop = world.createBody({ x: -10.0, y: 4.0 }); loop.createFixture( new Chain( [ - new Vec2(0.0, 0.0), - new Vec2(6.0, 0.0), - new Vec2(6.0, 2.0), - new Vec2(4.0, 1.0), - new Vec2(2.0, 2.0), - new Vec2(0.0, 2.0), - new Vec2(-2.0, 2.0), - new Vec2(-4.0, 3.0), - new Vec2(-6.0, 2.0), - new Vec2(-6.0, 0.0), + { x: 0.0, y: 0.0 }, + { x: 6.0, y: 0.0 }, + { x: 6.0, y: 2.0 }, + { x: 4.0, y: 1.0 }, + { x: 2.0, y: 2.0 }, + { x: 0.0, y: 2.0 }, + { x: -2.0, y: 2.0 }, + { x: -4.0, y: 3.0 }, + { x: -6.0, y: 2.0 }, + { x: -6.0, y: 0.0 }, ], true, ), @@ -79,7 +89,7 @@ loop.createFixture( // Square character 1 const char1 = world.createBody({ - position: new Vec2(-3.0, 8.0), + position: { x: -3.0, y: 8.0 }, type: "dynamic", fixedRotation: true, allowSleep: false, @@ -88,7 +98,7 @@ char1.createFixture(new Box(0.5, 0.5), 20.0); // Square character 2 const char2 = world.createBody({ - position: new Vec2(-5.0, 5.0), + position: { x: -5.0, y: 5.0 }, type: "dynamic", fixedRotation: true, allowSleep: false, @@ -97,7 +107,7 @@ char2.createFixture(new Box(0.25, 0.25), 20.0); // Hexagon character const hex = world.createBody({ - position: new Vec2(-5.0, 8.0), + position: { x: -5.0, y: 8.0 }, type: "dynamic", fixedRotation: true, allowSleep: false, @@ -105,9 +115,9 @@ const hex = world.createBody({ let angle = 0.0; const delta = Math.PI / 3.0; -const vertices: Vec2[] = []; +const vertices: Vec2Value[] = []; for (let i = 0; i < 6; ++i) { - vertices[i] = new Vec2(0.5 * Math.cos(angle), 0.5 * Math.sin(angle)); + vertices[i] = { x: 0.5 * Math.cos(angle), y: 0.5 * Math.sin(angle) }; angle += delta; } @@ -115,7 +125,7 @@ hex.createFixture(new Polygon(vertices), 20.0); // Circle character const circle = world.createBody({ - position: new Vec2(3.0, 5.0), + position: { x: 3.0, y: 5.0 }, type: "dynamic", fixedRotation: true, allowSleep: false, @@ -124,7 +134,7 @@ circle.createFixture(new Circle(0.5), 20.0); // Circle character const character = world.createBody({ - position: new Vec2(-7.0, 6.0), + position: { x: -7.0, y: 6.0 }, type: "dynamic", allowSleep: false, }); diff --git a/example/CollisionFiltering.ts b/example/CollisionFiltering.ts index 987acdd4..71eeb26a 100644 --- a/example/CollisionFiltering.ts +++ b/example/CollisionFiltering.ts @@ -10,7 +10,7 @@ // The 3 large ones never collide. // The boxes don't collide with triangles (except if both are small). -import { World, Vec2, Edge, Polygon, Box, Circle, PrismaticJoint, Testbed } from "planck"; +import { World, Edge, Polygon, Box, Circle, PrismaticJoint, Testbed } from "planck"; const SMALL_GROUP = 1; const LARGE_GROUP = -1; @@ -23,14 +23,14 @@ const TRIANGLE_MASK = 0xffff; const BOX_MASK = 0xffff ^ TRIANGLE_CATEGORY; const CIRCLE_MAX = 0xffff; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); // Ground body const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), { +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), { friction: 0.3, }); @@ -44,10 +44,14 @@ const smallTriangle = { const body1 = world.createBody({ type: "dynamic", - position: new Vec2(-5.0, 2.0), + position: { x: -5.0, y: 2.0 }, }); body1.createFixture( - new Polygon([new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 2.0)]), + new Polygon([ + { x: -1.0, y: 0.0 }, + { x: 1.0, y: 0.0 }, + { x: 0.0, y: 2.0 }, + ]), smallTriangle, ); @@ -61,15 +65,19 @@ const largeTriangle = { const body2 = world.createBody({ type: "dynamic", - position: new Vec2(-5.0, 6.0), + position: { x: -5.0, y: 6.0 }, fixedRotation: true, // look at me! }); body2.createFixture( - new Polygon([new Vec2(-2.0, 0.0), new Vec2(2.0, 0.0), new Vec2(0.0, 4.0)]), + new Polygon([ + { x: -2.0, y: 0.0 }, + { x: 2.0, y: 0.0 }, + { x: 0.0, y: 4.0 }, + ]), largeTriangle, ); -const body = world.createDynamicBody(new Vec2(-5.0, 10.0)); +const body = world.createDynamicBody({ x: -5.0, y: 10.0 }); body.createFixture(new Box(0.5, 1.0), 1.0); world.createJoint( @@ -77,9 +85,9 @@ world.createJoint( bodyA: body2, bodyB: body, enableLimit: true, - localAnchorA: new Vec2(0.0, 4.0), - localAnchorB: new Vec2(), - localAxisA: new Vec2(0.0, 1.0), + localAnchorA: { x: 0.0, y: 4.0 }, + localAnchorB: { x: 0, y: 0 }, + localAxisA: { x: 0.0, y: 1.0 }, lowerTranslation: -1.0, upperTranslation: 1.0, }), @@ -94,7 +102,7 @@ const smallBox = { filterGroupIndex: SMALL_GROUP, }; -const body3 = world.createDynamicBody(new Vec2(0.0, 2.0)); +const body3 = world.createDynamicBody({ x: 0.0, y: 2.0 }); body3.createFixture(new Box(1.0, 0.5), smallBox); // Large box (recycle definitions) @@ -106,7 +114,7 @@ const largeBox = { filterGroupIndex: LARGE_GROUP, }; -const body4 = world.createDynamicBody(new Vec2(0.0, 6.0)); +const body4 = world.createDynamicBody({ x: 0.0, y: 6.0 }); body4.createFixture(new Box(2.0, 1.0), largeBox); // Small circle @@ -117,7 +125,7 @@ const smallCircle = { filterGroupIndex: SMALL_GROUP, }; -const body5 = world.createDynamicBody(new Vec2(5.0, 2.0)); +const body5 = world.createDynamicBody({ x: 5.0, y: 2.0 }); body5.createFixture(new Circle(1.0), smallCircle); // Large circle @@ -128,5 +136,5 @@ const largeCircle = { filterGroupIndex: LARGE_GROUP, }; -const body6 = world.createDynamicBody(new Vec2(5.0, 6.0)); +const body6 = world.createDynamicBody({ x: 5.0, y: 6.0 }); body6.createFixture(new Circle(2.0), largeCircle); diff --git a/example/CollisionProcessing.ts b/example/CollisionProcessing.ts index f46f733b..5e8b6fb7 100644 --- a/example/CollisionProcessing.ts +++ b/example/CollisionProcessing.ts @@ -3,29 +3,17 @@ * Licensed under the MIT license */ -import { - World, - Body, - Fixture, - Vec2, - Vec2Value, - Edge, - Polygon, - Box, - Circle, - Math, - Testbed, -} from "planck"; +import { World, Body, Fixture, Vec2Value, Edge, Polygon, Box, Circle, Math, Testbed } from "planck"; // This test shows collision processing and tests // deferred body destruction. -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); // Ground body -world.createBody().createFixture(new Edge(new Vec2(-50.0, 0.0), new Vec2(50.0, 0.0))); +world.createBody().createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: 50.0, y: 0.0 })); const xLo = -5.0; const xHi = 5.0; @@ -33,33 +21,59 @@ const yLo = 2.0; const yHi = 35.0; // Small triangle -const body1 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body1 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body1.createFixture( - new Polygon([new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 2.0)]), + new Polygon([ + { x: -1.0, y: 0.0 }, + { x: 1.0, y: 0.0 }, + { x: 0.0, y: 2.0 }, + ]), 1.0, ); // Large triangle (recycle definitions) -const body2 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body2 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body2.createFixture( - new Polygon([new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0), new Vec2(0.0, 2.0)]), + new Polygon([ + { x: -1.0, y: 0.0 }, + { x: 1.0, y: 0.0 }, + { x: 0.0, y: 2.0 }, + ]), 1.0, ); // Small box -const body3 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body3 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body3.createFixture(new Box(1.0, 0.5), 1.0); // Large box (recycle definitions) -const body4 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body4 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body4.createFixture(new Box(2.0, 1.0), 1.0); // Small circle -const body5 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body5 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body5.createFixture(new Circle(1.0), 1.0); // Large circle -const body6 = world.createDynamicBody(new Vec2(Math.random(xLo, xHi), Math.random(yLo, yHi))); +const body6 = world.createDynamicBody({ + x: Math.random(xLo, xHi), + y: Math.random(yLo, yHi), +}); body6.createFixture(new Circle(2.0), 1.0); interface ContactPoint { diff --git a/example/CompoundShapes.ts b/example/CompoundShapes.ts index 0ddc8d66..8669fad4 100644 --- a/example/CompoundShapes.ts +++ b/example/CompoundShapes.ts @@ -6,21 +6,25 @@ // TODO_ERIN test joints on compounds. import { World, Vec2, Transform, Math, Edge, Circle, Polygon, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); -world - .createBody(new Vec2(0.0, 0.0)) - .createFixture(new Edge(new Vec2(50.0, 0.0), new Vec2(-50.0, 0.0)), 0.0); +{ + const ground = world.createBody({ x: 0.0, y: 0.0 }); + ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: -50.0, y: 0.0 }), 0.0); +} -const circle1 = new Circle(new Vec2(-0.5, 0.5), 0.5); -const circle2 = new Circle(new Vec2(0.5, 0.5), 0.5); +const circle1 = new Circle({ x: -0.5, y: 0.5 }, 0.5); +const circle2 = new Circle({ x: 0.5, y: 0.5 }, 0.5); for (let i = 0; i < 10; ++i) { const body = world.createDynamicBody({ - position: new Vec2(Math.random(-0.1, 0.1) + 5.0, 1.05 + 2.5 * i), + position: { + x: Math.random(-0.1, 0.1) + 5.0, + y: 1.05 + 2.5 * i, + }, angle: Math.random(-Math.PI, Math.PI), }); body.createFixture(circle1, 2.0); @@ -28,11 +32,11 @@ for (let i = 0; i < 10; ++i) { } const polygon1 = new Box(0.25, 0.5); -const polygon2 = new Box(0.25, 0.5, new Vec2(0.0, -0.5), 0.5 * Math.PI); +const polygon2 = new Box(0.25, 0.5, { x: 0.0, y: -0.5 }, 0.5 * Math.PI); for (let i = 0; i < 10; ++i) { const body = world.createDynamicBody({ - position: new Vec2(Math.random(-0.1, 0.1) - 5.0, 1.05 + 2.5 * i), + position: { x: Math.random(-0.1, 0.1) - 5.0, y: 1.05 + 2.5 * i }, angle: Math.random(-Math.PI, Math.PI), }); body.createFixture(polygon1, 2.0); @@ -44,7 +48,11 @@ xf1.q.set(0.3524 * Math.PI); xf1.p.set(xf1.q.getXAxis()); const triangle1 = 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(); @@ -52,12 +60,19 @@ xf2.q.set(-0.3524 * Math.PI); xf2.p.set(Vec2.neg(xf2.q.getXAxis())); const triangle2 = 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)), ); for (let i = 0; i < 10; ++i) { const body = world.createDynamicBody({ - position: new Vec2(Math.random(-0.1, 0.1), 2.05 + 2.5 * i), + position: { + x: Math.random(-0.1, 0.1), + y: 2.05 + 2.5 * i, + }, angle: 0.0, }); body.createFixture(triangle1, 2.0); @@ -65,10 +80,10 @@ for (let i = 0; i < 10; ++i) { } const bottom = new Box(1.5, 0.15); -const left = new Box(0.15, 2.7, new Vec2(-1.45, 2.35), 0.2); -const right = new Box(0.15, 2.7, new Vec2(1.45, 2.35), -0.2); +const left = new Box(0.15, 2.7, { x: -1.45, y: 2.35 }, 0.2); +const right = new Box(0.15, 2.7, { x: 1.45, y: 2.35 }, -0.2); -const container = world.createBody(new Vec2(0.0, 2.0)); +const container = world.createBody({ x: 0.0, y: 2.0 }); container.createFixture(bottom, 4.0); container.createFixture(left, 4.0); container.createFixture(right, 4.0); diff --git a/example/Confined.ts b/example/Confined.ts index 80c8f40b..c9479907 100644 --- a/example/Confined.ts +++ b/example/Confined.ts @@ -3,7 +3,7 @@ * Licensed under the MIT license */ -import { World, Vec2, Edge, Circle, Testbed } from "planck"; +import { World, Edge, Circle, Testbed } from "planck"; const world = new World(); @@ -15,16 +15,16 @@ const e_rowCount = 0; const ground = world.createBody(); // Floor -ground.createFixture(new Edge(new Vec2(-10, 0), new Vec2(10, 0)), 0); +ground.createFixture(new Edge({ x: -10, y: 0 }, { x: 10, y: 0 }), 0); // Left wall -ground.createFixture(new Edge(new Vec2(-10, 0), new Vec2(-10, 20)), 0); +ground.createFixture(new Edge({ x: -10, y: 0 }, { x: -10, y: 20 }), 0); // Right wall -ground.createFixture(new Edge(new Vec2(10, 0), new Vec2(10, 20)), 0); +ground.createFixture(new Edge({ x: 10, y: 0 }, { x: 10, y: 20 }), 0); // Roof -ground.createFixture(new Edge(new Vec2(-10, 20), new Vec2(10, 20)), 0); +ground.createFixture(new Edge({ x: -10, y: 20 }, { x: 10, y: 20 }), 0); const radius = 0.5; const shape = new Circle(radius); @@ -36,15 +36,23 @@ const fd = { for (let j = 0; j < e_columnCount; ++j) { for (let i = 0; i < e_rowCount; ++i) { - const body = world.createDynamicBody( - new Vec2(-10 + (2.1 * j + 1 + 0.01 * i) * radius, (2 * i + 1) * radius), - ); + const body = world.createDynamicBody({ + position: { + x: -10 + (2.1 * j + 1 + 0.01 * i) * radius, + y: (2 * i + 1) * radius, + }, + }); body.createFixture(shape, fd); } } function CreateCircle() { - const body = world.createDynamicBody(new Vec2(Math.random() * 10 - 5, Math.random() * 10 + 5)); + const body = world.createDynamicBody({ + position: { + x: Math.random() * 10 - 5, + y: Math.random() * 10 + 5, + }, + }); // bd.allowSleep = false; body.createFixture(new Circle(Math.random() * 2.5 + 0.5), { density: 1.0, diff --git a/example/ContinuousTest.ts b/example/ContinuousTest.ts index a77f3be7..2948de74 100644 --- a/example/ContinuousTest.ts +++ b/example/ContinuousTest.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Body, stats, Circle, Edge, Box, Testbed } from "planck"; +import { World, Body, stats, Circle, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -13,31 +13,31 @@ testbed.start(world); let bullet: Body; let angularVelocity: number; -const ground = world.createBody(new Vec2(0.0, 0.0)); +const ground = world.createBody({ x: 0.0, y: 0.0 }); -ground.createFixture(new Edge(new Vec2(-10.0, 0.0), new Vec2(10.0, 0.0)), 0.0); -ground.createFixture(new Box(0.2, 1.0, new Vec2(0.5, 1.0), 0.0), 0.0); +ground.createFixture(new Edge({ x: -10.0, y: 0.0 }, { x: 10.0, y: 0.0 }), 0.0); +ground.createFixture(new Box(0.2, 1.0, { x: 0.5, y: 1.0 }, 0.0), 0.0); if (true) { // angle = 0.1; - bullet = world.createDynamicBody(new Vec2(0.0, 20.0)); + bullet = world.createDynamicBody({ x: 0.0, y: 20.0 }); bullet.createFixture(new Box(2.0, 0.1), 1.0); angularVelocity = Math.random() * 100 - 50; // angularVelocity = 46.661274; - bullet.setLinearVelocity(new Vec2(0.0, -100.0)); + bullet.setLinearVelocity({ x: 0.0, y: -100.0 }); bullet.setAngularVelocity(angularVelocity); } else { const shape = new Circle(0.5); - world.createDynamicBody(new Vec2(0.0, 2.0)).createFixture(shape, 1.0); + world.createDynamicBody({ x: 0.0, y: 2.0 }).createFixture(shape, 1.0); const body = world.createDynamicBody({ bullet: true, - position: new Vec2(0.0, 2.0), + position: { x: 0.0, y: 2.0 }, }); body.createFixture(shape, 1.0); - body.setLinearVelocity(new Vec2(0.0, -100.0)); + body.setLinearVelocity({ x: 0.0, y: -100.0 }); } function launch() { @@ -52,9 +52,9 @@ function launch() { stats.toiTime = 0.0; stats.toiMaxTime = 0.0; - bullet.setTransform(new Vec2(0.0, 20.0), 0.0); + bullet.setTransform({ x: 0.0, y: 20.0 }, 0.0); angularVelocity = Math.random() * 100 - 50; - bullet.setLinearVelocity(new Vec2(0.0, -100.0)); + bullet.setLinearVelocity({ x: 0.0, y: -100.0 }); bullet.setAngularVelocity(angularVelocity); } diff --git a/example/ConvexHull.ts b/example/ConvexHull.ts index e95dab67..9e1826e4 100644 --- a/example/ConvexHull.ts +++ b/example/ConvexHull.ts @@ -24,8 +24,8 @@ let shape: PolygonShape; generate(); function generate() { - const lowerBound = new Vec2(-8.0, -8.0); - const upperBound = new Vec2(8.0, 8.0); + const lowerBound = { x: -8.0, y: -8.0 }; + const upperBound = { x: 8.0, y: 8.0 }; points.length = 0; for (let i = 0; i < COUNT; ++i) { @@ -34,7 +34,7 @@ function generate() { // Clamp onto a square to help create collinearities. // This will stress the convex hull algorithm. - const v = Vec2.clampVec2(new Vec2(x, y), lowerBound, upperBound); + const v = Vec2.clampVec2({ x: x, y: y }, lowerBound, upperBound); points.push(v); } @@ -58,7 +58,7 @@ testbed.step = function () { for (let i = 0; i < points.length; ++i) { testbed.drawPoint(points[i], 3.0, testbed.color(0.3, 0.9, 0.3)); - // testbed.drawString(points[i] + new Vec2(0.05, 0.05), "%d", i); + // testbed.drawString(points[i] + ({ x: 0.05, y: 0.05 }), "%d", i); } // if (shape.validate() == false) { diff --git a/example/ConveyorBelt.ts b/example/ConveyorBelt.ts index 4538026f..bcb22b88 100644 --- a/example/ConveyorBelt.ts +++ b/example/ConveyorBelt.ts @@ -3,25 +3,25 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Box, Testbed } from "planck"; +import { World, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); // Ground const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Platform const platform = world - .createBody(new Vec2(-5.0, 5.0)) + .createBody({ x: -5.0, y: 5.0 }) .createFixture(new Box(10.0, 0.5), { friction: 0.8 }); // Boxes for (let i = 0; i < 5; ++i) { - world.createDynamicBody(new Vec2(-10.0 + 2.0 * i, 7.0)).createFixture(new Box(0.5, 0.5), 20.0); + world.createDynamicBody({ x: -10.0 + 2.0 * i, y: 7.0 }).createFixture(new Box(0.5, 0.5), 20.0); } world.on("pre-solve", function (contact, oldManifold) { diff --git a/example/DistanceTest.ts b/example/DistanceTest.ts index 82b366eb..210937ce 100644 --- a/example/DistanceTest.ts +++ b/example/DistanceTest.ts @@ -3,7 +3,7 @@ * Licensed under the MIT license */ -import { World, Vec2, Transform, Box, Distance, Testbed } from "planck"; +import { World, Transform, Box, Distance, Testbed } from "planck"; const DistanceInput = Distance.Input; const DistanceOutput = Distance.Output; @@ -14,11 +14,11 @@ const world = new World(); const testbed = Testbed.mount(); testbed.start(world); -const transformA = new Transform(new Vec2(0.0, -0.2)); +const transformA = new Transform({ x: 0.0, y: -0.2 }); const polygonA = new Box(10.0, 0.2); -const positionB = new Vec2(12.017401, 0.13678508); +const positionB = { x: 12.017401, y: 0.13678508 }; let angleB = -0.0109265; const transformB = new Transform(positionB, angleB); diff --git a/example/Dominos.ts b/example/Dominos.ts index 51c09c5e..ce47e170 100644 --- a/example/Dominos.ts +++ b/example/Dominos.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Box, RevoluteJoint, DistanceJoint, Circle, Testbed } from "planck"; +import { World, Edge, Box, RevoluteJoint, DistanceJoint, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.width = 50; @@ -13,39 +13,39 @@ testbed.height = 50; testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40, 0), new Vec2(40, 0)), 0); +ground.createFixture(new Edge({ x: -40, y: 0 }, { x: 40, y: 0 }), 0); -world.createBody(new Vec2(-1.5, 10)).createFixture(new Box(6, 0.25), 0); +world.createBody({ x: -1.5, y: 10 }).createFixture(new Box(6, 0.25), 0); const columnShape = new Box(0.1, 1); for (let i = 0; i < 10; ++i) { - world.createDynamicBody(new Vec2(-6 + 1 * i, 11.25)).createFixture(columnShape, { + world.createDynamicBody({ x: -6 + 1 * i, y: 11.25 }).createFixture(columnShape, { density: 20, friction: 0.1, }); } -world.createBody(new Vec2(1, 6)).createFixture(new Box(7, 0.25, new Vec2(), 0.3), 0); +world.createBody({ x: 1, y: 6 }).createFixture(new Box(7, 0.25, { x: 0, y: 0 }, 0.3), 0); -const b2 = world.createBody(new Vec2(-7, 4)); +const b2 = world.createBody({ x: -7, y: 4 }); b2.createFixture(new Box(0.25, 1.5), 0); -const b3 = world.createDynamicBody(new Vec2(-0.9, 1), -0.15); +const b3 = world.createDynamicBody({ x: -0.9, y: 1 }, -0.15); b3.createFixture(new Box(6, 0.125), 10); const jd = { collideConnected: true, }; -world.createJoint(new RevoluteJoint(jd, ground, b3, new Vec2(-2, 1))); +world.createJoint(new RevoluteJoint(jd, ground, b3, { x: -2, y: 1 })); -const b4 = world.createDynamicBody(new Vec2(-10, 15)); +const b4 = world.createDynamicBody({ x: -10, y: 15 }); b4.createFixture(new Box(0.25, 0.25), 10); -world.createJoint(new RevoluteJoint(jd, b2, b4, new Vec2(-7, 15))); +world.createJoint(new RevoluteJoint(jd, b2, b4, { x: -7, y: 15 })); -const b5 = world.createDynamicBody(new Vec2(6.5, 3)); +const b5 = world.createDynamicBody({ x: 6.5, y: 3 }); { const fd = { @@ -53,27 +53,27 @@ const b5 = world.createDynamicBody(new Vec2(6.5, 3)); friction: 0.1, }; - b5.createFixture(new Box(1, 0.1, new Vec2(0, -0.9), 0), fd); - b5.createFixture(new Box(0.1, 1, new Vec2(-0.9, 0), 0), fd); - b5.createFixture(new Box(0.1, 1, new Vec2(0.9, 0), 0), fd); + b5.createFixture(new Box(1, 0.1, { x: 0, y: -0.9 }, 0), fd); + b5.createFixture(new Box(0.1, 1, { x: -0.9, y: 0 }, 0), fd); + b5.createFixture(new Box(0.1, 1, { x: 0.9, y: 0 }, 0), fd); } -world.createJoint(new RevoluteJoint(jd, ground, b5, new Vec2(6, 2))); +world.createJoint(new RevoluteJoint(jd, ground, b5, { x: 6, y: 2 })); -const b6 = world.createDynamicBody(new Vec2(6.5, 4.1)); +const b6 = world.createDynamicBody({ x: 6.5, y: 4.1 }); b6.createFixture(new Box(1, 0.1), 30); -world.createJoint(new RevoluteJoint(jd, b5, b6, new Vec2(7.5, 4))); +world.createJoint(new RevoluteJoint(jd, b5, b6, { x: 7.5, y: 4 })); -const b7 = world.createDynamicBody(new Vec2(7.4, 1)); +const b7 = world.createDynamicBody({ x: 7.4, y: 1 }); b7.createFixture(new Box(0.1, 1), 10); world.createJoint( new DistanceJoint({ bodyA: b3, - localAnchorA: new Vec2(6, 0), + localAnchorA: { x: 6, y: 0 }, bodyB: b7, - localAnchorB: new Vec2(0, -1), + localAnchorB: { x: 0, y: -1 }, }), ); @@ -81,7 +81,7 @@ world.createJoint( const radius = 0.2; const circleShape = new Circle(radius); for (let i = 0; i < 4; ++i) { - const body = world.createDynamicBody(new Vec2(5.9 + 2 * radius * i, 2.4)); + const body = world.createDynamicBody({ x: 5.9 + 2 * radius * i, y: 2.4 }); body.createFixture(circleShape, 10); } } diff --git a/example/DynamicTreeTest.ts b/example/DynamicTreeTest.ts index 02920120..f3ffd886 100644 --- a/example/DynamicTreeTest.ts +++ b/example/DynamicTreeTest.ts @@ -32,7 +32,7 @@ for (let i = 0; i < ACTOR_COUNT; ++i) { actor.proxyId = tree.createProxy(actor.aabb, actor); } -const queryAABB = new AABB(new Vec2(-3.0, -4.0 + WORLD_EXTENT), new Vec2(5.0, 6.0 + WORLD_EXTENT)); +const queryAABB = new AABB({ x: -3.0, y: -4.0 + WORLD_EXTENT }, { x: 5.0, y: 6.0 + WORLD_EXTENT }); function queryCallback(proxyId: number) { const actor = tree.getUserData(proxyId); @@ -54,10 +54,10 @@ function runQuery(tree: DynamicTree) { } const rayCastInput: RayCastInput = { - // p1: new Vec2(0.0, 2.0 + worldExtent), - // p2: new Vec2(0.0, -2.0 + worldExtent), - p1: new Vec2(-5.0, 5.0 + WORLD_EXTENT), - p2: new Vec2(7.0, -4.0 + WORLD_EXTENT), + // p1: { x: 0.0, y: 2.0 + worldExtent }, + // p2: { x: 0.0, y: -2.0 + worldExtent }, + p1: { x: -5.0, y: 5.0 + WORLD_EXTENT }, + p2: { x: 7.0, y: -4.0 + WORLD_EXTENT }, maxFraction: 1.0, }; @@ -205,15 +205,24 @@ function getRandomAABB(aabb: AABB) { } function moveAABB(aabb: AABB) { - const d = new Vec2(Math.random(-0.5, 0.5), Math.random(-0.5, 0.5)); + const d = { + x: Math.random(-0.5, 0.5), + y: Math.random(-0.5, 0.5), + }; // d.x = 2.0; // d.y = 0.0; aabb.lowerBound.add(d); aabb.upperBound.add(d); const c0 = Vec2.mid(aabb.lowerBound, aabb.upperBound); - const min = new Vec2(-WORLD_EXTENT, 0.0); - const max = new Vec2(WORLD_EXTENT, 2.0 * WORLD_EXTENT); + const min = { + x: -WORLD_EXTENT, + y: 0.0, + }; + const max = { + x: WORLD_EXTENT, + y: 2.0 * WORLD_EXTENT, + }; const c = Vec2.clampVec2(c0, min, max); aabb.lowerBound.add(c).sub(c0); diff --git a/example/EdgeShapes.ts b/example/EdgeShapes.ts index cde98f2d..d31e8826 100644 --- a/example/EdgeShapes.ts +++ b/example/EdgeShapes.ts @@ -18,7 +18,7 @@ import { Testbed, } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -39,16 +39,24 @@ const shapes: Shape[] = []; const x2 = x1 + 0.5; const y2 = 2.0 * Math.cos((x2 / 10.0) * Math.PI); - ground.createFixture(new Edge(new Vec2(x1, y1), new Vec2(x2, y2)), 0.0); + ground.createFixture(new Edge({ x: x1, y: y1 }, { x: x2, y: y2 }), 0.0); x1 = x2; y1 = y2; } } -shapes[0] = new Polygon([new Vec2(-0.5, 0.0), new Vec2(0.5, 0.0), new Vec2(0.0, 1.5)]); +shapes[0] = new Polygon([ + { x: -0.5, y: 0.0 }, + { x: 0.5, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); -shapes[1] = new Polygon([new Vec2(-0.1, 0.0), new Vec2(0.1, 0.0), new Vec2(0.0, 1.5)]); +shapes[1] = new Polygon([ + { x: -0.1, y: 0.0 }, + { x: 0.1, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); { const w = 1.0; @@ -56,14 +64,14 @@ shapes[1] = new Polygon([new Vec2(-0.1, 0.0), new Vec2(0.1, 0.0), new Vec2(0.0, const s = Math.sqrt(2.0) * b; const vertices = [ - new Vec2(0.5 * s, 0.0), - new Vec2(0.5 * w, b), - new Vec2(0.5 * w, b + s), - new Vec2(0.5 * s, w), - new Vec2(-0.5 * s, w), - new Vec2(-0.5 * w, b + s), - new Vec2(-0.5 * w, b), - new Vec2(-0.5 * s, 0.0), + { x: 0.5 * s, y: 0.0 }, + { x: 0.5 * w, y: b }, + { x: 0.5 * w, y: b + s }, + { x: 0.5 * s, y: w }, + { x: -0.5 * s, y: w }, + { x: -0.5 * w, y: b + s }, + { x: -0.5 * w, y: b }, + { x: -0.5 * s, y: 0.0 }, ]; shapes[2] = new Polygon(vertices); @@ -81,7 +89,10 @@ function createItem(index: number) { } const bd: BodyDef = { - position: new Vec2(Math.random(-10.0, 10.0), Math.random(10.0, 20.0)), + position: { + x: Math.random(-10.0, 10.0), + y: Math.random(10.0, 20.0), + }, angle: Math.random(-Math.PI, Math.PI), type: "dynamic", }; @@ -159,8 +170,11 @@ testbed.step = function () { const advanceRay = !pause; // settings.pause == 0 || settings.singleStep; const L = 25.0; - const point1 = new Vec2(0.0, 10.0); - const d = new Vec2(L * Math.cos(angle), -L * Math.abs(Math.sin(angle))); + const point1 = { x: 0.0, y: 10.0 }; + const d = { + x: L * Math.cos(angle), + y: -L * Math.abs(Math.sin(angle)), + }; const point2 = Vec2.add(point1, d); rayCastReset(); diff --git a/example/EdgeTest.ts b/example/EdgeTest.ts index b2347e00..77bb29a8 100644 --- a/example/EdgeTest.ts +++ b/example/EdgeTest.ts @@ -3,22 +3,22 @@ * Licensed under the MIT license */ -import { Vec2, World, Circle, Box, Edge, Testbed } from "planck"; +import { World, Circle, Box, Edge, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const ground = world.createBody(); -const v1 = new Vec2(-10.0, 0.0); -const v2 = new Vec2(-7.0, -2.0); -const v3 = new Vec2(-4.0, 0.0); -const v4 = new Vec2(0.0, 0.0); -const v5 = new Vec2(4.0, 0.0); -const v6 = new Vec2(7.0, 2.0); -const v7 = new Vec2(10.0, 0.0); +const v1 = { x: -10.0, y: 0.0 }; +const v2 = { x: -7.0, y: -2.0 }; +const v3 = { x: -4.0, y: 0.0 }; +const v4 = { x: 0.0, y: 0.0 }; +const v5 = { x: 4.0, y: 0.0 }; +const v6 = { x: 7.0, y: 2.0 }; +const v7 = { x: 10.0, y: 0.0 }; const shape1 = new Edge(v1, v2); shape1.setNextVertex(v3); @@ -51,7 +51,7 @@ ground.createFixture(shape6, 0.0); world .createBody({ type: "dynamic", - position: new Vec2(-0.5, 0.6), + position: { x: -0.5, y: 0.6 }, allowSleep: false, }) .createFixture(new Circle(0.5), 1.0); @@ -59,7 +59,7 @@ world world .createBody({ type: "dynamic", - position: new Vec2(1.0, 0.6), + position: { x: 1.0, y: 0.6 }, allowSleep: false, }) .createFixture(new Box(0.5, 0.5), 1.0); diff --git a/example/Gears.ts b/example/Gears.ts index 62addc8d..50446d85 100644 --- a/example/Gears.ts +++ b/example/Gears.ts @@ -4,7 +4,6 @@ */ import { - Vec2, World, Circle, Box, @@ -15,24 +14,24 @@ import { Testbed, } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(50.0, 0.0), new Vec2(-50.0, 0.0))); +ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: -50.0, y: 0.0 })); const radius1 = 1.0; const radius2 = 2.0; -const gearA1 = world.createBody(new Vec2(10.0, 9.0)); +const gearA1 = world.createBody({ x: 10.0, y: 9.0 }); gearA1.createFixture(new Circle(radius1), 5.0); -const plankA1 = world.createDynamicBody(new Vec2(10.0, 8.0)); +const plankA1 = world.createDynamicBody({ x: 10.0, y: 8.0 }); plankA1.createFixture(new Box(0.5, 5.0), 5.0); -const gearA2 = world.createDynamicBody(new Vec2(10.0, 6.0)); +const gearA2 = world.createDynamicBody({ x: 10.0, y: 6.0 }); gearA2.createFixture(new Circle(radius2), 5.0); const jointA1 = world.createJoint(new RevoluteJoint({}, plankA1, gearA1, gearA1.getPosition())); @@ -40,17 +39,17 @@ const jointA2 = world.createJoint(new RevoluteJoint({}, plankA1, gearA2, gearA2. world.createJoint(new GearJoint({}, gearA1, gearA2, jointA1!, jointA2!, radius2 / radius1)); -const gearB1 = world.createDynamicBody(new Vec2(-3.0, 12.0)); +const gearB1 = world.createDynamicBody({ x: -3.0, y: 12.0 }); gearB1.createFixture(new Circle(1.0), 5.0); const jointB1 = world.createJoint(new RevoluteJoint({}, ground, gearB1, gearB1.getPosition())); -const gearB2 = world.createDynamicBody(new Vec2(0.0, 12.0)); +const gearB2 = world.createDynamicBody({ x: 0.0, y: 12.0 }); gearB2.createFixture(new Circle(2.0), 5.0); const jointB2 = world.createJoint(new RevoluteJoint({}, ground, gearB2, gearB2.getPosition())); -const plankB1 = world.createDynamicBody(new Vec2(2.5, 12.0)); +const plankB1 = world.createDynamicBody({ x: 2.5, y: 12.0 }); plankB1.createFixture(new Box(0.5, 5.0), 5.0); const jointB3 = world.createJoint( @@ -63,7 +62,7 @@ const jointB3 = world.createJoint( ground, plankB1, plankB1.getPosition(), - new Vec2(0.0, 1.0), + { x: 0.0, y: 1.0 }, ), ); diff --git a/example/HeavyOnLight.ts b/example/HeavyOnLight.ts index 73004bc3..82919c0f 100644 --- a/example/HeavyOnLight.ts +++ b/example/HeavyOnLight.ts @@ -3,15 +3,15 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Circle, Testbed } from "planck"; +import { World, Edge, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); -world.createBody().createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0))); +world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); -world.createDynamicBody(new Vec2(0.0, 4.5)).createFixture(new Circle(0.5), 10.0); +world.createDynamicBody({ x: 0.0, y: 4.5 }).createFixture(new Circle(0.5), 10.0); -world.createDynamicBody(new Vec2(0.0, 10.0)).createFixture(new Circle(5.0), 10.0); +world.createDynamicBody({ x: 0.0, y: 10.0 }).createFixture(new Circle(5.0), 10.0); diff --git a/example/HeavyOnLightTwo.ts b/example/HeavyOnLightTwo.ts index 7b4c06f5..b1bf6ea0 100644 --- a/example/HeavyOnLightTwo.ts +++ b/example/HeavyOnLightTwo.ts @@ -3,19 +3,19 @@ * Licensed under the MIT license */ -import { World, Body, Vec2, Circle, Edge, Testbed } from "planck"; +import { World, Body, Circle, Edge, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.info("X: Add/Remove heavy circle"); testbed.start(world); -world.createBody().createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -world.createDynamicBody(new Vec2(0.0, 2.5)).createFixture(new Circle(0.5), 10.0); +world.createDynamicBody({ x: 0.0, y: 2.5 }).createFixture(new Circle(0.5), 10.0); -world.createDynamicBody(new Vec2(0.0, 3.5)).createFixture(new Circle(0.5), 10.0); +world.createDynamicBody({ x: 0.0, y: 3.5 }).createFixture(new Circle(0.5), 10.0); let heavy: Body | null = null; @@ -24,7 +24,7 @@ function toggleHeavy() { world.destroyBody(heavy); heavy = null; } else { - heavy = world.createDynamicBody(new Vec2(0.0, 9.0)); + heavy = world.createDynamicBody({ x: 0.0, y: 9.0 }); heavy.createFixture(new Circle(5.0), 10.0); } } diff --git a/example/Mixer.ts b/example/Mixer.ts index 9d6424ec..7602eb74 100644 --- a/example/Mixer.ts +++ b/example/Mixer.ts @@ -1,34 +1,45 @@ -import { Vec2, World, Edge, Circle, Box, Chain, Math, Testbed } from "planck"; +import { World, Edge, Circle, Box, Chain, Math, 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 = 0; testbed.start(world); const container = world.createKinematicBody(); -container.createFixture(new Edge(new Vec2(15, -5), new Vec2(25, 5))); -container.createFixture(new Circle(new Vec2(-10, -10), 3)); -container.createFixture(new Circle(new Vec2(10, 10), 3)); -container.createFixture(new Box(3, 3, new Vec2(-10, 10))); -container.createFixture(new Box(3, 3, new Vec2(10, -10))); +container.createFixture(new Edge({ x: 15, y: -5 }, { x: 25, y: 5 })); +container.createFixture(new Circle({ x: -10, y: -10 }, 3)); +container.createFixture(new Circle({ x: 10, y: 10 }, 3)); +container.createFixture(new Box(3, 3, { x: -10, y: 10 })); +container.createFixture(new Box(3, 3, { x: 10, y: -10 })); container.createFixture( - new Chain([new Vec2(-20, -20), new Vec2(20, -20), new Vec2(20, 20), new Vec2(-20, 20)], true), + new Chain( + [ + { x: -20, y: -20 }, + { x: 20, y: -20 }, + { x: 20, y: 20 }, + { x: -20, y: 20 }, + ], + true, + ), ); const n = 15; for (let i = -n; i <= n; i++) { for (let j = -n; j <= n; j++) { - const particle = world.createDynamicBody(new Vec2(i * 1, j * 1)); + const particle = world.createDynamicBody({ x: i * 1, y: j * 1 }); particle.createFixture(Math.random() > 0.5 ? new Circle(0.4) : new Box(0.4, 0.4)); particle.setMassData({ mass: 2, - center: new Vec2(), + center: { x: 0, y: 0 }, I: 0.4, }); - particle.applyForceToCenter(new Vec2(Math.random(-100, 100), Math.random(-100, 100))); + particle.applyForceToCenter({ + x: Math.random(-100, 100), + y: Math.random(-100, 100), + }); } } diff --git a/example/Mobile.ts b/example/Mobile.ts index 3f5bf138..fd779914 100644 --- a/example/Mobile.ts +++ b/example/Mobile.ts @@ -5,7 +5,7 @@ import { Vec2, World, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -1)); +const world = new World({ x: 0, y: -1 }); const testbed = Testbed.mount(); testbed.y = -15; @@ -16,24 +16,24 @@ testbed.start(world); const DEPTH = 4; const DENSITY = 20.0; -const ground = world.createBody(new Vec2(0.0, 20.0)); +const ground = world.createBody({ x: 0.0, y: 20.0 }); const a = 0.5; -const h = new Vec2(0.0, a); +const h = { x: 0.0, y: a }; -const root = addNode(ground, new Vec2(), 0, 3.0, a); +const root = addNode(ground, { x: 0, y: 0 }, 0, 3.0, a); world.createJoint( new RevoluteJoint({ bodyA: ground, bodyB: root, - localAnchorA: new Vec2(0, 0), + localAnchorA: { x: 0, y: 0 }, localAnchorB: h, }), ); function addNode(parent, localAnchor, depth, offset, a) { - const h = new Vec2(0.0, a); + const h = { x: 0.0, y: a }; const node = world.createBody({ type: "dynamic", @@ -46,8 +46,8 @@ function addNode(parent, localAnchor, depth, offset, a) { return node; } - const left = new Vec2(offset, -a); - const right = new Vec2(-offset, -a); + const left = { x: offset, y: -a }; + const right = { x: -offset, y: -a }; const leftChild = addNode(node, left, depth + 1, 0.5 * offset, a); const rightChild = addNode(node, right, depth + 1, 0.5 * offset, a); diff --git a/example/MobileBalanced.ts b/example/MobileBalanced.ts index 842c7ffc..e7855814 100644 --- a/example/MobileBalanced.ts +++ b/example/MobileBalanced.ts @@ -5,7 +5,7 @@ import { World, Vec2, Box, RevoluteJoint, 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; @@ -16,24 +16,24 @@ testbed.start(world); const DEPTH = 4; const DENSITY = 20.0; -const ground = world.createBody(new Vec2(0.0, 20.0)); +const ground = world.createBody({ x: 0.0, y: 20.0 }); const a = 0.5; -const h = new Vec2(0.0, a); +const h = { x: 0.0, y: a }; -const root = addNode(ground, new Vec2(), 0, 3.0, a); +const root = addNode(ground, { x: 0, y: 0 }, 0, 3.0, a); world.createJoint( new RevoluteJoint({ bodyA: ground, bodyB: root, - localAnchorA: new Vec2(), + localAnchorA: { x: 0, y: 0 }, localAnchorB: h, }), ); function addNode(parent, localAnchor, depth, offset, a) { - const h = new Vec2(0.0, a); + const h = { x: 0.0, y: a }; const p = new Vec2(parent.getPosition()).add(localAnchor).sub(h); @@ -45,10 +45,10 @@ function addNode(parent, localAnchor, depth, offset, a) { return node; } - node.createFixture(new Box(offset, 0.25 * a, new Vec2(0, -a), 0.0), DENSITY); + node.createFixture(new Box(offset, 0.25 * a, { x: 0, y: -a }, 0.0), DENSITY); - const right = new Vec2(offset, -a); - const left = new Vec2(-offset, -a); + const right = { x: offset, y: -a }; + const left = { x: -offset, y: -a }; const rightChild = addNode(node, right, depth + 1, 0.5 * offset, a); const leftChild = addNode(node, left, depth + 1, 0.5 * offset, a); diff --git a/example/MotorJoint.ts b/example/MotorJoint.ts index 587420d4..2b02bb76 100644 --- a/example/MotorJoint.ts +++ b/example/MotorJoint.ts @@ -7,9 +7,9 @@ // can be used to animate a dynamic body. With finite motor forces // the body can be blocked by collision with other bodies. -import { Vec2, World, MotorJoint, Box, Edge, Testbed } from "planck"; +import { World, MotorJoint, Box, Edge, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -17,10 +17,10 @@ testbed.start(world); let time = 0; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0))); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 })); // Define motorized body -const body = world.createDynamicBody(new Vec2(0.0, 8.0)); +const body = world.createDynamicBody({ x: 0.0, y: 8.0 }); body.createFixture(new Box(2.0, 0.5), { friction: 0.6, density: 2.0, @@ -40,6 +40,9 @@ const joint = world.createJoint( testbed.step = function (dt) { time += Math.min(dt, 100) / 1000; - joint?.setLinearOffset(new Vec2(6.0 * Math.sin(2.0 * time), 8.0 + 4.0 * Math.sin(1.0 * time))); + joint?.setLinearOffset({ + x: 6.0 * Math.sin(2.0 * time), + y: 8.0 + 4.0 * Math.sin(1.0 * time), + }); joint?.setAngularOffset(4.0 * time); }; diff --git a/example/OneSidedPlatform.ts b/example/OneSidedPlatform.ts index 34704e09..68d4a191 100644 --- a/example/OneSidedPlatform.ts +++ b/example/OneSidedPlatform.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Box, Circle, Testbed } from "planck"; +import { World, Edge, Box, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -22,16 +22,16 @@ const state = UNKNOWN; // Ground const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-20.0, 0.0), new Vec2(20.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -20.0, y: 0.0 }, { x: 20.0, y: 0.0 }), 0.0); // Platform -const platform = world.createBody(new Vec2(0.0, 10.0)); +const platform = world.createBody({ x: 0.0, y: 10.0 }); const platformFix = platform.createFixture(new Box(3.0, 0.5), 0.0); // Actor -const character = world.createDynamicBody(new Vec2(0.0, 12.0)); +const character = world.createDynamicBody({ x: 0.0, y: 12.0 }); const characterFix = character.createFixture(new Circle(radius), 20.0); -character.setLinearVelocity(new Vec2(0.0, -50.0)); +character.setLinearVelocity({ x: 0.0, y: -50.0 }); world.on("pre-solve", function (contact, oldManifold) { const fixA = contact.getFixtureA(); diff --git a/example/Pinball.ts b/example/Pinball.ts index 9d2e835b..fe6fe59d 100644 --- a/example/Pinball.ts +++ b/example/Pinball.ts @@ -6,9 +6,9 @@ // This tests bullet collision and provides an example of a gameplay scenario. // This also uses a loop shape. -import { World, Vec2, Circle, Box, Chain, RevoluteJoint, Testbed } from "planck"; +import { World, Circle, Box, Chain, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -18,11 +18,11 @@ const ground = world.createBody(); ground.createFixture( new Chain( [ - new Vec2(0.0, -2.0), - new Vec2(8.0, 6.0), - new Vec2(8.0, 20.0), - new Vec2(-8.0, 20.0), - new Vec2(-8.0, 6.0), + { x: 0.0, y: -2.0 }, + { x: 8.0, y: 6.0 }, + { x: 8.0, y: 20.0 }, + { x: -8.0, y: 20.0 }, + { x: -8.0, y: 6.0 }, ], true, ), @@ -30,11 +30,11 @@ ground.createFixture( ); // Flippers -const pLeft = new Vec2(-2.0, 0.0); -const pRight = new Vec2(2.0, 0.0); +const pLeft = { x: -2.0, y: 0.0 }; +const pRight = { x: 2.0, y: 0.0 }; -const leftFlipper = world.createDynamicBody(new Vec2(-2.0, 0.0)); -const rightFlipper = world.createDynamicBody(new Vec2(2.0, 0.0)); +const leftFlipper = world.createDynamicBody({ x: -2.0, y: 0.0 }); +const rightFlipper = world.createDynamicBody({ x: 2.0, y: 0.0 }); leftFlipper.createFixture(new Box(1.75, 0.1), 1.0); rightFlipper.createFixture(new Box(1.75, 0.1), 1.0); @@ -72,7 +72,7 @@ world.createJoint(rightJoint); // Circle character const ball = world.createBody({ - position: new Vec2(1.0, 15.0), + position: { x: 1.0, y: 15.0 }, type: "dynamic", bullet: true, }); diff --git a/example/PolyCollision.ts b/example/PolyCollision.ts index 75b345b6..dbf41644 100644 --- a/example/PolyCollision.ts +++ b/example/PolyCollision.ts @@ -3,19 +3,19 @@ * Licensed under the MIT license */ -import { World, Vec2, Transform, Manifold, CollidePolygons, Box, Testbed } from "planck"; +import { World, Transform, Manifold, CollidePolygons, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.info("Use arrow keys to move and Z or X to rotate."); testbed.start(world); const polygonA = new Box(2, 4); -const transformA = new Transform(new Vec2(0.0, 0.0), 0.0); +const transformA = new Transform({ x: 0.0, y: 0.0 }, 0.0); const polygonB = new Box(5, 5); -const positionB = new Vec2(5, 4); +const positionB = { x: 5, y: 4 }; let angleB = 1.9160721; const transformB = new Transform(positionB, angleB); diff --git a/example/PolyShapes.ts b/example/PolyShapes.ts index 4fae7ada..8f9fdcb8 100644 --- a/example/PolyShapes.ts +++ b/example/PolyShapes.ts @@ -4,7 +4,6 @@ */ import { - Vec2, Transform, AABB, CircleShape, @@ -27,7 +26,7 @@ import { // overlap a circle. Up to 4 overlapped fixtures will be highlighted with a // yellow border. -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -38,11 +37,19 @@ const bodies: Body[] = []; const shapes: Shape[] = []; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -shapes[0] = new Polygon([new Vec2(-0.5, 0.0), new Vec2(0.5, 0.0), new Vec2(0.0, 1.5)]); +shapes[0] = new Polygon([ + { x: -0.5, y: 0.0 }, + { x: 0.5, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); -shapes[1] = new Polygon([new Vec2(-0.1, 0.0), new Vec2(0.1, 0.0), new Vec2(0.0, 1.5)]); +shapes[1] = new Polygon([ + { x: -0.1, y: 0.0 }, + { x: 0.1, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); { const w = 1.0; @@ -50,14 +57,14 @@ shapes[1] = new Polygon([new Vec2(-0.1, 0.0), new Vec2(0.1, 0.0), new Vec2(0.0, const s = Math.sqrt(2.0) * b; shapes[2] = new Polygon([ - new Vec2(0.5 * s, 0.0), - new Vec2(0.5 * w, b), - new Vec2(0.5 * w, b + s), - new Vec2(0.5 * s, w), - new Vec2(-0.5 * s, w), - new Vec2(-0.5 * w, b + s), - new Vec2(-0.5 * w, b), - new Vec2(-0.5 * s, 0.0), + { x: 0.5 * s, y: 0.0 }, + { x: 0.5 * w, y: b }, + { x: 0.5 * w, y: b + s }, + { x: 0.5 * s, y: w }, + { x: -0.5 * s, y: w }, + { x: -0.5 * w, y: b + s }, + { x: -0.5 * w, y: b }, + { x: -0.5 * s, y: 0.0 }, ]); } @@ -72,7 +79,7 @@ function createBody(index: number) { const bd: BodyDef = { type: "dynamic", - position: new Vec2(Math.random() * 0.4 - 2.0, 10.0), + position: { x: Math.random() * 0.4 - 2.0, y: 10.0 }, angle: Math.random() * 2 * Math.PI - Math.PI, }; @@ -132,7 +139,7 @@ testbed.keydown = function (code, char) { testbed.info("1-5: Drop new objects, Z: Activate/deactivate some bodies, X: Destroy an object"); const aabb = new AABB(); -const circle = new CircleShape(new Vec2(0.0, 1.1), 2.0); +const circle = new CircleShape({ x: 0.0, y: 1.1 }, 2.0); const transform = new Transform(); let count = 0; diff --git a/example/Prismatic.ts b/example/Prismatic.ts index 5422f860..96eaeff4 100644 --- a/example/Prismatic.ts +++ b/example/Prismatic.ts @@ -7,7 +7,7 @@ import { World, Vec2, PrismaticJoint, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -15,19 +15,18 @@ testbed.start(world); const MOTOR_SPEED = 10; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const body = world.createBody({ type: "dynamic", - position: new Vec2(-10.0, 10.0), + position: { x: -10.0, y: 10.0 }, angle: 0.5 * Math.PI, allowSleep: false, }); body.createFixture(new Box(2.0, 0.5), 5.0); // Bouncy limit -const axis = new Vec2(2.0, 1.0); -axis.normalize(); +const axis = Vec2.normalize({ x: 2.0, y: 1.0 }); const joint = new PrismaticJoint( { motorSpeed: MOTOR_SPEED, @@ -39,12 +38,12 @@ const joint = new PrismaticJoint( }, ground, body, - new Vec2(0.0, 0.0), + { x: 0.0, y: 0.0 }, axis, ); // Non-bouncy limit -// (ground, body, new Vec2(-10.0, 10.0), new Vec2(1.0, 0.0)); +// (ground, body, ({ x: -10.0, y: 10.0 }), ({ x: 1.0, y: 0.0 })); world.createJoint(joint); diff --git a/example/Pulleys.ts b/example/Pulleys.ts index 3c995ae8..525ddf58 100644 --- a/example/Pulleys.ts +++ b/example/Pulleys.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Circle, Box, PulleyJoint, Testbed } from "planck"; +import { World, Circle, Box, PulleyJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -17,24 +17,24 @@ const b = 2.0; const ground = world.createBody(); -// ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +// ground.createFixture(new Edge(({ x: -40.0, y: 0.0 }), ({ x: 40.0, y: 0.0 })), 0.0); -ground.createFixture(new Circle(new Vec2(-10.0, y + b + L), 2.0), 0.0); -ground.createFixture(new Circle(new Vec2(10.0, y + b + L), 2.0), 0.0); +ground.createFixture(new Circle({ x: -10.0, y: y + b + L }, 2.0), 0.0); +ground.createFixture(new Circle({ x: 10.0, y: y + b + L }, 2.0), 0.0); const shape = new Box(a, b); // bd.fixedRotation = true; -const box1 = world.createDynamicBody(new Vec2(-10.0, y)); +const box1 = world.createDynamicBody({ x: -10.0, y: y }); box1.createFixture(shape, 5.0); -const box2 = world.createDynamicBody(new Vec2(10.0, y)); +const box2 = world.createDynamicBody({ x: 10.0, y: y }); box2.createFixture(shape, 5.0); -const anchor1 = new Vec2(-10.0, y + b); -const anchor2 = new Vec2(10.0, y + b); -const groundAnchor1 = new Vec2(-10.0, y + b + L); -const groundAnchor2 = new Vec2(10.0, y + b + L); +const anchor1 = { x: -10.0, y: y + b }; +const anchor2 = { x: 10.0, y: y + b }; +const groundAnchor1 = { x: -10.0, y: y + b + L }; +const groundAnchor2 = { x: 10.0, y: y + b + L }; const joint1 = world.createJoint( new PulleyJoint({}, box1, box2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5), diff --git a/example/Pyramid.ts b/example/Pyramid.ts index 84fe1185..07cc7acd 100644 --- a/example/Pyramid.ts +++ b/example/Pyramid.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { Vec2, World, Edge, Box, Testbed } from "planck"; +import { World, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -13,24 +13,27 @@ testbed.start(world); const COUNT = 20; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const a = 0.5; const box = new Box(a, a); -const x = new Vec2(-7.0, 0.75); -const y = new Vec2(); -const deltaX = new Vec2(0.5625, 1.25); -const deltaY = new Vec2(1.125, 0.0); +const x = { x: -7.0, y: 0.75 }; +const y = { x: 0, y: 0 }; +const deltaX = { x: 0.5625, y: 1.25 }; +const deltaY = { x: 1.125, y: 0.0 }; for (let i = 0; i < COUNT; ++i) { - y.set(x); + y.x = x.x; + y.y = x.y; for (let j = i; j < COUNT; ++j) { world.createDynamicBody(y).createFixture(box, 5.0); - y.add(deltaY); + y.x += deltaY.x; + y.y += deltaY.y; } - x.add(deltaX); + x.x += deltaX.x; + x.y += deltaX.y; } testbed.step = function () { diff --git a/example/RayCast.ts b/example/RayCast.ts index 1d160fd2..0684f70a 100644 --- a/example/RayCast.ts +++ b/example/RayCast.ts @@ -139,7 +139,7 @@ function callbackMultiple( return 1.0; } -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.width = 40; @@ -160,27 +160,35 @@ const shapes: Shape[] = []; let angle = 0.0; let mode = CLOSEST; -shapes[0] = new Polygon([new Vec2(-0.5, 0.0), new Vec2(0.5, 0.0), new Vec2(0.0, 1.5)]); -shapes[1] = new Polygon([new Vec2(-0.1, 0.0), new Vec2(0.1, 0.0), new Vec2(0.0, 1.5)]); +shapes[0] = new Polygon([ + { x: -0.5, y: 0.0 }, + { x: 0.5, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); +shapes[1] = new Polygon([ + { x: -0.1, y: 0.0 }, + { x: 0.1, y: 0.0 }, + { x: 0.0, y: 1.5 }, +]); const w = 1.0; const b = w / (2.0 + Math.sqrt(2.0)); const s = Math.sqrt(2.0) * b; shapes[2] = new Polygon([ - new Vec2(0.5 * s, 0.0), - new Vec2(0.5 * w, b), - new Vec2(0.5 * w, b + s), - new Vec2(0.5 * s, w), - new Vec2(-0.5 * s, w), - new Vec2(-0.5 * w, b + s), - new Vec2(-0.5 * w, b), - new Vec2(-0.5 * s, 0.0), + { x: 0.5 * s, y: 0.0 }, + { x: 0.5 * w, y: b }, + { x: 0.5 * w, y: b + s }, + { x: 0.5 * s, y: w }, + { x: -0.5 * s, y: w }, + { x: -0.5 * w, y: b + s }, + { x: -0.5 * w, y: b }, + { x: -0.5 * s, y: 0.0 }, ]); shapes[3] = new Box(0.5, 0.5); shapes[4] = new Circle(0.5); -shapes[5] = new Edge(new Vec2(-1.0, 0.0), new Vec2(1.0, 0.0)); +shapes[5] = new Edge({ x: -1.0, y: 0.0 }, { x: 1.0, y: 0.0 }); function createBody(index: number) { if (bodies.length > MAX_BODIES) { @@ -191,7 +199,7 @@ function createBody(index: number) { const y = Math.random() * 20; const bd: BodyDef = {}; - bd.position = new Vec2(x, y); + bd.position = { x: x, y: y }; bd.angle = Math.random() * 2 * Math.PI - Math.PI; bd.userData = index; @@ -270,8 +278,8 @@ testbed.step = function () { const advanceRay = true; const L = 11.0; - const point1 = new Vec2(0.0, 10.0); - const d = new Vec2(L * Math.cos(angle), L * Math.sin(angle)); + const point1 = { x: 0.0, y: 10.0 }; + const d = { x: L * Math.cos(angle), y: L * Math.sin(angle) }; const point2 = Vec2.add(point1, d); if (mode === CLOSEST) { @@ -325,12 +333,12 @@ testbed.step = function () { const shape = new Box(22.875, 3.0); const input = { - p1: new Vec2(10.2725, 1.71372), - p2: new Vec2(10.2353, 2.21807), + p1: { x: 10.2725, y: 1.71372 }, + p2: { x: 10.2353, y: 2.21807 }, maxFraction: 0.56762173, }; - const xf = new Transform(new Vec2(23.0, 5.0)); + const xf = new Transform({ x: 23.0, y: 5.0 }); const output = {} as RayCastOutput; let hit = shape.rayCast(output, input, xf, 0); diff --git a/example/Revolute.ts b/example/Revolute.ts index 3d375fbb..3a4d6e7f 100644 --- a/example/Revolute.ts +++ b/example/Revolute.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { World, Vec2, Edge, Circle, Box, Polygon, RevoluteJoint, Testbed } from "planck"; +import { World, Edge, Circle, Box, Polygon, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -18,14 +18,14 @@ const groundFD = { filterMaskBits: 0xffff, filterGroupIndex: 0, }; -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), groundFD); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), groundFD); -const rotator = world.createDynamicBody(new Vec2(-10.0, 20.0)); +const rotator = world.createDynamicBody({ x: -10.0, y: 20.0 }); rotator.createFixture(new Circle(0.5), 5.0); const w = 100.0; rotator.setAngularVelocity(w); -rotator.setLinearVelocity(new Vec2(-8.0 * w, 0.0)); +rotator.setLinearVelocity({ x: -8.0 * w, y: 0.0 }); const joint = world.createJoint( new RevoluteJoint( @@ -40,22 +40,22 @@ const joint = world.createJoint( }, ground, rotator, - new Vec2(-10.0, 12.0), + { x: -10.0, y: 12.0 }, ), ); -const ball = world.createDynamicBody(new Vec2(5.0, 30.0)); +const ball = world.createDynamicBody({ x: 5.0, y: 30.0 }); ball.createFixture(new Circle(3.0), { density: 5.0, // filterMaskBits: 1, }); const platform = world.createBody({ - position: new Vec2(20.0, 10.0), + position: { x: 20.0, y: 10.0 }, type: "dynamic", bullet: true, }); -platform.createFixture(new Box(10.0, 0.2, new Vec2(-10.0, 0.0), 0.0), 2.0); +platform.createFixture(new Box(10.0, 0.2, { x: -10.0, y: 0.0 }, 0.0), 2.0); world.createJoint( new RevoluteJoint( @@ -66,7 +66,7 @@ world.createJoint( }, ground, platform, - new Vec2(20.0, 10.0), + { x: 20.0, y: 10.0 }, ), ); @@ -74,7 +74,11 @@ world.createJoint( const triangle = world.createDynamicBody(); triangle.createFixture( - new Polygon([new Vec2(17.63, 36.31), new Vec2(17.52, 36.69), new Vec2(17.19, 36.36)]), + new Polygon([ + { x: 17.63, y: 36.31 }, + { x: 17.52, y: 36.69 }, + { x: 17.19, y: 36.36 }, + ]), 1, ); // assertion hits inside here @@ -92,7 +96,7 @@ testbed.keydown = function (code, char) { testbed.step = function () { // if (stepCount++ == 360) { - // ball.setTransform(new Vec2(0.0, 0.5), 0.0); + // ball.setTransform(({ x: 0.0, y: 0.5 }), 0.0); // } testbed.status("Motor Torque", joint?.getMotorTorque(testbed.hz)); diff --git a/example/RopeJoint.ts b/example/RopeJoint.ts index e4fb7da1..d328890b 100644 --- a/example/RopeJoint.ts +++ b/example/RopeJoint.ts @@ -12,7 +12,7 @@ // This test also shows how to use contact filtering. Filtering is configured // so that the payload does not collide with the chain. -import { Vec2, World, Edge, Box, RevoluteJoint, RopeJoint, Testbed, BodyDef } from "planck"; +import { World, Edge, Box, RevoluteJoint, RopeJoint, Testbed, BodyDef } from "planck"; const world = new World({ x: 0, y: -10 }); @@ -22,7 +22,7 @@ testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const segmentDef = { density: 20.0, @@ -43,13 +43,13 @@ for (let i = 0; i < N; ++i) { let shape = new Box(0.5, 0.125); const bd: BodyDef = { type: "dynamic", - position: new Vec2(0.5 + 1.0 * i, y), + position: { x: 0.5 + 1.0 * i, y: y }, }; if (i === N - 1) { shape = new Box(1.5, 1.5); segmentDef.density = 100.0; segmentDef.filterCategoryBits = 0x0002; - bd.position = new Vec2(1.0 * i, y); + bd.position = { x: 1.0 * i, y: y }; bd.angularDamping = 0.4; } @@ -57,7 +57,7 @@ for (let i = 0; i < N; ++i) { body.createFixture(shape, segmentDef); - const anchor = new Vec2(i, y); + const anchor = { x: i, y: y }; world.createJoint(new RevoluteJoint(segmentJointDef, prevBody, body, anchor)); prevBody = body; @@ -65,8 +65,8 @@ for (let i = 0; i < N; ++i) { const ropeJointDef = { maxLength: N - 1.0 + 0.01, - localAnchorA: new Vec2(0.0, y), - localAnchorB: new Vec2(0, 0), + localAnchorA: { x: 0.0, y: y }, + localAnchorB: { x: 0, y: 0 }, }; let rope = world.createJoint(new RopeJoint(ropeJointDef, ground, prevBody)); diff --git a/example/SensorTest.ts b/example/SensorTest.ts index 0c071d49..624bd508 100644 --- a/example/SensorTest.ts +++ b/example/SensorTest.ts @@ -7,7 +7,7 @@ import { World, Body, Fixture, Vec2, CircleShape, Box, Edge, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -23,16 +23,16 @@ const bodies: Body[] = []; const touching: UserDate[] = []; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); if (0) { sensor = ground.createFixture({ - shape: new Box(10.0, 2.0, new Vec2(0.0, 20.0), 0.0), + shape: new Box(10.0, 2.0, { x: 0.0, y: 20.0 }, 0.0), isSensor: true, }); } else { sensor = ground.createFixture({ - shape: new CircleShape(new Vec2(0.0, 10.0), 5.0), + shape: new CircleShape({ x: 0.0, y: 10.0 }, 5.0), isSensor: true, }); } @@ -42,7 +42,7 @@ const circle = new CircleShape(1.0); for (let i = 0; i < COUNT; ++i) { touching[i] = { touching: false }; - bodies[i] = world.createDynamicBody(new Vec2(-10.0 + 3.0 * i, 20.0)); + bodies[i] = world.createDynamicBody({ x: -10.0 + 3.0 * i, y: 20.0 }); bodies[i].setUserData(touching[i]); bodies[i].createFixture(circle, 1.0); } diff --git a/example/ShapeCast.ts b/example/ShapeCast.ts index 09b3f872..f0f5c529 100644 --- a/example/ShapeCast.ts +++ b/example/ShapeCast.ts @@ -16,6 +16,7 @@ import { Distance, SimplexCache, Testbed, + Vec2Value, } from "planck"; const world = new World(); @@ -25,62 +26,73 @@ testbed.width = 40; testbed.height = 40; testbed.start(world); -const vAs = new Array(3).fill(null).map(() => Vec2.zero()); +const vAs = new Array(3).fill(null).map(() => ({ x: 0, y: 0 })); let countA: number; let radiusA: number; -const vBs = new Array(Settings.maxPolygonVertices).fill(null).map(() => Vec2.zero()); +const vBs = new Array(Settings.maxPolygonVertices).fill(null).map(() => ({ x: 0, y: 0 })); let countB: number; let radiusB: number; let transformA: Transform; let transformB: Transform; -let translationB: Vec2; +let translationB: Vec2Value; if (true) { - vAs[0].set(-0.5, 1.0); - vAs[1].set(0.5, 1.0); - vAs[2].set(0.0, 0.0); + vAs[0].x = -0.5; + vAs[0].y = 1.0; + vAs[1].x = 0.5; + vAs[1].y = 1.0; + vAs[2].x = 0.0; + vAs[2].y = 0.0; countA = 3; radiusA = Settings.polygonRadius; - - vBs[0].set(-0.5, -0.5); - vBs[1].set(0.5, -0.5); - vBs[2].set(0.5, 0.5); - vBs[3].set(-0.5, 0.5); + vBs[0].x = -0.5; + vBs[0].y = -0.5; + vBs[1].x = 0.5; + vBs[1].y = -0.5; + vBs[2].x = 0.5; + vBs[2].y = 0.5; + vBs[3].x = -0.5; + vBs[3].y = 0.5; countB = 4; radiusB = Settings.polygonRadius; - transformA = new Transform(new Vec2(4, 0.25)); - transformB = new Transform(new Vec2(-4, 0)); - translationB = new Vec2(8.0, 0.0); + transformA = new Transform({ x: 4, y: 0.25 }); + transformB = new Transform({ x: -4, y: 0 }); + translationB = { x: 8.0, y: 0.0 }; } else if (true) { - vAs[0].set(0.0, 0.0); + vAs[0].x = 0.0; + vAs[0].y = 0.0; countA = 1; radiusA = 0.5; - vBs[0].set(0.0, 0.0); + vBs[0].x = 0.0; + vBs[0].y = 0.0; countB = 1; radiusB = 0.5; - transformA = new Transform(new Vec2(0, 0.25)); - transformB = new Transform(new Vec2(-4, 0)); - translationB = new Vec2(8.0, 0.0); + transformA = new Transform({ x: 0, y: 0.25 }); + transformB = new Transform({ x: -4, y: 0 }); + translationB = { x: 8.0, y: 0.0 }; } else { - vAs[0].set(0.0, 0.0); - vAs[1].set(2.0, 0.0); + vAs[0].x = 0.0; + vAs[0].y = 0.0; + vAs[1].x = 2.0; + vAs[1].y = 0.0; countA = 2; radiusA = Settings.polygonRadius; - vBs[0].set(0.0, 0.0); + vBs[0].x = 0.0; + vBs[0].y = 0.0; countB = 1; radiusB = 0.25; // Initial overlap - transformA = new Transform(new Vec2(0, 0)); - transformB = new Transform(new Vec2(-0.244360745, 0.05999358)); + transformA = new Transform({ x: 0, y: 0 }); + transformB = new Transform({ x: -0.244360745, y: 0.05999358 }); transformB.q.setIdentity(); - translationB = new Vec2(0.0, 0.0399999991); + translationB = { x: 0.0, y: 0.0399999991 }; } testbed.step = function () { diff --git a/example/ShapeEditing.ts b/example/ShapeEditing.ts index a0e5d192..6a532363 100644 --- a/example/ShapeEditing.ts +++ b/example/ShapeEditing.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { World, Fixture, Vec2, Edge, Circle, Box, Testbed } from "planck"; +import { World, Fixture, Edge, Circle, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.info("C: Create a shape, X: Destroy a shape, Z: Sensor"); @@ -14,11 +14,11 @@ testbed.start(world); let sensor = true; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); -const body = world.createDynamicBody(new Vec2(0.0, 10.0)); +const body = world.createDynamicBody({ x: 0.0, y: 10.0 }); -const fixture1 = body.createFixture(new Box(4.0, 4.0, new Vec2(0.0, 0.0), 0.0), 10.0); +const fixture1 = body.createFixture(new Box(4.0, 4.0, { x: 0.0, y: 0.0 }, 0.0), 10.0); let fixture2: Fixture | null = null; @@ -26,7 +26,7 @@ testbed.keydown = function (code, char) { switch (char) { case "C": if (fixture2 == null) { - const shape = new Circle(new Vec2(0.5, -4.0), 3.0); + const shape = new Circle({ x: 0.5, y: -4.0 }, 3.0); fixture2 = body.createFixture(shape, 10.0); body.setAwake(true); fixture2.setSensor(sensor); diff --git a/example/Shuffle.ts b/example/Shuffle.ts index c348b19d..a325677c 100644 --- a/example/Shuffle.ts +++ b/example/Shuffle.ts @@ -1,4 +1,4 @@ -import { World, Vec2, Circle, Chain, Settings, Testbed } from "planck"; +import { World, Vec2Value, Circle, Chain, Settings, Testbed } from "planck"; const width = 10.0; const height = 10.0; @@ -19,10 +19,10 @@ testbed.mouseForce = -100; testbed.start(world); const walls = [ - new Vec2(-width * 0.5, -height * 0.5), - new Vec2(-width * 0.5, +height * 0.5), - new Vec2(+width * 0.5, +height * 0.5), - new Vec2(+width * 0.5, -height * 0.5), + { x: -width * 0.5, y: -height * 0.5 }, + { x: -width * 0.5, y: +height * 0.5 }, + { x: +width * 0.5, y: +height * 0.5 }, + { x: +width * 0.5, y: -height * 0.5 }, ]; const wallFixDef = { @@ -43,7 +43,7 @@ const ballBodyDef = { world.createBody().createFixture(new Chain(walls, true), wallFixDef); row(1, 8, BALL_R, BALL_D) - .map((v) => Vec2.add(v, new Vec2(height * 0.4, 0))) + .map((v) => ({ x: v.x + height * 0.4, y: v.y + 0 })) .forEach(function (p) { const ball = world.createDynamicBody(ballBodyDef); ball.setPosition(p); @@ -53,7 +53,7 @@ row(1, 8, BALL_R, BALL_D) }); row(1, 8, BALL_R, BALL_D) - .map((v) => Vec2.add(v, new Vec2(-height * 0.4, 0))) + .map((v) => ({ x: v.x + -height * 0.4, y: v.y + 0 })) .forEach(function (p) { const ball = world.createDynamicBody(ballBodyDef); ball.setPosition(p); @@ -89,15 +89,13 @@ world.on("post-solve", function (contact) { }); function row(n: number, m: number, r: number, l: number) { - const balls: Vec2[] = []; + const balls: Vec2Value[] = []; for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { - balls.push( - new Vec2( - i * l - (n - 1) * 0.5 * l + Math.random() * r * 0.02, - j * l - (m - 1) * 0.5 * l + Math.random() * r * 0.02, - ), - ); + balls.push({ + x: i * l - (n - 1) * 0.5 * l + Math.random() * r * 0.02, + y: j * l - (m - 1) * 0.5 * l + Math.random() * r * 0.02, + }); } } return balls; diff --git a/example/SliderCrank.ts b/example/SliderCrank.ts index c339e263..77abfa6e 100644 --- a/example/SliderCrank.ts +++ b/example/SliderCrank.ts @@ -5,19 +5,19 @@ // A motor driven slider crank with joint friction. -import { World, Vec2, RevoluteJoint, PrismaticJoint, Edge, Box, Testbed } from "planck"; +import { World, RevoluteJoint, PrismaticJoint, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); testbed.info("Z: Toggle friction, X: Toggle motor"); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); // Define crank. -const crank = world.createDynamicBody(new Vec2(0.0, 7.0)); +const crank = world.createDynamicBody({ x: 0.0, y: 7.0 }); crank.createFixture(new Box(0.5, 2.0), 2.0); const joint1 = world.createJoint( @@ -29,25 +29,25 @@ const joint1 = world.createJoint( }, ground, crank, - new Vec2(0.0, 5.0), + { x: 0.0, y: 5.0 }, ), ); // Define follower. -const follower = world.createDynamicBody(new Vec2(0.0, 13.0)); +const follower = world.createDynamicBody({ x: 0.0, y: 13.0 }); follower.createFixture(new Box(0.5, 4.0), 2.0); -world.createJoint(new RevoluteJoint({ enableMotor: false }, crank, follower, new Vec2(0.0, 9.0))); +world.createJoint(new RevoluteJoint({ enableMotor: false }, crank, follower, { x: 0.0, y: 9.0 })); // Define piston const piston = world.createBody({ type: "dynamic", fixedRotation: true, - position: new Vec2(0.0, 17.0), + position: { x: 0.0, y: 17.0 }, }); piston.createFixture(new Box(1.5, 1.5), 2.0); -world.createJoint(new RevoluteJoint({}, follower, piston, new Vec2(0.0, 17.0))); +world.createJoint(new RevoluteJoint({}, follower, piston, { x: 0.0, y: 17.0 })); const joint2 = world.createJoint( new PrismaticJoint( @@ -57,13 +57,13 @@ const joint2 = world.createJoint( }, ground, piston, - new Vec2(0.0, 17.0), - new Vec2(0.0, 1.0), + { x: 0.0, y: 17.0 }, + { x: 0.0, y: 1.0 }, ), ); // Create a payload -const payload = world.createDynamicBody(new Vec2(0.0, 23.0)); +const payload = world.createDynamicBody({ x: 0.0, y: 23.0 }); payload.createFixture(new Box(1.5, 1.5), 2.0); testbed.keydown = function (code, char) { diff --git a/example/Soccer.ts b/example/Soccer.ts index 680aa3f1..68f2df78 100644 --- a/example/Soccer.ts +++ b/example/Soccer.ts @@ -1,4 +1,4 @@ -import { World, Vec2, Circle, Chain, Settings, Testbed } from "planck"; +import { World, Circle, Chain, Settings, Testbed } from "planck"; const testbed = Testbed.mount(); @@ -19,7 +19,10 @@ Settings.velocityThreshold = 0; const world = new World(); testbed.start(world); -const goal = [new Vec2(0, -height * 0.2), new Vec2(0, +height * 0.2)]; +const goal = [ + { x: 0, y: -height * 0.2 }, + { x: 0, y: +height * 0.2 }, +]; const wallFixDef = { friction: 0, @@ -58,8 +61,21 @@ const playerBodyDef = { world.createBody().createFixture(new Chain(walls(), true), wallFixDef); -world.createBody(new Vec2(-width * 0.5 - BALL_R, 0)).createFixture(new Chain(goal), goalFixDef); -world.createBody(new Vec2(+width * 0.5 + BALL_R, 0)).createFixture(new Chain(goal), goalFixDef); +{ + // goal left + const body = world.createBody({ + position: { x: -width * 0.5 - BALL_R, y: 0 }, + }); + const fixture = body.createFixture(new Chain(goal), goalFixDef); +} + +{ + // goal right + const body = world.createBody({ + position: { x: +width * 0.5 + BALL_R, y: 0 }, + }); + const fixture = body.createFixture(new Chain(goal), goalFixDef); +} const ball = world.createDynamicBody(ballBodyDef); ball.createFixture(new Circle(BALL_R), ballFixDef); @@ -73,7 +89,7 @@ team().forEach(function (p) { }); team() - .map((v) => new Vec2(-v.x, v.y)) + .map((v) => ({ x: -v.x, y: v.y })) .forEach(function (p) { const player = world.createDynamicBody(playerBodyDef); player.setPosition(p); @@ -110,8 +126,8 @@ world.on("post-solve", function (contact) { // do not change world immediately setTimeout(function () { if (ball && goal) { - ball.setPosition(new Vec2(0, 0)); - ball.setLinearVelocity(new Vec2(0, 0)); + ball.setPosition({ x: 0, y: 0 }); + ball.setLinearVelocity({ x: 0, y: 0 }); // world.destroyBody(ball); } }, 1); @@ -119,33 +135,33 @@ world.on("post-solve", function (contact) { function team() { const positions = [ - new Vec2(-width * 0.45, 0), - new Vec2(-width * 0.3, -height * 0.2), - new Vec2(-width * 0.3, +height * 0.2), - new Vec2(-width * 0.1, -height * 0.1), - new Vec2(-width * 0.1, +height * 0.1), + { x: -width * 0.45, y: 0 }, + { x: -width * 0.3, y: -height * 0.2 }, + { x: -width * 0.3, y: +height * 0.2 }, + { x: -width * 0.1, y: -height * 0.1 }, + { x: -width * 0.1, y: +height * 0.1 }, ]; return positions; } function walls() { const chain = [ - new Vec2(-width * 0.5 + 0.2, -height * 0.5), - new Vec2(-width * 0.5, -height * 0.5 + 0.2), - new Vec2(-width * 0.5, -height * 0.2), - new Vec2(-width * 0.6, -height * 0.2), - new Vec2(-width * 0.6, +height * 0.2), - new Vec2(-width * 0.5, +height * 0.2), - new Vec2(-width * 0.5, +height * 0.5 - 0.2), - new Vec2(-width * 0.5 + 0.2, +height * 0.5), - new Vec2(+width * 0.5 - 0.2, +height * 0.5), - new Vec2(+width * 0.5, +height * 0.5 - 0.2), - new Vec2(+width * 0.5, +height * 0.2), - new Vec2(+width * 0.6, +height * 0.2), - new Vec2(+width * 0.6, -height * 0.2), - new Vec2(+width * 0.5, -height * 0.2), - new Vec2(+width * 0.5, -height * 0.5 + 0.2), - new Vec2(+width * 0.5 - 0.2, -height * 0.5), + { x: -width * 0.5 + 0.2, y: -height * 0.5 }, + { x: -width * 0.5, y: -height * 0.5 + 0.2 }, + { x: -width * 0.5, y: -height * 0.2 }, + { x: -width * 0.6, y: -height * 0.2 }, + { x: -width * 0.6, y: +height * 0.2 }, + { x: -width * 0.5, y: +height * 0.2 }, + { x: -width * 0.5, y: +height * 0.5 - 0.2 }, + { x: -width * 0.5 + 0.2, y: +height * 0.5 }, + { x: +width * 0.5 - 0.2, y: +height * 0.5 }, + { x: +width * 0.5, y: +height * 0.5 - 0.2 }, + { x: +width * 0.5, y: +height * 0.2 }, + { x: +width * 0.6, y: +height * 0.2 }, + { x: +width * 0.6, y: -height * 0.2 }, + { x: +width * 0.5, y: -height * 0.2 }, + { x: +width * 0.5, y: -height * 0.5 + 0.2 }, + { x: +width * 0.5 - 0.2, y: -height * 0.5 }, ]; return chain; } diff --git a/example/SphereStack.ts b/example/SphereStack.ts index 80411437..9a8b3ad0 100644 --- a/example/SphereStack.ts +++ b/example/SphereStack.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { World, Body, Vec2, Edge, Circle, Testbed } from "planck"; +import { World, Body, Edge, Circle, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -14,12 +14,12 @@ const COUNT = 10; const bodies: Body[] = []; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0)), 0.0); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 }), 0.0); const circle = new Circle(1.0); for (let i = 0; i < COUNT; ++i) { - bodies[i] = world.createDynamicBody(new Vec2(0.0, 4.0 + 3.0 * i)); + bodies[i] = world.createDynamicBody({ x: 0.0, y: 4.0 + 3.0 * i }); bodies[i].createFixture(circle, 1.0); - bodies[i].setLinearVelocity(new Vec2(0.0, -50.0)); + bodies[i].setLinearVelocity({ x: 0.0, y: -50.0 }); } diff --git a/example/TheoJansen.ts b/example/TheoJansen.ts index 451c70a8..8aa45c8c 100644 --- a/example/TheoJansen.ts +++ b/example/TheoJansen.ts @@ -16,9 +16,10 @@ import { RevoluteJoint, DistanceJoint, Testbed, + Vec2Value, } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -26,18 +27,18 @@ testbed.start(world); const motorSpeed = 2.0; const motorOn = true; -const offset = new Vec2(0.0, 8.0); -const pivot = new Vec2(0.0, 0.8); +const offset = { x: 0.0, y: 8.0 }; +const pivot = { x: 0.0, y: 0.8 }; // Ground const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-50.0, 0.0), new Vec2(50.0, 0.0)), 0.0); -ground.createFixture(new Edge(new Vec2(-50.0, 0.0), new Vec2(-50.0, 10.0)), 0.0); -ground.createFixture(new Edge(new Vec2(50.0, 0.0), new Vec2(50.0, 10.0)), 0.0); +ground.createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: 50.0, y: 0.0 }), 0.0); +ground.createFixture(new Edge({ x: -50.0, y: 0.0 }, { x: -50.0, y: 10.0 }), 0.0); +ground.createFixture(new Edge({ x: 50.0, y: 0.0 }, { x: 50.0, y: 10.0 }), 0.0); // Balls for (let i = 0; i < 40; ++i) { - world.createDynamicBody(new Vec2(-40.0 + 2.0 * i, 0.5)).createFixture(new Circle(0.25), 1.0); + world.createDynamicBody({ x: -40.0 + 2.0 * i, y: 0.5 }).createFixture(new Circle(0.25), 1.0); } // Chassis @@ -67,7 +68,7 @@ const motorJoint = world.createJoint( ), ); -const wheelAnchor = new Vec2(0.0, -0.8).add(pivot); +const wheelAnchor = { x: 0.0 + pivot.x, y: -0.8 + pivot.y }; createLeg(-1.0, wheelAnchor); createLeg(1.0, wheelAnchor); @@ -80,22 +81,22 @@ wheel.setTransform(wheel.getPosition(), (-120.0 * Math.PI) / 180.0); createLeg(-1.0, wheelAnchor); createLeg(1.0, wheelAnchor); -function createLeg(s: number, wheelAnchor: Vec2) { - const p1 = new Vec2(5.4 * s, -6.1); - const p2 = new Vec2(7.2 * s, -1.2); - const p3 = new Vec2(4.3 * s, -1.9); - const p4 = new Vec2(3.1 * s, 0.8); - const p5 = new Vec2(6.0 * s, 1.5); - const p6 = new Vec2(2.5 * s, 3.7); +function createLeg(s: number, wheelAnchor: Vec2Value) { + const p1 = { x: 5.4 * s, y: -6.1 }; + const p2 = { x: 7.2 * s, y: -1.2 }; + const p3 = { x: 4.3 * s, y: -1.9 }; + const p4 = { x: 3.1 * s, y: 0.8 }; + const p5 = { x: 6.0 * s, y: 1.5 }; + const p6 = { x: 2.5 * s, y: 3.7 }; let poly1: PolygonShape; let poly2: PolygonShape; if (s > 0.0) { poly1 = new PolygonShape([p1, p2, p3]); - poly2 = new PolygonShape([new Vec2(), Vec2.sub(p5, p4), Vec2.sub(p6, p4)]); + poly2 = new PolygonShape([{ x: 0, y: 0 }, Vec2.sub(p5, p4), Vec2.sub(p6, p4)]); } else { poly1 = new PolygonShape([p1, p3, p2]); - poly2 = new PolygonShape([new Vec2(), Vec2.sub(p6, p4), Vec2.sub(p5, p4)]); + poly2 = new PolygonShape([{ x: 0, y: 0 }, Vec2.sub(p6, p4), Vec2.sub(p5, p4)]); } const body1 = world.createDynamicBody({ diff --git a/example/Tiles.ts b/example/Tiles.ts index f4545486..acbb914e 100644 --- a/example/Tiles.ts +++ b/example/Tiles.ts @@ -6,9 +6,9 @@ // This stress tests the dynamic tree broad-phase. This also shows that tile // based collision is _not_ smooth due to Box2D not knowing about adjacency. -import { World, Vec2, Box, Testbed } from "planck"; +import { World, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -19,12 +19,12 @@ let fixtureCount = 0; { const a = 0.5; - const ground = world.createBody(new Vec2(0, -a)); + const ground = world.createBody({ x: 0, y: -a }); if (true) { const N = 200; const M = 10; - const position = new Vec2(); + const position = { x: 0, y: 0 }; position.y = 0.0; for (let j = 0; j < M; ++j) { position.x = -N * a; @@ -38,7 +38,7 @@ let fixtureCount = 0; } else { const N = 200; const M = 10; - const position = new Vec2(); + const position = { x: 0, y: 0 }; position.x = -N * a; for (let i = 0; i < N; ++i) { position.y = 0.0; @@ -54,13 +54,14 @@ let fixtureCount = 0; const a = 0.5; const shape = new Box(a, a); - const x = new Vec2(-7.0, 0.75); - const y = new Vec2(); - const deltaX = new Vec2(0.5625, 1.25); - const deltaY = new Vec2(1.125, 0.0); + const x = { x: -7.0, y: 0.75 }; + const y = { x: 0, y: 0 }; + const deltaX = { x: 0.5625, y: 1.25 }; + const deltaY = { x: 1.125, y: 0.0 }; for (let i = 0; i < COUNT; ++i) { - y.set(x); + y.x = x.x; + y.y = x.y; for (let j = i; j < COUNT; ++j) { // bd.allowSleep = !(i == 0 && j == 0) @@ -68,10 +69,12 @@ let fixtureCount = 0; const body = world.createDynamicBody(y); body.createFixture(shape, 5.0); ++fixtureCount; - y.add(deltaY); + y.x += deltaY.x; + y.y += deltaY.y; } - x.add(deltaX); + x.x += deltaX.x; + x.y += deltaX.y; } } const createTime = Date.now(); diff --git a/example/Tumbler.ts b/example/Tumbler.ts index 81af2dfc..5795902c 100644 --- a/example/Tumbler.ts +++ b/example/Tumbler.ts @@ -3,9 +3,9 @@ * Licensed under the MIT license */ -import { World, Vec2, Box, RevoluteJoint, Testbed } from "planck"; +import { World, Box, RevoluteJoint, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); @@ -16,13 +16,13 @@ const ground = world.createBody(); const container = world.createDynamicBody({ allowSleep: false, - position: new Vec2(0, 10), + position: { x: 0, y: 10 }, }); -container.createFixture(new Box(0.5, 20, new Vec2(20, 0), 0), 5); -container.createFixture(new Box(0.5, 20, new Vec2(-20, 0), 0), 5); -container.createFixture(new Box(20, 0.5, new Vec2(0, 20), 0), 5); -container.createFixture(new Box(20, 0.5, new Vec2(0, -20), 0), 5); +container.createFixture(new Box(0.5, 20, { x: 20, y: 0 }, 0), 5); +container.createFixture(new Box(0.5, 20, { x: -20, y: 0 }, 0), 5); +container.createFixture(new Box(20, 0.5, { x: 0, y: 20 }, 0), 5); +container.createFixture(new Box(20, 0.5, { x: 0, y: -20 }, 0), 5); world.createJoint( new RevoluteJoint( @@ -33,7 +33,7 @@ world.createJoint( }, ground, container, - new Vec2(0, 10), + { x: 0, y: 10 }, ), ); @@ -41,7 +41,10 @@ const shape = new Box(0.5, 0.5); let count = 0; while (count < COUNT) { const body = world.createDynamicBody(); - body.setPosition(new Vec2(Math.random() * 20 - 10, 10 + Math.random() * 20 - 10)); + body.setPosition({ + x: Math.random() * 20 - 10, + y: 10 + Math.random() * 20 - 10, + }); body.createFixture(shape, 1); ++count; } diff --git a/example/VaryingFriction.ts b/example/VaryingFriction.ts index c8ee8773..4ce82593 100644 --- a/example/VaryingFriction.ts +++ b/example/VaryingFriction.ts @@ -3,31 +3,31 @@ * Licensed under the MIT license */ -import { World, Vec2, Edge, Box, Testbed } from "planck"; +import { World, Edge, Box, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); -world.createBody().createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0))); +world.createBody().createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); -world.createBody(new Vec2(-4.0, 22.0), -0.25).createFixture(new Box(13.0, 0.25), 0.0); +world.createBody({ x: -4.0, y: 22.0 }, -0.25).createFixture(new Box(13.0, 0.25), 0.0); -world.createBody(new Vec2(10.5, 19.0)).createFixture(new Box(0.25, 1.0), 0.0); +world.createBody({ x: 10.5, y: 19.0 }).createFixture(new Box(0.25, 1.0), 0.0); -world.createBody(new Vec2(4.0, 14.0), 0.25).createFixture(new Box(13.0, 0.25), 0.0); +world.createBody({ x: 4.0, y: 14.0 }, 0.25).createFixture(new Box(13.0, 0.25), 0.0); -world.createBody(new Vec2(-10.5, 11.0)).createFixture(new Box(0.25, 1.0), 0.0); +world.createBody({ x: -10.5, y: 11.0 }).createFixture(new Box(0.25, 1.0), 0.0); -world.createBody(new Vec2(-4.0, 6.0), -0.25).createFixture(new Box(13.0, 0.25), 0.0); +world.createBody({ x: -4.0, y: 6.0 }, -0.25).createFixture(new Box(13.0, 0.25), 0.0); const friction = [0.75, 0.5, 0.35, 0.1, 0.0]; const circle = new Box(0.5, 0.5); for (let i = 0; i < friction.length; ++i) { - const ball = world.createDynamicBody(new Vec2(-15.0 + 4.0 * i, 28.0)); + const ball = world.createDynamicBody({ x: -15.0 + 4.0 * i, y: 28.0 }); ball.createFixture(circle, { density: 25.0, friction: friction[i], diff --git a/example/VaryingRestitution.ts b/example/VaryingRestitution.ts index d41e46cc..5c55d1e7 100644 --- a/example/VaryingRestitution.ts +++ b/example/VaryingRestitution.ts @@ -6,22 +6,22 @@ // Note: even with a restitution of 1.0, there is some energy change // due to position correction. -import { World, Vec2, Circle, Edge, Testbed } from "planck"; +import { World, Circle, Edge, Testbed } from "planck"; -const world = new World(new Vec2(0, -10)); +const world = new World({ x: 0, y: -10 }); const testbed = Testbed.mount(); testbed.start(world); const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0))); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); const restitution = [0.0, 0.1, 0.3, 0.5, 0.75, 0.9, 1.0]; const circle = new Circle(1.0); for (let i = 0; i < restitution.length; ++i) { - const ball = world.createDynamicBody(new Vec2(-10.0 + 3.0 * i, 20.0)); + const ball = world.createDynamicBody({ x: -10.0 + 3.0 * i, y: 20.0 }); ball.createFixture(circle, { density: 1.0, restitution: restitution[i], diff --git a/example/VerticalStack.ts b/example/VerticalStack.ts index fde4e409..dc62755f 100644 --- a/example/VerticalStack.ts +++ b/example/VerticalStack.ts @@ -3,10 +3,10 @@ * Licensed under the MIT license */ -import { World, Body, Vec2, Edge, Circle, Box, Testbed } from "planck"; +import { World, Body, Edge, Circle, Box, Testbed } from "planck"; const world = new World({ - gravity: new Vec2(0, -10), + gravity: { x: 0, y: -10 }, blockSolve: true, }); @@ -22,8 +22,8 @@ const bodies: Body[] = []; const indices: number[] = []; const ground = world.createBody(); -ground.createFixture(new Edge(new Vec2(-40.0, 0.0), new Vec2(40.0, 0.0))); -ground.createFixture(new Edge(new Vec2(20.0, 0.0), new Vec2(20.0, 20.0))); +ground.createFixture(new Edge({ x: -40.0, y: 0.0 }, { x: 40.0, y: 0.0 })); +ground.createFixture(new Edge({ x: 20.0, y: 0.0 }, { x: 20.0, y: 20.0 })); const xs = [0.0, -10.0, -5.0, 5.0, 10.0]; @@ -39,7 +39,7 @@ for (let j = 0; j < columnCount; ++j) { const body = world.createDynamicBody(); body.setUserData(indices[n]); - body.setPosition(new Vec2(xs[j] + x, 0.55 + 1.1 * i)); + body.setPosition({ x: xs[j] + x, y: 0.55 + 1.1 * i }); body.createFixture(shape, { density: 1.0, friction: 0.3, @@ -60,7 +60,7 @@ testbed.keydown = function (code, char) { bullet = world.createBody({ type: "dynamic", bullet: true, - position: new Vec2(-31.0, 5.0), + position: { x: -31.0, y: 5.0 }, }); bullet.createFixture({ @@ -69,7 +69,7 @@ testbed.keydown = function (code, char) { restitution: 0.05, }); - bullet.setLinearVelocity(new Vec2(400.0, 0.0)); + bullet.setLinearVelocity({ x: 400.0, y: 0.0 }); break; case "Z": @@ -93,7 +93,7 @@ testbed.step = function () { bullet = world.createBody({ type: "dynamic", bullet: true, - position: new Vec2(-31.0, 5.0), + position: { x: -31.0, y: 5.0 }, }); bullet.createFixture({ shape: new Circle(0.25), @@ -101,6 +101,9 @@ testbed.step = function () { restitution: 0.05, }); - bullet.setLinearVelocity(new Vec2(400.0, Math.random() * 100 - 50)); + bullet.setLinearVelocity({ + x: 400.0, + y: Math.random() * 100 - 50, + }); } }; diff --git a/example/Web.ts b/example/Web.ts index 0e816c24..158a09f2 100644 --- a/example/Web.ts +++ b/example/Web.ts @@ -5,7 +5,7 @@ // This tests distance joints, body destruction, and joint destruction. -import { World, Body, Joint, Vec2, Box, DistanceJoint, Testbed } from "planck"; +import { World, Body, Joint, Box, DistanceJoint, Testbed } from "planck"; const world = new World(); @@ -19,16 +19,16 @@ let joints: Joint[] = []; const box = new Box(0.5, 0.5); -bodies[0] = world.createDynamicBody(new Vec2(-5.0, 5.0)); +bodies[0] = world.createDynamicBody({ x: -5.0, y: 5.0 }); bodies[0].createFixture(box, 5.0); -bodies[1] = world.createDynamicBody(new Vec2(5.0, 5.0)); +bodies[1] = world.createDynamicBody({ x: 5.0, y: 5.0 }); bodies[1].createFixture(box, 5.0); -bodies[2] = world.createDynamicBody(new Vec2(5.0, 15.0)); +bodies[2] = world.createDynamicBody({ x: 5.0, y: 15.0 }); bodies[2].createFixture(box, 5.0); -bodies[3] = world.createDynamicBody(new Vec2(-5.0, 15.0)); +bodies[3] = world.createDynamicBody({ x: -5.0, y: 15.0 }); bodies[3].createFixture(box, 5.0); const jd = { @@ -40,9 +40,9 @@ world.createJoint( (joints[0] = new DistanceJoint({ ...jd, bodyA: ground, - localAnchorA: new Vec2(-10.0, 0.0), + localAnchorA: { x: -10.0, y: 0.0 }, bodyB: bodies[0], - localAnchorB: new Vec2(-0.5, -0.5), + localAnchorB: { x: -0.5, y: -0.5 }, })), ); @@ -50,9 +50,9 @@ world.createJoint( (joints[1] = new DistanceJoint({ ...jd, bodyA: ground, - localAnchorA: new Vec2(10.0, 0.0), + localAnchorA: { x: 10.0, y: 0.0 }, bodyB: bodies[1], - localAnchorB: new Vec2(0.5, -0.5), + localAnchorB: { x: 0.5, y: -0.5 }, })), ); @@ -60,9 +60,9 @@ world.createJoint( (joints[2] = new DistanceJoint({ ...jd, bodyA: ground, - localAnchorA: new Vec2(10.0, 20.0), + localAnchorA: { x: 10.0, y: 20.0 }, bodyB: bodies[2], - localAnchorB: new Vec2(0.5, 0.5), + localAnchorB: { x: 0.5, y: 0.5 }, })), ); @@ -70,9 +70,9 @@ world.createJoint( (joints[3] = new DistanceJoint({ ...jd, bodyA: ground, - localAnchorA: new Vec2(-10.0, 20.0), + localAnchorA: { x: -10.0, y: 20.0 }, bodyB: bodies[3], - localAnchorB: new Vec2(-0.5, 0.5), + localAnchorB: { x: -0.5, y: 0.5 }, })), ); @@ -80,9 +80,9 @@ world.createJoint( (joints[4] = new DistanceJoint({ ...jd, bodyA: bodies[0], - localAnchorA: new Vec2(0.5, 0.0), + localAnchorA: { x: 0.5, y: 0.0 }, bodyB: bodies[1], - localAnchorB: new Vec2(-0.5, 0.0), + localAnchorB: { x: -0.5, y: 0.0 }, })), ); @@ -90,9 +90,9 @@ world.createJoint( (joints[5] = new DistanceJoint({ ...jd, bodyA: bodies[1], - localAnchorA: new Vec2(0.0, 0.5), + localAnchorA: { x: 0.0, y: 0.5 }, bodyB: bodies[2], - localAnchorB: new Vec2(0.0, -0.5), + localAnchorB: { x: 0.0, y: -0.5 }, })), ); @@ -100,9 +100,9 @@ world.createJoint( (joints[6] = new DistanceJoint({ ...jd, bodyA: bodies[2], - localAnchorA: new Vec2(-0.5, 0.0), + localAnchorA: { x: -0.5, y: 0.0 }, bodyB: bodies[3], - localAnchorB: new Vec2(0.5, 0.0), + localAnchorB: { x: 0.5, y: 0.0 }, })), ); @@ -110,9 +110,9 @@ world.createJoint( (joints[7] = new DistanceJoint({ ...jd, bodyA: bodies[3], - localAnchorA: new Vec2(0.0, -0.5), + localAnchorA: { x: 0.0, y: -0.5 }, bodyB: bodies[0], - localAnchorB: new Vec2(0.0, 0.5), + localAnchorB: { x: 0.0, y: 0.5 }, })), );