2D wormhole

just playing around:

spirals

copy and paste to timeline:

var bd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0);
addChild(new Bitmap(bd));
var bf:BlurFilter = new BlurFilter(4,4,3);
var container:Sprite = new Sprite();
 
//Mouse.hide();
 
function newParticle(color:uint):MovieClip
{
	var p:MovieClip = new MovieClip();
	p.graphics.beginFill(color);
	p.graphics.drawCircle(0, 0, 1);
	p.graphics.endFill()
	return p;
}
 
var particles:Array = [];
var numParticles:int = 24;
var radius:int = 2;
var angle:Number = 0;
var speed:int = 1;
var sx:int = stage.stageWidth / 2;
var sy:int = stage.stageHeight / 2;
var friction:int = 30;
 
var t:Timer = new Timer(33);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
addEventListener(Event.ENTER_FRAME, moveParticles);
 
function onTimer(e:TimerEvent):void
{
	for(var i:int = 0; i < numParticles; i++)
	{
		var color:uint = 0x00BBFF;
		var p:MovieClip = newParticle(color);
 
		angle = ((i / numParticles) * 360) * Math.PI / 180;
 
		p.x = sx + (Math.sin(angle)*radius);
		p.y = sy + (Math.cos(angle)*radius)*-1;
		container.addChild(p);
		p.vx = Math.sin(angle) * speed;
		p.vy = -Math.cos(angle) * speed;
		particles.push(p);
	} 
}
 
function moveParticles(e:Event):void
{
	sx += (mouseX-sx) / friction;
	sy += (mouseY-sy) / friction;
	for(var i:int = 0; i < particles.length; i++)
	{
		var p:MovieClip = particles[i];
		p.x += p.vx;
		p.y += p.vy;
		p.scaleX = p.scaleY += 0.006;
		p.alpha -=0.01;
		if(p.alpha <= 0) 
		{
			container.removeChild(p);
			particles.splice(i, 1);
		}
		else if(p.x-p.width/2 > stage.stageWidth || p.x+p.width/2 < 0)
		{
			container.removeChild(p);
			particles.splice(i, 1);
		}
		else if(p.y-p.height/2 > stage.stageHeight || p.y+p.height/2 < 0)
		{
			container.removeChild(p);
			particles.splice(i, 1);
		}  
	}
	bd.fillRect(bd.rect, 0);
	bd.draw(container);
	//bd.applyFilter(bd,bd.rect,new Point(),bf);
}

AS3 Basic Trigonometry Math

Simple usage of rotating and moving an object towards another….

Click the image to see in action:
birdmouse
Code:

var dx:Number = 0;
var dy:Number = 0;
var dist:Number = 0;
var angle:Number = 0;
 
mouse.dx = 1;
mouse.dy = 1;
mouse.speed = 6;
 
addEventListener(Event.ENTER_FRAME, moveBall);
 
function moveBall(e:Event):void
{
	//move mouse
	mouse.x += mouse.speed * mouse.dx;
	mouse.y += mouse.speed * mouse.dy;
	if(mouse.x > stage.stageWidth || mouse.x < 0) mouse.dx = -mouse.dx;
	if(mouse.y > stage.stageHeight || mouse.y < 0) mouse.dy = -mouse.dy;
 
	//set rotation of mouse
	angle = Math.atan2(mouse.dy, mouse.dx) * 180/Math.PI;
	mouse.rotation = angle;
 
	//set rotation of bird
	dx = mouse.x - bird.x;
	dy = mouse.y - bird.y;
	angle = Math.atan2(dy, dx) * 180/Math.PI;
	bird.rotation = angle;
 
	//move bird towards mouse
	bird.x += dx / 30;
	bird.y += dy / 30;
}

AS3 Ball on Ball Collision, Part 1

For my university final project I am developing an 8 ball pool game with the focus on creating a computer generated oponent, however first I need to get down the basic game itself before I can start with the ai.

Collision detection and physics are both a major part of creating a pool game, and after finding this (Linky ) article on Emanuele Feronato’s website, and optimising/modifying it slightly for my style of naming conventions I have created this little example you can see below.

Although this does not take friction or mass into account, and perhaps the framerate of the movie is too high as sometimes you can see balls travelling through each other…
Anyway, click the image to see the result:

balls01

Code:

Document Class

