A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Any FOAM experts?

  1. #1
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441

    Any FOAM experts?

    I've got a handful of Circles (stationary) and a handful of triangles - and one 'gravity well' - basically a circle with a ton of mass that I'm re-positioning every step (so it doesnt 'drift').

    The problem is that when my triangles bump into a circle, it kind of flips out - the triangles jump in and out of the circle until it figures out how to right itself...anyone have any ideas?

  2. #2
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    if you post the code i'll probably be able to help

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    This is the first time I've used it so I'm still figuring out how to do some things (like making things stationary when they are picking up force from collisions?)...any help would be greatly appreciated.

    PHP Code:
    package com {
        
        
    /*
        *    Flash 9.0 :: ActionScript 3.0
        */

         //--------------------------------------
        // PACKAGES
        //--------------------------------------

            
    import flash.display.Sprite;
            
    import flash.display.MovieClip;
            
    import flash.display.DisplayObject;
            
    import flash.events.Event;
            
    import flash.utils.Dictionary;
            
            
    import org.generalrelativity.foam.Foam;
            
    import org.generalrelativity.foam.dynamics.element.body.RigidBody;
            
    import org.generalrelativity.foam.dynamics.enum.Simplification;
            
    import org.generalrelativity.foam.dynamics.element.body.Circle;
            
    import org.generalrelativity.foam.dynamics.element.particle.*;
            
    import org.generalrelativity.foam.dynamics.force.*;
            
    import org.generalrelativity.foam.dynamics.element.ISimulatable;
            
    import org.generalrelativity.foam.dynamics.element.body.RigidBody;
            
    import org.generalrelativity.foam.dynamics.enum.Simplification;
            
    import org.generalrelativity.foam.util.ShapeUtil;
            
    import org.generalrelativity.foam.dynamics.force.Gravity;
            
    import org.generalrelativity.foam.math.Vector;
            
    import org.generalrelativity.foam.dynamics.force.Friction;
            
    import org.generalrelativity.foam.dynamics.force.spring.RigidBodyBungee;
            
    import org.generalrelativity.foam.view.Renderable;
            
    import org.generalrelativity.foam.dynamics.element.body.Circle;
            
    import example.orbit.force.GravitationalForceGenerator;
            
    import example.rocket.view.DisplayObjectFoamRenderer;
            
    import example.rocket.view.DisplayObjectData;
            
        public final class 
    GravTest extends Sprite {

            
    //--------------------------------------
            // CLASS CONSTANTS
            //--------------------------------------
                
                
    private const SHIPS:Array = new Array();
                private const 
    LEVELS:Array = new Array();
                private const 
    MASS_BY_LEVEL:Dictionary = new Dictionary();            
                private const 
    FOAM:Foam = new Foam();
                
            
    //--------------------------------------
            //  CONSTRUCTOR
            //--------------------------------------

                
    public function GravTest(){
                    
    super();

                    
    addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void{
                        
    e.target.removeEventListener(e.typearguments.callee);                    
                        
    init();
                    });
                  }

            
    //--------------------------------------
            //  PRIVATE VARIABLES
            //--------------------------------------
                
                
    private var m:Circle;
                private var 
    _currentPlanet:Sprite;
                private var 
    resFlag:Boolean true;
                
            
    //--------------------------------------
            //  GETTER/SETTERS
            //--------------------------------------
                
              //--------------------------------------
              //  EVENT HANDLERS
              //--------------------------------------

                
    private function newShip(i:int):void{
                    
    SHIPS[i] = addShip(0,0);
                }


                private function 
    onFoamStep(e:Event):void{
                    var 
    i:int SHIPS.length;
                    var 
    tempFlag:Boolean false;

                    
    //  loop through all ships
                    
    while(i){
                        
    i--;
                        var 
    r:RigidBody SHIPS[i];
                        
                        
    //  if first spawned, clear inertia
                        
    if(resFlagr.vx r.vy r.av 0;

                        
    //  always point toward grav well (this is still kind of broken)
                        
    r.Math.atan2(_currentPlanet.r.y_currentPlanet.r.x);

                        
    //  if close to grav well, delete and spawn new ship
                        
    if(r.m.<= && r.m.<= 3){
                            
    //  replace this with some kind of collision event handler???
                            
    FOAM.removeElement(r);
                            
    SHIPS[i] = addShip(0,0);
                        }
                    }

                    
    //  clear spawn flag
                    
    resFlag false;

                    
    //  keep grav well from moving around
                    //  is there a way to do this automatically ???
                    
    m._currentPlanet.x;
                    
    m._currentPlanet.y;
                    
    m.clearTorque();

                }

            
    //--------------------------------------
            //  PRIVATE & PROTECTED INSTANCE METHODS
            //--------------------------------------

                
    private function init(e:Event null):void{
                    
    LEVELS.push(part1);
                    
    LEVELS.push(part2);
                    
    LEVELS.push(part3);
                    
    LEVELS.push(part4);
                    
    LEVELS.push(part5);
                    
    LEVELS.push(part6);
                    
    LEVELS.push(part7);
                    
    LEVELS.push(part8);
                    
    LEVELS.push(part9);
                    
    LEVELS.push(part10);
                    
    LEVELS.push(part11);
                    
    LEVELS.push(part12);
                    
    LEVELS.push(part13);
                    
    LEVELS.push(part14);
                    
    LEVELS.push(part15);
                    
    LEVELS.push(part16);
                    
    LEVELS.push(part17);
                    
    LEVELS.push(part18);
                    
                    
    _currentPlanet part9;
                    
    FOAM.setRenderer( new DisplayObjectFoamRenderer() );                
                    
    initPhysics();
                }
                
                private function 
    initPhysics():void{
                    
    addChild(FOAM);
                    
    swapChildren(FOAMplanets);        
                    
    FOAM.solverIterations 2;

                    
    //  build masses underneath the 'part' graphics on stage
                    
    for each(var p:Sprite in LEVELS){
                        var 
    mass:Circle = new Circle(p.xp.y, (p.width .55), Simplification.INFINITE_MASS00.5.1500);
                        
    MASS_BY_LEVEL[p] = mass;
                        
    p.alpha 1;
                        
    p.visible false;
                        
    FOAM.addElement(masstruefalse);
                    }
                    
                    
    //  build gravity well
                    
    = new Circle(2020101000);
                    
    m.100;
                    
    m.300;
                    
    FOAM.addElement(mfalsefalse);

                    
    //  apply grav to objects after this point in time...ie. ships only
                    
    FOAM.addGlobalForceGenerator(new GravitationalForceGenerator(m), false);

                    
    //  fill SHIPS array with new ships
                    
    for(var i:int 020i++){
                        
    SHIPS.push(addShip(21 i40));
                    }
                    
                    
    FOAM.addEventListenerFoam.STEPonFoamStep);                
                    
    FOAM.simulate();
                }
                
                private function 
    addShip(xx:Numberyy:Number):RigidBody{
                    var 
    ship:DisplayObject = new Greenie();
                    var 
    shape:RigidBody = new RigidBody(xxyy100, [new Vector(-1, -5), new Vector(-1,5), new Vector(10,0)]);

                    
    //  start off from one of the planets
                    
    var departure:Sprite LEVELS[4];
                    
    shape.departure.Math.random() * 10 5;
                    
    shape.departure.Math.random() * 10 5;
                    
    shape.Math.atan2(_currentPlanet.shape.y_currentPlanet.shape.x);

                    
    FOAM.addElement(shapetruetrue, new DisplayObjectData(ship));
                    return(
    shape);
                }
        }


  4. #4
    Senior Member
    Join Date
    May 2006
    Location
    Manhattan
    Posts
    246
    sorry this took me a while to get back to you. the problem (i think) comes from you performing a positional update (setting r.q in your FOAM step handler):
    PHP Code:
    //  always point toward grav well (this is still kind of broken) 
    r.Math.atan2(_currentPlanet.r.y_currentPlanet.r.x); 
    when there are forces inducing torsion, this will counteract them and create a jittering effect when not complementary. basically, you're breaking newton's laws! soooo, i'd suggest instead, creating an IForceGenerator that applies a force to turn your ship toward the current planet, instead of setting the orientation explicitly. this way, a collision will respond to these forces instead of "fighting" with rotation update.

    if you comment out that line of code does it fix the jittering?
    Last edited by newblack; 07-20-2008 at 02:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center