Tuesday, April 22, 2008

examples of Robert Penner's Vector3d(AS1.0)









Work finished long ago, simple examples of Rober Penner's Vector3d.
SOURCE(as1 timeline code 3 in 1)

//============================//
//timeline code actionscript1 //
//============================//
//Robert Penner's "vector3d_class.as" can be found 
//here: http://www.robertpenner.com/profmx/
//==========================
//grass
//==========================

#include "vector3d_class.as"

Vector3d.prototype.createOvalPoly=function(a,b,NumPts,rotation){
 var Pts=[];
 var angle=2*Math.PI/NumPts;
 for(i=0;i<=NumPts-1;i++){
  Pts.push(new Vector3d(this.x+a*Math.cos(angle*i+rotation),this.y,this.z+b*Math.sin(angle*i+rotation)));
 }
 return Pts;
};

Vector3d.prototype.rotateYnew = function (angle) {
    with (Math) {
        var ca = cos(angle*Math.PI/180);
        var sa = sin(angle*Math.PI/180);
    }
    with (this) {
        var tempX = x * ca + z * sa;
        var tempZ = x * -sa + z * ca;      
    }
 return new Vector3d(tempX,this.y,tempZ);
};

MovieClip.prototype.curveToPts=function(p1,p2){
 this.curveTo(p1.x,p1.y,p2.x,p2.y);
};

var a1=[];
var a2=[];
var a3=[];
var a4=[];
var a5=[];
var a6=[];
var a7=[];
var a8=[];

a1o=new Vector3d(300,300,100);
a1=a1o.createOvalPoly(100,100,4,0);
a3o=new Vector3d(a1o.x,a1o.y-150,a1o.z);
a3=a3o.createOvalPoly(80,80,6,0);
a2o=new Vector3d((a1o.x+a3o.x)/2,(a1o.y+a3o.y)/2,(a1o.z+a3o.z)/2);
a2=a2o.createOvalPoly(50,50,4,0);
a8o=new Vector3d(a1o.x,a1o.y-20,a1o.z);
a4o=new Vector3d((a3o.x+a8o.x)/2,(a3o.y+a8o.y)/2,(a3o.z+a8o.z)/2);
a4=a2o.createOvalPoly(50,50,6,0);

for(i=0;i<=3;i++){
a1[i].y=a1[i].y-120;
}

for(i=0;i<=5;i++){
a5[i]=a4[i].rotateYnew(10);
a6[i]=a4[i].rotateYnew(-10);
}

for(i=0;i<=3;i++){
a7[i]=a2[i].rotateYnew(5);
a8[i]=a2[i].rotateYnew(-5);
}

//project all
this.lineStyle(2,0x0000FF);
this.onEnterFrame=function(){
  if(a1o.x<=-60){
a1o=new Vector3d(_xmouse,_ymouse,100);
a1=a1o.createOvalPoly(100,100,4,0);
a3o=new Vector3d(a1o.x,a1o.y-150,a1o.z);
a3=a3o.createOvalPoly(80,80,6,0);
a2o=new Vector3d((a1o.x+a3o.x)/2,(a1o.y+a3o.y)/2,(a1o.z+a3o.z)/2);
a2=a2o.createOvalPoly(50,50,4,0);
a8o=new Vector3d(a1o.x,a1o.y-20,a1o.z);
a4o=new Vector3d((a3o.x+a8o.x)/2,(a3o.y+a8o.y)/2,(a3o.z+a8o.z)/2);
a4=a2o.createOvalPoly(50,50,6,0);

for(i=0;i<=3;i++){
a1[i].y=a1[i].y-120;
}

for(i=0;i<=5;i++){
a5[i]=a4[i].rotateYnew(10);
a6[i]=a4[i].rotateYnew(-10);
}

for(i=0;i<=3;i++){
a7[i]=a2[i].rotateYnew(5);
a8[i]=a2[i].rotateYnew(-5);
}
  
 }
    else{
 a1o.rotateY(_xmouse/1000);
 a2o.rotateY(_xmouse/1000);
 a3o.rotateY(_xmouse/1000);
 a1o.rotateZ(_ymouse/1000);
 a2o.rotateZ(_ymouse/1000);
 a3o.rotateZ(_ymouse/1000);

for(i=0;i<=3;i++){
  a1[i].rotateY(_xmouse/1000);
  a7[i].rotateY(_xmouse/1000);
  a8[i].rotateY(_xmouse/1000);
  a1[i].rotateZ(_ymouse/1000);
  a7[i].rotateZ(_ymouse/1000);
  a8[i].rotateZ(_ymouse/1000);
 }
 for(i=0;i<=5;i++){
  a3[i].rotateY(_xmouse/1000);
  a5[i].rotateY(_xmouse/1000);
  a6[i].rotateY(_xmouse/1000);
  a3[i].rotateZ(_ymouse/1000);
  a5[i].rotateZ(_ymouse/1000);
  a6[i].rotateZ(_ymouse/1000);
 }
 }
 a1o.perProject();
 a2o.perProject();
 a3o.perProject();
 for(i=0;i<=3;i++){
  a1[i].perProject();
  a7[i].perProject();
  a8[i].perProject();
 }
 for(i=0;i<=5;i++){
  a3[i].perProject();
  a5[i].perProject();
  a6[i].perProject();
 }
  
this.clear();
this.lineStyle(2,0x0000FF);
this.moveTo(a1o.x,a1o.y);
this.curveToPts(a2o,a3o);

for(i=0;i<=3;i++){
 this.moveTo(a1o.x,a1o.y);
 this.curveToPts(a7[i],a1[i]);
 this.moveTo(a1o.x,a1o.y);
 this.curveToPts(a8[i],a1[i]);
}
for(i=0;i<=5;i++){
 this.moveTo(a3o.x,a3o.y);
 this.curveToPts(a5[i],a3[i]);
    this.moveTo(a3o.x,a3o.y);
 this.curveToPts(a6[i],a3[i]);
}

}
//==========================
//cube
//==========================
#include "vector3d_class.as"

