Wykrywanie kolizji w trzech wymiarach 3D [ ActionScript 3 ]
Skrypt pokazuje jak wykrywać kolizje pomiędzy obiektami w trzech wymiarach.
Piłki zmieniają kolor na niebieski kiedy dochodzi do kolizji pomiędzy obiektami na tych samych poziomach.
Wersja On-line:
Listing
//KOD DLA KLASY Kolizja3D.as
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.ColorTransform;
public class Kolizja3D extends Sprite
{
private var pilki:Array;
private var iloscPileks:uint = 20;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var gora:Number = -200;
private var dol:Number = 200;
private var lewo:Number = -200;
private var prawo:Number = 200;
private var przod:Number = 200;
private var tyl:Number = -200;
public function Kolizja3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilki = new Array();
for(var i:uint = 0; i < iloscPileks; i++)
{
var pilka:Pilka3D = new Pilka3D(15);
pilki.push(pilka);
pilka.xpos = Math.random() * 400 - 200;
pilka.ypos = Math.random() * 400 - 200;
pilka.zpos = Math.random() * 400 - 200;
pilka.vx = Math.random() * 10 - 5;
pilka.vy = Math.random() * 10 - 5;
pilka.vz = Math.random() * 10 - 5;
addChild(pilka);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < iloscPileks; i++)
{
var pilka:Pilka3D = pilki[i];
move(pilka);
}
for(i = 0; i < iloscPileks - 1; i++)
{
var pilkaA:Pilka3D = pilki[i];
for(var j:uint = i + 1; j < iloscPileks; j++)
{
var pilkaB:Pilka3D = pilki[j];
var dx:Number = pilkaA.xpos - pilkaB.xpos;
var dy:Number = pilkaA.ypos - pilkaB.ypos;
var dz:Number = pilkaA.zpos - pilkaB.zpos;
var dist:Number = Math.sqrt(dx * dx + dy * dy + dz * dz);
if(dist < pilkaA.radius + pilkaB.radius)
{
var blueTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 0, 255, 0);
pilkaA.transform.colorTransform = blueTransform;
pilkaB.transform.colorTransform = blueTransform;
}
}
}
sortZ();
}
private function move(pilka:Pilka3D):void
{
var radius:Number = pilka.radius;
pilka.xpos += pilka.vx;
pilka.ypos += pilka.vy;
pilka.zpos += pilka.vz;
if(pilka.xpos + radius > prawo)
{
pilka.xpos = prawo - radius;
pilka.vx *= -1;
}
else if(pilka.xpos - radius < lewo)
{
pilka.xpos = lewo + radius;
pilka.vx *= -1;
}
if(pilka.ypos + radius > dol)
{
pilka.ypos = dol - radius;
pilka.vy *= -1;
}
else if(pilka.ypos - radius < gora)
{
pilka.ypos = gora + radius;
pilka.vy *= -1;
}
if(pilka.zpos + radius > przod)
{
pilka.zpos = przod - radius;
pilka.vz *= -1;
}
else if(pilka.zpos - radius < tyl)
{
pilka.zpos = tyl + radius;
pilka.vz *= -1;
}
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 < iloscPileks; 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;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.ColorTransform;
public class Kolizja3D extends Sprite
{
private var pilki:Array;
private var iloscPileks:uint = 20;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var gora:Number = -200;
private var dol:Number = 200;
private var lewo:Number = -200;
private var prawo:Number = 200;
private var przod:Number = 200;
private var tyl:Number = -200;
public function Kolizja3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilki = new Array();
for(var i:uint = 0; i < iloscPileks; i++)
{
var pilka:Pilka3D = new Pilka3D(15);
pilki.push(pilka);
pilka.xpos = Math.random() * 400 - 200;
pilka.ypos = Math.random() * 400 - 200;
pilka.zpos = Math.random() * 400 - 200;
pilka.vx = Math.random() * 10 - 5;
pilka.vy = Math.random() * 10 - 5;
pilka.vz = Math.random() * 10 - 5;
addChild(pilka);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < iloscPileks; i++)
{
var pilka:Pilka3D = pilki[i];
move(pilka);
}
for(i = 0; i < iloscPileks - 1; i++)
{
var pilkaA:Pilka3D = pilki[i];
for(var j:uint = i + 1; j < iloscPileks; j++)
{
var pilkaB:Pilka3D = pilki[j];
var dx:Number = pilkaA.xpos - pilkaB.xpos;
var dy:Number = pilkaA.ypos - pilkaB.ypos;
var dz:Number = pilkaA.zpos - pilkaB.zpos;
var dist:Number = Math.sqrt(dx * dx + dy * dy + dz * dz);
if(dist < pilkaA.radius + pilkaB.radius)
{
var blueTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 0, 255, 0);
pilkaA.transform.colorTransform = blueTransform;
pilkaB.transform.colorTransform = blueTransform;
}
}
}
sortZ();
}
private function move(pilka:Pilka3D):void
{
var radius:Number = pilka.radius;
pilka.xpos += pilka.vx;
pilka.ypos += pilka.vy;
pilka.zpos += pilka.vz;
if(pilka.xpos + radius > prawo)
{
pilka.xpos = prawo - radius;
pilka.vx *= -1;
}
else if(pilka.xpos - radius < lewo)
{
pilka.xpos = lewo + radius;
pilka.vx *= -1;
}
if(pilka.ypos + radius > dol)
{
pilka.ypos = dol - radius;
pilka.vy *= -1;
}
else if(pilka.ypos - radius < gora)
{
pilka.ypos = gora + radius;
pilka.vy *= -1;
}
if(pilka.zpos + radius > przod)
{
pilka.zpos = przod - radius;
pilka.vz *= -1;
}
else if(pilka.zpos - radius < tyl)
{
pilka.zpos = tyl + radius;
pilka.vz *= -1;
}
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 < iloscPileks; 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