Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get 3d coordinates from 2d point #312

Closed
bagg opened this issue Jul 1, 2011 · 6 comments
Closed

Get 3d coordinates from 2d point #312

bagg opened this issue Jul 1, 2011 · 6 comments
Labels

Comments

@bagg
Copy link

bagg commented Jul 1, 2011

Hi guys

I would like to know how can i get the 3d coordinates from a collision point mouse 2d
from this code

function onDocumentMouseDown( event ) {
   event.preventDefault();
   mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
   mouse2d.y = - ( event.clientY  / window.innerHeight ) * 2 + 1;
   mouse2d.z = 1;

   var r = new THREE.Ray();
   r.origin.copy( mouse2d );
   var matrix = camera.matrixWorld.clone();
   matrix.multiplySelf( THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
   matrix.multiplyVector3( r.origin );
   r.direction = r.origin.clone().subSelf( camera.position );
   var c = THREE.Collisions.rayCastNearest( r );
   if( c ) {
//////////////////HERE GET THE 3D POINT COORDINATES////////////////////////
      c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
   } else {
      plane.materials[0].color.setHex( 0xF9EFD7 );
   }
};

The collision object is a plane which i use as ground and i want to know the collision point 3d coords in order to set an object's position.
Thanks in advance

@bagg
Copy link
Author

bagg commented Jul 1, 2011

Never mind found it

@bagg bagg closed this as completed Jul 1, 2011
@mrdoob
Copy link
Owner

mrdoob commented Jul 1, 2011

How did you end up doing it?

@inear
Copy link

inear commented Aug 3, 2011

I'm having this problem myself. I'm not even get a hit from the collider. How did you solve it?

@mrdoob
Copy link
Owner

mrdoob commented Aug 3, 2011

@inear: did you try with the Ray class?

@inear
Copy link

inear commented Aug 3, 2011

Thanks! That works like a charm. The point-property is just what I needed.

var vector = mouse2D;
projector.unprojectVector( vector, camera );
var r = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
var c = r.intersectObject(wave)
if( c.length > 0 ) {
   sphere.position = c[0].point;
}

@halay08
Copy link

halay08 commented May 27, 2017

var getZvalue = function (e)
{
	var rect = e.target.getBoundingClientRect();
	// 2D
	var mouseX = e.clientX - rect.left;
	var mouseY = e.clientY - rect.top;

	// 3D
	SCREEN_WIDTH = window.innerWidth;
	SCREEN_HEIGHT= window.innerHeight;
	var mouse = new THREE.Vector2();
	mouse.x = (mouseX/SCREEN_WIDTH) *2 - 1;
	mouse.y = -(mouseY/SCREEN_HEIGHT) *2 + 1;

	// Raycaster
	if (TerraDroneViewer.raycaster == null)
	{
		TerraDroneViewer.raycaster = new THREE.Raycaster();
	}

	TerraDroneViewer.raycaster.setFromCamera( mouse, TerraDroneViewer.camera );

	var intersects = TerraDroneViewer.raycaster.intersectObjects( TerraDroneViewer.maps.children );
	console.log("raycast=" + intersects.length);

	if (intersects.length != 0)
	{
		var x = mouse.x * 10;
		var y = mouse.y * 10;
		console.log("x = " + intersects[0].point.x + " y = " + intersects[0].point.y);
		var z = parseFloat(intersects[0].point.z) * 10;

		return {
                     x: x,
                     y: y,
                     z: z
                }
	}

        return null;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants