chore: 添加第三方依赖库
This commit is contained in:
89
thirdparty/rapier.js/testbed2d/src/demos/characterController.ts
vendored
Normal file
89
thirdparty/rapier.js/testbed2d/src/demos/characterController.ts
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(15.0, 0.1);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
// Dynamic cubes.
|
||||
let rad = 0.5;
|
||||
let num = 5;
|
||||
let i, j, k;
|
||||
let shift = rad * 2.5;
|
||||
let center = num * rad;
|
||||
let height = 5.0;
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
for (k = i; k < num; ++k) {
|
||||
let x = (i * shift) / 2.0 + (k - i) * shift - center;
|
||||
let y = (i * shift) / 2.0 + height;
|
||||
|
||||
// Create dynamic cube.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad / 2.0);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
|
||||
// Character.
|
||||
let characterDesc =
|
||||
RAPIER.RigidBodyDesc.kinematicPositionBased().setTranslation(
|
||||
-10.0,
|
||||
4.0,
|
||||
);
|
||||
let character = world.createRigidBody(characterDesc);
|
||||
let characterColliderDesc = RAPIER.ColliderDesc.cuboid(0.6, 1.2);
|
||||
let characterCollider = world.createCollider(
|
||||
characterColliderDesc,
|
||||
character,
|
||||
);
|
||||
|
||||
let characterController = world.createCharacterController(0.1);
|
||||
characterController.enableAutostep(0.7, 0.3, true);
|
||||
characterController.enableSnapToGround(0.7);
|
||||
|
||||
let speed = 0.2;
|
||||
let movementDirection = {x: 0.0, y: -speed};
|
||||
|
||||
let updateCharacter = () => {
|
||||
characterController.computeColliderMovement(
|
||||
characterCollider,
|
||||
movementDirection,
|
||||
);
|
||||
|
||||
let movement = characterController.computedMovement();
|
||||
let newPos = character.translation();
|
||||
newPos.x += movement.x;
|
||||
newPos.y += movement.y;
|
||||
character.setNextKinematicTranslation(newPos);
|
||||
console.log("here");
|
||||
};
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.setpreTimestepAction(updateCharacter);
|
||||
|
||||
document.onkeydown = function (event: KeyboardEvent) {
|
||||
if (event.key == "ArrowLeft") movementDirection.x = -speed;
|
||||
if (event.key == "ArrowRight") movementDirection.x = speed;
|
||||
if (event.key == " ") movementDirection.y = speed;
|
||||
};
|
||||
|
||||
document.onkeyup = function (event: KeyboardEvent) {
|
||||
if (event.key == "ArrowLeft") movementDirection.x = 0.0;
|
||||
if (event.key == "ArrowRight") movementDirection.x = 0.0;
|
||||
if (event.key == " ") movementDirection.y = -speed; // Gravity
|
||||
};
|
||||
|
||||
testbed.lookAt({
|
||||
target: {x: 0.0, y: -1.0},
|
||||
zoom: 50.0,
|
||||
});
|
||||
}
|
||||
79
thirdparty/rapier.js/testbed2d/src/demos/collisionGroups.ts
vendored
Normal file
79
thirdparty/rapier.js/testbed2d/src/demos/collisionGroups.ts
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
/*
|
||||
* Ground
|
||||
*/
|
||||
let ground_size = 5.0;
|
||||
let ground_height = 0.1;
|
||||
|
||||
let groundBodyDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
0.0,
|
||||
-ground_height,
|
||||
);
|
||||
let groundBody = world.createRigidBody(groundBodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(ground_size, ground_height);
|
||||
world.createCollider(colliderDesc, groundBody);
|
||||
|
||||
/*
|
||||
* Setup groups
|
||||
*/
|
||||
let group1 = 0x00010001;
|
||||
let group2 = 0x00020002;
|
||||
|
||||
/*
|
||||
* A green floor that will collide with the first group only.
|
||||
*/
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(1.0, 0.1)
|
||||
.setTranslation(0.0, 1.0)
|
||||
.setCollisionGroups(group1);
|
||||
world.createCollider(colliderDesc, groundBody);
|
||||
|
||||
/*
|
||||
* A blue floor that will collide with the second group only.
|
||||
*/
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(1.0, 0.1)
|
||||
.setTranslation(0.0, 2.0)
|
||||
.setCollisionGroups(group2);
|
||||
world.createCollider(colliderDesc, groundBody);
|
||||
|
||||
/*
|
||||
* Create the cubes
|
||||
*/
|
||||
let num = 8;
|
||||
let rad = 0.1;
|
||||
|
||||
let shift = rad * 2.0;
|
||||
let centerx = shift * (num / 2);
|
||||
let centery = 2.5;
|
||||
let i, j;
|
||||
|
||||
for (j = 0; j < 4; ++j) {
|
||||
for (i = 0; i < num; ++i) {
|
||||
let x = i * shift - centerx;
|
||||
let y = j * shift + centery;
|
||||
|
||||
// Alternate between the green and blue groups.
|
||||
let group = i % 2 == 0 ? group1 : group2;
|
||||
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(
|
||||
rad,
|
||||
rad,
|
||||
).setCollisionGroups(group);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: 0.0, y: -1.0},
|
||||
zoom: 100.0,
|
||||
});
|
||||
}
|
||||
68
thirdparty/rapier.js/testbed2d/src/demos/convexPolygons.ts
vendored
Normal file
68
thirdparty/rapier.js/testbed2d/src/demos/convexPolygons.ts
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
import seedrandom from "seedrandom";
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
/*
|
||||
* Ground
|
||||
*/
|
||||
// Create Ground.
|
||||
let groundSize = 30.0;
|
||||
let grounds = [
|
||||
{x: 0.0, y: 0.0, hx: groundSize, hy: 1.2},
|
||||
{x: -groundSize, y: groundSize, hx: 1.2, hy: groundSize},
|
||||
{x: groundSize, y: groundSize, hx: 1.2, hy: groundSize},
|
||||
];
|
||||
|
||||
grounds.forEach((ground) => {
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
ground.x,
|
||||
ground.y,
|
||||
);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(ground.hx, ground.hy);
|
||||
world.createCollider(colliderDesc, body);
|
||||
});
|
||||
|
||||
/*
|
||||
* Create the convex polygons
|
||||
*/
|
||||
let num = 14;
|
||||
let scale = 4.0;
|
||||
|
||||
let shift = scale;
|
||||
let centerx = (shift * num) / 2.0;
|
||||
let centery = shift / 2.0;
|
||||
|
||||
let i, j, k;
|
||||
let rng = seedrandom("convexPolygon");
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
for (j = 0; j < num * 2; ++j) {
|
||||
let x = i * shift - centerx;
|
||||
let y = j * shift * 2.0 + centery + 2.0;
|
||||
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
|
||||
let points = [];
|
||||
for (k = 0; k < 10; ++k) {
|
||||
points.push(rng() * scale, rng() * scale);
|
||||
}
|
||||
let colliderDesc = RAPIER.ColliderDesc.convexHull(
|
||||
new Float32Array(points),
|
||||
);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: -10.0, y: -30.0},
|
||||
zoom: 7.0,
|
||||
});
|
||||
}
|
||||
56
thirdparty/rapier.js/testbed2d/src/demos/cubes.ts
vendored
Normal file
56
thirdparty/rapier.js/testbed2d/src/demos/cubes.ts
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let groundSize = 40.0;
|
||||
let grounds = [
|
||||
{x: 0.0, y: 0.0, hx: groundSize, hy: 0.1},
|
||||
{x: -groundSize, y: groundSize, hx: 0.1, hy: groundSize},
|
||||
{x: groundSize, y: groundSize, hx: 0.1, hy: groundSize},
|
||||
];
|
||||
|
||||
grounds.forEach((ground) => {
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
ground.x,
|
||||
ground.y,
|
||||
);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(ground.hx, ground.hy);
|
||||
world.createCollider(colliderDesc, body);
|
||||
});
|
||||
|
||||
// Dynamic cubes.
|
||||
let num = 20;
|
||||
let numy = 50;
|
||||
let rad = 1.0;
|
||||
|
||||
let shift = rad * 2.0 + rad;
|
||||
let centerx = shift * (num / 2);
|
||||
let centery = shift / 2.0;
|
||||
|
||||
let i, j;
|
||||
|
||||
for (j = 0; j < numy; ++j) {
|
||||
for (i = 0; i < num; ++i) {
|
||||
let x = i * shift - centerx;
|
||||
let y = j * shift + centery + 3.0;
|
||||
|
||||
// Create dynamic cube.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: -10.0, y: -30.0},
|
||||
zoom: 7.0,
|
||||
});
|
||||
}
|
||||
65
thirdparty/rapier.js/testbed2d/src/demos/heightfield.ts
vendored
Normal file
65
thirdparty/rapier.js/testbed2d/src/demos/heightfield.ts
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
let i, j;
|
||||
|
||||
/*
|
||||
* Ground
|
||||
*/
|
||||
let ground_size = {x: 50.0, y: 1.0};
|
||||
let nsubdivs = 100;
|
||||
let heights = [];
|
||||
|
||||
heights.push(40.0);
|
||||
for (i = 1; i < nsubdivs; ++i) {
|
||||
heights.push(Math.cos((i * ground_size.x) / nsubdivs) * 2.0);
|
||||
}
|
||||
heights.push(40.0);
|
||||
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.heightfield(
|
||||
new Float32Array(heights),
|
||||
ground_size,
|
||||
);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
/*
|
||||
* Create the cubes
|
||||
*/
|
||||
let num = 15;
|
||||
let rad = 0.5;
|
||||
|
||||
let shift = rad * 2.0;
|
||||
let centerx = shift * (num / 2);
|
||||
let centery = shift / 2.0;
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
for (j = 0; j < num * 5; ++j) {
|
||||
let x = i * shift - centerx;
|
||||
let y = j * shift + centery + 3.0;
|
||||
|
||||
// Build the rigid body.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
|
||||
if (j % 2 == 0) {
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad);
|
||||
world.createCollider(colliderDesc, body);
|
||||
} else {
|
||||
let colliderDesc = RAPIER.ColliderDesc.ball(rad);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: -10.0, y: -15.0},
|
||||
zoom: 10.0,
|
||||
});
|
||||
}
|
||||
92
thirdparty/rapier.js/testbed2d/src/demos/keva.ts
vendored
Normal file
92
thirdparty/rapier.js/testbed2d/src/demos/keva.ts
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
import type * as RAPIER from "@dimforge/rapier2d";
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
function buildBlock(
|
||||
RAPIER: RAPIER_API,
|
||||
world: RAPIER.World,
|
||||
halfExtents: RAPIER.Vector,
|
||||
shift: RAPIER.Vector,
|
||||
numx: number,
|
||||
numy: number,
|
||||
numz: number,
|
||||
) {
|
||||
let halfExtents_yx = {x: halfExtents.y, y: halfExtents.x};
|
||||
let dimensions = [halfExtents, halfExtents_yx];
|
||||
let spacing = (halfExtents.y * numx - halfExtents.x) / (numz - 1.0);
|
||||
|
||||
let i;
|
||||
let j;
|
||||
let y = 0.0;
|
||||
|
||||
for (i = 0; i <= numy; ++i) {
|
||||
let dim = dimensions[i % 2];
|
||||
[numx, numz] = [numz, numx];
|
||||
|
||||
for (j = 0; j < numx; ++j) {
|
||||
let x = i % 2 == 0 ? spacing * j * 2.0 : dim.x * j * 2.0;
|
||||
|
||||
// Build the rigid body.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(
|
||||
x + dim.x + shift.x,
|
||||
y + dim.y + shift.y,
|
||||
);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(dim.x, dim.y);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
|
||||
y += dim.y * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let groundSize = 150.0;
|
||||
let groundHeight = 0.1;
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
0.0,
|
||||
-groundHeight,
|
||||
);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(groundSize, groundHeight);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
// Keva tower.
|
||||
let halfExtents = new RAPIER.Vector2(0.5, 2.0);
|
||||
let blockHeight = 0.0;
|
||||
// These should only be set to odd values otherwise
|
||||
// the blocks won't align in the nicest way.
|
||||
let numyArr = [0, 3, 5, 5, 7, 9];
|
||||
let numBlocksBuilt = 0;
|
||||
let i;
|
||||
|
||||
for (i = 5; i >= 1; --i) {
|
||||
let numx = i * 15;
|
||||
let numy = numyArr[i];
|
||||
let numz = numx * 2 + 1;
|
||||
let blockWidth = numx * halfExtents.y * 2.0;
|
||||
buildBlock(
|
||||
RAPIER,
|
||||
world,
|
||||
halfExtents,
|
||||
new RAPIER.Vector2(-blockWidth / 2.0, blockHeight),
|
||||
numx,
|
||||
numy,
|
||||
numz,
|
||||
);
|
||||
blockHeight += (numy + 1) * (halfExtents.x + halfExtents.y);
|
||||
numBlocksBuilt += numx * numy;
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
|
||||
testbed.lookAt({
|
||||
target: {x: -10.0, y: -5.0},
|
||||
zoom: 4.0,
|
||||
});
|
||||
}
|
||||
51
thirdparty/rapier.js/testbed2d/src/demos/lockedRotations.ts
vendored
Normal file
51
thirdparty/rapier.js/testbed2d/src/demos/lockedRotations.ts
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
/*
|
||||
* The ground
|
||||
*/
|
||||
let ground_size = 1.8;
|
||||
let ground_height = 1.0;
|
||||
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(
|
||||
0.0,
|
||||
-ground_height,
|
||||
);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(ground_size, ground_height);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
/*
|
||||
* A rectangle that only rotates along the `x` axis.
|
||||
*/
|
||||
bodyDesc = RAPIER.RigidBodyDesc.dynamic()
|
||||
.setTranslation(0.0, 3.0)
|
||||
.lockTranslations();
|
||||
body = world.createRigidBody(bodyDesc);
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(2.0, 0.6);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
/*
|
||||
* A cuboid that cannot rotate.
|
||||
*/
|
||||
bodyDesc = RAPIER.RigidBodyDesc.dynamic()
|
||||
.setTranslation(0.4, 5.0)
|
||||
.lockRotations();
|
||||
body = world.createRigidBody(bodyDesc);
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(0.4, 0.6);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
/*
|
||||
* Setup the testbed.
|
||||
*/
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: 0.0, y: -2.0},
|
||||
zoom: 50.0,
|
||||
});
|
||||
}
|
||||
100
thirdparty/rapier.js/testbed2d/src/demos/pidController.ts
vendored
Normal file
100
thirdparty/rapier.js/testbed2d/src/demos/pidController.ts
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(15.0, 0.1);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
// Dynamic cubes.
|
||||
let rad = 0.5;
|
||||
let num = 5;
|
||||
let i, j, k;
|
||||
let shift = rad * 2.5;
|
||||
let center = num * rad;
|
||||
let height = 5.0;
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
for (k = i; k < num; ++k) {
|
||||
let x = (i * shift) / 2.0 + (k - i) * shift - center;
|
||||
let y = (i * shift) / 2.0 + height;
|
||||
|
||||
// Create dynamic cube.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad / 2.0);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
|
||||
// Character.
|
||||
let characterDesc = RAPIER.RigidBodyDesc.dynamic()
|
||||
.setTranslation(-10.0, 4.0)
|
||||
.setGravityScale(10.0)
|
||||
.setSoftCcdPrediction(10.0);
|
||||
let character = world.createRigidBody(characterDesc);
|
||||
let characterColliderDesc = RAPIER.ColliderDesc.cuboid(0.6, 1.2);
|
||||
world.createCollider(characterColliderDesc, character);
|
||||
|
||||
let pidController = world.createPidController(
|
||||
60.0,
|
||||
0.0,
|
||||
1.0,
|
||||
RAPIER.PidAxesMask.AllAng,
|
||||
);
|
||||
let speed = 0.2;
|
||||
let movementDirection = {x: 0.0, y: 0.0};
|
||||
let targetVelocity = {x: 0.0, y: 0.0};
|
||||
let targetRotation = 0.0;
|
||||
|
||||
let updateCharacter = () => {
|
||||
if (movementDirection.x == 0.0 && movementDirection.y == 0.0) {
|
||||
// Only adjust the rotation, but let translation.
|
||||
pidController.setAxes(RAPIER.PidAxesMask.AllAng);
|
||||
} else if (movementDirection.y == 0.0) {
|
||||
// Don’t control the linear Y axis so the player can fall down due to gravity.
|
||||
pidController.setAxes(
|
||||
RAPIER.PidAxesMask.AllAng | RAPIER.PidAxesMask.LinX,
|
||||
);
|
||||
} else {
|
||||
pidController.setAxes(RAPIER.PidAxesMask.All);
|
||||
}
|
||||
|
||||
let targetPoint = character.translation();
|
||||
targetPoint.x += movementDirection.x;
|
||||
targetPoint.y += movementDirection.y;
|
||||
|
||||
pidController.applyLinearCorrection(
|
||||
character,
|
||||
targetPoint,
|
||||
targetVelocity,
|
||||
);
|
||||
pidController.applyAngularCorrection(character, 0.0, 0.0);
|
||||
};
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.setpreTimestepAction(updateCharacter);
|
||||
|
||||
document.onkeydown = function (event: KeyboardEvent) {
|
||||
if (event.key == "ArrowLeft") movementDirection.x = -speed;
|
||||
if (event.key == "ArrowRight") movementDirection.x = speed;
|
||||
if (event.key == " ") movementDirection.y = speed;
|
||||
};
|
||||
|
||||
document.onkeyup = function (event: KeyboardEvent) {
|
||||
if (event.key == "ArrowLeft") movementDirection.x = 0.0;
|
||||
if (event.key == "ArrowRight") movementDirection.x = 0.0;
|
||||
if (event.key == " ") movementDirection.y = 0.0;
|
||||
};
|
||||
|
||||
testbed.lookAt({
|
||||
target: {x: 0.0, y: -1.0},
|
||||
zoom: 50.0,
|
||||
});
|
||||
}
|
||||
64
thirdparty/rapier.js/testbed2d/src/demos/polyline.ts
vendored
Normal file
64
thirdparty/rapier.js/testbed2d/src/demos/polyline.ts
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
let i, j;
|
||||
|
||||
/*
|
||||
* Ground
|
||||
*/
|
||||
let nsubdivs = 100;
|
||||
let points = [];
|
||||
let groundSize = 30.0;
|
||||
let stepSize = groundSize / nsubdivs;
|
||||
|
||||
points.push(-groundSize / 2.0, 25.0);
|
||||
for (i = 1; i < nsubdivs; ++i) {
|
||||
let x = -groundSize / 2.0 + i * stepSize;
|
||||
let y = Math.cos(i * stepSize) * 2.0;
|
||||
points.push(x, y);
|
||||
}
|
||||
points.push(groundSize / 2.0, 25.0);
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.polyline(new Float32Array(points));
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
/*
|
||||
* Create the cubes
|
||||
*/
|
||||
let num = 10;
|
||||
let rad = 0.5;
|
||||
|
||||
let shift = rad * 2.0;
|
||||
let centerx = shift * (num / 2);
|
||||
let centery = shift / 2.0;
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
for (j = 0; j < num * 5; ++j) {
|
||||
let x = i * shift - centerx;
|
||||
let y = j * shift + centery + 3.0;
|
||||
|
||||
// Build the rigid body.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
|
||||
if (j % 2 == 0) {
|
||||
let colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad);
|
||||
world.createCollider(colliderDesc, body);
|
||||
} else {
|
||||
let colliderDesc = RAPIER.ColliderDesc.ball(rad);
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: -10.0, y: -15.0},
|
||||
zoom: 10.0,
|
||||
});
|
||||
}
|
||||
59
thirdparty/rapier.js/testbed2d/src/demos/revoluteJoints.ts
vendored
Normal file
59
thirdparty/rapier.js/testbed2d/src/demos/revoluteJoints.ts
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
let bodies = [];
|
||||
|
||||
let rad = 0.4;
|
||||
let numi = 30; // Num vertical nodes.
|
||||
let numk = 30; // Num horizontal nodes.
|
||||
let shift = 1.0;
|
||||
let i, k;
|
||||
|
||||
for (k = 0; k < numk; ++k) {
|
||||
for (i = 0; i < numi; ++i) {
|
||||
let status =
|
||||
k >= numk / 2 - 3 && k <= numk / 2 + 3 && i == 0
|
||||
? RAPIER.RigidBodyType.Fixed
|
||||
: RAPIER.RigidBodyType.Dynamic;
|
||||
|
||||
let bodyDesc = new RAPIER.RigidBodyDesc(status).setTranslation(
|
||||
k * shift,
|
||||
-i * shift,
|
||||
);
|
||||
let child = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc = RAPIER.ColliderDesc.ball(rad);
|
||||
world.createCollider(colliderDesc, child);
|
||||
|
||||
// Vertical joint.
|
||||
if (i > 0) {
|
||||
let parent = bodies[bodies.length - 1];
|
||||
let anchor1 = new RAPIER.Vector2(0.0, 0.0);
|
||||
let anchor2 = new RAPIER.Vector2(0.0, shift);
|
||||
let JointData = RAPIER.JointData.revolute(anchor1, anchor2);
|
||||
world.createImpulseJoint(JointData, parent, child, true);
|
||||
}
|
||||
|
||||
// Horizontal joint.
|
||||
if (k > 0) {
|
||||
let parentIndex = bodies.length - numi;
|
||||
let parent = bodies[parentIndex];
|
||||
let anchor1 = new RAPIER.Vector2(0.0, 0.0);
|
||||
let anchor2 = new RAPIER.Vector2(-shift, 0.0);
|
||||
let JointData = RAPIER.JointData.revolute(anchor1, anchor2);
|
||||
world.createImpulseJoint(JointData, parent, child, true);
|
||||
}
|
||||
|
||||
bodies.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: 30.0, y: 30.0},
|
||||
zoom: 10.0,
|
||||
});
|
||||
}
|
||||
90
thirdparty/rapier.js/testbed2d/src/demos/voxels.ts
vendored
Normal file
90
thirdparty/rapier.js/testbed2d/src/demos/voxels.ts
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
import type {Testbed} from "../Testbed";
|
||||
|
||||
type RAPIER_API = typeof import("@dimforge/rapier2d");
|
||||
|
||||
function generateVoxels(n: number) {
|
||||
let points = [];
|
||||
|
||||
let i;
|
||||
for (i = 0; i <= n; ++i) {
|
||||
let y = Math.max(-0.8, Math.min(Math.sin((i / n) * 10.0), 0.8)) * 8.0;
|
||||
points.push(i - n / 2.0, y);
|
||||
}
|
||||
return {
|
||||
points: new Float32Array(points),
|
||||
voxelSize: {x: 1.0, y: 1.2},
|
||||
};
|
||||
}
|
||||
|
||||
export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
|
||||
let gravity = new RAPIER.Vector2(0.0, -9.81);
|
||||
let world = new RAPIER.World(gravity);
|
||||
|
||||
// Create Ground.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let voxels = generateVoxels(100);
|
||||
let colliderDesc = RAPIER.ColliderDesc.voxels(
|
||||
voxels.points,
|
||||
voxels.voxelSize,
|
||||
);
|
||||
world.createCollider(colliderDesc, body);
|
||||
|
||||
// Dynamic cubes.
|
||||
let num = 10;
|
||||
let numy = 4;
|
||||
let rad = 1.0;
|
||||
|
||||
let shift = rad * 2.0 + rad;
|
||||
let centery = shift / 2.0;
|
||||
|
||||
let offset = -num * (rad * 2.0 + rad) * 0.5;
|
||||
let i, j;
|
||||
|
||||
for (j = 0; j < numy; ++j) {
|
||||
for (i = 0; i < num; ++i) {
|
||||
let x = i * shift + offset;
|
||||
let y = j * shift + centery + 10.0;
|
||||
|
||||
// Create dynamic cube.
|
||||
let bodyDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(x, y);
|
||||
let body = world.createRigidBody(bodyDesc);
|
||||
let colliderDesc;
|
||||
|
||||
switch (j % 3) {
|
||||
case 0:
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(rad, rad);
|
||||
break;
|
||||
case 1:
|
||||
colliderDesc = RAPIER.ColliderDesc.ball(rad);
|
||||
break;
|
||||
case 2:
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(
|
||||
rad / 2.0,
|
||||
rad / 2.0,
|
||||
);
|
||||
world.createCollider(colliderDesc, body);
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(
|
||||
rad / 2.0,
|
||||
rad,
|
||||
).setTranslation(rad, 0.0);
|
||||
world.createCollider(colliderDesc, body);
|
||||
colliderDesc = RAPIER.ColliderDesc.cuboid(
|
||||
rad / 2.0,
|
||||
rad,
|
||||
).setTranslation(-rad, 0.0);
|
||||
break;
|
||||
}
|
||||
|
||||
world.createCollider(colliderDesc, body);
|
||||
}
|
||||
|
||||
offset -= 0.05 * rad * (num - 1.0);
|
||||
}
|
||||
|
||||
testbed.setWorld(world);
|
||||
testbed.lookAt({
|
||||
target: {x: 0.0, y: 0.0},
|
||||
zoom: 20.0,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user