Vector3d.prototype.createOvalPoly=function(a,b,NumPts,rotation){
 var Pts=[];
 var angle=2*Math.PI/NumPts;
 for(i=0;i<=NumPts-1;i++){
  Pts.push(new Vector3d(this.x+a*Math.cos(angle*i+rotation),this.y,this.z+b*Math.sin(angle*i+rotation)));
 }
 return Pts;
};

MovieClip.prototype.curveToPts=function(p1,p2){
 this.curveTo(p1.x,p1.y,p2.x,p2.y);
};

var p=new Vector3d(100,200,0);
var q=new Vector3d(100,100,0);
var x=[];
var y=[];
var z=[];
var o=new Vector3d((p.x+q.x)/2,(p.y+q.y)/2,(p.z+q.z)/2);

x=p.createOvalPoly(50,50,4,0);
y=q.createOvalPoly(50,50,4,0);
z=o.createOvalPoly(60,60,4,0);

this.lineStyle(2,0x0000FF);
this.onEnterFrame=function(){
    this.clear();
 this.beginFill(0x00CC22,40);
 for(i=0;i<=3;i++){
 if(Key.isDown(Key.UP)){x[i].rotateY(1);
 y[i].rotateY(1);
 z[i].rotateY(1);
 }
 if(Key.isDown(Key.DOWN)){x[i].rotateZ(1);
 y[i].rotateZ(1);
 z[i].rotateZ(1);
 }
 x[i].rotateY(_xmouse/20);
 y[i].rotateY(_xmouse/20);
 z[i].rotateY(_xmouse/20);
 x[i].rotateZ(_ymouse/20);
 y[i].rotateZ(_ymouse/20);
 z[i].rotateZ(_ymouse/20);
 x[i].perProject();
 y[i].perProject();
 z[i].perProject();
    this.lineStyle(2,0x0000FF);
 this.moveTo(y[i].x,y[i].y);
 this.curveToPts(z[i],x[i]);
 }

 for(i=0;i<=2;i++){
    this.beginFill(0x00CC22,40);
 this.moveTo(y[i].x,y[i].y);
 this.lineTo(x[i].x,x[i].y);
 this.lineTo(x[i+1].x,x[i+1].y);
 this.lineTo(y[i+1].x,y[i+1].y);
 }
 this.beginFill(0x00CC22,40);
 this.moveTo(y[3].x,y[3].y);
 this.lineTo(x[3].x,x[3].y);
 this.lineTo(x[0].x,x[0].y);
 this.lineTo(y[0].x,y[0].y);
 this.endFill();
}
 
//==========================
//curve
//==========================
//frame1
#include "vector3d_class.as"

Vector3d.prototype.createOvalPoly=function(a,b,NumPts,rotation){
 var Pts=[];
 var angle=2*Math.PI/NumPts;
 for(i=0;i<=NumPts-1;i++){
  Pts.push(new Vector3d(this.x+a*Math.cos(angle*i+rotation),this.y,this.z+b*Math.sin(angle*i+rotation)));
 }
 return Pts;
};

MovieClip.prototype.curveToPts=function(p1,p2){
 this.curveTo(p1.x,p1.y,p2.x,p2.y);
};

var p=new Vector3d(100,200,0);
var q=new Vector3d(100,100,0);
var x=[];
var y=[];
var z=[];
var o=new Vector3d((p.x+q.x)/2,(p.y+q.y)/2,(p.z+q.z)/2);

