Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

Hallo lieber KI-Chatbot, Ich habe eine Frage zu babylon.js und Typescript. Bei einer…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User

    Hallo lieber KI-Chatbot,

    Ich habe eine Frage zu babylon.js und Typescript. Bei einer ArcRotateCamera gibt es camera.zoomToMouseLocation. Ich möchte das jetzt auch mit Pinch-Gesten erreichen.

    Ich habe es wie folgt versucht. Das funktioniert leider sehr unzuverlässig Kannst du helfen ?

    private activePointers: Map<number, BABYLON.Vector2> = new Map();
    private lastPinchDistance: number = 0;
    private initialRadius: number | null = null;
    private pointerObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.PointerInfo>> = null;
    
    public enablePinchZoom(): void {
        const camera = this.camera.babylonCamera;
        const scene = this._babylon.scene;
    
        const pointersInput = camera.inputs.attached.pointers as BABYLON.ArcRotateCameraPointersInput;
        if (pointersInput) {
            pointersInput.pinchZoom = false; // Standard-Pinch-Zoom deaktivieren
        } else {
            console.warn("no cam-controls");
            return;
        }
    
        if (this.pointerObserver) {
            scene.onPointerObservable.remove(this.pointerObserver);
        }
    
        this.pointerObserver = scene.onPointerObservable.add((pointerInfo: BABYLON.PointerInfo) => {
            const event = pointerInfo.event as PointerEvent;
    
            switch (pointerInfo.type) {
                case BABYLON.PointerEventTypes.POINTERDOWN:
                    this.activePointers.set(event.pointerId, new BABYLON.Vector2(event.clientX, event.clientY));
    
                    if (this.activePointers.size === 2) {
                        const pointerPositions = Array.from(this.activePointers.values());
                        this.lastPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        this.initialRadius = camera.radius;
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERUP:
                    this.activePointers.delete(event.pointerId);
    
                    if (this.activePointers.size < 2) {
                        this.lastPinchDistance = 0;
                        this.initialRadius = null;
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERMOVE:
                    if (this.activePointers.size === 2 && this.initialRadius !== null) {
                        const pointerPosition = this.activePointers.get(event.pointerId);
                        if (!pointerPosition) return;
                        pointerPosition.set(event.clientX, event.clientY);
                        const pointerPositions = Array.from(this.activePointers.values());
                        const newPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        if (this.lastPinchDistance === 0 || newPinchDistance === 0) {
                            this.lastPinchDistance = newPinchDistance;
                            return;
                        }
                        const zoomFactor = newPinchDistance / this.lastPinchDistance;
                        const newRadius = this.initialRadius / zoomFactor;
                        camera.radius = newRadius;
                        this.lastPinchDistance = newPinchDistance;
                    }
                    break;
            }
        });
    }
    
  2. Claude Assistant

    Ich sehe das Problem mit deinem Pinch-Zoom-Code. Hier sind einige Verbesserungen, die die Zuverlässigkeit erhöhen sollten:

    private activePointers: Map<number, BABYLON.Vector2> = new Map();
    private lastPinchDistance: number = 0;
    private initialRadius: number | null = null;
    private pointerObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.PointerInfo>> = null;
    private pinchCenter: BABYLON.Vector2 | null = null;
    
    public enablePinchZoom(): void {
        const camera = this.camera.babylonCamera as BABYLON.ArcRotateCamera;
        const scene = this._babylon.scene;
    
        const pointersInput = camera.inputs.attached.pointers as BABYLON.ArcRotateCameraPointersInput;
        if (pointersInput) {
            pointersInput.pinchZoom = false; // Standard-Pinch-Zoom deaktivieren
        } else {
            console.warn("no cam-controls");
            return;
        }
    
        if (this.pointerObserver) {
            scene.onPointerObservable.remove(this.pointerObserver);
        }
    
        this.pointerObserver = scene.onPointerObservable.add((pointerInfo: BABYLON.PointerInfo) => {
            const event = pointerInfo.event as PointerEvent;
    
            switch (pointerInfo.type) {
                case BABYLON.PointerEventTypes.POINTERDOWN:
                    this.activePointers.set(event.pointerId, new BABYLON.Vector2(event.clientX, event.clientY));
    
                    if (this.activePointers.size === 2) {
                        const pointerPositions = Array.from(this.activePointers.values());
                        this.lastPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        this.initialRadius = camera.radius;
                        
                        // Berechne den Mittelpunkt zwischen den beiden Fingern
                        this.pinchCenter = new BABYLON.Vector2(
                            (pointerPositions[0].x + pointerPositions[1].x) / 2,
                            (pointerPositions[0].y + pointerPositions[1].y) / 2
                        );
                        
                        // Konvertiere den Pinch-Center in einen Strahl für die Kamera
                        const ray = scene.createPickingRay(
                            this.pinchCenter.x,
                            this.pinchCenter.y,
                            BABYLON.Matrix.Identity(),
                            camera
                        );
                        
                        // Speichere den aktuellen Target-Punkt der Kamera
                        camera.storeState();
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERUP:
                case BABYLON.PointerEventTypes.POINTERCANCEL:
                    this.activePointers.delete(event.pointerId);
    
                    if (this.activePointers.size < 2) {
                        this.lastPinchDistance = 0;
                        this.initialRadius = null;
                        this.pinchCenter = null;
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERMOVE:
                    if (this.activePointers.size === 2 && this.initialRadius !== null && this.pinchCenter !== null) {
                        // Aktualisiere die Position des bewegten Pointers
                        const pointerPosition = this.activePointers.get(event.pointerId);
                        if (!pointerPosition) return;
                        pointerPosition.set(event.clientX, event.clientY);
                        
                        // Berechne die neue Pinch-Distanz
                        const pointerPositions = Array.from(this.activePointers.values());
                        const newPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        
                        // Verhindere Division durch Null
                        if (this.lastPinchDistance <= 0 || newPinchDistance <= 0) {
                            this.lastPinchDistance = Math.max(1, newPinchDistance);
                            return;
                        }
                        
                        // Berechne den Zoom-Faktor
                        const zoomFactor = newPinchDistance / this.lastPinchDistance;
                        
                        // Begrenze den Zoom-Faktor, um extreme Änderungen zu vermeiden
                        const limitedZoomFactor = Math.min(Math.max(zoomFactor, 0.9), 1.1);
                        
                        // Aktualisiere den Pinch-Center
                        this.pinchCenter.x = (pointerPositions[0].x + pointerPositions[1].x) / 2;
                        this.pinchCenter.y = (pointerPositions[0].y + pointerPositions[1].y) / 2;
                        
                        // Verwende zoomToMouseLocation mit dem Pinch-Center
                        const zoomToTarget = camera.zoomToMouseLocation;
                        if (zoomToTarget) {
                            // Erstelle einen Picking-Ray vom Pinch-Center
                            const ray = scene.createPickingRay(
                                this.pinchCenter.x,
                                this.pinchCenter.y,
                                BABYLON.Matrix.Identity(),
                                camera
                            );
                            
                            // Berechne den Schnittpunkt mit der Szene
                            const pickInfo = scene.pickWithRay(ray);
                            
                            if (pickInfo && pickInfo.hit && pickInfo.pickedPoint) {
                                // Wenn ein Objekt getroffen wurde, zoome zu diesem Punkt
                                const direction = pickInfo.pickedPoint.subtract(camera.position).normalize();
                                const distance = BABYLON.Vector3.Distance(camera.position, pickInfo.pickedPoint);
                                
                                // Berechne die neue Position basierend auf dem Zoom-Faktor
                                const newDistance = distance / limitedZoomFactor;
                                const newPosition = camera.position.add(direction.scale(distance - newDistance));
                                
                                camera.position = newPosition;
                            } else {
                                // Wenn kein Objekt getroffen wurde, verwende den Standard-Zoom
                                const newRadius = camera.radius / limitedZoomFactor;
                                camera.radius = newRadius;
                            }
                        } else {
                            // Fallback, wenn zoomToMouseLocation nicht verfügbar ist
                            const newRadius = camera.radius / limitedZoomFactor;
                            camera.radius = newRadius;
                        }
                        
                        this.lastPinchDistance = newPinchDistance;
                    }
                    break;
            }
        });
    }
    

    Die wichtigsten Verbesserungen sind:

    1. Pinch-Center-Berechnung: Ich berechne den Mittelpunkt zwischen den beiden Fingern und verwende diesen als Fokuspunkt für den Zoom.

    2. Picking-Ray-Verwendung: Ich erstelle einen Picking-Ray vom Pinch-Center aus, um den genauen Punkt in der 3D-Szene zu finden, auf den der Benutzer zoomt.

    3. Begrenzter Zoom-Faktor: Ich begrenze den Zoom-Faktor auf einen vernünftigen Bereich (0.9 bis 1.1), um abrupte Änderungen zu vermeiden.

    4. Bessere Fehlerbehandlung: Ich habe zusätzliche Prüfungen hinzugefügt, um Probleme mit ungültigen Werten zu vermeiden.

    5. POINTERCANCEL-Behandlung: Ich habe die Behandlung von POINTERCANCEL-Events hinzugefügt, was bei Touch-Geräten wichtig sein kann.

    6. Kamera-Zustandsspeicherung: Ich speichere den Kamerazustand zu Beginn des Pinch-Zooms, um eine bessere Referenz zu haben.

    Diese Implementierung sollte zuverlässiger funktionieren, da sie:

    • Den Zoom-Punkt genauer bestimmt
    • Extreme Zoom-Änderungen verhindert
    • Besser mit Edge-Cases umgeht
    • Die Kamera-Position und nicht nur den Radius anpasst

    Möchtest du, dass ich bestimmte Teile des Codes genauer erkläre?

  3. User

    Es gibt gein POINTERCANCEL, das ist ein dom-event

  4. Claude Assistant

    Du hast Recht, ich habe einen Fehler gemacht. In Babylon.js gibt es tatsächlich kein BABYLON.PointerEventTypes.POINTERCANCEL. Hier ist die korrigierte Version ohne diesen Event-Typ:

    private activePointers: Map<number, BABYLON.Vector2> = new Map();
    private lastPinchDistance: number = 0;
    private initialRadius: number | null = null;
    private pointerObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.PointerInfo>> = null;
    private pinchCenter: BABYLON.Vector2 | null = null;
    
    public enablePinchZoom(): void {
        const camera = this.camera.babylonCamera as BABYLON.ArcRotateCamera;
        const scene = this._babylon.scene;
    
        const pointersInput = camera.inputs.attached.pointers as BABYLON.ArcRotateCameraPointersInput;
        if (pointersInput) {
            pointersInput.pinchZoom = false; // Standard-Pinch-Zoom deaktivieren
        } else {
            console.warn("no cam-controls");
            return;
        }
    
        if (this.pointerObserver) {
            scene.onPointerObservable.remove(this.pointerObserver);
        }
    
        this.pointerObserver = scene.onPointerObservable.add((pointerInfo: BABYLON.PointerInfo) => {
            const event = pointerInfo.event as PointerEvent;
    
            switch (pointerInfo.type) {
                case BABYLON.PointerEventTypes.POINTERDOWN:
                    this.activePointers.set(event.pointerId, new BABYLON.Vector2(event.clientX, event.clientY));
    
                    if (this.activePointers.size === 2) {
                        const pointerPositions = Array.from(this.activePointers.values());
                        this.lastPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        this.initialRadius = camera.radius;
                        
                        // Berechne den Mittelpunkt zwischen den beiden Fingern
                        this.pinchCenter = new BABYLON.Vector2(
                            (pointerPositions[0].x + pointerPositions[1].x) / 2,
                            (pointerPositions[0].y + pointerPositions[1].y) / 2
                        );
                        
                        // Speichere den aktuellen Target-Punkt der Kamera
                        camera.storeState();
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERUP:
                    this.activePointers.delete(event.pointerId);
    
                    if (this.activePointers.size < 2) {
                        this.lastPinchDistance = 0;
                        this.initialRadius = null;
                        this.pinchCenter = null;
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERMOVE:
                    if (this.activePointers.size === 2 && this.initialRadius !== null && this.pinchCenter !== null) {
                        // Aktualisiere die Position des bewegten Pointers
                        const pointerPosition = this.activePointers.get(event.pointerId);
                        if (!pointerPosition) return;
                        pointerPosition.set(event.clientX, event.clientY);
                        
                        // Berechne die neue Pinch-Distanz
                        const pointerPositions = Array.from(this.activePointers.values());
                        const newPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        
                        // Verhindere Division durch Null
                        if (this.lastPinchDistance <= 0 || newPinchDistance <= 0) {
                            this.lastPinchDistance = Math.max(1, newPinchDistance);
                            return;
                        }
                        
                        // Berechne den Zoom-Faktor
                        const zoomFactor = newPinchDistance / this.lastPinchDistance;
                        
                        // Begrenze den Zoom-Faktor, um extreme Änderungen zu vermeiden
                        const limitedZoomFactor = Math.min(Math.max(zoomFactor, 0.9), 1.1);
                        
                        // Aktualisiere den Pinch-Center
                        this.pinchCenter.x = (pointerPositions[0].x + pointerPositions[1].x) / 2;
                        this.pinchCenter.y = (pointerPositions[0].y + pointerPositions[1].y) / 2;
                        
                        // Verwende zoomToMouseLocation mit dem Pinch-Center
                        if (camera.useInputToMoveLookAt) {
                            camera.inertialRadiusOffset = 0;
                            
                            // Erstelle einen Picking-Ray vom Pinch-Center
                            const ray = scene.createPickingRay(
                                this.pinchCenter.x,
                                this.pinchCenter.y,
                                BABYLON.Matrix.Identity(),
                                camera
                            );
                            
                            // Berechne den Schnittpunkt mit der Szene
                            const pickInfo = scene.pickWithRay(ray);
                            
                            if (pickInfo && pickInfo.hit && pickInfo.pickedPoint) {
                                // Wenn ein Objekt getroffen wurde, zoome zu diesem Punkt
                                camera.target = pickInfo.pickedPoint;
                                camera.radius = camera.radius / limitedZoomFactor;
                            } else {
                                // Wenn kein Objekt getroffen wurde, verwende den Standard-Zoom
                                camera.radius = camera.radius / limitedZoomFactor;
                            }
                        } else {
                            // Fallback, wenn useInputToMoveLookAt nicht aktiviert ist
                            camera.radius = camera.radius / limitedZoomFactor;
                        }
                        
                        this.lastPinchDistance = newPinchDistance;
                    }
                    break;
            }
        });
    }
    

    Ich habe auch einige weitere Verbesserungen vorgenommen:

    1. Entfernt den nicht existierenden POINTERCANCEL-Event-Typ
    2. Vereinfacht die Logik für das Zoomen zur Mausposition
    3. Verwendet camera.useInputToMoveLookAt als Bedingung für das Ändern des Zielpunkts
    4. Setzt camera.inertialRadiusOffset = 0, um unerwünschte Trägheitseffekte zu vermeiden

    Diese Version sollte besser mit der Babylon.js-API übereinstimmen und zuverlässiger funktionieren.

  5. User

    Es gibt kein zoomToMouseLocation

  6. Claude Assistant

    Du hast Recht, ich habe mich in der API geirrt. Lass mich eine korrekte Implementierung basierend auf den tatsächlichen Babylon.js-Funktionen erstellen:

    private activePointers: Map<number, BABYLON.Vector2> = new Map();
    private lastPinchDistance: number = 0;
    private initialRadius: number | null = null;
    private pointerObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.PointerInfo>> = null;
    private pinchCenter: BABYLON.Vector2 | null = null;
    
    public enablePinchZoom(): void {
        const camera = this.camera.babylonCamera as BABYLON.ArcRotateCamera;
        const scene = this._babylon.scene;
    
        const pointersInput = camera.inputs.attached.pointers as BABYLON.ArcRotateCameraPointersInput;
        if (pointersInput) {
            pointersInput.pinchZoom = false; // Standard-Pinch-Zoom deaktivieren
        } else {
            console.warn("no cam-controls");
            return;
        }
    
        if (this.pointerObserver) {
            scene.onPointerObservable.remove(this.pointerObserver);
        }
    
        this.pointerObserver = scene.onPointerObservable.add((pointerInfo: BABYLON.PointerInfo) => {
            const event = pointerInfo.event as PointerEvent;
    
            switch (pointerInfo.type) {
                case BABYLON.PointerEventTypes.POINTERDOWN:
                    this.activePointers.set(event.pointerId, new BABYLON.Vector2(event.clientX, event.clientY));
    
                    if (this.activePointers.size === 2) {
                        const pointerPositions = Array.from(this.activePointers.values());
                        this.lastPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        this.initialRadius = camera.radius;
                        
                        // Berechne den Mittelpunkt zwischen den beiden Fingern
                        this.pinchCenter = new BABYLON.Vector2(
                            (pointerPositions[0].x + pointerPositions[1].x) / 2,
                            (pointerPositions[0].y + pointerPositions[1].y) / 2
                        );
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERUP:
                    this.activePointers.delete(event.pointerId);
    
                    if (this.activePointers.size < 2) {
                        this.lastPinchDistance = 0;
                        this.initialRadius = null;
                        this.pinchCenter = null;
                    }
                    break;
    
                case BABYLON.PointerEventTypes.POINTERMOVE:
                    if (this.activePointers.size === 2 && this.initialRadius !== null && this.pinchCenter !== null) {
                        // Aktualisiere die Position des bewegten Pointers
                        const pointerPosition = this.activePointers.get(event.pointerId);
                        if (!pointerPosition) return;
                        pointerPosition.set(event.clientX, event.clientY);
                        
                        // Berechne die neue Pinch-Distanz
                        const pointerPositions = Array.from(this.activePointers.values());
                        const newPinchDistance = BABYLON.Vector2.Distance(pointerPositions[0], pointerPositions[1]);
                        
                        // Verhindere Division durch Null
                        if (this.lastPinchDistance <= 0 || newPinchDistance <= 0) {
                            this.lastPinchDistance = Math.max(1, newPinchDistance);
                            return;
                        }
                        
                        // Berechne den Zoom-Faktor
                        const zoomFactor = newPinchDistance / this.lastPinchDistance;
                        
                        // Begrenze den Zoom-Faktor, um extreme Änderungen zu vermeiden
                        const limitedZoomFactor = Math.min(Math.max(zoomFactor, 0.95), 1.05);
                        
                        // Aktualisiere den Pinch-Center
                        this.pinchCenter.x = (pointerPositions[0].x + pointerPositions[1].x) / 2;
                        this.pinchCenter.y = (pointerPositions[0].y + pointerPositions[1].y) / 2;
                        
                        // Erstelle einen Picking-Ray vom Pinch-Center
                        const ray = scene.createPickingRay(
                            this.pinchCenter.x,
                            this.pinchCenter.y,
                            BABYLON.Matrix.Identity(),
                            camera
                        );
                        
                        // Speichere die aktuelle Kameraposition und Target
                        const oldTarget = camera.target.clone();
                        const oldRadius = camera.radius;
                        
                        // Berechne den neuen Radius
                        const newRadius = oldRadius / limitedZoomFactor;
                        
                        // Berechne den Schnittpunkt mit der Szene
                        const pickInfo = scene.pickWithRay(ray);
                        
                        if (pickInfo && pickInfo.hit && pickInfo.pickedPoint) {
                            // Wenn ein Objekt getroffen wurde, verschiebe das Target in Richtung des getroffenen Punkts
                            const direction = pickInfo.pickedPoint.subtract(camera.position).normalize();
                            const targetDistance = BABYLON.Vector3.Distance(camera.position, oldTarget);
                            const hitDistance = BABYLON.Vector3.Distance(camera.position, pickInfo.pickedPoint);
                            
                            // Berechne, wie stark das Target verschoben werden soll (abhängig vom Zoom-Faktor)
                            const targetShift = (1 - limitedZoomFactor) * 0.5;
                            
                            // Verschiebe das Target in Richtung des getroffenen Punkts
                            if (limitedZoomFactor < 1) { // Zoom in
                                camera.target = oldTarget.add(direction.scale(targetShift * targetDistance));
                            } else { // Zoom out
                                camera.target = oldTarget.subtract(direction.scale(targetShift * targetDistance));
                            }
                        }
                        
                        // Setze den neuen Radius
                        camera.radius = newRadius;
                        
                        this.lastPinchDistance = newPinchDistance;
                    }
                    break;
            }
        });
    }
    

    Diese Implementierung:

    1. Berechnet den Mittelpunkt zwischen den beiden Fingern (Pinch-Center)
    2. Erstellt einen Picking-Ray von diesem Punkt aus
    3. Wenn ein Objekt getroffen wird, verschiebt das Target der Kamera leicht in Richtung des getroffenen Punkts
    4. Passt den Radius der Kamera basierend auf dem Zoom-Faktor an

    Diese Methode simuliert das Verhalten von "Zoom to Mouse Location", indem sie das Target der Kamera in Richtung des Punktes verschiebt, auf den der Benutzer zoomt. Das sorgt dafür, dass der Zoom-Fokus näher am Pinch-Center liegt.

    Ich habe auch den Zoom-Faktor noch stärker begrenzt (0.95 bis 1.05), um eine sanftere Zoom-Erfahrung zu gewährleisten.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.