A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: 3D question

  1. #21
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hi,
    Here is a simple VR set-up
    Here is an example of it uploaded

    http://www.trump.net.au/members/ship...example3D.html

    And here is the code. I am sure you will have a number of questions to ask in order to use this code for your purposes. So I will await your questions.
    The following code is all there is to the example if you want to play with it. I don't think I can make it much simpler than this. All aspects can be extended if needed.

    Code:
    function Vect3(x,y,z){
    	this.x=x;
    	this.y=y;
    	this.z=z;
    };
    Vect3.prototype.Set=function(x,y,z){
    	this.x=x;
    	this.y=y;
    	this.z=z;
    };
    function Matrix3x3(){
    	this.xx=1;	this.xy=0;	this.xz=0;
    	this.yx=0;  this.yy=1;	this.yz=0;
    	this.zx=0;	this.zy=0;	this.zz=1;
    };
    Matrix3x3.prototype.Set=function(a,b,c,d,e,f,g,h,i){
    	this.xx=a;	this.xy=b;	this.xz=c;
    	this.yx=d;  this.yy=e;	this.yz=f;
    	this.zx=g;	this.zy=h;	this.zz=i;
    };
    Matrix3x3.prototype.SetRotation=function(x,y,z){
    	var a = Math.cos(x*(Math.PI/180));//x plane
        var b = Math.sin(x*(Math.PI/180));
    	var c = Math.cos(y*(Math.PI/180));//y plane
    	var d = Math.sin(y*(Math.PI/180));
    	var e = Math.cos(z*(Math.PI/180));//z plane
    	var f = Math.sin(z*(Math.PI/180));
    	this.xx=(c*e);		this.xy=(b*d*e+a*f);		this.xz=(-a*d*e+b*f);
    	this.yx=(-c*f);		this.yy=(-b*d*f+a*e);  	    this.yz=(a*d*f+b*e);
    	this.zx=d;			this.zy=(-b*c);  			this.zz=(a*c); 
    };
    function Object3D(){
    	this.Position=new Vect3(0,0,0);
    	this.Orientation=new Matrix3x3();
    };
    Object3D.prototype.AddVertices=function(vertices){
    	this.vertList=vertices;
    };
    Object3D.prototype.OutPutObjectSpace=function(){
    	for(i in this.vertList){
    		trace(this.vertList[i].x);
    	}
    };
    Object3D.prototype.TransformVertices=function(){
    	//displacement camera to object 
    	var dx=this.Position.x-Camera.Position.x;
    	var dy=this.Position.y-Camera.Position.y;
    	var dz=this.Position.z-Camera.Position.z;
    	//transform object position into camera space using Camera orientation matrix
    	var x =  dx * Camera.Orientation.xx + dy * Camera.Orientation.xy + dz * Camera.Orientation.xz;
    	var y =  dx * Camera.Orientation.yx + dy * Camera.Orientation.yy + dz * Camera.Orientation.yz;      
    	var z =  dx * Camera.Orientation.zx + dy * Camera.Orientation.zy + dz * Camera.Orientation.zz;
    	//multiply object and camera orientation matrices
    	var  mxx = this.Orientation.xx * Camera.Orientation.xx + this.Orientation.xy * Camera.Orientation.xy + this.Orientation.xz * Camera.Orientation.xz; 
    	var  mxy = this.Orientation.yx * Camera.Orientation.xx + this.Orientation.yy * Camera.Orientation.xy + this.Orientation.yz * Camera.Orientation.xz;
    	var  mxz = this.Orientation.zx * Camera.Orientation.xx + this.Orientation.zy * Camera.Orientation.xy + this.Orientation.zz * Camera.Orientation.xz;
    	var  myx = this.Orientation.xx * Camera.Orientation.yx + this.Orientation.xy * Camera.Orientation.yy + this.Orientation.xz * Camera.Orientation.yz; 
    	var  myy = this.Orientation.yx * Camera.Orientation.yx + this.Orientation.yy * Camera.Orientation.yy + this.Orientation.yz * Camera.Orientation.yz;
    	var  myz = this.Orientation.zx * Camera.Orientation.yx + this.Orientation.zy * Camera.Orientation.yy + this.Orientation.zz * Camera.Orientation.yz;
    	var  mzx = this.Orientation.xx * Camera.Orientation.zx + this.Orientation.xy * Camera.Orientation.zy + this.Orientation.xz * Camera.Orientation.zz; 
    	var  mzy = this.Orientation.yx * Camera.Orientation.zx + this.Orientation.yy * Camera.Orientation.zy + this.Orientation.yz * Camera.Orientation.zz;
    	var  mzz = this.Orientation.zx * Camera.Orientation.zx + this.Orientation.zy * Camera.Orientation.zy + this.Orientation.zz * Camera.Orientation.zz;
        //transform object vertices from Object Space -> World Space -> Camera Space -> 2D screen cordinates
    	var p,o;
    	for(i in this.vertList){
    		p=this.vertList[i];
    		o=Camera.focalLength/((p.x * mzx + p.y * mzy + p.z * mzz)+ z);
    		p.sy=  -(((p.x * myx + p.y * myy + p.z * myz)+ y) * o) + (Camera.viewHeight/2);
    		p.sx=   (((p.x * mxx + p.y * mxy + p.z * mxz)+ x) * o) + (Camera.viewWidth/2);
    	}
    };
    Object3D.prototype.Render=function(mc){
    	mc.clear();
    	mc.moveTo(this.vertList[0].sx,this.vertList[0].sy);
    	mc.beginFill(0xFFFFFF,100);
    	mc.lineTo(this.vertList[1].sx,this.vertList[1].sy);
    	mc.lineTo(this.vertList[2].sx,this.vertList[2].sy);
        mc.lineTo(this.vertList[3].sx,this.vertList[3].sy);
        mc.endFill();
    };
    Object3D.prototype.MoveForward=function(incr){
    	this.Position.x+=this.Orientation.zx * -incr;
    	this.Position.y+=this.Orientation.zy * -incr;
    	this.Position.z+=this.Orientation.zz * -incr;
    };
    Object3D.prototype.MoveBackward=function(incr){
    	this.Position.x+=this.Orientation.zx * incr;
    	this.Position.y+=this.Orientation.zy * incr;
    	this.Position.z+=this.Orientation.zz * incr;
    };
    function Camera3D(){
    	this.Position=new Vect3(0,0,-350);
    	this.Orientation=new Matrix3x3();
    	
    	this.focalLength=950;
    	this.viewWidth = 700;
    	this.viewHeight= 500;
    };
    Camera3D.prototype.MoveForward=function(incr){
    	this.Position.x+=this.Orientation.zx * incr;
    	this.Position.y+=this.Orientation.zy * incr;
    	this.Position.z+=this.Orientation.zz * incr;
    };
    Camera3D.prototype.MoveBackward=function(incr){
    	this.Position.x+=this.Orientation.zx * -incr;
    	this.Position.y+=this.Orientation.zy * -incr;
    	this.Position.z+=this.Orientation.zz * -incr;
    };
    //create new camera
    var Camera = new Camera3D();
    //position camera along world z axis
    Camera.Position.Set(0,0,-350);
    //create a basic 3D object
    var myQuad = new Object3D();
    //add vertices/points to object
    myQuad.AddVertices([new Vect3(10,10,0),new Vect3(-10,10,0),new Vect3(-10,-10,0),new Vect3(10,-10,0)]);
    //set up animation loop
    var animationLoop = setInterval(Update,1);
    function Update(){
    	//update object rotation
    	myQuad.Orientation.SetRotation(0,(objectRotation+=.1),0);
    	//transform vertices
    	myQuad.TransformVertices();
    	//render quad
    	myQuad.Render(QuadGraphic);
    };
    ////////////////////////////////////Camera Keyboard Control/////////////////////////////////////////////
    var CameraTranslationIncrement=1;
    var CameraRotationIncrement=1;
    var CameraRotationX = 0;
    var CameraRotationY = 0;
    var CameraRotationZ = 0;
    Kdwn=Key.isDown;
    myListener = new Object();
    myListener.onKeyDown = function () {
    	if(Kdwn(38)){// pan camera around X axis
    	    CameraRotationX+=CameraRotationIncrement;
    	    Camera.Orientation.SetRotation(CameraRotationX,CameraRotationY,CameraRotationZ);
    	}
    	if(Kdwn(40)){// -pan camera around X axis
       		CameraRotationX-=CameraRotationIncrement;
    	    Camera.Orientation.SetRotation(CameraRotationX,CameraRotationY,CameraRotationZ);
    	}
    	if(Kdwn(37)){// pan camera around Y axis
    		CameraRotationY+=CameraRotationIncrement;
    	    Camera.Orientation.SetRotation(CameraRotationX,CameraRotationY,CameraRotationZ);
    	}
    	if(Kdwn(39)){// -pan camera around Y axis
    		CameraRotationY-=CameraRotationIncrement;
    	    Camera.Orientation.SetRotation(CameraRotationX,CameraRotationY,CameraRotationZ);
    	}
    	//translate camera forward 
    	if(Kdwn(32)){Camera.MoveForward(10);}
    	//translate camera backward
        if(Kdwn(66)){Camera.MoveBackward(10);}
    }
    Key.addListener(myListener);
    ///////////////////////////////////////Quad Mouse Event Handler////////////////////////////////////
    //movie clip to render to
    var QuadGraphic = createEmptyMovieClip("obj1",1);
    //mouse event handlers
    QuadGraphic.onRollOver=function(){
    	myQuad.MoveForward(10);
    	
    };
    QuadGraphic.onRollOut=function(){
    	myQuad.MoveBackward(10);
    };

  2. #22
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Looks like the code got knocked around a bit in the transfer so here it is in a text file
    Attached Files Attached Files
    Last edited by shipstern; 02-04-2007 at 03:52 AM.

  3. #23
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    hey thanks!

  4. #24
    Senior Member
    Join Date
    Nov 2003
    Posts
    524
    Hope it helps. Just let me know if you need any advice using it in your set-up. Or would like to add something to it ... like simple lighting or back-face culling.

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