topical media & game development
actionscript-wii-Rotation3.ax
actionscript-wii-Rotation3.ax
[swf]
flex
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
[SWF(backgroundColor=0xffffff)]
public class @ax-actionscript-wii-Rotation3 extends Sprite
{
private var balls:Array;
private var numBalls:uint = 50;
private var limit:Number;
private var fl:Number;
private var vpX:Number;
private var vpY:Number;
private var cursorX:Number;
private var cursorY:Number;
private var myWiimote:Wiimote;
private var cursor:actionscript_wii_Cursor;
public function @ax-actionscript-wii-Rotation3()
{
init();
}
private function init():void
{
stage.displayState = "fullScreen";
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
myWiimote = new Wiimote();
myWiimote.connect();
cursor = new actionscript_wii_Cursor();
cursor.x = cursorX = vpX = stage.stageWidth / 2;
cursor.y = cursorY = vpY = stage.stageHeight / 2;
cursor.visible = false;
addChild(cursor);
fl = limit = 0.5 * stage.stageWidth / 2;
balls = new Array();
for(var i:uint = 0; i < numBalls; i++)
{
var ball:actionscript_wii_Ball3D = new actionscript_wii_Ball3D(15);
balls.push(ball);
ball.xpos = Math.random() * limit - (limit / 2);
ball.ypos = Math.random() * limit - (limit / 2);
ball.zpos = Math.random() * limit - (limit / 2);
addChild(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
myWiimote.addEventListener(WiimoteEvent.UPDATE, onUpdated);
myWiimote.addEventListener(ButtonEvent.A_PRESS, onAPressed);
myWiimote.addEventListener(ButtonEvent.A_RELEASE, onAReleased);
myWiimote.addEventListener(ButtonEvent.B_PRESS, onBPressed);
}
private function onEnterFrame(event:Event):void
{
var angleX:Number = (cursorY - vpY) * .0001;
var angleY:Number = (cursorX - vpX) * .0001;
for(var i:uint = 0; i < numBalls; i++)
{
var ball:actionscript_wii_Ball3D = balls[i];
rotateX(ball, angleX);
rotateY(ball, angleY);
doPerspective(ball);
}
sortZ();
}
private function rotateX(ball:actionscript_wii_Ball3D, angleX:Number):void
{
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = ball.ypos * cosX - ball.zpos * sinX;
var z1:Number = ball.zpos * cosX + ball.ypos * sinX;
ball.ypos = y1;
ball.zpos = z1;
}
private function rotateY(ball:actionscript_wii_Ball3D, angleY:Number):void
{
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = ball.xpos * cosY - ball.zpos * sinY;
var z1:Number = ball.zpos * cosY + ball.xpos * sinY;
ball.xpos = x1;
ball.zpos = z1;
}
private function doPerspective(ball:actionscript_wii_Ball3D):void
{
if(ball.zpos > -fl)
{
var scale:Number = fl / (fl + ball.zpos);
ball.scaleX = ball.scaleY = scale;
ball.x = vpX + ball.xpos * scale;
ball.y = vpY + ball.ypos * scale;
ball.visible = true;
}
else
{
ball.visible = false;
}
}
private function sortZ():void
{
balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < numBalls; i++)
{
var ball:actionscript_wii_Ball3D = balls[i];
setChildIndex(ball, i);
}
}
private function onUpdated ( pEvt:WiimoteEvent ):void
{
if (myWiimote.ir.p1==true && myWiimote.a) {// && myWiimote.ir.p2==true) {
//statusText.text = "Status:" + ((1-myWiimote.ir.x1) * stage.stageWidth) + (myWiimote.ir.y1 * stage.stageHeight);
cursor.x = cursorX = (1-myWiimote.ir.x1) * stage.stageWidth;
cursor.y = cursorY = myWiimote.ir.y1 * stage.stageHeight;
//force feedback
if (cursorX<10 || cursorX>stage.stageWidth-10) {
myWiimote.rumbleTimeout = 300;
}
if (cursorY<10 || cursorY>stage.stageHeight-10) {
myWiimote.rumbleTimeout = 300;
}
}
//zooming
if (myWiimote.minus && fl<limit*2) {
fl += 1;
} else if (myWiimote.home) {
fl = limit;
} else if (myWiimote.plus && fl>(0.4*limit)) {
fl -= 1;
}
}
private function onAPressed ( pEvt:ButtonEvent ):void
{
cursor.visible = true;
}
private function onAReleased ( pEvt:ButtonEvent ):void
{
cursor.visible = false;
}
private function onBPressed ( pEvt:ButtonEvent ):void
{
cursorX = vpX;
cursorY = vpY;
}
}
}
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.