Skip to content

Commit

Permalink
feat: added joints
Browse files Browse the repository at this point in the history
  • Loading branch information
KeSuave committed Dec 9, 2024
1 parent e7b1ab0 commit cb35089
Show file tree
Hide file tree
Showing 29 changed files with 2,108 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docssrc/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { HomePage } from "vocs/components";
[KAPLAY](https://kaplayjs.com/).
</HomePage.Description>
<HomePage.Buttons>
<HomePage.Button href="/getting-started" variant="accent">
<HomePage.Button href="/introduction" variant="accent">
Get started
</HomePage.Button>
<HomePage.Button href="https://github.com/KeSuave/KaPlanck">
Expand Down
12 changes: 3 additions & 9 deletions src/examples/8-Ball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const eightBallScene = (k: KAPLANCKCtx) => () => {
const ballRadius = 1;
const pocketRadius = 1.6;

const width = k.p2u(k.width() - 200);
const height = k.p2u(k.height() - 200);
const width = k.p2m(k.width() - 200);
const height = k.p2m(k.height() - 200);

const railH = [
new Vec2(pocketRadius, height * 0.5),
Expand All @@ -81,18 +81,12 @@ const eightBallScene = (k: KAPLANCKCtx) => () => {
const railFixDef = {
friction: 0.1,
restitution: 0.9,
userData: {
tag: "rail",
},
};
const pocketFixDef = {
userData: { tag: "pocket" },
};
const pocketFixDef = {};
const ballFixDef: KPFixtureDef = {
friction: 0.1,
restitution: 0.99,
density: 1,
userData: { tag: "ball" },
};
const ballBodyDef: KPBodyDef = {
type: "dynamic",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/AddPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const addPairScene = (k: KAPLANCKCtx) => () => {
k.kpFixture({ density: 1 }),
]);

box.body?.setLinearVelocity({ x: 100, y: 0 });
box.setLinearVelocity({ x: 100, y: 0 });

addScenesButtons(k, scene);
};
Expand Down
39 changes: 23 additions & 16 deletions src/examples/ApplyForce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* SOFTWARE.
*/

import { FrictionJoint, Transform, Vec2 } from "planck";
import { Transform, Vec2 } from "planck";
import { KPFixtureDef } from "../lib";
import { addScenesButtons, type KAPLANCKCtx } from "../shared";

Expand Down Expand Up @@ -86,14 +86,14 @@ const applyForceScene = (k: KAPLANCKCtx) => () => {
const xf1 = new Transform();
const xf2 = new Transform();

xf1.q.set(0.3524 * Math.PI);
xf1.q.set(-0.3524 * Math.PI);
xf1.p.set(xf1.q.getXAxis());
xf2.q.set(-0.3524 * Math.PI);
xf2.q.set(0.3524 * Math.PI);
xf2.p.set(Vec2.neg(xf2.q.getXAxis()));

const jet = worldContainer.add([
k.kpPos(k.kpCenter().add({ x: 0, y: 18 })),
k.kpRotate(),
k.kpRotate(Math.PI),
k.kpBody({
type: "dynamic",
angularDamping: 2,
Expand All @@ -107,22 +107,26 @@ const applyForceScene = (k: KAPLANCKCtx) => () => {
vertices: [
new Vec2(-1.0, 0.0),
new Vec2(1.0, 0.0),
new Vec2(0.0, 0.5),
new Vec2(0.0, -0.5),
].map((v) => Transform.mul(xf1, v)),
draw: true,
fill: false,
}),
k.kpFixture({ density: 2 }),
k.outline(1, new k.Color(255, 255, 255)),
]);
jet.add([
k.kpPolygonShape({
vertices: [
new Vec2(-1.0, 0.0),
new Vec2(1.0, 0.0),
new Vec2(0.0, 0.5),
new Vec2(0.0, -0.5),
].map((v) => Transform.mul(xf2, v)),
draw: true,
fill: false,
}),
k.kpFixture({ density: 2 }),
k.outline(1, new k.Color(255, 255, 255)),
]);

if (!ground.body) return;
Expand All @@ -136,9 +140,10 @@ const applyForceScene = (k: KAPLANCKCtx) => () => {
const box = worldContainer.add([
k.kpPos(k.kpCenter().add({ x: 0, y: 15 - 1.54 * i })),
k.kpRotate(),
k.kpBoxShape({ width: 1, height: 1, draw: true }),
k.kpBoxShape({ width: 1, height: 1, draw: true, fill: false }),
k.kpBody({ type: "dynamic" }),
k.kpFixture(boxFixtureDef),
k.outline(1, new k.Color(255, 255, 255)),
]);

if (!box.body) return;
Expand All @@ -148,19 +153,21 @@ const applyForceScene = (k: KAPLANCKCtx) => () => {
const mass = box.body.getMass();
const radius = Math.sqrt((2 * I) / mass);

// TODO: create a component so that the joint can be drawn/visualized on debug mode
worldContainer.world.createJoint(
new FrictionJoint(
worldContainer.add([
k.kpFrictionJoint(
{
collideConnected: true,
maxForce: mass * gravity,
maxTorque: mass * radius * gravity,
localAnchorA: Vec2.zero(),
localAnchorB: Vec2.zero(),
draw: true,
},
ground.body,
box.body,
Vec2.zero(),
ground,
box,
worldContainer,
),
);
]);
}

scene.onUpdate(() => {
Expand All @@ -173,8 +180,8 @@ const applyForceScene = (k: KAPLANCKCtx) => () => {
if (k.isKeyDown("up")) {
if (!jet.body) return;

const f = jet.body.getWorldVector(new Vec2(0, -1));
const p = jet.body.getWorldPoint(new Vec2(0, 2));
const f = jet.body.getWorldVector(new Vec2(0, 1));
const p = jet.body.getWorldPoint(new Vec2(0, -2));

jet.body?.applyLinearImpulse(f, p, true);
}
Expand Down
88 changes: 88 additions & 0 deletions src/examples/MotorJoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* MIT License
* Copyright (c) 2019 Erin Catto
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import { Vec2 } from "planck";
import { addScenesButtons, type KAPLANCKCtx } from "../shared";

const motorJointScene = (k: KAPLANCKCtx) => () => {
const scene = k.add([]);

const worldContainer = scene.add([k.kpWorld(new Vec2(0, 10))]);

const ground = worldContainer.add([
k.kpPos(k.kpCenter()),
k.kpRotate(),
k.kpBody(),
k.kpEdgeShape({
v1: new Vec2(-20, 0),
v2: new Vec2(20, 0),
draw: true,
}),
k.kpFixture(),
]);

const box = worldContainer.add([
k.kpPos(k.kpCenter().add({ x: 0, y: -8 })),
k.kpRotate(),
k.kpBody({ type: "dynamic" }),
k.kpBoxShape({
width: 4,
height: 1,
draw: true,
fill: false,
}),
k.kpFixture({
friction: 0.6,
density: 2,
}),
k.outline(1, new k.Color(255, 255, 255)),
]);

const joint = worldContainer.add([
k.kpMotorJoint(
{
maxForce: 1000,
maxTorque: 1000,
draw: true,
},
ground,
box,
worldContainer,
),
]);

let time = 0;

worldContainer.onUpdate(() => {
time += k.dt();

joint.setLinearOffset(
new Vec2(6 * Math.sin(2 * time), -8 + 4 * Math.sin(time)),
);
joint.setAngularOffset(4 * time);
});

addScenesButtons(k, scene);
};

export default motorJointScene;
43 changes: 34 additions & 9 deletions src/examples/Tumbler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import { RevoluteJoint, Vec2 } from "planck";
/*
* MIT License
* Copyright (c) 2019 Erin Catto
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import { Vec2 } from "planck";
import { addScenesButtons, type KAPLANCKCtx } from "../shared";

const tumblerScene = (k: KAPLANCKCtx) => () => {
Expand Down Expand Up @@ -99,19 +122,21 @@ const tumblerScene = (k: KAPLANCKCtx) => () => {
k.outline(1, new k.Color(200, 200, 200)),
]);

// TODO: create a component so that the joint can be drawn/visualized on debug mode
worldContainer.world.createJoint(
new RevoluteJoint(
worldContainer.add([
k.kpRevoluteJoint(
{
motorSpeed: 0.08 * Math.PI,
motorSpeed: 0.08 * -Math.PI,
maxMotorTorque: 1e8,
enableMotor: true,
localAnchorA: k.kpCenter(),
localAnchorB: Vec2.zero(),
referenceAngle: 0,
},
ground.body,
container.body,
k.kpCenter(),
ground,
container,
worldContainer,
),
);
]);

let count = 200;

Expand Down
Loading

0 comments on commit cb35089

Please sign in to comment.