スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

[ActionScript 3.0]Fisixを使った、サイクロイド曲線を球が転がるシミュレーション

2007-08-04 | 16:12

緑のボタンを押すと開始されます
package
{
    import com.fileitup.fisixengine.collisions.DetectionModes;
    import com.fileitup.fisixengine.collisions.ReactionModes;
    import com.fileitup.fisixengine.core.FisixEngine;
    import com.fileitup.fisixengine.core.FisixObject;
    import com.fileitup.fisixengine.core.Vector;
    import com.fileitup.fisixengine.particles.CircleParticle;
    import com.fileitup.fisixengine.particles.WheelParticle;
    import com.fileitup.fisixengine.primitives.Surface;
   
    import flash.display.*;
    import flash.events.*;
    import flash.text.TextField;
   
    [SWF(width='700',height='550',backgroundColor='0xffffff',frameRate='30')]
    public class FisixCycloid extends Sprite{
       
        private var myEngine:FisixEngine
        private var txtFPS:TextField
       
        private var particle1:WheelParticle;
        private var particle2:WheelParticle;

        public function FisixCycloid(){
           
            var button:SimpleButton = new SimpleButton( );
            button.x = 200;
            button.y = 40;
            button.upState = createCircle( 0x00FF00, 30 );
            button.overState = createCircle( 0xFFFFFF, 32 );
            button.downState = createCircle( 0xCCCCCC, 30 );
            button.hitTestState = button.upState;
            button.addEventListener( MouseEvent.CLICK, onMouseDown );
            addChild( button );
               
            //first, create an instance of the fisixengine object
            myEngine = new FisixEngine()
           
            //set the gravity to pull down at a rate of 1 pixel per second
            myEngine.setGravity(0,1)
           
            myEngine.setReactionMode(ReactionModes.PHYSICAL)
           
            myEngine.boundsCollisions = true
           
            //turn on primitive rendering
            myEngine.setRender(true)
            //tell the engine where to render to
            myEngine.setRenderGraphics(graphics)

            addEventListener(Event.ENTER_FRAME,onEnterFrame)
           
            txtFPS = new TextField()
            addChild(txtFPS)
           
            var a:Number = 150;
            var x:Number;
            var y:Number;
            var x_old:Number = 0.0;
            var y_old:Number = 30.0;
            for(var t:Number = 0; t <= Math.PI; t +=0.01){
                x= (t - Math.sin(t))*a;
                y= (1 - Math.cos(t))*a + 30;
                myEngine.newSurface(new Vector(x_old, y_old), new Vector(x, y));
                x_old = x;
                y_old = y;
            }

            x_old= 0.0;
            y_old= 190.0;
            for(t= 0; t <= Math.PI*a; t += 1){
                x = t;
                y = x * 2.0 / Math.PI + 190;
                myEngine.newSurface(new Vector(x_old, y_old), new Vector(x, y));
                x_old = x;
                y_old = y;
            }

            myEngine.newSurface(new Vector(620, 0), new Vector(620, 600));
            myEngine.newSurface(new Vector(0, 500), new Vector(620, 500));


        }

        private function onMouseDown(e:MouseEvent):void{
            trace("onMouseDown");
            if(particle1){
                myEngine.removeObject(particle1);
                myEngine.removeObject(particle2);
            }
            particle1= myEngine.newWheelParticle(10,20,10)
            particle1.friction=0.5;
            particle2= myEngine.newWheelParticle(10,180,10)
            particle2.friction=0.5;

        }
        private function onEnterFrame(e:Event):void{
            myEngine.mainLoop(1)
            txtFPS.text = int(myEngine.getRealFPS()).toString()
        }
        private function createCircle( color:uint, radius:Number ):Shape {
            var circle:Shape = new Shape( );
            circle.graphics.lineStyle( 1, 0x000000 );
            circle.graphics.beginFill( color );
            circle.graphics.drawCircle( 0, 0, radius );
            circle.graphics.endFill( );
            return circle;
        }
    }
}
スポンサーサイト

Comment

Post a comment

Secret

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。