Untitled

 avatar
unknown
plain_text
3 months ago
3.4 kB
2
Indexable
 private findEquipmentTopToSnapTo(position: { x: number; z: number }, equipment: Equipment): SnapResult | null {
        if (!this.selectedObject) {
            return null;
        }
        // Get current equipment height
        const currentBoundingBox = this.scene.getMeshByName(MeshEnum.BOUNDING_BOX + equipment.id);
        if (!currentBoundingBox) return null;
        const currentHeight = currentBoundingBox.getBoundingInfo().boundingBox.extendSize.y * 2;
        // Get all equipment transform nodes except the current one
        const equipmentTransformNodes: BABYLON.TransformNode[] = [];
        this.scene.transformNodes.forEach((node: BABYLON.TransformNode) => {
            if (node.name.startsWith(MeshEnum.TRANSFORM_NODE) && node.name !== MeshEnum.TRANSFORM_NODE + equipment.id) {
                equipmentTransformNodes.push(node);
            }
        });

        let closest = null;
        let minDistance = this.SNAP_THRESHOLD;

        for (const transformNode of equipmentTransformNodes) {
            const equipmentIdMatch = transformNode.name.match(/^transformNode(\d+)$/);
            if (!equipmentIdMatch) continue;

            const equipmentId = parseInt(equipmentIdMatch[1], 10);
            if (isNaN(equipmentId)) continue;

            const otherEquipment = this.findEquipmentById(equipmentId);
            if (!otherEquipment) continue;

            const boundingBox = this.scene.getMeshByName(MeshEnum.BOUNDING_BOX + equipmentId);
            if (!boundingBox) continue;

            // Check if the position is within the x/z bounds of the other equipment
            const boxSize = boundingBox.getBoundingInfo().boundingBox.extendSize;
            const otherEquipmentWidth = boxSize.x * 2;
            const otherEquipmentLength = boxSize.z * 2;

            // Check if position is over the other equipment
            if (
                Math.abs(position.x - transformNode.position.x) <= otherEquipmentWidth / 2 + this.SNAP_THRESHOLD &&
                Math.abs(position.z - transformNode.position.z) <= otherEquipmentLength / 2 + this.SNAP_THRESHOLD
            ) {
                // Calculate top position
                const otherHeight = boxSize.y * 2;
                // Calculate vertical distance to the top
                const topY = transformNode.position.y + otherHeight / 2;
                const verticalDistance = Math.abs(this.selectedObject.position.y - topY + currentHeight / 2);
                console.log(
                    'verticalDistance',
                    verticalDistance,
                    'minDistance',
                    minDistance,
                    'currentHeight',
                    currentHeight,
                    'otherHeight',
                    otherHeight,
                );
                if (verticalDistance < minDistance) {
                    minDistance = verticalDistance;
                    closest = {
                        position: { x: position.x, z: position.z },
                        distance: minDistance,
                        snapY: topY - currentHeight / 2, // Store the Y position to snap to
                    };
                }
            }
        }

        if (closest) {
            // Apply vertical snap
            console.log('closest', closest);
            this.selectedObject.position.y = closest.snapY;
        }

        return closest;
    }
Editor is loading...
Leave a Comment