Sphere - Cube collision

by mutantleg
Use arrow keys and W S to move
♥0 | Line 172 | Modified 2012-10-23 16:11:53 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lI5F
 */

package {
    import flash.display.BlendMode;
    import flash.display.BitmapData;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    //import flash.media.Camera;
    
    //http://alternativaplatform.com/en/docs/7.7.0/index.html
    
    import alternativ7.engine3d.containers.ConflictContainer;
    import alternativ7.engine3d.containers.BSPContainer;
    import alternativ7.engine3d.core.Camera3D;
    import alternativ7.engine3d.primitives.Plane;
    import alternativ7.engine3d.primitives.Box;
    import alternativ7.engine3d.primitives.Sphere;
    import alternativ7.engine3d.core.View;
     import alternativ7.engine3d.materials.Material;
    import alternativ7.engine3d.materials.FillMaterial;
    import alternativ7.engine3d.materials.TextureMaterial;
    import alternativ7.engine3d.core.Object3D;
    import alternativ7.types.Texture;
    import flash.display.Sprite;
    
    public class FlashTest extends Sprite {
        
        public var cam:Camera3D;
        public var cont:BSPContainer = new BSPContainer();
        public var gBox:Object3D;
        public var rad:Number = 128;
        public var testOb:cObst;
        
        public function FlashTest() {
            // write as3 code here..
               cam = new Camera3D();
                cam.view = new View(500,500); 
                cam.z = -1000;
                cam.y = -500
                cam.lookAt(0,0,0);
                
               addChild(cam.view);
                cont.addChild(cam);
                
                var bm:BitmapData;
                bm = new BitmapData(64,64,false,0);
                bm.noise(123213,0,16,3,true);
                
                var p:Plane;
                
                p = new Plane(1000, 1000, 8, 8);
                p.setMaterialToAllFaces( new TextureMaterial( bm ) );
                p.rotationX = 1.57;
                p.y = 200;
                //cont.addChild(p);
                
                bm = new BitmapData(64,64,false,0);
                bm.noise(123123);
                
                var box:Sphere; //Box;
                //box = new Box(200,200,200,1,1,1);
                  box = new Sphere(rad,12,8);
               // box.setMaterialToAllFaces( new FillMaterial(0) );
                box.x = -384;
                box.setMaterialToAllFaces( new TextureMaterial( bm) );
               
                  cont.addChild(box);
                  gBox = box;
                  
                testOb = new cObst();
                cont.addChild(testOb.obj);  
                  
                  
                  stage.addEventListener(Event.ENTER_FRAME, onEnter);  
                  stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
                  stage.addEventListener(KeyboardEvent.KEY_UP, kup);    
               
               
        }//ctor
        
        
        
        public function checkCol(a:cObst):void
        {    
                var cx:Number = gBox.x;
                var cy:Number = gBox.y;
                var cz:Number = gBox.z;
            
            
                var ix:Number;
                var iy:Number;
                var iz:Number;
                var dx:Number;
                var dy:Number;
                var dz:Number;
                var mag:Number;
                
                if (cx < a.cx - a.xrad) ix = a.cx - a.xrad;
                else if (cx > a.cx + a.xrad) ix = a.cx + a.xrad;
                else ix = cx;
                
                if (cy < a.cy - a.yrad) iy = a.cy - a.yrad;
                else if (cy > a.cy+ a.yrad) iy = a.cy + a.yrad;
                else iy = cy;
           
                if (cz < a.cz - a.zrad) iz = a.cz - a.zrad;
                else if (cz > a.cz+ a.zrad) iz = a.cz + a.zrad;
                else iz = cz;
                
                dx = cx - ix; 
                dy = cy - iy;
                dz = cz - iz;
                
                mag = Math.sqrt(dx * dx + dy * dy + dz *dz);
                if (mag == 0) {mag = 0.0001;}
                if (rad < mag) { return; }
                
                dx /= mag;
                dy /= mag;
                dz /= mag;
                
                var diff:Number = rad - mag;
                
                cx += dx * diff;
                cy += dy * diff;
                cz += dz * diff;
                
                gBox.x = cx;
                gBox.y = cy;
                gBox.z = cz;
                
            
        }//checkcol
        
        
        
        public function onEnter(e:Event):void
        {
            //ref
         //http://www.dakmm.com/?p=272
            if (cKeyMan.isKeyDown(39) ) { gBox.x += 20;}
            if (cKeyMan.isKeyDown(37) ) { gBox.x -= 20;}
            if (cKeyMan.isKeyDown(87) ) { gBox.y -= 20;}
            if (cKeyMan.isKeyDown(83) ) { gBox.y += 20;}
            if (cKeyMan.isKeyDown(38) ) { gBox.z += 20;}
            if (cKeyMan.isKeyDown(40) ) { gBox.z -= 20;}
            
            //gBox.rotationY += 0.1;
            checkCol(testOb);
            
             cam.render();
             
        }//onenter
        
        public function kdown(e:KeyboardEvent):void
        {
         cKeyMan.setKey(e.keyCode, true);   
        }//kdown
        
        public function kup(e:KeyboardEvent):void
        {
          cKeyMan.setKey(e.keyCode, false);  
        }//kup
        
        
    }//flashtest
}//package

import alternativ7.engine3d.containers.ConflictContainer;
    import alternativ7.engine3d.containers.BSPContainer;
    import alternativ7.engine3d.core.Camera3D;
    import alternativ7.engine3d.primitives.Plane;
    import alternativ7.engine3d.primitives.Box;
    import alternativ7.engine3d.primitives.Sphere;
    import alternativ7.engine3d.core.View;
     import alternativ7.engine3d.materials.Material;
    import alternativ7.engine3d.materials.FillMaterial;
    import alternativ7.engine3d.materials.TextureMaterial;
    import alternativ7.engine3d.core.Object3D;
    import alternativ7.types.Texture;
    
internal class cObst
{
    
    public function cObst()
    {
       initDisp(); 
       }//ctor
    
    public var cx:Number = 0;
    public var cy:Number = 0;
    public var cz:Number = 0;
    
    public var xrad:Number = 160;
    public var yrad:Number = 160;
    public var zrad:Number = 160;
    
    public var obj:Object3D;
    
    public function initDisp():void
    {
        var b:Box = new Box(xrad*2,yrad*2,zrad*2);
        b.setMaterialToAllFaces( new FillMaterial(0xFFFF0000,1,1,0) );
        obj = b;
        
    }//indisp
    
    public function setPos(x:Number, y:Number, z:Number):void
    {
        cx = x;
        cy =y ;
        cz = z;
        obj.x = x;
        obj.y = y;
        obj.z = z;
    }//setpos
    
    
}//cobst



internal class cKeyMan
   {
       public function cKeyMan() {}//ctor (unused)
       
       public static var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,true);
       
       public static function setKey(k:int, b:Boolean):void
       {
           if (k < 0) return;
           if (k >= 512) return;
           vecKey[k] = b;
       }//setkey
       
       public static function isKeyDown(k:int):Boolean
       {
           if  (k < 0) return false;
           if (k >= 512) return false;
           return vecKey[k];
       }//iskey
       
   }//keyman