exploding mesh with alternativa3d 7
As I’ve been developing my alternativa3d blender exporter I’ve had some time to work on a few ideas and tests I’ve had. One of those was being able to take a mesh and explode it into many pieces. This is something quite commonly used in games that involve destroying objects or enemies etc. I decided that in order to do this i would take an object and then split each face of that object into its own mesh that could be controlled and moved in the container. This worked well but i could definitely see this becoming a problem with larger meshes with lots of faces.
package
{
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3DContainer;
import alternativa.engine3d.core.View;
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.primitives.Box;
import alternativa.engine3d.core.MouseEvent3D;
import alternativa.engine3d.objects.Mesh;
import alternativa.engine3d.core.Face;
import alternativa.engine3d.core.Vertex;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import caurina.transitions.*;
[SWF(backgroundColor="#666666", frameRate="100", width="800", height="600")]
public class exploder extends Sprite
{
private var rootContainer:Object3DContainer = new Object3DContainer();
private var camera:Camera3D;
private var box:Box;
private var nmlist:Array;
public function exploder():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
camera = new Camera3D();
camera.view = new View(stage.stageWidth, stage.stageHeight);
addChild(camera.view);
addChild(camera.diagram);
camera.rotationX = -120*Math.PI/180;
camera.y = -1200;
camera.z = 400;
rootContainer.addChild(camera);
box = new Box(500, 500, 500, 5, 5, 5);
var material:FillMaterial = new FillMaterial(0x6facff, 1, 1);
box.setMaterialToAllFaces(material);
rootContainer.addChild(box);
nmlist = new Array();
box.addEventListener(MouseEvent3D.CLICK, onClick);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
camera.view.width = stage.stageWidth;
camera.view.height = stage.stageHeight;
box.rotationZ -= 0.01;
for(var i:int = 0; i < nmlist.length; i++)
{
nmlist[i].rotationZ -= 0.01;
}
camera.render();
}
private function onClick(e:MouseEvent3D):void
{
explodeIt(box);
}
private function explodeIt(m:Mesh):void
{
//loop over each face in mesh
for each(var f:Face in m.faces)
{
//make a face its own new mesh
var nm:Mesh = new Mesh();
var verts:Vector. = new Vector.();
for each(var v:Vertex in f.vertices)
{
verts.push(nm.addVertex(v.x,v.y,v.z,v.u,v.v,v.id));
}
nm.addFace(verts, f.material, null);
nm.calculateFacesNormals();
rootContainer.addChild(nm);
nmlist.push(nm);
}
//remove the original
rootContainer.removeChild(box);
//now explode it
for(var i:int = 0; i < nmlist.length; i++)
{
Tweener.addTween(nmlist[i],{y:randRange(-1000,1000), x:randRange(-1000,1000), z:randRange(-1000,1000), alpha:0, time:1, transition:"easeOutCubic", onComplete:rem, onCompleteParams:[nmlist[i]]});
}
}
private function rem(fm:Mesh):void
{
rootContainer.removeChild(fm);
}
private function randRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
}
}
Comments
Comments are currently closed