Not just Actionscript noodlings


02 Jul

Find which face is clicked on a Papervision3D Cube.


Firstly you need to add an Event Listener to your Cube.

cube.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onClickHandler);

Inside the onClickHandler is the function for determining which face is being clicked on. I have only included the front and back in the switch case, but you can also have “top”, “bottom”, “right” and “left”.

private function onClickHandler(event:InteractiveScene3DEvent):void 
		{ 			
			var face:String = faceHandler(event);
 
			switch (face)
			{				
				case "front" : 
					// Do something when the front is clicked
				break;
 
				case "back" : 
					// Do something when the back is clicked
				break;
			}
		}

Here is the faceHandler function. It uses the InteractiveUtils.getMapCoordAtPoint Papervision function to determine which face is being clicked on.

private function faceHandler(event:InteractiveScene3DEvent):String
		{
			var faceNormals = { back:new Number3D(0,0,1), front:new Number3D(0,0,-1), right:new Number3D(1,0,0), left:new Number3D(-1,0,0),bottom:new Number3D(0,-1,0), top:new Number3D(0,1,0)}; 
 
			var point:Object = 
			InteractiveUtils.getMapCoordAtPoint(event.face3d, event.sprite.mouseX, event.sprite.mouseY); 
 
			for(var key:String in faceNormals) 
			{ 
				if ( Number3D.dot(faceNormals[key], event.face3d.faceNormal) > 0.99 )  return key; 
			} 
			return "none"; 
		}

I can’t take credit for the initial code (I can’t find where I got it, to credit them) – however i have edited it and expanded the functionality.