iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚀

Three.js: Correctly Drag and Drop Grouped Objects by Adjusting Local Coordinates

に公開

Conclusion

  • Calculate the coordinates of the Group (parent) and the Object (child)
    • Object coordinates - Group coordinates
let parent = this.draggable.parent
// Move the position of the object to be dragged to the intersection coordinates
this.draggable.position.x = target.x - parent.position.x; // Get parent (group) and perform coordinate correction
this.draggable.position.y = target.y - parent.position.y;

Calculate the coordinates of the slider (the child object being dragged) by obtaining the coordinates of the parent (group).

I've written this here as a reminder for myself. Since the program runs within a custom class, please interpret this.raycaster, etc., by removing this or adapting it to your context. Also, please note that I am focusing solely on the position correction for Drag & Drop.

What kind of bug is it?

Nature of the bug

https://twitter.com/Tebasaki_lab/status/1704015195298037793

Result of fix

https://twitter.com/Tebasaki_lab/status/1704015401817096481

Solution

Object definition

const geometry = new THREE.BoxGeometry(width, height, depth);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
let backMesh=new THREE.Mesh(geometry, material);
backMesh.userData.ground = true;

const sliderGeo = new THREE.BoxGeometry(3, 2,2);
const sliderMat = new THREE.MeshBasicMaterial({ color: 0x0000ff });
let sliderMesh = new THREE.Mesh(sliderGeo, sliderMat);
sliderMesh.userData.draggable=true
sliderMesh.userData.name="SLIDER"

this.obj = new THREE.Group();

this.obj.add(backMesh);
this.obj.add(sliderMesh);

This part is as usual.

Drag & Drop processing

let parent = this.draggable.parent
// Move the position of the object to be dragged to the intersection coordinates
this.draggable.position.x = target.x - parent.position.x; // Get parent (group) and perform coordinate correction
this.draggable.position.y = target.y - parent.position.y;

Ultimately, this is what matters.

    dragObject() {
        if (this.draggable != null) {
            this.raycaster.setFromCamera(this.pointer, this.camera);
            const found = this.raycaster.intersectObjects(this.scene.children); // Get intersected objects
            if (found.length > 0) {
                for (let i = 0; i < found.length; i++) {
                    if (!found[i].object.userData.ground) continue; // If it's an object other than ground, do not get coordinates

                    let target = found[i].point; // Get intersection coordinates when hitting ground, etc.
                    if (this.draggable.userData.name == "SLIDER") {
                        let parent = this.draggable.parent
                        // Move the position of the object to be dragged to the intersection coordinates
+                        this.draggable.position.x = target.x - parent.position.x; // Get parent (group) and perform coordinate correction
+                        this.draggable.position.y = target.y - parent.position.y;
                        
                    }else{
                        this.draggable.position.x = target.x;
                        this.draggable.position.y = target.y;
                    }

                }
            }
        }
    }

Discussion