package
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
 
	public class BallCollisions extends Sprite
	{
		private const NUM_BALLS:int = 16;
		private var balls:Array;
 
		public function BallCollisions() : void
		{
			balls = [];
 
			for (var i:int = 0; i < NUM_BALLS; i++)
			{
				var b:Ball = new Ball(Math.floor((Math.random() * 20) - 10), Math.floor((Math.random() * 20) - 10));
				b.x = Math.random() * stage.stageWidth;
				b.y = Math.random() * stage.stageHeight;
				addChild(b);
				balls.push(b);
			}
 
			addEventListener(Event.ENTER_FRAME, moveBalls);
		}
 
		private function moveBalls(e:Event):void
		{
			for (var i:int = 0; i < balls.length; i++)
			{
				var b1:Ball = balls[i];  //asign current ball in array to a var
 
				//move the ball around the stage
				b1.x -= b1.vx;
				b1.y -= b1.vy;				
 
				//check walls for collisions
				if (b1.x >= stage.stageWidth - b1.radius)
				{
					b1.x = stage.stageWidth - b1.radius;
					b1.vx *= -1;
				}
 
				else if (b1.x <= 0 + b1.radius)
				{
					b1.x = 0 + b1.radius;
					b1.vx *= -1;
				}
 
				if (b1.y >= stage.stageHeight - b1.radius)
				{
					b1.y = stage.stageHeight - b1.radius;
					b1.vy *= -1;
				}
 
				else if (b1.y <= 0 + b1.radius)
				{
					b1.y = 0 + b1.radius;
					b1.vy *= -1;
				}
 
				//check collisions against other balls
				for (var j:int = i + 1; j < balls.length; j++)
				{
					var b2:Ball = balls[j];
 
					var dx:Number = b1.x - b2.x;
					var dy:Number = b1.y - b2.y;
					var dist:Number = Math.sqrt(dx * dx + dy * dy);
 
					if (dist < b1.radius + b2.radius)
					{
						resolveBalls (b1, b2);
					}
				}
			}
		}
 
		private function resolveBalls(b1:Ball, b2:Ball):void
		{
			var dx:Number = b2.x - b1.x;
			var dy:Number = b2.y - b1.y;
 
			var dist:Number = Math.sqrt (dx * dx + dy * dy);
 
			//normals
			var nx:Number = dx / dist;
			var ny:Number = dy / dist;
 
			//midpoints
			var mx:Number = (b1.x + b2.x) / 2;
			var my:Number = (b1.y + b2.y) / 2;
 
			b1.x = mx - nx * b1.radius;
			b1.y = my - ny * b1.radius;
			b2.x = mx + nx * b2.radius;
			b2.y = my + ny * b2.radius;
 
			var vec:Number = ((b1.vx - b2.vx) * nx) + ((b1.vy - b2.vy) * ny);
			var nvx:Number = vec * nx;
			var nvy:Number = vec * ny;
 
			b1.vx -= nvx;
			b1.vy -= nvy;
			b2.vx += nvx;
			b2.vy += nvy;
		}
	}
}

Ball Class:

package
{
	import flash.display.Sprite;
 
	public class Ball extends Sprite
	{
		public var vx:Number;
		public var vy:Number;
		public var radius:int = 10;
		public var friction:Number = 0.95;
 
		public function Ball(vx:Number, vy:Number):void
		{
			this.vx = vx;
			this.vy = vy;
 
			draw();
		}
 
		private function draw():void
		{
			graphics.beginFill(Math.random() * 0xFFFFFF);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
	}
}

AS3 Particles – Fire

Same technique as before, but this time with more particles and mouse interaction…

Click image to view:

particlefire

Source Code:

var container:Sprite = new Sprite();
var bd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0);
var b:Bitmap = new Bitmap(bd);
addChild(b);
var bf:BlurFilter = new BlurFilter(12,12,2);
var a:Array = [];
var colors:Array = [0xFF6F27,0xFFFF32];
var t:Timer = new Timer(33);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
 
var dx:Number=0;
var dy:Number=0;
 
var halfWidth:Number = stage.stageWidth/2;
var halfHeight:Number = stage.stageHeight/2;
 
var px:Number= halfWidth;
var py:Number = halfHeight;
 
function onTimer(e:TimerEvent):void
{
	for(var i:int = 0; i < 10; i++)
	{
		var p:MovieClip = drawCircle();
		p.x = px;
		p.y = py;
		container.addChild(p);
		p.vx = Math.random() * 2 - 1;
		p.vy = Math.random() * 2 - 1;
		a.push(p);
	}
}
 
addEventListener(Event.ENTER_FRAME, blur);
 
function blur(e:Event):void
{
	dx = mouseX - halfWidth;
	dy = mouseY - halfHeight;
 
	px += (mouseX - px)/5;
	py += (mouseY - py)/5;
 
	for(var i:int = 0; i < a.length; i++)
	{
		var p:MovieClip = a[i];
		p.x+=p.vx*2;
		p.y+=p.vy*2;
		if(p.x > stage.stageWidth) removeParticle(i,p);
		if(p.x < 0) removeParticle(i,p);
		if(p.y > stage.stageHeight) removeParticle(i,p);
		if(p.y < 0) removeParticle(i,p);
	}
	bd.draw(container);
	bd.applyFilter(bd,bd.rect,new Point(),bf);
	//bd.scroll(0,-5);
	num_particles_txt.text = "Num Particles: " + String(a.length);
}
 
function removeParticle(i:int,p:MovieClip):void
{
	if(container.contains(p)) container.removeChild(p);
	a.splice(i,1);
}
 
