A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help with converting mouse points(x,y) to 3d(x,y,z)

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    1

    Help with converting mouse points(x,y) to 3d(x,y,z)

    Hi,
    This problem is causing me big headaches. Rotating and projecting in a 3d is easy... Going backwards where getting 3d coordinates from a mouse coordinate (x,y) is really hard.
    Please help if you can.

    mapToScreen works... mapTo3D doesn't.... and the problem is in the applying the perspective code.. I took them out and got it to work but it needs perspective projection.
    Code:
    	function mapToScreen (xpp:Number, ypp:Number, zpp:Number) : Object {
    		
    		//transform - rotate object
    		var yp = ypp;
    		var xp = xpp*Math.cos(alpha) + zpp*Math.sin(alpha);
    		var zp = zpp*Math.cos(alpha) - xpp*Math.sin(alpha);
    		var x1 = xp;
    		var y1 = yp*Math.cos(theta)-zp*Math.sin(theta);
    		var z1 = zp*Math.cos(theta)+yp*Math.sin(theta);
    		
    		//apply perspective
    		var pers = d/(d + z1);
    		var x2 = x1 * pers;
    		var y2 = y1 * pers;
    		
    		return {x: x2*scale + xCenter, y: y2*scale + yCenter};
    	}
    	
    	function mapTo3D(inx, iny) {
    		//re shift mouse coord
    		iny = iny - yCenter + _root.container._y;
    		inx = inx - xCenter + _root.container._x;
    		
    		//transform - re - rotate to find x,z
    		var z1 = ( inx/Math.cos(alpha) - iny/(Math.sin(alpha)*Math.sin(theta)) ) * 
    				 ( 1/(Math.cos(alpha)/Math.sin(alpha) + Math.sin(alpha)/Math.cos(alpha)) );
    		var x1 = (1/Math.cos(alpha)) * (inx - z1*Math.sin(alpha));
    
    		//take out perspective
    		var pers = d/(d + z1);
    		var zp = z1/pers;
    		var xp = x1/pers;
    		
    		
    		return {x: xp/scale, y: 0, z: zp/scale };
    	
    	}

  2. #2
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472
    Wouldn't you need some sort of raycasting/collision detection algorithm here to see which objects currently intersect with the mouse?

    It's not too hard to map the mouse pos to a point in 3d, but the problem is that there's an infinite amount of points that can fit the description (and all have different z-values)

    So you can map out x/y but you'd have to set z, or get it by raycasting or something similar...
    Pixelwave Flash-based iPhone framework
    iPhone Games: Flyloop | Freedom Run

    Twitter: Oztune

  3. #3
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    If you are wanting to use the mouse pointer for something like object selection (ie, detect a click on the face of a rotated cube ) there might be an easier alternative to raycasting.

    Create an additional list of "hotspot" vertices in your geometry, placed at the centroids of your faces (for a cube, that gives you 6 points). Apply the same "mapToScreen" transformation to the hotspots as you apply to the rest of the 3D geometry. When a click occurs, step through the list of hotspots and see if any of them is close to the mouse cursor (this check takes place in screen coordinates). If one matches closely (say, a hotspot is <20 pixels away from the mouseclick point), select the matching face.

    Any help? Like ozmic said, the problem is not well posed because infinitely many 3D points collapse/project into the same 2D screen point.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center