Archive for the 'code' Category

AS3 BitmapData.hitTest() with rotation

I saw a tweet from @mesh today, asking about hitTest when one object is rotated. A bit of horrible hackery, and here’s the result. Warning, nasty thrown-together code! ;)

var red:Sprite = new Sprite();
red.graphics.beginFill(0xFF0000);
red.graphics.lineTo(40, 0);
red.graphics.lineTo(40, 150);
red.graphics.lineTo(0,150);
red.graphics.lineTo(0,0);

red.x = 40;
red.y = 40;

addChild(red);

var green:Sprite = new Sprite();

green.graphics.beginFill(0x00FF00);
green.graphics.lineTo(40, 0);
green.graphics.lineTo(40, 150);
green.graphics.lineTo(0,150);
green.graphics.lineTo(0,0);

green.x = 300;
green.y = 40;

addChild(green);

var useRotation:Number=10;

var redBmpData = new BitmapData(red.width, red.height, true, 0);
redBmpData.draw(red);

var useDimension:int = Math.ceil(Math.sqrt((green.width * green.width) + (green.height * green.height)));
var diffWidth:int = useDimension - green.width;
var diffHeight:int = useDimension - green.height;
var newBD:BitmapData = new BitmapData(useDimension,useDimension,true,0);
var angle_in_radians:Number = Math.PI * 2 * (useRotation / 360);
var m:Matrix = new Matrix();
m.translate((green.width / 2) * -1, (green.height / 2) * -1);
m.rotate(angle_in_radians);
m.translate(green.width / 2, green.height / 2);
m.translate(diffWidth / 2, diffHeight / 2);
var translatedPoint:Point = m.transformPoint(new Point(0,0));

trace(useDimension);
var greenBmpData = new BitmapData(useDimension,useDimension,true,0);
greenBmpData.draw(green,m);

/*var sampleGreen:Bitmap =new Bitmap(greenBmpData);
addChild(sampleGreen);
sampleGreen.x=200;
sampleGreen.y=200;
*/
green.rotation=useRotation;

stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

function onMouseMove(e:MouseEvent):void {
	green.x = mouseX;
	green.y = mouseY;

	if (redBmpData.hitTest(new Point(red.x, red.y),
	255,
	greenBmpData,
	new Point(green.x-translatedPoint.x, green.y-translatedPoint.y),
	255

	  )) {
		trace("hit");
		red.filters = [new GlowFilter()];
	} else {
		red.filters = [];
	}
}

I’ll blog a better example of this, with comments and an explanation of what it’s doing in the next few days.

Bookmark and Share

Useful stuff: Papervision GUI, AS3 Code Snippet, UML editor, Firefox Plugin

Here’s a few useful things I’ve seen recently. Thought I’d share..

VizualPV3D

A GUI for Papervision! It looks rather similar to Lightwave, although orders of magnitude less powerful. I’ll be interested to see how this evolves. Could come in handy one day!
http://www.juxtinteractive.com/work/vizualpv3d/

Swf Class Explorer

This was a useful utility on a recent project, as there’s no “ApplicationDomain.getAllDefinitions():Array”. Worth bookmarking.
http://flassari.is/2008/07/swf-class-explorer-for-as3/

Violet UML Editor

Jon from Unwrong showed me this rather nifty UML editor. Great for drawing quick, simple diagrams. Cross-platform, and free.
http://alexdp.free.fr/violetumleditor/page.php

Talon: Firefox Plugin

A great plugin for Firefox, which allows you to right-click and capture a page (or region). From there you can upload/share/edit on Aviary, copy to clipboard, or save to desktop.
http://www.readwriteweb.com/archives/aviarys_talon_a_drool-worthy_firefox_plug-in_for_e.php

Bookmark and Share