Difference between revisions of "3D Space Example.as"

From Organic Design wiki
(Z-ordering is working here! why not in the space?)
(Colour them for distinction and put in an array like the space)
Line 10: Line 10:
  
 
// testing z-ordering algorithm
 
// testing z-ordering algorithm
var balls = 5;
+
var balls = [];
for ( var i = 1; i <= balls; i++ ) attachMovie( 'ball', 'ball'+i, i );
+
for ( var i = 0; i < 5; i++ ) {
 +
attachMovie( 'ball', 'ball'+i, i );
 +
o = balls[i] = _root['ball'+i];
 +
c = new Color(o);
 +
r = i & 1 ? 0xff : 0;
 +
g = i & 2 ? 0xff : 0;
 +
b = i & 4 ? 0xff : 0;
 +
o.setTransform( { ra:r, ga:g, ba:b } );
 +
}
  
  
Line 25: Line 33:
 
a += 0.01;
 
a += 0.01;
 
c = 2*Math.PI/balls;
 
c = 2*Math.PI/balls;
for ( var i = 1; i <= balls; i++ ) {
+
for ( var i = 0; i < balls.length; i++ ) {
ball = _root['ball'+i];
+
obj = balls[i];
 
b = a + c * i;
 
b = a + c * i;
ball._x = width/2+Math.sin(b)*width/3;
+
obj._x = width/2+Math.sin(b)*width/3;
ball.az = ball._xscale = ball._yscale = 50+Math.cos(b)*25;
+
obj.az = obj._xscale = ball._yscale = 50+Math.cos(b)*25;
ball.az = -ball.az;
+
obj.az = -obj.az; // this is -ve because z should get greater with distance
ball._y = height/2;
+
obj._y = height/2;
 
}
 
}
  
 
// Sort buffer by Z and synchronise rendering levels
 
// Sort buffer by Z and synchronise rendering levels
for ( var i = 1; i < balls; i++ ) {
+
for ( var i = 0; i < balls.length-1; i++ ) {
obj = _root['ball'+i];
+
obj = balls[i];
 
var maxz = obj.az, maxp = i;
 
var maxz = obj.az, maxp = i;
for ( var j = i+1; j <= balls; j++ ) if ( _root['ball'+j].az > maxz ) {
+
for ( var j = 0; j < balls.length; j++ ) if ( balls[j].az > maxz ) {
maxz = _root['ball'+j].az;
+
maxz = balls[j].az;
 
maxp = j;
 
maxp = j;
 
}
 
}
 
if ( maxp != i ) {
 
if ( maxp != i ) {
obj.swapDepths( _root['ball'+maxp] );
+
obj.swapDepths( balls[maxp] );
_root['ball'+i] = _root['ball'+maxp];
+
balls[i] = balls[maxp];
_root['ball'+maxp] = obj;
+
balls[maxp] = obj;
 
}
 
}
 
}
 
}

Revision as of 04:19, 20 March 2006

  1. include "Space.as"


// Instantiate a new 3D-space //createEmptyMovieClip( 'test', 1 ); //initialiseSpace( test ); //test._x = width/2; //test._y = height/2;

// testing z-ordering algorithm var balls = []; for ( var i = 0; i < 5; i++ ) { attachMovie( 'ball', 'ball'+i, i ); o = balls[i] = _root['ball'+i]; c = new Color(o); r = i & 1 ? 0xff : 0; g = i & 2 ? 0xff : 0; b = i & 4 ? 0xff : 0; o.setTransform( { ra:r, ga:g, ba:b } ); }


// Per frame function reduce() { now = new Date(); time = now.getTime(); //if (_root.ctr++ < 2) // for ( var i = 0; i < allSpaces.length; i++ ) // if ( allSpaces[i].active ) allSpaces[i].frame();

// testing z-ordering algorithm a += 0.01; c = 2*Math.PI/balls; for ( var i = 0; i < balls.length; i++ ) { obj = balls[i]; b = a + c * i; obj._x = width/2+Math.sin(b)*width/3; obj.az = obj._xscale = ball._yscale = 50+Math.cos(b)*25; obj.az = -obj.az; // this is -ve because z should get greater with distance obj._y = height/2; }

// Sort buffer by Z and synchronise rendering levels for ( var i = 0; i < balls.length-1; i++ ) { obj = balls[i]; var maxz = obj.az, maxp = i; for ( var j = 0; j < balls.length; j++ ) if ( balls[j].az > maxz ) { maxz = balls[j].az; maxp = j; } if ( maxp != i ) { obj.swapDepths( balls[maxp] ); balls[i] = balls[maxp]; balls[maxp] = obj; } }


}

createTextField( 'debug', 0, 0, 0, width, height ); with ( debug ) { selectable = true; border = false; multiline = true; wordWrap = true; html = true; }

// Test object //test.create( [ 0, 0, , 'torus', 5, 200, 5000 ] );