three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Issue with object positioning in webGL on drag

Open abincg opened this issue 13 years ago • 1 comments

I am working with a webGl program in which I loads objects in the webGL container using THREE.OBJMTLLoader(). But when I used raycaster to select the intersecting object, it selects only the intersecting texture of the object. So I changed my code to select the parent of the intersecting object and the selection is working properly.

But the issue is that, when I select and drag the object to place in a new position, it go back to the origin and get dragged and placed with respect to the origin. Not with respect to the position from which I selected the object.

The below code is used by me to perform the events. Could any one help me on this issue..?

Note:

    mouse.x = ((event.pageX-(curX+12))/width)*2-1;
    mouse.y = -((event.pageY-curY)/height)*2+1;

The above code may look different. But its ok. I placed my webGL container as a part of HTML page. So I takes the mouse position with respect to the page position and the container position. curX and curY denotes the top and left position of the container with respect to the page.

function onDocumentMouseMove( event ) { event.preventDefault();

        mouse.x = ((event.pageX-(curX+12))/width)*2-1;
        mouse.y = -((event.pageY-curY)/height)*2+1;


        var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
        projector.unprojectVector( vector, camera );

        var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );


        if ( SELECTED ) {
            var intersects = raycaster.intersectObject( plane);
            SELECTED.position.copy( intersects[ 0 ].point.sub( offset ) );
            return;
                    }


        var intersects = raycaster.intersectObjects( objects, true );

        if ( intersects.length > 0 ) {

            if ( INTERSECTED != intersects[ 0 ].object) {

                if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );

                INTERSECTED = intersects[ 0 ].object;
                INTERSECTED.currentHex = INTERSECTED.material.color.getHex();

                plane.position.copy( INTERSECTED.position );
                plane.lookAt( camera.position );

            }

            container.style.cursor = 'pointer';

        } else {

            if ( INTERSECTED ) INTERSECTED.material.color.setHex( INTERSECTED.currentHex );

            INTERSECTED = null;

            container.style.cursor = 'auto';

        }
    }

    function onDocumentMouseDown( event ) {

        event.preventDefault();

        var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
        projector.unprojectVector( vector, camera );

        var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

        var intersects = raycaster.intersectObjects( objects, true );


        if ( intersects.length > 0 ) {

            controls.enabled = false;

            SELECTED = intersects[ 0 ].object.parent;

            var intersects = raycaster.intersectObject( plane );
            offset.copy( intersects[ 0 ].point ).sub( plane.position );

            container.style.cursor = 'move';                    

        }

    }

    function onDocumentMouseUp( event ) {

        event.preventDefault();

        controls.enabled = true;

        if ( INTERSECTED ) {

            plane.position.copy( INTERSECTED.position );
            SELECTED = null;        
        }
     }

abincg avatar Jul 05 '13 07:07 abincg

As stated in the guidelines, help requests should be directed to stackoverflow. This board is for bugs and feature requests.

WestLangley avatar Jul 05 '13 14:07 WestLangley