Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 190x 190x 190x 190x 190x 190x 190x 190x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 150x 150x 10x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 121x 121x 121x 121x 121x 121x 156x 156x 156x 124x 124x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 10x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 10x 5x 5x 3x 3x 5x 5x 5x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 10x 17x 10x 10x 10x 17x 17x 17x 17x 17x 17x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 5x 5x 10x 6x 6x 10x 4x 4x 4x 10x 2x 2x 2x 10x 10x 10x 1x 1x 3x 3x 1x 1x 1x 10x | import * as THREE from 'three';
import RAPIER from '@dimforge/rapier3d-compat';
import { MissionElement } from './types';
import {
LEGO_COLORS,
getLegoMaterial,
createTechnicBeamGroup,
createLegoPlateGroup,
} from '../view/lego-visuals';
import { DualLockMarker } from './dual-lock-marker';
/**
* 4-Axle Riser Mission Mechanism
*
* Implements an authentic FIRST LEGO League 4-bar scissor toggle linkage built from
* official LEGO Technic liftarms, friction pins, and studded bricks.
*
* One end is securely anchored to the field mat. Pushing the opposite end (red slider plate)
* compresses the 4-axle linkage, forcing the middle scoring module to elevate vertically.
* Authentic Technic pin friction holds the assembly aloft against gravity once raised.
*/
export class AxleRiserMission implements MissionElement {
public readonly id = 'axle-riser';
public readonly name = 'Mission 1: 4-Axle Toggle Riser';
public readonly description = 'Push the red slider plate toward the fixed base to elevate the middle scoring block.';
public readonly rootGroup: THREE.Group;
private world!: RAPIER.World;
private basePos: { x: number; y: number; z: number } = { x: 0, y: 0, z: 0 };
private yawDegrees: number = 0;
public isPlacedOnField: boolean = true;
public isDualLocked: boolean = true;
private dualLockMarker: DualLockMarker | null = null;
private dualLockLocalOffset: { x: number; z: number } | null = null;
private anchorBody!: RAPIER.RigidBody;
private sliderBody!: RAPIER.RigidBody;
private riserBody!: RAPIER.RigidBody;
// Visual Groups
private baseplateGroup!: THREE.Group;
private anchorMesh!: THREE.Group;
private sliderMesh!: THREE.Group;
private riserMesh!: THREE.Group;
private linkAMesh!: THREE.Group;
private linkBMesh!: THREE.Group;
// Current Kinematic State
private currentProgress = 0.0; // 0.0 (rest/flat) to 1.0 (fully elevated)
private initialSliderOffset = 0.120; // 120mm spacing at rest
constructor() {
this.rootGroup = new THREE.Group();
}
public init(world: RAPIER.World, basePosition: { x: number; y: number; z: number }, yawDegrees = 0): void {
this.world = world;
this.basePos = { ...basePosition };
this.yawDegrees = yawDegrees;
this.createPhysicsBodies();
this.createLegoVisuals();
this.syncVisuals();
this.updateDualLockVisualMesh();
}
private getDirectionVectors(): { ux: number; uz: number; wx: number; wz: number } {
const rad = (this.yawDegrees * Math.PI) / 180;
return {
ux: Math.cos(rad),
uz: -Math.sin(rad),
wx: Math.sin(rad),
wz: Math.cos(rad),
};
}
private createPhysicsBodies(): void {
const { x, y, z } = this.basePos;
const { ux, uz } = this.getDirectionVectors();
const rad = (this.yawDegrees * Math.PI) / 180;
const qy = Math.sin(rad / 2);
const qw = Math.cos(rad / 2);
const yawQuat = { x: 0, y: qy, z: 0, w: qw };
// 1. Fixed Base Anchor Body (Anchored firmly to field)
const anchorDesc = RAPIER.RigidBodyDesc.fixed()
.setTranslation(x - 0.09 * ux, y + 0.015, z - 0.09 * uz)
.setRotation(yawQuat);
this.anchorBody = this.world.createRigidBody(anchorDesc);
this.world.createCollider(
RAPIER.ColliderDesc.cuboid(0.040, 0.015, 0.045).setFriction(0.8),
this.anchorBody
);
// 2. Dynamic Slider Push Body (Direct contact target for robot bumper and mouse tool)
// Positioned at robot bumper height (Y = 0.015m to 0.045m)
const sliderDesc = RAPIER.RigidBodyDesc.dynamic()
.setTranslation(x + this.initialSliderOffset * ux, y + 0.024, z + this.initialSliderOffset * uz)
.setRotation(yawQuat)
.setLinearDamping(4.5) // Technic friction pin resistance opposes runaway movement
.setAngularDamping(8.0)
.lockRotations(); // Keep slider aligned with field track
this.sliderBody = this.world.createRigidBody(sliderDesc);
// Solid push plate collider (Width 36mm, Height 36mm, Depth 88mm)
const sliderCollider = RAPIER.ColliderDesc.cuboid(0.020, 0.018, 0.044)
.setDensity(2.5)
.setFriction(0.6)
.setRestitution(0.0);
this.world.createCollider(sliderCollider, this.sliderBody);
// 3. Middle Elevated Riser Body (Scoring element body)
const riserDesc = RAPIER.RigidBodyDesc.kinematicPositionBased()
.setTranslation(x, y + 0.025, z)
.setRotation(yawQuat);
this.riserBody = this.world.createRigidBody(riserDesc);
const riserCollider = RAPIER.ColliderDesc.cuboid(0.022, 0.020, 0.035)
.setFriction(0.5);
this.world.createCollider(riserCollider, this.riserBody);
}
private createLegoVisuals(): void {
// 1. LEGO Dark Bluish Gray Baseplate (16 x 8 studs with authentic studs on top)
this.baseplateGroup = createLegoPlateGroup(18, 10, LEGO_COLORS.DARK_GRAY, 1);
this.rootGroup.add(this.baseplateGroup);
// 2. Base Anchor Support (Fixed Technic upright frame)
this.anchorMesh = new THREE.Group();
const anchorUpright = createTechnicBeamGroup(5, LEGO_COLORS.DARK_BLUE, { width: 0.012, thickness: 0.036 });
anchorUpright.rotation.z = Math.PI / 2;
anchorUpright.position.set(0, 0.018, 0);
this.anchorMesh.add(anchorUpright);
// Technic friction pin connecting beam pivot
const anchorPin = new THREE.Mesh(
new THREE.CylinderGeometry(0.003, 0.003, 0.038, 16),
getLegoMaterial(LEGO_COLORS.BLACK, 0.4)
);
anchorPin.rotation.x = Math.PI / 2;
anchorPin.position.set(0, 0.022, 0);
this.anchorMesh.add(anchorPin);
this.rootGroup.add(this.anchorMesh);
// 3. Link A (Left Technic 11L Liftarm in Light Bluish Gray with 11 Technic holes)
this.linkAMesh = createTechnicBeamGroup(11, LEGO_COLORS.LIGHT_GRAY, {
width: 0.008,
thickness: 0.008,
withPinsAt: [0, 10],
});
this.rootGroup.add(this.linkAMesh);
// 4. Link B (Right Technic 11L Liftarm in Light Bluish Gray with 11 Technic holes)
this.linkBMesh = createTechnicBeamGroup(11, LEGO_COLORS.LIGHT_GRAY, {
width: 0.008,
thickness: 0.008,
withPinsAt: [0, 10],
});
this.rootGroup.add(this.linkBMesh);
// 5. Middle Scoring Riser (Yellow LEGO scoring module with green indicator flag)
this.riserMesh = new THREE.Group();
const riserBox = createLegoPlateGroup(6, 6, LEGO_COLORS.YELLOW, 3);
riserBox.position.set(0, -0.010, 0);
this.riserMesh.add(riserBox);
// High-visibility Green Scoring Flag on top
const flagPole = new THREE.Mesh(
new THREE.CylinderGeometry(0.002, 0.002, 0.025, 12),
getLegoMaterial(LEGO_COLORS.LIGHT_GRAY)
);
flagPole.position.set(0, 0.022, 0);
this.riserMesh.add(flagPole);
const flagMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.024, 0.016, 0.002),
getLegoMaterial(LEGO_COLORS.GREEN, 0.25)
);
flagMesh.position.set(0.012, 0.028, 0);
flagMesh.castShadow = true;
this.riserMesh.add(flagMesh);
this.rootGroup.add(this.riserMesh);
// 6. Red Pusher Slider Plate (Contact plate with white chevron arrows)
this.sliderMesh = new THREE.Group();
// Red Pusher Beam Frame
const sliderBase = createTechnicBeamGroup(7, LEGO_COLORS.RED, {
width: 0.016,
thickness: 0.028,
withPinsAt: [0, 3, 6],
});
sliderBase.rotation.y = Math.PI / 2;
this.sliderMesh.add(sliderBase);
// White LEGO Target Chevron Tile on contact face
const chevronTile = new THREE.Mesh(
new THREE.BoxGeometry(0.004, 0.022, 0.060),
getLegoMaterial(LEGO_COLORS.WHITE, 0.3)
);
chevronTile.position.set(0.012, 0, 0);
chevronTile.castShadow = true;
this.sliderMesh.add(chevronTile);
this.rootGroup.add(this.sliderMesh);
}
public update(_dt: number): void {
this.updateKinematics();
}
private updateKinematics(): void {
if (!this.sliderBody || !this.anchorBody) return;
const { ux, uz } = this.getDirectionVectors();
const currTrans = this.sliderBody.translation();
// 1. Project slider position onto local mechanism track axis u
const distSpan = (currTrans.x - this.basePos.x) * ux + (currTrans.z - this.basePos.z) * uz;
const minS = 0.035;
const maxS = this.initialSliderOffset + 0.01;
const clampedS = THREE.MathUtils.clamp(distSpan, minS, maxS);
const clampedX = this.basePos.x + clampedS * ux;
const clampedY = this.basePos.y + 0.024;
const clampedZ = this.basePos.z + clampedS * uz;
const dx = currTrans.x - clampedX;
const dz = currTrans.z - clampedZ;
if (Math.sqrt(dx * dx + dz * dz) > 0.001) {
this.sliderBody.setTranslation({ x: clampedX, y: clampedY, z: clampedZ }, true);
const vel = this.sliderBody.linvel();
const dotV = vel.x * ux + vel.z * uz;
const dampedV = Math.min(dotV, 0);
this.sliderBody.setLinvel({ x: dampedV * ux, y: 0, z: dampedV * uz }, true);
}
// 2. High Pin Friction: Damp velocity to simulate tight Technic friction pins
const linvel = this.sliderBody.linvel();
const speed = Math.sqrt(linvel.x * linvel.x + linvel.z * linvel.z);
if (speed > 0.001) {
this.sliderBody.setLinvel({ x: linvel.x * 0.88, y: 0, z: linvel.z * 0.88 }, true);
}
// 3. Compute 4-Bar Scissor Toggle Kinematics
const distanceSpan = clampedS + 0.09;
const restSpan = this.initialSliderOffset + 0.09;
const compressedSpan = 0.120;
const progress = THREE.MathUtils.clamp((restSpan - distanceSpan) / (restSpan - compressedSpan), 0.0, 1.0);
this.currentProgress = progress;
// Kinematic riser elevation
const elevationY = this.basePos.y + 0.020 + progress * 0.052; // Rises up to 7.4cm!
const midS = (clampedS - 0.09) / 2;
this.riserBody.setTranslation({
x: this.basePos.x + midS * ux,
y: elevationY + 0.016,
z: this.basePos.z + midS * uz,
}, true);
}
public syncVisuals(): void {
if (!this.sliderBody || !this.anchorBody) return;
const { ux, uz, wx, wz } = this.getDirectionVectors();
if (!this.isDualLocked) {
const aP = this.anchorBody.translation();
this.basePos.x = aP.x + 0.09 * ux;
this.basePos.z = aP.z + 0.09 * uz;
}
const { x, y, z } = this.basePos;
const radYaw = (this.yawDegrees * Math.PI) / 180;
// Baseplate
this.baseplateGroup.position.set(x, y + 0.002, z);
this.baseplateGroup.rotation.y = radYaw;
// Anchor
this.anchorMesh.position.set(x - 0.09 * ux, y + 0.008, z - 0.09 * uz);
this.anchorMesh.rotation.y = radYaw;
// Slider
const sP = this.sliderBody.translation();
this.sliderMesh.position.set(sP.x, sP.y, sP.z);
this.sliderMesh.rotation.y = radYaw;
// Kinematic Riser
const rP = this.riserBody.translation();
this.riserMesh.position.set(rP.x, rP.y, rP.z);
this.riserMesh.rotation.y = radYaw;
// 4-Bar Linkages: Link A and Link B
// Link A connects Anchor (x - 0.09, y + 0.022) to Riser (rP.x - 0.022, rP.y)
const pAnchor = new THREE.Vector3(x - 0.09 * ux, y + 0.022, z - 0.09 * uz);
const pRiserLeft = new THREE.Vector3(rP.x - 0.022 * ux, rP.y - 0.004, rP.z - 0.022 * uz);
const midA = new THREE.Vector3().addVectors(pAnchor, pRiserLeft).multiplyScalar(0.5);
const deltaAY = pRiserLeft.y - pAnchor.y;
const deltaAH = Math.hypot(pRiserLeft.x - pAnchor.x, pRiserLeft.z - pAnchor.z);
const angleA = Math.atan2(deltaAY, deltaAH);
this.linkAMesh.position.set(midA.x + 0.014 * wx, midA.y, midA.z + 0.014 * wz);
this.linkAMesh.rotation.set(0, 0, 0);
this.linkAMesh.rotation.y = radYaw;
this.linkAMesh.rotateZ(angleA);
// Link B connects Riser (rP.x + 0.022, rP.y) to Slider (sP.x, y + 0.022)
const pRiserRight = new THREE.Vector3(rP.x + 0.022 * ux, rP.y - 0.004, rP.z + 0.022 * uz);
const pSlider = new THREE.Vector3(sP.x, y + 0.022, sP.z);
const midB = new THREE.Vector3().addVectors(pRiserRight, pSlider).multiplyScalar(0.5);
const deltaBY = pSlider.y - pRiserRight.y;
const deltaBH = Math.hypot(pSlider.x - pRiserRight.x, pSlider.z - pRiserRight.z);
const angleB = Math.atan2(deltaBY, deltaBH);
this.linkBMesh.position.set(midB.x + 0.014 * wx, midB.y, midB.z + 0.014 * wz);
this.linkBMesh.rotation.set(0, 0, 0);
this.linkBMesh.rotation.y = radYaw;
this.linkBMesh.rotateZ(angleB);
}
public reset(): void {
const { x, y, z } = this.basePos;
const { ux, uz } = this.getDirectionVectors();
const zeroVel = { x: 0, y: 0, z: 0 };
const rad = (this.yawDegrees * Math.PI) / 180;
const qy = Math.sin(rad / 2);
const qw = Math.cos(rad / 2);
const yawQuat = { x: 0, y: qy, z: 0, w: qw };
this.anchorBody.setTranslation({ x: x - 0.09 * ux, y: y + 0.015, z: z - 0.09 * uz }, true);
this.anchorBody.setRotation(yawQuat, true);
this.sliderBody.setTranslation({
x: x + this.initialSliderOffset * ux,
y: y + 0.024,
z: z + this.initialSliderOffset * uz,
}, true);
this.sliderBody.setRotation(yawQuat, true);
this.sliderBody.setLinvel(zeroVel, true);
this.sliderBody.setAngvel(zeroVel, true);
this.riserBody.setTranslation({ x, y: y + 0.038, z }, true);
this.riserBody.setRotation(yawQuat, true);
this.currentProgress = 0.0;
this.syncVisuals();
}
public setPosition(pos: { x: number; y: number; z: number }, yawDegrees?: number): void {
this.basePos = { ...pos };
if (yawDegrees !== undefined) {
this.yawDegrees = yawDegrees;
}
this.reset();
this.updateDualLockVisualMesh();
}
public setDualLocked(locked: boolean, anchorPoint?: { x: number; z: number }): void {
this.isDualLocked = locked;
if (locked) {
if (anchorPoint) {
const radYaw = (this.yawDegrees * Math.PI) / 180;
const cosY = Math.cos(radYaw);
const sinY = Math.sin(radYaw);
const dx = anchorPoint.x - this.basePos.x;
const dz = anchorPoint.z - this.basePos.z;
this.dualLockLocalOffset = {
x: dx * cosY - dz * sinY,
z: dx * sinY + dz * cosY,
};
} else if (!this.dualLockLocalOffset) {
this.dualLockLocalOffset = { x: -0.09, z: 0 };
}
} else {
this.dualLockLocalOffset = null;
}
if (this.anchorBody) {
this.anchorBody.setBodyType(
locked ? RAPIER.RigidBodyType.Fixed : RAPIER.RigidBodyType.Dynamic,
true
);
if (!locked) {
this.anchorBody.setLinearDamping(2.5);
this.anchorBody.setAngularDamping(3.5);
this.anchorBody.wakeUp();
}
}
this.updateDualLockVisualMesh();
}
public getDualLockPosition(): { x: number; z: number } | null {
if (!this.isDualLocked) return null;
const offset = this.dualLockLocalOffset || { x: -0.09, z: 0 };
const radYaw = (this.yawDegrees * Math.PI) / 180;
const cosY = Math.cos(radYaw);
const sinY = Math.sin(radYaw);
return {
x: this.basePos.x + (offset.x * cosY + offset.z * sinY),
z: this.basePos.z + (-offset.x * sinY + offset.z * cosY),
};
}
private updateDualLockVisualMesh(): void {
if (!this.dualLockMarker) {
this.dualLockMarker = new DualLockMarker(this.id);
this.rootGroup.add(this.dualLockMarker.group);
}
this.dualLockMarker.setVisible(this.isDualLocked);
if (this.isDualLocked) {
const pos = this.getDualLockPosition() || this.basePos;
this.dualLockMarker.setPosition(pos.x, 0.002, pos.z);
}
}
public getDualLockMarker(): DualLockMarker | null {
return this.dualLockMarker;
}
public setDualLockHoverHighlight(isHovered: boolean, isEraseMode: boolean = false): void {
this.dualLockMarker?.setHoverHighlight(isHovered, isEraseMode);
}
public setRotation(yawDegrees: number): void {
if (this.isDualLocked) {
const fixedPoint = this.getDualLockPosition();
if (fixedPoint) {
const offset = this.dualLockLocalOffset || { x: -0.09, z: 0 };
const radNew = (yawDegrees * Math.PI) / 180;
const cosN = Math.cos(radNew);
const sinN = Math.sin(radNew);
this.basePos.x = fixedPoint.x - (offset.x * cosN + offset.z * sinN);
this.basePos.z = fixedPoint.z - (-offset.x * sinN + offset.z * cosN);
}
}
this.yawDegrees = yawDegrees;
this.reset();
this.updateDualLockVisualMesh();
}
public getYawDegrees(): number {
return this.yawDegrees;
}
public getPosition(): { x: number; y: number; z: number } {
return { ...this.basePos };
}
public isSolved(): boolean {
this.updateKinematics();
return this.currentProgress >= 0.70; // Elevates by at least 70%
}
public getScore(): number {
this.updateKinematics();
return Math.min(100, Math.max(0, Math.round(this.currentProgress * 100)));
}
public getInteractiveMeshes(): THREE.Object3D[] {
return this.sliderMesh ? [this.sliderMesh] : [];
}
public applyUserDrag(groundTarget: THREE.Vector3): void {
if (!this.sliderBody) return;
const { ux, uz } = this.getDirectionVectors();
const currTrans = this.sliderBody.translation();
const currS = (currTrans.x - this.basePos.x) * ux + (currTrans.z - this.basePos.z) * uz;
const targetS = THREE.MathUtils.clamp(
(groundTarget.x - this.basePos.x) * ux + (groundTarget.z - this.basePos.z) * uz,
0.035,
this.initialSliderOffset
);
const velS = (targetS - currS) * 15.0;
this.sliderBody.setLinvel({ x: velS * ux, y: 0, z: velS * uz }, true);
}
public destroy(): void {
const bodies = [this.anchorBody, this.sliderBody, this.riserBody];
for (const b of bodies) {
if (b) this.world.removeRigidBody(b);
}
this.dualLockMarker?.destroy();
this.rootGroup.clear();
}
}
|