ࡱ> Root EntryRoot Entryp|YrRASHPnContents!@Page 19  "#$%&'()*+,-./0123456789:;<=>?@ACPicPage CPicLayer CPicFrameCPicText W \P E& Arial(Projection-plane based 3D Engine by  Arial(Milan Toth (MilGra)/ Arial( milgra@freemail.hu 2004 Use Mouse and Arrows?Zz//Projection-plane based 3D Engine by Milan Toth //Copyright (c) 2004 Milan Toth milgra@freemail.hu //THIS SOURCE CODE MUSTN'T BE USED FOR COMMERCIAL PURPOSES //WITHOUT AUTHORISATION. milgra@freemail.hu // // Vector Class // Vector = function (i, j, k) { this.i = i; this.j = j; this.k = k; this.r = Math.sqrt(i*i+j*j+k*k); }; // // Standard Summa // Vector.prototype.Sum = function (ve) { var i=this.i+ve.i; var j=this.j+ve.j; var k=this.k+ve.k; return new Vector(i,j,k); }; // // Standard Substraction // Vector.prototype.Sub = function (ve) { var i=this.i-ve.i; var j=this.j-ve.j; var k=this.k-ve.k; return new Vector(i,j,k); }; // // Dot (vectorial) Multiplication of two vectors // Standard 3x3 Matrix Determinant Calculation // Vector.prototype.DotMul = function(ve) { var i = this.j*ve.k-this.k*ve.j; var j = -(this.i*ve.k-this.k*ve.i); var k = this.i*ve.j-this.j*ve.i; return new Vector(i, j, k); }; // // Scalar Multiplication of two Vectors // Vector.prototype.VectMul = function(ve) { return this.i*ve.i+this.j*ve.j+this.k*ve.k; }; // // Multiplication with a Scalar // Vector.prototype.SkalMul = function(skal) { var i = this.i*skal; var j = this.j*skal; var k = this.k*skal; return new Vector(i, j, k); }; // // Angle Calculation between two vectors // Vector.prototype.Angle = function(ve) { var vmult = this.i*ve.i+this.j*ve.j+this.k*ve.k; var rmult = this.r*ve.r; var alpha = Math.acos(Math.round(vmult)/Math.round(rmult)); return alpha; }; // // Plane Class // constructor receives the two base vectors // Plane = function (v1, v2) { this.v1 = v1; this.v2 = v2; this.Init(); }; // // Calculating additional vectors // normal vector, d1 (xpar), d2 (ypar) // Plane.prototype.Init = function() { this.norm_ve = this.v2.Sub(this.v1); this.xpar_ve = new Vector(-1*this.norm_ve.k, 0, this.norm_ve.i); this.ypar_ve = this.norm_ve.DotMul(this.xpar_ve); }; // // Calculating intersection of line and plane as shown in fig1 // Plane.prototype.CrossPoint = function(pt) { var dir_ve = new Vector(pt.x, pt.y, pt.z); var t = (this.v1.VectMul(this.norm_ve)-dir_ve.VectMul(this.norm_ve))/(this.norm_ve.VectMul(this.norm_ve)); var dx = t*this.norm_ve.i; var dy = t*this.norm_ve.j; var dz = t*this.norm_ve.k; var dist = Math.sqrt(dx*dx+dy*dy+dz*dz); if (t>0) dist *= -1; return {x:pt.x+dx, y:pt.y+dy, z:pt.z+dz, d:dist}; }; // // Converting 3D coordinate to the plane`s local system, as shown in fig 3 and fig 4 // Plane.prototype.Project = function(pt) { var a = this.CrossPoint(pt); var b = this.v1; var dir_ve = new Vector((a.x-b.i), (a.y-b.j), (a.z-b.k)); var alpha = dir_ve.Angle(this.xpar_ve); var betha = dir_ve.Angle(this.ypar_ve); if (dir_ve.r == 0) { var xc = 0; var yc = 0; } else { var xc = Math.cos(alpha)*dir_ve.r; var yc = -Math.sin(alpha)*dir_ve.r; } if (betha>Math.PI/2) yc *= -1; // setting perspective distortion var focal = (focuslength-a.d)/focuslength; xc *= focal; yc *= focal; //if t>0 not visible var infront = a.d < 0 ? 0 : 1; if ((xc<200 && xc>-200) && (yc>-200 && yc<200)) var onscreen=1; else var onscreen=0; return {x:xc, y:yc, v:infront*onscreen}; }; // // Move plane target inverse kinematicly // Plane.prototype.Move = function(ve) { this.v2=this.v2.Sum(ve); var dir_ve=this.v2.Sub(this.v1); var ratio=this.norm_ve.r/dir_ve.r; var new_ve=dir_ve.SkalMul(-ratio); this.v1=this.v2.Sum(new_ve); this.Init(); }; // // Move plane horizontal // Plane.prototype.HorMove = function(amnt) { var ratio=amnt/this.xpar_ve.r; var dir_ve=this.xpar_ve.SkalMul(ratio); this.v1=this.v1.Sum(dir_ve); this.v2=this.v2.Sum(dir_ve); this.Init(); } // // Move plane vertical // Plane.prototype.VertMove = function(amnt) { var ratio=amnt/this.norm_ve.r; var dir_ve=this.norm_ve.SkalMul(ratio); this.v1=this.v1.Sum(dir_ve); this.v2=this.v2.Sum(dir_ve); this.Init(); } // // Rotate plane vertical // Plane.prototype.VertRot = function(amnt) { var x=this.norm_ve.i; var y=this.norm_ve.j; var z=this.norm_ve.k; var r=Math.sqrt(z*z+y*y); var theta=Math.atan2(y,z); var nz=Math.cos(theta+amnt)*r; var ny=Math.sin(theta+amnt)*r; this.norm_ve=new Vector(x,ny,nz); this.v2=this.v1.Sum(this.norm_ve); this.Init(); } // // Rotate plane horizontal // Plane.prototype.HorRot = function(amnt) { var x=this.norm_ve.i; var y=this.norm_ve.j; var z=this.norm_ve.k; var r=Math.sqrt(x*x+z*z); var theta=Math.atan2(z,x); var nx=Math.cos(theta+amnt)*r; var nz=Math.sin(theta+amnt)*r; this.norm_ve=new Vector(nx,y,nz); this.v2=this.v1.Sum(this.norm_ve); this.Init(); } // // Draw 2d points // function DrawWorld() { var arr = []; for (var a = 0; a<cube.length; ++a) { var point = plane.Project(cube[a]); arr.push(point); } display.clear(); display.lineStyle(1, 0x000000, 100); display.moveTo(-200,-200); display.lineTo(200,-200); display.lineTo(200,200); display.lineTo(-200,200); display.lineTo(-200,-200); for (var a=0;a<sides.length;++a) { display.moveTo(arr[sides[a][0]].x,arr[sides[a][0]].y); for (var b=1;b<sides[a].length;++b) { if (arr[sides[a][b]].v==0) break; display.lineTo(arr[sides[a][b]].x,arr[sides[a][b]].y); } display.lineTo(arr[sides[a][0]].x,arr[sides[a][0]].y); } } // // Draw top view // function DrawTop() { top.clear(); top.lineStyle(1,0x000000,100); top.moveTo(plane.v2.i, plane.v2.k); top.lineTo(plane.v1.i, plane.v1.k); for (var a=0;a<sides.length;++a) { top.moveTo(cube[sides[a][0]].x,cube[sides[a][0]].z); for (var b=1;b<sides[a].length;++b) { top.lineTo(cube[sides[a][b]].x,cube[sides[a][b]].z); } top.lineTo(cube[sides[a][0]].x,cube[sides[a][0]].z); } } // // // focuslength = 800; plane = new Plane(new Vector(0,-90,190), new Vector(0,-90,0)); // cube = []; cube.push({x:-50, y:-50, z:50}); cube.push({x:50, y:-50, z:50}); cube.push({x:50, y:-50, z:-50}); cube.push({x:-50, y:-50, z:-50}); cube.push({x:-50, y:50, z:50}); cube.push({x:50, y:50, z:50}); cube.push({x:50, y:50, z:-50}); cube.push({x:-50, y:50, z:-50}); // sides = []; sides.push([2,1,5,6]); sides.push([1,0,4,5]); sides.push([5,4,7,6]); sides.push([0,3,7,4]); sides.push([3,2,6,7]); // createEmptyMovieClip("display",1); createEmptyMovieClip("top",2); display._x=200; display._y=200; top._x=475; top._y=200; oldx=_xmouse; oldy=_ymouse; Mouse.hide(); this.onEnterFrame = function() { if (Key.isDown(Key.LEFT)) plane.HorMove(-10); if (Key.isDown(Key.RIGHT)) plane.HorMove(10); if (Key.isDown(Key.UP)) plane.VertMove(10); if (Key.isDown(Key.DOWN)) plane.VertMove(-10); var dx=_xmouse-oldx; var dy=_ymouse-oldy; if (Math.abs(dx)>14) dx*=14/Math.abs(dx); if (Math.abs(dy)>14) dy*=14/Math.abs(dy); if (dx !=0) plane.HorRot(dx/180); if (dy !=0) plane.VertRot(dy/180); oldx=_xmouse; oldy=_ymouse; DrawWorld(); DrawTop(); };  Layer 1OO8 CDocumentPagePage 1Scene 1.AA*@hhhhh!PublishGifProperties::PaletteName PublishRNWKProperties::speed256K0"PublishHtmlProperties::StartPaused0%PublishFormatProperties::htmlFileName Untitled-1 PublishQTProperties::LayerOption PublishQTProperties::AlphaOption"PublishQTProperties::MatchMovieDim1Vector::Debugging Permitted0PublishProfileProperties::nameDefaultPublishHtmlProperties::Loop1PublishFormatProperties::jpeg0PublishQTProperties::Width550$PublishPNGProperties::OptimizeColors1&PublishRNWKProperties::speedSingleISDN0&PublishRNWKProperties::singleRateAudio0Vector::External Player%PublishHtmlProperties::showTagWarnMsg1PublishHtmlProperties::Units04PublishHtmlProperties::UsingDefaultAlternateFilename1PublishGifProperties::Smooth1%PublishRNWKProperties::mediaCopyright(c) 2000#PublishRNWKProperties::flashBitRate1200Vector::Compress Movie1Vector::Package Paths&PublishFormatProperties::flashFileNameUntitled-1.swf'PublishFormatProperties::gifDefaultName1%PublishFormatProperties::projectorMac0"PublishGifProperties::DitherOption!PublishRNWKProperties::exportSMIL1 PublishRNWKProperties::speed384K0"PublishRNWKProperties::exportAudio1Vector::FireFox0PublishHtmlProperties::Quality4(PublishHtmlProperties::VerticalAlignment1$PublishFormatProperties::pngFileNameUntitled-1.pngPublishFormatProperties::html1"PublishPNGProperties::FilterOption'PublishRNWKProperties::mediaDescriptionVector::Override Sounds0!PublishHtmlProperties::DeviceFont0-PublishFormatProperties::generatorDefaultName1PublishQTProperties::Flatten1PublishPNGProperties::BitDepth24-bit with AlphaPublishPNGProperties::Smooth1"PublishGifProperties::DitherSolids0PublishGifProperties::Interlace0PublishJpegProperties::DPI4718592Vector::Quality80Vector::Protect0"PublishHtmlProperties::DisplayMenu1*PublishHtmlProperties::HorizontalAlignment12PublishHtmlProperties::VersionDetectionIfAvailable0Vector::Template0*PublishFormatProperties::generatorFileNameUntitled-1.swt(PublishFormatProperties::rnwkDefaultName1(PublishFormatProperties::jpegDefaultName1PublishFormatProperties::gif0PublishGifProperties::Loop1PublishGifProperties::Width550$PublishRNWKProperties::mediaKeywords!PublishRNWKProperties::mediaTitlePublishRNWKProperties::speed28K1#PublishFormatProperties::qtFileNameUntitled-1.mov"PublishPNGProperties::DitherOption#PublishGifProperties::PaletteOption#PublishGifProperties::MatchMovieDim1$PublishRNWKProperties::speedDualISDN0$PublishRNWKProperties::realVideoRate100000PublishJpegProperties::Quality80PublishFormatProperties::flash1#PublishPNGProperties::PaletteOption#PublishPNGProperties::MatchMovieDim1$PublishJpegProperties::MatchMovieDim1Vector::Package Export Frame1!PublishProfileProperties::version1PublishHtmlProperties::Align0-PublishFormatProperties::projectorWinFileNameUntitled-1.exe'PublishFormatProperties::pngDefaultName10PublishFormatProperties::projectorMacDefaultName1#PublishQTProperties::PlayEveryFrame0"PublishPNGProperties::DitherSolids0"PublishJpegProperties::Progressive0Vector::Debugging PasswordVector::Omit Trace Actions0PublishHtmlProperties::Height400PublishHtmlProperties::Width550%PublishFormatProperties::jpegFileNameUntitled-1.jpg)PublishFormatProperties::flashDefaultName1PublishPNGProperties::Interlace0PublishGifProperties::Height400PublishJpegProperties::Size0Vector::DeviceSound0Vector::TopDown0'PublishHtmlProperties::TemplateFileName Default.html!PublishHtmlProperties::WindowMode02PublishHtmlProperties::UsingDefaultContentFilename1-PublishFormatProperties::projectorMacFileNameUntitled-1.hqx(PublishFormatProperties::htmlDefaultName1PublishFormatProperties::rnwk0PublishFormatProperties::png0PublishQTProperties::Height400%PublishPNGProperties::RemoveGradients0PublishGifProperties::MaxColors255'PublishGifProperties::TransparentOptionPublishGifProperties::LoopCountPublishRNWKProperties::speed56K1Vector::Report0+PublishHtmlProperties::OwnAlternateFilename(PublishHtmlProperties::AlternateFilename&PublishHtmlProperties::ContentFilename"PublishFormatProperties::generator0$PublishGifProperties::OptimizeColors1"PublishRNWKProperties::audioFormat0Vector::Version7Vector::Event Format0Vector::Stream Compress7PublishFormatProperties::qt0PublishPNGProperties::Height400PublishPNGProperties::Width550%PublishGifProperties::RemoveGradients0 PublishRNWKProperties::speed512K0PublishJpegProperties::Height400Vector::ActionScriptVersion2Vector::Event Compress7PublishHtmlProperties::Scale00PublishFormatProperties::projectorWinDefaultName1PublishQTProperties::Looping0*PublishQTProperties::UseQTSoundCompression0!PublishPNGProperties::PaletteName!PublishPNGProperties::Transparent0&PublishGifProperties::TransparentAlpha128PublishGifProperties::Animated0"PublishRNWKProperties::mediaAuthor(PublishRNWKProperties::speedCorporateLAN0&PublishRNWKProperties::showBitrateDlog1"PublishRNWKProperties::exportFlash1PublishJpegProperties::Width550Vector::Stream Format0"PublishHtmlProperties::VersionInfo87,0,0,0;6,0,0,0;5,0,0,0;4,0,0,0;3,0,0,0;2,0,0,0;1,0,0,0;$PublishFormatProperties::gifFileNameUntitled-1.gif&PublishFormatProperties::qtDefaultName1"PublishQTProperties::PausedAtStart0%PublishQTProperties::ControllerOption0PublishPNGProperties::MaxColors255,PublishHtmlProperties::UsingOwnAlternateFile0%PublishFormatProperties::rnwkFileNameUntitled-1.smil%PublishFormatProperties::projectorWin0%PublishFormatProperties::defaultNames1 CColorDef3PfP0PHP`Px333(3f<03CH3F`3Hxf0f30ff(0f5Hf<`f@x3330333xf3d03]H3Z`3Xx3333303f3PPH33Px`33Px33Pf30f33PHff3(PHf3<x`f3Cxf3Ffff`f03f0ffx0fkHfd`f`x3f033fPH3ffxPH3fdx`3f]x3fZff0f3fPHfff`ffP0xffPxffPH3HfHxH̙n`hx3H33x`3fx`3xx`3̙kx3dfHf3x`ff0xfx0xf̙dxf]`3`f``x`px3`33x3fx3x3xx3nf`f3xffxfxfxxfkx3xfxxxxx3x333f333xfxf3fffffxxH3 HfH(H2`8x`3 `f`̙`(`0xx3xfxx x(xPx3H33x`f3x`3(x`35x3<3`33xf3 x̙3x3(x323x33f3 333(xfH3fx`ff0xf(0xf<xfCf`3fxffx̙fxf(xf5fx3ffff ff((xH3x`f0x̙PPP`3xfx̙P̙(P<x3f̙(xx`3xfxPxPd`3xfx̙PPx3f(xx3fxx3f̙xx3ff`zf*]"PublishQTProperties::QTSndSettingsCQTAudioSettingsh