Trying to get the engine to work

Im trying to make a simple example in which i make boxes at random and watch em interact with each other.
Im however having a few difficulties with it. Im not getting any errors, its just not showing anything.

Ive got a 800x600 flash movie made, and its running "Main" as its document class. This is viewed below.
The movie is trying to run at 120 FPS

Code:
package {
   import flash.events.Event;
   import flash.display.*;
   import flash.text.*;
   
   import Game.*;
   import flash.display.Sprite;
   import flash.display.MovieClip;
   public class Main extends MovieClip {
      var pg:PhysicsWorld;
      var timer:Number = 0;
      public function Main() {
         trace("Starting BoxWars");
         pg = new PhysicsWorld();
         addEventListener(Event.ENTER_FRAME, update, false, 0, true);
      }
      private function update(e:Event):void {
         var now:Date = new Date();
         var time:Number = now.getMilliseconds();
         var timeSince:Number = time - timer;
         timer = time;
         gameUpdate(timeSince);
         fpsMC.text = ""+ Math.round(1/(timeSince/1000));

      }
      private function gameUpdate(update:Number):void {
         pg.updateGame(update);
      }
   }
}


This class basically just figures out how long since the last frame and calls the
PhysicsWorld() metho din the PhysicsWorld class, viewable below.

Code:
package Game{

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.utils.Timer;
   import flash.events.TimerEvent;

   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;

   public class PhysicsWorld extends Sprite {
      public var the_world:b2World;
      
      // make new shapes timer
      private var time_count:Timer=new Timer(1000);
      
      public function PhysicsWorld():void {
         
         trace("game init");
         
         var environment:b2AABB = new b2AABB();
         environment.lowerBound.Set(-100.0, -100.0);
         environment.upperBound.Set(100.0, 100.0);
         
         var gravity:b2Vec2=new b2Vec2(0.0,10.0);
         the_world=new b2World(environment,gravity,true);
         
         var debug_draw:b2DebugDraw = new b2DebugDraw();
         var debug_sprite:Sprite = new Sprite();
         addChild(debug_sprite);
         
         debug_draw.m_sprite=debug_sprite;
         debug_draw.m_drawScale=30;
         debug_draw.m_fillAlpha=0.5;
         debug_draw.m_lineThickness=1;
         debug_draw.m_drawFlags=b2DebugDraw.e_shapeBit;
         the_world.SetDebugDraw(debug_draw);
         
         var final_body:b2Body;
         var the_body:b2BodyDef;
         var the_box:b2PolygonDef;
         
         the_body = new b2BodyDef();
         the_body.position.Set(8.5, 13);
         
         the_box = new b2PolygonDef();
         the_box.SetAsBox(8.5, 0.5);
         the_box.friction=0.3;
         the_box.density=0;
         
         final_body=the_world.CreateBody(the_body);
         final_body.CreateShape(the_box);
         final_body.SetMassFromShapes();
         
         time_count.addEventListener(TimerEvent.TIMER, makeBox);
         time_count.start();
         
         trace("init done");
      }
      
      public function makeBox(e:Event) {
         var final_body:b2Body;
         var the_body:b2BodyDef;
         var the_box:b2PolygonDef;
         the_body = new b2BodyDef();
         the_body.position.Set(Math.random()*10+2, 0);
         the_box = new b2PolygonDef();
         the_box.SetAsBox(Math.random()+0.1,Math.random()+0.1);
         the_box.friction=0.3;
         the_box.density=1;
         final_body=the_world.CreateBody(the_body);
         final_body.CreateShape(the_box);
         final_body.SetMassFromShapes();
         trace("new box made");
      }
      
      // update variable is the milliseconds since last update runs every frame of game.
      // tries to run at 120 fps
      public function updateGame(update:Number):void {
         the_world.Step(update/1000, 10);
      }
   }
}


I am getting no compile errors, just nothing showing on screen.
What have i done wrong? very new to the box2d world.

thanks for help
if this is all the code, looks like you haven't added your physicsWorld to the stage. I don't see an addChild(pg) in the document class.
that did it, Thanks..
been away from as3 for a long time and dont remember all the stuff i need to =P
Hey.
Thought id just post under this topic again as my question is still about understanding the engine.

im using a method in which i tell the engine to update by the time since the last update. I think i read somewhere that doing so was not a good idea so im wondering whats good and not..

Code:
m_world.Step(timeSince/1000, m_iterations);


timeSince Is equal to the number of milliseconds since last update.
I wanted to do it this way so even if a player has a crap-ass computer the game dosent move slower than it should.

I've stripped down the testbed example and redone some of the code to get a better understanding of it.

The bridge example turns out like this (none of the shapes are moving)
Image

if i only do 1/30 updates i dont get that behaviour.

If i put the movie to max at 60 fps and update using the code above i dont get any errors, is that still ok and is 60 fps the limit then?
Intead of mucking with the time step, my solution was to muck with the timer that the world.step was called on. Timers in AS3 momentarilly pause while any functions called by the timer are running (so it doesn't call the function again while it's still running the first one!)

