Sandy Ray Casting

by darknet
...
@author Darknet

Credit by: lovexylitol
-Converting 2D to 3D coordinates

Information: Ray Casting. This will move along x and z axis. y = 0.
Note this is just a test. Just play with the settings.
Cam has z = -1
♥0 | Line 98 | Modified 2009-06-15 06:14:20 | MIT License
play

ActionScript3 source code

/**
 * Copyright darknet ( http://wonderfl.net/user/darknet )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8z4J
 */

package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import sandy.core.data.Matrix4;
	import sandy.core.data.Point3D;
	import sandy.core.scenegraph.Camera3D;
	import sandy.core.scenegraph.Group;
	import sandy.core.scenegraph.Shape3D;
	import sandy.materials.Appearance;
	import sandy.materials.attributes.LightAttributes;
	import sandy.materials.attributes.LineAttributes;
	import sandy.materials.attributes.MaterialAttributes;
	import sandy.materials.ColorMaterial;
	import sandy.materials.Material;
	import sandy.math.Matrix4Math;
	import sandy.core.Scene3D;
	import sandy.primitive.Box;
	import sandy.primitive.Plane3D;
	import sandy.view.*;
	
	/**
	 * ...
	 * @author Darknet
	 * 
	 * Credit by: lovexylitol
	 * -Converting 2D to 3D coordinates
	 * 
	 * Information: Ray Casting. This will move along x and z axis. y = 0.
	 * Note this is just a test. Just play with the settings.
	 * Cam has z = -1
	 * 
	 */
	
	[SWF(width="464", height="464", backgroundColor="#FFFFFF", frameRate="30")]
	public class SandyRayPlane5 extends Sprite{
		
		public var scene:Scene3D;
		public var camera:Camera3D
		public var g:Group = new Group("myGroup");
		public var box:Box = new Box('box');
		public var ground:Shape3D = new Plane3D('ground', 256, 256, 10, 10, Plane3D.ZX_ALIGNED, 'quad');
		public var HEIGHT:Number = 464; //Screen Size
		public var WIDTH:Number = 464; //Screen Size
		
		public function SandyRayPlane5(){
			camera = new Camera3D(464,464);
			camera.far = 500;
			camera.near = 0;
			camera.y = 500;
			//camera.z = 500;
			camera.z = -500;
			camera.lookAt(0, 0, 0);
			var root:Group = createScene();
			scene = new Scene3D( "scene", this, camera, root );
			
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );
			stage.addEventListener(MouseEvent.MOUSE_MOVE, frameMouseMove);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, addKey, false, 0, true);
			
			var materialAttr:MaterialAttributes = new MaterialAttributes(
			new LineAttributes( 0.5, 0x2111BB, 0.4 ),
			new LightAttributes( true, 0.1));
			
			var material:Material = new ColorMaterial( 0x999999, 1, materialAttr );
			var app:Appearance = new Appearance( material );
			box.appearance  = app;
			
			material = new ColorMaterial( 0xFFCC33, 1, materialAttr );
			app = new Appearance( material );
			ground.appearance = app;
			ground.useSingleContainer = false;
		}
		
		public function frameMouseMove(e:MouseEvent):void {
			var bp:Point3D = S2W(mouseX, mouseY);
			box.x = bp.x;
			box.y = bp.y;
			box.z = bp.z;
			trace(bp);

		}
		
		public function S2W(sx:Number, sy:Number):Point3D {
			var Mcmm:Matrix4 = camera.modelMatrix;
			
			var NEAR:Number = 1;
			var FAR:Number = 500;
			
			var z1:Number = NEAR;
			var x1:Number = (sx - WIDTH/2) * z1 / camera.focalLength;
			var y1:Number = ((HEIGHT / 2 - sy) * z1 / camera.focalLength);
			
			var z2:Number = FAR;
			var x2:Number = (sx - WIDTH/2) * z2 / camera.focalLength;
			var y2:Number = ((HEIGHT / 2 - sy) * z2 / camera.focalLength);
			
			var p1:Point3D = new Point3D(x1,y1,z1);
			var p2:Point3D = new Point3D(x2,y2,z2);
			
			var rp1:Point3D = Matrix4Math.transform(Mcmm,p1);
			var rp2:Point3D = Matrix4Math.transform(Mcmm,p2);
			
			var t:Number = rp2.y / (rp2.y - rp1.y);
            var Px:Number = (rp1.x-rp2.x)*t+rp2.x;
            var Pz:Number = (rp1.z - rp2.z) * t + rp2.z;
			
			return new Point3D(Px,0,Pz);
		}
		
		private function enterFrameHandler(event:Event) : void {
			scene.render();
		}
		
		private function createScene():Group {
			g.addChild(box);
			g.addChild(ground);
			return g;
		}
		
		public function addKey(e:KeyboardEvent):void {
			if (e.keyCode == 39) {//right
				camera.x+=10;
			}
			if (e.keyCode == 37) {//left
				camera.x-=10;
			}
		}
		
	}
	
}