ActionScript 3D Particle Field


Particle altContent

Go get some Flash-Player


AS3 Source Snippet:
private function distance(obj1:Box, obj2:Box):Number
{
	// distance between two objects:
	var distanceX:Number = obj1.x - obj2.x;
	var distanceY:Number = obj1.y - obj2.y;
	var distanceZ:Number = obj1.z - obj2.z;
	return Math.sqrt(distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ);
}
 
private function exchange(obj1:Box, obj1Index:int, obj2:Box, obj2Index:int):void {
	if (obj1Index != obj2Index) {
		if (distance(obj1, obj2) < objDimension / 2) {
			snd.play();
			while (distance(obj1, obj2) < objDimension / 2 + 1) {
				obj1.x += 1;
				obj1.y += 1;
				obj1.z += 1;
				obj2.x -= 1;
				obj2.y -= 1;
				obj2.z -= 1;
			}	
			// objects sollen sich abstossen, wenn sie sich zu Nahe kommen
			var tempXtempo:int = obj_Move_x[obj1Index];
			var tempYtempo:int = obj_Move_y[obj1Index];
			var tempZtempo:int = obj_Move_z[obj1Index];
			obj_Move_x[obj1Index] = obj_Move_x[obj2Index];
			obj_Move_y[obj1Index] = obj_Move_y[obj2Index]; 
			obj_Move_z[obj1Index] = obj_Move_z[obj2Index];
			obj_Move_x[obj2Index] = tempXtempo;
			obj_Move_y[obj2Index] = tempYtempo;
			obj_Move_z[obj2Index] = tempZtempo;
			quit = true;
		}
	}
}
 
private function onEnterFrame_Main(event:Event):void {
	for (var i:int = 0; i < obj.length; i++) {
		obj [i].roll += (obj_Move_x [i] + obj_Move_y [i] + obj_Move_z [i]) *2;
		// Bounce
		if ((obj [i].x >= 80) || (obj [i].x <= -80)) { 
		  obj_Move_x [i] = -1 * obj_Move_x [i]; 
		  obj [i].x += obj_Move_x [i];
		}
		if ((obj [i].y >= 80) || (obj [i].y <= -80)) { 
		  obj_Move_y [i] = -1 * obj_Move_y [i];
		  obj [i].y += obj_Move_y [i];
		} 
		if ((obj [i].z >= 80) || (obj [i].z <= -80)) { 
		  obj_Move_z [i] = -1 * obj_Move_z [i];
		  obj [i].z += obj_Move_z [i];
		}
	}	
	for (var e:int = 0; e < obj.length; e++) {	
		for (var j:int = 0; j < e; j++) {
			exchange(obj [e], e, obj [j], j);
		}	
	}
	quit = false;
	for (var k:int = 0; k < obj.length; k++) {
		obj [k].x += obj_Move_x [k];
		obj [k].y += obj_Move_y [k];
		obj [k].z += obj_Move_z [k];				
	}
	worldMain.render();
}