For Box2D, that means that if the physics step (plus whatever else is running on the timer) is taking 10 milliseconds on a slow computer, the timer pauses for 10 milliseconds. However, this is easily meaured, so you can adjust the timer delay. If your delay is supposed to be thirty milliseconds, at the end of each world.step you can re-write the delay to be 30 minus however long the world step was (to a minimum of one). Then it runs every 30 milliseconds reliably, unless the world step is taking longer than that. Any leftover stuttering is just graphics.

I tried a time step tied into the frame rate with much the same math you're implementing. I threw it out very quickly - the rest of my code wound up constantly compensating for it. User input for changing velocities was a nightmare, and trying to get an obstacle to change velocities produced different results every time, depending on the length of the time step.
Quote:
For Box2D, that means that if the physics step (plus whatever else is running on the timer) is taking 10 milliseconds on a slow computer, the timer pauses for 10 milliseconds. However, this is easily meaured, so you can adjust the timer delay. If your delay is supposed to be thirty milliseconds, at the end of each world.step you can re-write the delay to be 30 minus however long the world step was (to a minimum of one). Then it runs every 30 milliseconds reliably, unless the world step is taking longer than that. Any leftover stuttering is just graphics.


Nice. I'll have to remember this. 🙂
I wrote an implementation for this but it is not all that good.
Im still getting laggyish behaviour and these error every so often when the update took a little to long:
Code:
RangeError: Error #2066: The Timer delay specified is out of range.
   at Error$/throwError()
   at flash.utils::Timer/set delay()
   at Main/update()
   at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
   at flash.utils::Timer/flash.utils:Timer::tick()

The simulation continually feels more choppy then my other implementation. But that too studdered sometimes.
Is this something one has to live with or am i perhaps not doing something right?

the code for the timer update is :
Code:
package {

   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Dynamics.Joints.*;
   import Box2D.Dynamics.Contacts.*;
   import Box2D.Common.Math.*;

   import General.*;
   import TestBed.*;
   import TestBed.TestBridge;

   import flash.utils.Timer;
   import flash.events.TimerEvent;
   import flash.events.Event;
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.display.*;
   import flash.text.*;

   public class Main extends MovieClip {
      // update test timer variables
      private var updateDelay:int = 30;
      private var myTimer:Timer = new Timer(updateDelay);

      public var m_currId:int = 0;
      public var m_currTest:Test;

      static public  var m_sprite:Sprite;
      // input
      public var m_input:Input;
      // timer used to find out time since last frame
      var timer:Number = 0;
      public function Main() {

         m_sprite = new Sprite();
         addChild(m_sprite);
         // input
         m_input = new Input(m_sprite);
         m_currTest = new TestBridge();

         //addEventListener(Event.ENTER_FRAME, update, false, 0, true);
         myTimer.addEventListener(TimerEvent.TIMER, update);
         myTimer.start();
      }
      public function update(e:TimerEvent):void {
         // figure out when function is first called
         var now:Date = new Date();
         var timeStart:Number = now.valueOf();

         m_sprite.graphics.clear();
         m_currTest.Update(updateDelay);
         Input.update();

         // figure out how long it took to execute function
         var now:Date = new Date();
         var timeEnd:Number = now.valueOf();
         trace("nextUpdateIn: " + ( updateDelay - (timeEnd - timeStart)) + " \n \tupdatetime: "+ (timeEnd - timeStart));

         // set the new delay to the update time minus the time it took to run this current update
         myTimer.delay = updateDelay - (timeEnd - timeStart);
      }
   }
}


The trace in the code outputs this:
Code:
nextUpdateIn: 18
   updatetime: 12
nextUpdateIn: -13
   updatetime: 43
RangeError: Error #2066: The Timer delay specified is out of range.
   at Error$/throwError()
   at flash.utils::Timer/set delay()
   at Main/update()
   at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
   at flash.utils::Timer/flash.utils:Timer::tick()
nextUpdateIn: 16
   updatetime: 14
nextUpdateIn: 17
   updatetime: 13
nextUpdateIn: 17
   updatetime: 13
nextUpdateIn: 16
   updatetime: 14
nextUpdateIn: 18
   updatetime: 12
nextUpdateIn: 20
   updatetime: 10
nextUpdateIn: 19
   updatetime: 11
nextUpdateIn: 19
   updatetime: 11


So every once and a while it gets a little bit to much to chew on, its reduced when increasing the delay between updates but its still not great.
Try this - it won't throw errors, at the very least.
Code:
var timerInterval:int = 36;  //My own interval length, yours would be 30
var temps:int;
temps = getTimer();
the_world.Step(step_rate, iterations);
//Some other functions you run on your own timer, like updateGraphics() or whatever
temps = timerInterval - (getTimer() - temps);
if (temps < 1)
{
   temps = 1;
}
myTimer.delay = temps;


As for any slowdown you might have after that, good luck with the rest of your code - this chunk won't be the problem anymore.
What's the advantage of using timers over enter frame events?
As I understand it, it's because the frame rate is variable, while the timer is not (so you always know how much time passed, as opposed to frame rate).

Original thread: https://box2d.org/forum/viewtopic.php?t=4232