All files / src/sandbox interaction-tool.ts

0% Statements 0/167
0% Branches 0/1
0% Functions 0/1
0% Lines 0/167

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import * as THREE from 'three';
import RAPIER from '@dimforge/rapier3d-compat';
import { LEGO_COLORS, createLegoPlateGroup } from '../view/lego-visuals';
 
/**
 * Interactive Mouse Testing Tool & Dynamic Test Blocks
 *
 * Allows users in Sandbox Mode to physically manipulate mission mechanisms using
 * a mouse-controlled kinematic pusher probe and spawnable dynamic LEGO-style test blocks.
 */
export class SandboxInteractionTool {
  public rootGroup: THREE.Group;
  public isActive = true;
 
  private world!: RAPIER.World;
  private pusherBody: RAPIER.RigidBody | null = null;
  private pusherMesh: THREE.Group | null = null;
 
  // Dynamic test blocks spawned by user
  private spawnedBlocks: Array<{
    body: RAPIER.RigidBody;
    mesh: THREE.Object3D;
  }> = [];
 
  private currentTargetPos = new THREE.Vector3(0, 0.035, 0.20);
 
  constructor() {
    this.rootGroup = new THREE.Group();
  }
 
  public init(world: RAPIER.World, parentScene: THREE.Scene): void {
    this.world = world;
    parentScene.add(this.rootGroup);
 
    this.createPusherTool();
  }
 
  private createPusherTool(): void {
    // Kinematic position-based rigid body for physical contact forces
    const desc = RAPIER.RigidBodyDesc.kinematicPositionBased()
      .setTranslation(this.currentTargetPos.x, this.currentTargetPos.y, this.currentTargetPos.z);
    this.pusherBody = this.world.createRigidBody(desc);
 
    // Omni-directional cylindrical bumper: radius 4.2cm, half-height 2.5cm (spans Y = 0.010m to 0.060m)
    // Perfectly covers robot bumper, mission sliders, and turnstile blades
    const colliderDesc = RAPIER.ColliderDesc.cylinder(0.025, 0.042)
      .setFriction(0.8)
      .setRestitution(0.1);
    this.world.createCollider(colliderDesc, this.pusherBody);
 
    // Three.js visual mesh
    this.pusherMesh = new THREE.Group();
 
    // 1. High-visibility orange bumper body
    const cylinderGeom = new THREE.CylinderGeometry(0.042, 0.042, 0.046, 32);
    const cylinderMat = new THREE.MeshStandardMaterial({
      color: 0xf97316, // Vibrant safety orange
      roughness: 0.3,
      metalness: 0.2,
    });
    const cylinderMesh = new THREE.Mesh(cylinderGeom, cylinderMat);
    cylinderMesh.castShadow = true;
    this.pusherMesh.add(cylinderMesh);
 
    // 2. Heavy-duty rubber bumper ring
    const ringGeom = new THREE.TorusGeometry(0.042, 0.006, 12, 32);
    ringGeom.rotateX(Math.PI / 2);
    const ringMat = new THREE.MeshStandardMaterial({ color: 0x0f172a, roughness: 0.8 });
    const ringMesh = new THREE.Mesh(ringGeom, ringMat);
    this.pusherMesh.add(ringMesh);
 
    // 3. Ergonomic grip handle on top
    const handleGeom = new THREE.CylinderGeometry(0.014, 0.016, 0.035, 16);
    const handleMat = new THREE.MeshStandardMaterial({ color: 0x334155, roughness: 0.5, metalness: 0.3 });
    const handleMesh = new THREE.Mesh(handleGeom, handleMat);
    handleMesh.position.set(0, 0.036, 0);
    this.pusherMesh.add(handleMesh);
 
    // 4. Grip cap
    const capGeom = new THREE.SphereGeometry(0.018, 16, 12);
    const capMat = new THREE.MeshStandardMaterial({ color: 0x0284c7, roughness: 0.2 });
    const capMesh = new THREE.Mesh(capGeom, capMat);
    capMesh.position.set(0, 0.054, 0);
    this.pusherMesh.add(capMesh);
 
    this.rootGroup.add(this.pusherMesh);
    this.syncVisuals();
  }
 