function drawCircle():MovieClip
{
	var m:MovieClip = new MovieClip();
	m.graphics.beginFill(colors[Math.floor(Math.random()*colors.length)]);
	m.graphics.drawCircle(0,0,1);
	m.graphics.endFill();
	return m;
}

AS3 Particles With Bitmap Blur

soo… I love particles :D

This was great fun to create, using the old but fascinatingly simple technique of bitmapdata blurring..

click image to begin

ParticleBlur

Source code:

var container:Sprite = new Sprite();
var bd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0);
var b:Bitmap = new Bitmap(bd);
addChild(b);
var bf:BlurFilter = new BlurFilter(2,2,1);
var a:Array = [];
var colors:Array = [0x5427C9,0x2396CC];
var t:Timer = new Timer(33);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
 
function onTimer(e:TimerEvent):void
{
	for(var i:int = 0; i < 5; i++)
	{
		var p:MovieClip = drawCircle();
		p.x=stage.stageWidth/2;
		p.y=stage.stageHeight/2;
		container.addChild(p);
		p.vx=Math.random()*2-1;
		p.vy=Math.random()*2-1;
		a.push(p);
	}
}
 
addEventListener(Event.ENTER_FRAME, blur);
 
function blur(e:Event):void
{
	for(var i:int = 0; i < a.length; i++)
	{
		var p:MovieClip = a[i];
		p.x+=p.vx*2;
		p.y+=p.vy*2;
		if(p.x > stage.stageWidth) removeParticle(i,p);
		if(p.x < 0) removeParticle(i,p);
		if(p.y > stage.stageHeight) removeParticle(i,p);
		if(p.y < 0) removeParticle(i,p);
	}
	bd.draw(container);
	bd.applyFilter(bd,bd.rect,new Point(),bf);
	num_particles_txt.text = "Num Particles: " + String(a.length);
}
 
function removeParticle(i:int,p:MovieClip):void
{
	if(container.contains(p)) container.removeChild(p);
	a.splice(i,1);
}
 
function drawCircle():MovieClip
{
	var m:MovieClip = new MovieClip();
	m.graphics.beginFill(colors[Math.floor(Math.random()*colors.length)]);
	m.graphics.drawCircle(0,0,1);
	m.graphics.endFill();
	return m;
}

3D Zippo Part 2

So… started again with the modelling, took about 3 and a half hours to get the final result as seen below.

Not really happy with the blotchyness, dodgy lighting :D the model is not to any kind of scale dude to lazyness, and was pretty much all done by eye.

Click the image for the full 1920×1080 result:

Render01_Final_1024

3D Zippo Part 1

Started on this 3D model of a zippo this evening, at the moment it has basic textures/lighting and the zippo itself is not fully complete, but heres the current progress:

Zippo

AS3 Simple Graphics Fun

You can have a lot of fun the AS3 Graphics API, it can produce some nice/wierd effects with very little code.
Here’s a demonstration of such a thing:

graphics_api

timeline code:

var circles:Array = [];
var container:Sprite = new Sprite();
var bd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var b:Bitmap = new Bitmap(bd);
b.smoothing = true;
addChild(b);
//b.filters = [new BlurFilter(20,20,3)];
 
function ef(e:Event):void
{
	if(circles.length > 100) container.removeChild(circles.shift());
	for(var i = 0; i< circles.length;i++) 
	{
		var s = circles[i];
		s.scaleX = s.scaleY += 0.1;
	}
	bd.draw(container);
}
 
addEventListener(Event.ENTER_FRAME, ef);
 
var t:Timer = new Timer(100);
t.addEventListener(TimerEvent.TIMER, ot);
t.start();
 
function ot(e:TimerEvent):void
{
	circles.push(drawCircle());
}
 
function drawCircle():Sprite
{
	var s:Sprite = new Sprite();
	s.graphics.beginFill(Math.random() * 0xFFFFFF);
	s.graphics.drawCircle(0,0,5);
	container.addChild(s);
//	s.x = stage.stageWidth / 2;
//	s.y = stage.stageHeight / 2;
	s.x=mouseX;
	s.y=mouseY;
	return s;
}

Photography Album – AS3

Have re-uploaded my photogallery written for AS3 + Flash Player 10.

XML driven, you can add as many images as you like.
Like most of my other projects, its still uncomplete – it needs a concept of filtering photos by category/date etc – easy enough to implement so hopefully will get round to it soon.

Ive created many photo gallerys in flash and have still yet to make one im actually happy with… well at least I have a good enough structure I can use when I make my next one :D

Click the image to view:

gallery

AS3 Wordsearch – Part1

After coming back from holiday and realising just how awesome puzzle books are, I felt it neccessary to write my own xml driven wordsearch application. Well here’s stage 1, essentially the basic structure with basic graphics.

Click the picture below to have a play:
wordsearch

The source code can be found here: Source Code

To get started, all you should need in the first frame of your timeline is:

 var wordSearch:WordSearch = new WordSearch(this);