Obracanie 3D na osi X i Y obiektów w trójwymiarze [ ActionScript 3 ]
Skrypt tworzy losowo ułożone obiekty Sprite w 3D oraz według pozycji myszki obraca je proporcjonalnie do wychylenia.
Efekt trój wymiaru dla kilku obiektów.
Wersja On-Line:
Listing
//KOD DLA KLASY Obrot3D.as
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Obrot3D extends Sprite
{
private var pilki:Array;
private var iloscPilek:uint = 50;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
public function Obrot3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilki = new Array();
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = new Pilka3D(15);
pilki.push(pilka);
pilka.xpos = Math.random() * 200 - 100;
pilka.ypos = Math.random() * 200 - 100;
pilka.zpos = Math.random() * 200 - 100;
addChild(pilka);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var angleX:Number = (mouseY - vpY) * .001;
var angleY:Number = (mouseX - vpX) * .001;
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = pilki[i];
rotateX(pilka, angleX);
rotateY(pilka, angleY);
doPerspective(pilka);
}
sortZ();
}
private function rotateX(pilka:Pilka3D, angleX:Number):void
{
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = pilka.ypos * cosX - pilka.zpos * sinX;
var z1:Number = pilka.zpos * cosX + pilka.ypos * sinX;
pilka.ypos = y1;
pilka.zpos = z1;
}
private function rotateY(pilka:Pilka3D, angleY:Number):void
{
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = pilka.xpos * cosY - pilka.zpos * sinY;
var z1:Number = pilka.zpos * cosY + pilka.xpos * sinY;
pilka.xpos = x1;
pilka.zpos = z1;
}
private function doPerspective(pilka:Pilka3D):void
{
if(pilka.zpos > -fl)
{
var scale:Number = fl / (fl + pilka.zpos);
pilka.scaleX = pilka.scaleY = scale;
pilka.x = vpX + pilka.xpos * scale;
pilka.y = vpY + pilka.ypos * scale;
pilka.visible = true;
}
else
{
pilka.visible = false;
}
}
private function sortZ():void
{
pilki.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = pilki[i];
setChildIndex(pilka, i);
}
}
}
}
//KOD DLA KLASY Pilka3D.as
package {
import flash.display.Sprite;
public class Pilka3D extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Pilka3D(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Obrot3D extends Sprite
{
private var pilki:Array;
private var iloscPilek:uint = 50;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
public function Obrot3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilki = new Array();
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = new Pilka3D(15);
pilki.push(pilka);
pilka.xpos = Math.random() * 200 - 100;
pilka.ypos = Math.random() * 200 - 100;
pilka.zpos = Math.random() * 200 - 100;
addChild(pilka);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
var angleX:Number = (mouseY - vpY) * .001;
var angleY:Number = (mouseX - vpX) * .001;
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = pilki[i];
rotateX(pilka, angleX);
rotateY(pilka, angleY);
doPerspective(pilka);
}
sortZ();
}
private function rotateX(pilka:Pilka3D, angleX:Number):void
{
var cosX:Number = Math.cos(angleX);
var sinX:Number = Math.sin(angleX);
var y1:Number = pilka.ypos * cosX - pilka.zpos * sinX;
var z1:Number = pilka.zpos * cosX + pilka.ypos * sinX;
pilka.ypos = y1;
pilka.zpos = z1;
}
private function rotateY(pilka:Pilka3D, angleY:Number):void
{
var cosY:Number = Math.cos(angleY);
var sinY:Number = Math.sin(angleY);
var x1:Number = pilka.xpos * cosY - pilka.zpos * sinY;
var z1:Number = pilka.zpos * cosY + pilka.xpos * sinY;
pilka.xpos = x1;
pilka.zpos = z1;
}
private function doPerspective(pilka:Pilka3D):void
{
if(pilka.zpos > -fl)
{
var scale:Number = fl / (fl + pilka.zpos);
pilka.scaleX = pilka.scaleY = scale;
pilka.x = vpX + pilka.xpos * scale;
pilka.y = vpY + pilka.ypos * scale;
pilka.visible = true;
}
else
{
pilka.visible = false;
}
}
private function sortZ():void
{
pilki.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < iloscPilek; i++)
{
var pilka:Pilka3D = pilki[i];
setChildIndex(pilka, i);
}
}
}
}
//KOD DLA KLASY Pilka3D.as
package {
import flash.display.Sprite;
public class Pilka3D extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Pilka3D(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Dodano przez: divix
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno