0

So the idea is to create A plane at z = 0 and infinite x y.

then make A ray from the mouse position and find where it intersects.

I'm not sure how to create that plane and I don't know if the is the best way.

also this is a 2d scene and everything is on the z = 0

        var projector = new THREE.Projector();
        var  mouse_vector = new THREE.Vector3();
        var mouse = { x: 0, y: 0, z: 1 };
        ray = new THREE.Raycaster( new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0) ),
        var intersects = [];
        event_info.preventDefault(); 
        mouse.x = ( event_info.clientX / window.innerWidth ) * 2 – 1;
        mouse.y = – ( event_info.clientY / window.innerHeight ) * 2 + 1;
        mouse_vector.set( mouse.x, mouse.y, mouse.z );
        projector.unprojectVector( mouse_vector, camera );
        var direction = mouse_vector.sub( camera.position ).normalize();
        ray = ray.set( camera.position, direction );



        var plane = new THREE.Plane();
        plane.set(v1, v2, v3);
        var where = ray.intersectPlane (plane);

        intersects = ray.intersectPlane( Plane );

            alert(intersects);

        }
contehhh
  • 122
  • 1
  • 11
  • If it's a 2D scene and everything's on `z = 0` then doesn't it stand to reason that your mouse position is *always* going to be `0` on the z-axis? – monners Jan 18 '14 at 07:43
  • yes, however the camera z-axis can change. I am looking for the x and y coordinates that the mouse is hovering on. I'm starting to think that doing some math with the camera z-axis and the size of the canvas/window might eliminate all of this. – contehhh Jan 18 '14 at 07:49
  • testing this http://stackoverflow.com/questions/13055214/mouse-canvas-x-y-to-three-js-world-x-y-z – contehhh Jan 18 '14 at 08:19
  • this gave me the following number only on the x axis 3435.0137722635331010101010-8.120402680783053 – contehhh Jan 18 '14 at 08:43
  • Congratulations on solving your problem. To properly close this question, rather than edit it and add your solution in, simply create an answer to your question, and paste your solution. You will be able to accept the answer in two days. That's how we do it at Stackoverflow. – MPelletier Jan 18 '14 at 16:15
  • When in Rome right?! Turns out i was getting that number because i needed to multiply a number by 1 before factoring. it was adding as a string. – contehhh Jan 18 '14 at 18:07

1 Answers1

1
        function getXY(cX, cY){
            var projector = new THREE.Projector();
                    var planeZ = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
                    var mv = new THREE.Vector3(
                        (cX / window.innerWidth) * 2 - 1,
                        -(cY / window.innerHeight) * 2 + 1,
                        0.5 );
                    var raycaster = projector.pickingRay(mv, camera);
                    var pos = raycaster.ray.intersectPlane(planeZ);

                    return pos;
        }

this works perfectly.

contehhh
  • 122
  • 1
  • 11