  /**
   * Drops an authentic dynamic 2x4 LEGO brick with 8 studs onto the workbench
   */
  public spawnTestBlock(spawnPos?: { x: number; y: number; z: number }): void {
    const x = spawnPos?.x ?? (Math.random() * 0.1 - 0.05);
    const y = spawnPos?.y ?? 0.10; // Drop from above
    const z = spawnPos?.z ?? (0.08 + Math.random() * 0.06);
 
    const desc = RAPIER.RigidBodyDesc.dynamic()
      .setTranslation(x, y, z)
      .setLinearDamping(0.8)
      .setAngularDamping(1.5);
    const body = this.world.createRigidBody(desc);
 
    // Authentic LEGO 2x4 brick dimensions: 32mm x 16mm x 9.6mm
    const collider = RAPIER.ColliderDesc.cuboid(0.016, 0.0048, 0.008)
      .setDensity(1.2)
      .setFriction(0.65)
      .setRestitution(0.05);
    this.world.createCollider(collider, body);
 
    // Palette of vibrant LEGO colors
    const colors = [
      LEGO_COLORS.AZURE,
      LEGO_COLORS.RED,
      LEGO_COLORS.GREEN,
      LEGO_COLORS.YELLOW,
      LEGO_COLORS.ORANGE,
    ];
    const color = colors[this.spawnedBlocks.length % colors.length];
 
    // Real LEGO 2x4 brick with 8 embossed cylindrical studs
    const mesh = createLegoPlateGroup(4, 2, color, 3);
    mesh.castShadow = true;
    mesh.receiveShadow = true;
    this.rootGroup.add(mesh);
 
    this.spawnedBlocks.push({ body, mesh });
  }
 
  public getSpawnedBlocks(): Array<{ body: RAPIER.RigidBody; mesh: THREE.Object3D }> {
    return this.spawnedBlocks;
  }
 
  public dragBlock(item: { body: RAPIER.RigidBody; mesh: THREE.Object3D }, x: number, z: number): void {
    const clampedX = THREE.MathUtils.clamp(x, -0.45, 0.45);
    const clampedZ = THREE.MathUtils.clamp(z, -0.45, 0.45);
    item.body.setTranslation({ x: clampedX, y: 0.012, z: clampedZ }, true);
    item.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
    item.mesh.position.set(clampedX, 0.012, clampedZ);
  }
 
  public setPusherInitialPose(pos: { x: number; y: number; z: number }): void {
    this.currentTargetPos.set(pos.x, pos.y, pos.z);
    if (this.pusherBody) {
      this.pusherBody.setTranslation({ x: pos.x, y: pos.y, z: pos.z }, true);
    }
    if (this.pusherMesh) {
      this.pusherMesh.position.set(pos.x, pos.y, pos.z);
    }
  }
 
  /**
   * Updates target position of pusher tool based on mouse raycast coordinates
   */
  public movePusherTo(x: number, z: number): void {
    if (!this.pusherBody || !this.isActive) return;
 
    // Constrain tool to reasonable test bench boundaries
    const clampedX = THREE.MathUtils.clamp(x, -0.48, 0.48);
    const clampedZ = THREE.MathUtils.clamp(z, -0.48, 0.48);
    this.currentTargetPos.set(clampedX, 0.035, clampedZ);
 
    this.pusherBody.setNextKinematicTranslation({
      x: this.currentTargetPos.x,
      y: this.currentTargetPos.y,
      z: this.currentTargetPos.z,
    });
 
    // Immediate visual update for ultra-smooth responsiveness
    if (this.pusherMesh) {
      this.pusherMesh.position.set(clampedX, 0.035, clampedZ);
    }
  }
 
  public setActive(active: boolean): void {
    this.isActive = active;
    this.rootGroup.visible = active;
    if (this.pusherBody) {
      if (active) {
        this.pusherBody.setTranslation({
          x: this.currentTargetPos.x,
          y: this.currentTargetPos.y,
          z: this.currentTargetPos.z,
        }, true);
      } else {
        // Move off-stage when inactive
        this.pusherBody.setTranslation({ x: 0, y: -50, z: 0 }, true);
      }
    }
  }
 
  public syncVisuals(): void {
    if (this.pusherBody && this.pusherMesh) {
      const p = this.pusherBody.translation();
      const r = this.pusherBody.rotation();
      this.pusherMesh.position.set(p.x, p.y, p.z);
      this.pusherMesh.quaternion.set(r.x, r.y, r.z, r.w);
    }
 
    // Sync spawned blocks
    for (const item of this.spawnedBlocks) {
      const p = item.body.translation();
      const r = item.body.rotation();
      item.mesh.position.set(p.x, p.y, p.z);
      item.mesh.quaternion.set(r.x, r.y, r.z, r.w);
    }
  }
 
  public resetPusher(): void {
    if (!this.pusherBody) return;
    this.currentTargetPos.set(0, 0.035, 0.20);
    this.pusherBody.setTranslation({ x: 0, y: 0.035, z: 0.20 }, true);
    this.syncVisuals();
  }
 
  public clearSpawnedBlocks(): void {
    for (const item of this.spawnedBlocks) {
      this.world.removeRigidBody(item.body);
      this.rootGroup.remove(item.mesh);
    }
    this.spawnedBlocks = [];
  }
 
  public resetAll(): void {
    this.resetPusher();
    this.clearSpawnedBlocks();
  }
 
  public getPusherMesh(): THREE.Object3D | null {
    return this.pusherMesh;
  }
}