|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * Copyright (c) 2019 Erin Catto |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +import { FrictionJoint, Transform, Vec2 } from "planck"; |
| 25 | +import { KPFixtureDef } from "../lib"; |
| 26 | +import { addScenesButtons, type KAPLANCKCtx } from "./shared"; |
| 27 | + |
| 28 | +const applyForceScene = (k: KAPLANCKCtx) => () => { |
| 29 | + const scene = k.add([]); |
| 30 | + const worldContainer = scene.add([k.kpWorld()]); |
| 31 | + |
| 32 | + // ground |
| 33 | + const wallFixtureDef: KPFixtureDef = { |
| 34 | + density: 0, |
| 35 | + restitution: 0.4, |
| 36 | + }; |
| 37 | + |
| 38 | + const ground = worldContainer.add([ |
| 39 | + k.kpPos(k.kpCenter()), |
| 40 | + k.kpRotate(), |
| 41 | + k.kpShapes([ |
| 42 | + { |
| 43 | + type: "edge", |
| 44 | + opt: { |
| 45 | + v1: { x: -20, y: -20 }, |
| 46 | + v2: { x: -20, y: 20 }, |
| 47 | + draw: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + type: "edge", |
| 52 | + opt: { |
| 53 | + v1: { x: 20, y: -20 }, |
| 54 | + v2: { x: 20, y: 20 }, |
| 55 | + draw: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + type: "edge", |
| 60 | + opt: { |
| 61 | + v1: { x: -20, y: 20 }, |
| 62 | + v2: { x: 20, y: 20 }, |
| 63 | + draw: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + type: "edge", |
| 68 | + opt: { |
| 69 | + v1: { x: -20, y: -20 }, |
| 70 | + v2: { x: 20, y: -20 }, |
| 71 | + draw: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ]), |
| 75 | + k.kpBody(), |
| 76 | + k.kpFixtures([ |
| 77 | + { ...wallFixtureDef }, |
| 78 | + { ...wallFixtureDef }, |
| 79 | + { ...wallFixtureDef }, |
| 80 | + { ...wallFixtureDef }, |
| 81 | + ]), |
| 82 | + ]); |
| 83 | + |
| 84 | + const xf1 = new Transform(); |
| 85 | + const xf2 = new Transform(); |
| 86 | + |
| 87 | + xf1.q.set(0.3524 * Math.PI); |
| 88 | + xf1.p.set(xf1.q.getXAxis()); |
| 89 | + xf2.q.set(-0.3524 * Math.PI); |
| 90 | + xf2.p.set(Vec2.neg(xf2.q.getXAxis())); |
| 91 | + |
| 92 | + const jet = worldContainer.add([ |
| 93 | + k.kpPos(k.kpCenter().add({ x: 0, y: 18 })), |
| 94 | + k.kpRotate(), |
| 95 | + k.kpShapes([ |
| 96 | + { |
| 97 | + type: "polygon", |
| 98 | + opt: { |
| 99 | + vertices: [ |
| 100 | + new Vec2(-1.0, 0.0), |
| 101 | + new Vec2(1.0, 0.0), |
| 102 | + new Vec2(0.0, 0.5), |
| 103 | + ].map((v) => Transform.mul(xf1, v)), |
| 104 | + draw: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + type: "polygon", |
| 109 | + opt: { |
| 110 | + vertices: [ |
| 111 | + new Vec2(-1.0, 0.0), |
| 112 | + new Vec2(1.0, 0.0), |
| 113 | + new Vec2(0.0, 0.5), |
| 114 | + ].map((v) => Transform.mul(xf2, v)), |
| 115 | + draw: true, |
| 116 | + }, |
| 117 | + }, |
| 118 | + ]), |
| 119 | + k.kpBody({ |
| 120 | + type: "dynamic", |
| 121 | + angularDamping: 2, |
| 122 | + linearDamping: 0.5, |
| 123 | + allowSleep: false, |
| 124 | + }), |
| 125 | + k.kpFixtures([{ density: 2 }, { density: 2 }]), |
| 126 | + ]); |
| 127 | + |
| 128 | + if (!ground.body) return; |
| 129 | + |
| 130 | + const boxFixtureDef: KPFixtureDef = { |
| 131 | + density: 1, |
| 132 | + friction: 0.3, |
| 133 | + }; |
| 134 | + |
| 135 | + for (let i = 0; i < 10; ++i) { |
| 136 | + const box = worldContainer.add([ |
| 137 | + k.kpPos(k.kpCenter().add({ x: 0, y: 15 - 1.54 * i })), |
| 138 | + k.kpRotate(), |
| 139 | + k.kpBoxShape({ halfWidth: 0.5, halfHeight: 0.5, draw: true }), |
| 140 | + k.kpBody({ type: "dynamic" }), |
| 141 | + k.kpFixture(boxFixtureDef), |
| 142 | + ]); |
| 143 | + |
| 144 | + if (!box.body) return; |
| 145 | + |
| 146 | + const gravity = 10; |
| 147 | + const I = box.body.getInertia(); |
| 148 | + const mass = box.body.getMass(); |
| 149 | + const radius = Math.sqrt((2 * I) / mass); |
| 150 | + |
| 151 | + // TODO: create a component so that the joint can be drawn/visualized on debug mode |
| 152 | + worldContainer.world.createJoint( |
| 153 | + new FrictionJoint( |
| 154 | + { |
| 155 | + collideConnected: true, |
| 156 | + maxForce: mass * gravity, |
| 157 | + maxTorque: mass * radius * gravity, |
| 158 | + }, |
| 159 | + ground.body, |
| 160 | + box.body, |
| 161 | + Vec2.zero(), |
| 162 | + ), |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + scene.onUpdate(() => { |
| 167 | + if (k.isKeyDown(["right", "a"]) && !k.isKeyDown(["left", "d"])) { |
| 168 | + jet.body?.applyAngularImpulse(0.2, true); |
| 169 | + } else if (k.isKeyDown(["left", "d"]) && !k.isKeyDown(["right", "a"])) { |
| 170 | + jet.body?.applyAngularImpulse(-0.2, true); |
| 171 | + } |
| 172 | + |
| 173 | + if (k.isKeyDown(["up", "w"])) { |
| 174 | + if (!jet.body) return; |
| 175 | + |
| 176 | + const f = jet.body.getWorldVector(new Vec2(0, -1)); |
| 177 | + const p = jet.body.getWorldPoint(new Vec2(0, 2)); |
| 178 | + |
| 179 | + jet.body?.applyLinearImpulse(f, p, true); |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + addScenesButtons(k, scene); |
| 184 | +}; |
| 185 | + |
| 186 | +export default applyForceScene; |
0 commit comments