Seb Lee-Delisle

Menu

Smooth circles in Papervision

Here’s a handy little class that will make a smooth 3D circle out of curved lines in Papervision3D. Add an nice little sine wave movement curve and you have this! Click on the image to see it in action.

Smooth circles in Papervision

Here’s the source :

Circle class :

 
// Author : Seb Lee-Delisle
// Blog : www.sebleedelisle.com
// Company : www.pluginmedia.net
// Mail : [email protected]
//
// This work is licensed under a Creative Commons 2.0  non-commercial share-alike License.
 
// Full details at
// http://creativecommons.org/licenses/by-nc-sa/2.0/uk/
 
package net.pluginmedia.pv3d
{
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.special.LineMaterial;
 
	public class Circle3D extends Lines3D
	{
		public function Circle3D(mat:LineMaterial, radius : Number=100, divisions : int = 8, lineWeight : Number = 2, startAngle : Number = 0, endAngle : Number = 360)
		{
			super(mat);
			addCircle(0,0,0,radius, divisions, startAngle, endAngle, lineWeight);
		}
 
		public function addCircle(x:Number, y:Number, z:Number, r:Number, d:Number = 8, startAngle:Number =0, endAngle:Number = 360, linethickness:Number = 2):void
		{
			var temp : Number3D = new Number3D(r,0,0);
			var tempcurve:Number3D = new Number3D(0,0,0);
			var joinends : Boolean;
			var i:int;
			var pointcount : int;
 
			var angle:Number = (endAngle-startAngle)/d;
			var curveangle : Number = angle/2;
 
			tempcurve.x = r/Math.cos(curveangle * Number3D.toRADIANS);
			tempcurve.rotateY(curveangle+startAngle);
 
			if(endAngle-startAngle<360)
			{
				joinends = false;
				pointcount = d+1;
			}
			else
			{
				joinends = true;
				pointcount = d;
			}
 
 
 
			temp.rotateY(startAngle);
 
			var vertices:Array = new Array();
			var curvepoints:Array = new Array();
 
			for(i = 0; i< pointcount;i++)
			{
				vertices.push(new Vertex3D(x+temp.x, y+temp.y, z+temp.z));
				curvepoints.push(new Vertex3D(x+tempcurve.x, y+tempcurve.y, z+tempcurve.z));
				temp.rotateY(angle);
				tempcurve.rotateY(angle);
			}
 
			for(i = 0; i < d ;i++)
			{
				var line:Line3D = new Line3D(this, material as LineMaterial, linethickness, vertices[i], vertices[(i+1)%vertices.length]);
				line.addControlVertex(curvepoints[i].x, curvepoints[i].y, curvepoints[i].z );
 
				addLine(line);
			}
 
 
 
		}
 
	}
}

Circle demo :

package {
	import flash.events.Event;
 
	import net.pluginmedia.pv3d.Circle3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.view.BasicView;
 
 
	[SWF (width="640", height="480", backgroundColor="0x000000", frameRate="30")]
 
	public class CirclesTest extends BasicView
	{
		public var circles : Array;
		public var numCircles : int = 80;
		public var counter : int = 0;
 
 
		public function CirclesTest()
		{
			super(640,480,false,false);
 
			circles = new Array();
 
			var lineMaterial : LineMaterial;
			var circle : Circle3D;
 
			for (var i: int = 0; i< numCircles; i++)
			{
 
				var brightness : Number = 1 - (i/numCircles);
				var colour : int = brightness*0xcc << 16;
 
				lineMaterial= new LineMaterial(colour,1);
				circle = new Circle3D(lineMaterial,100,8,4);
				circle.z = i*80;
				circle.rotationX = 90;
 
				scene.addChild(circle);
				circles.push(circle);
 
			}
 
			camera.target = circles[2];
			camera.z = -200;
			camera.fov = 30;
 
			addEventListener(Event.ENTER_FRAME, enterFrame);
 
		}
 
 
		public function enterFrame (e:Event) : void
		{
			counter++;
 
			var circle : Circle3D;
			var lastCircle : Circle3D;
 
 
			for(var i:int = numCircles; i>=0; i--)
			{
				circle = circles[i];
				if(lastCircle)
				{
					lastCircle.x = circle.x;
					lastCircle.y = circle.y;
				}
 
				lastCircle = circle;
			}
 
			circle = circles[0];
 
			circle.x = Math.sin(counter*0.15)*120;
			circle.y = Math.sin(counter*0.12)*120;
 
			camera.x = circle.x*0.7;
			camera.y = circle.y*0.7;
 
			singleRender();
 
		}
 
	}
}
This entry was posted in Obsolete, Papervision3D. Bookmark the permalink.

10 Responses to Smooth circles in Papervision