x=p.createOvalPoly(50,50,4,0);
y=q.createOvalPoly(50,50,4,0);
z=o.createOvalPoly(60,60,4,0);

this.lineStyle(2,0x0000FF);
this.onEnterFrame=function(){
    this.clear();
 for(i=0;i<=3;i++){
 if(Key.isDown(Key.UP)){x[i].rotateY(1);
 y[i].rotateY(1);
 z[i].rotateY(1);
 }
 if(Key.isDown(Key.DOWN)){x[i].rotateZ(1);
 y[i].rotateZ(1);
 z[i].rotateZ(1);
 }
 x[i].rotateY(_xmouse/20);
 y[i].rotateY(_xmouse/20);
 z[i].rotateY(_xmouse/20);
 x[i].perProject();
 y[i].perProject();
 z[i].perProject();
    this.lineStyle(2,0x0000FF);
 this.moveTo(y[i].x,y[i].y);
 this.curveToPts(z[i],x[i]);}
 
}
 
//frame50

#include "vector3d_class.as"

Vector3d.prototype.createOvalPoly=function(a,b,NumPts,rotation){
 var Pts=[];
 var angle=2*Math.PI/NumPts;
 for(i=0;i<=NumPts-1;i++){
  Pts.push(new Vector3d(this.x+a*Math.cos(angle*i+rotation),this.y,this.z+b*Math.sin(angle*i+rotation)));
 }
 return Pts;
};

MovieClip.prototype.curveToPts=function(p1,p2){
 this.curveTo(p1.x,p1.y,p2.x,p2.y);
};

var p=new Vector3d(300,200,0);
var q=new Vector3d(300,250,0);
var x=[];
var y=[];
var z=[];
var o=new Vector3d((p.x+q.x)/2,(p.y+q.y)/2,(p.z+q.z)/2);

x=p.createOvalPoly(50,50,4,0);
y=q.createOvalPoly(50,50,4,0);
z=o.createOvalPoly(60,60,4,0);

this.lineStyle(2,0x0000FF);
this.onEnterFrame=function(){
 for(i=0;i<=3;i++){
 if(Key.isDown(Key.UP)){x[i].rotateY(1);
 y[i].rotateY(1);
 z[i].rotateY(1);
 }
 if(Key.isDown(Key.DOWN)){x[i].rotateZ(1);
 y[i].rotateZ(1);
 z[i].rotateZ(1);
 }
 x[i].rotateY(_xmouse/20);
 y[i].rotateY(_xmouse/20);
 z[i].rotateY(_xmouse/20);
 y[i].perProject();
 z[i].perProject();
    this.lineStyle(2,0x0000FF);
 this.moveTo(y[i].x,y[i].y);
 this.curveToPts(z[i],x[i]);}
}
 
//frame90

#include "vector3d_class.as"

Vector3d.prototype.createOvalPoly=function(a,b,NumPts,rotation){
 var Pts=[];
 var angle=2*Math.PI/NumPts;
 for(i=0;i<=NumPts-1;i++){
  Pts.push(new Vector3d(this.x+a*Math.cos(angle*i+rotation),this.y,this.z+b*Math.sin(angle*i+rotation)));
 }
 return Pts;
};

MovieClip.prototype.curveToPts=function(p1,p2){
 this.curveTo(p1.x,p1.y,p2.x,p2.y);
};

var p=new Vector3d(500,200,0);
var q=new Vector3d(500,250,0);
var x=[];
var y=[];
var z=[];
var o=new Vector3d((p.x+q.x)/2,(p.y+q.y)/2,(p.z+q.z)/2);

x=p.createOvalPoly(50,50,4,0);
y=q.createOvalPoly(50,50,4,0);
z=o.createOvalPoly(60,60,4,0);

this.lineStyle(2,0x0000FF);
this.onEnterFrame=function(){
 for(i=0;i<=3;i++){
 if(Key.isDown(Key.UP)){x[i].rotateY(1);
 y[i].rotateY(1);
 z[i].rotateY(1);
 }
 if(Key.isDown(Key.DOWN)){x[i].rotateZ(1);
 y[i].rotateZ(1);
 z[i].rotateZ(1);
 }
 x[i].rotateY(_xmouse/20);
 y[i].rotateY(_xmouse/20);
 z[i].rotateY(_xmouse/20);
 x[i].rotateZ(_ymouse/20);
 y[i].rotateZ(_ymouse/20);
 z[i].rotateZ(_ymouse/20);
 
 x[i].perProject();
 y[i].perProject();
 z[i].perProject();
    this.lineStyle(2,0x0000FF);
 this.moveTo(y[i].x,y[i].y);
 this.curveToPts(z[i],x[i]);}
}
Book: Robert Penner's Programming Macromedia Flash MX

2 comments:

Sponsors