Actionscript:
-
const TWO_PI:Number = Math.PI * 2;
-
const PI_HALF_PI:Number = Math.PI + Math.PI / 2;
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
var runCylinder:Function = makeCylinder(0,0, 100, 300, 0xFF0000, 0xFFCC00);
-
var dx:Number = 0;
-
var dy:Number = 0;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
dx += (mouseX / 50 - dx) / 4;
-
dy += (mouseY - dy) / 4;
-
runCylinder(dx, dy);
-
}
-
function makeCylinder(x:Number, y:Number, size:Number, length:Number, colA:uint, colB:uint):Function{
-
var s:Sprite = Sprite(addChild(new Sprite()));
-
s.x = x;
-
s.y = y;
-
var acol:uint = colB;
-
var bcol:uint = colB;
-
var halfLength:Number = length / 2;
-
var capA:Sprite = Sprite(s.addChild(new Sprite()));
-
var box:Sprite = Sprite(s.addChild(new Sprite()));
-
var w:Number = size * 2;
-
var h:Number = length;
-
with (box.graphics) beginFill(colA), drawRect(-w/2, -h/2, w,h);
-
var capB:Sprite = Sprite(s.addChild(new Sprite()));
-
return function(theta:Number, rot:Number){
-
theta %= TWO_PI;
-
if (theta <0){
-
theta = TWO_PI - -theta;
-
}
-
var scale:Number = Math.cos(theta);
-
if (theta> 1.57){
-
acol = colA;
-
s.addChild(capB);
-
bcol = colB;
-
} else {
-
acol = colB;
-
bcol = colA;
-
s.addChild(capA);
-
}
-
if (theta> PI_HALF_PI){
-
acol = colB;
-
bcol = colA;
-
s.addChild(capA);
-
}
-
-
var sin:Number = Math.sin(theta);
-
box.scaleY = sin;
-
capA.y = halfLength * sin;
-
capB.y = -capA.y;
-
with(capA.graphics) clear(), beginFill(acol), scaleYcircle(capA.graphics,size, scale);
-
with(capB.graphics) clear(), beginFill(bcol), scaleYcircle(capB.graphics,size, scale);
-
s.rotation = rot;
-
}
-
}
-
// original circle function by senocular (www.senocular.com) from here http://www.actionscript.org/forums/showthread.php3?s=&threadid=30328
-
// circle that can be scaled on the y axis
-
function scaleYcircle(g:Graphics, r:Number, s:Number = 1):void {
-
var c1:Number = r * (Math.SQRT2 - 1);
-
var c2:Number = r * Math.SQRT2 / 2;
-
var rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;
-
var x_r:Number = -r, y_r:Number = -rs, x_c2:Number = -c2;
-
var y_c2:Number = -c2s, x_c1:Number = -c1, y_c1:Number = -c1s
-
g.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);
-
g.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);
-
g.curveTo(x_r, c1s, x_r, 0), g.curveTo(x_r,y_c1,x_c2,y_c2);
-
g.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);
-
g.curveTo(r,y_c1,r,0);
-
}
This is similar to the last post I did.... inspired by some of the great stuff over at VectorPark.com... this snippet creates a 3D cylinder out of two circles and a box...