QuickBox2D FRIM

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. // try altering your frame rate
  5. [SWF(backgroundColor=0x000000, width=700, height=600, frameRate=30)]
  6. // try setting frim to false
  7. var sim:QuickBox2D = new QuickBox2D(this, {iterations:20, timeStep:1 / 60, frim:true});
  8.  
  9. sim.setDefault({fillColor:0x003366, lineAlpha:0});
  10. sim.createStageWalls();
  11.  
  12. sim.addBox({x:10, y:18, height:3, density:0});
  13.  
  14. sim.setDefault({fillColor:0x004466, lineColor:0x2B80F5});
  15.  
  16. var cVel:b2Vec2 = new b2Vec2();
  17.  
  18. for (var i:int = 0; i<32; i++){
  19.     var c:QuickObject = sim.addCircle({x:13 + i % 8, y:10 + int(i / 8), friction:0.01, radius:0.2 + Math.random()*0.3, angularDamping:1});
  20.     cVel.x = Math.random() * 4 - 2;
  21.     cVel.y = Math.random() * 4 - 2;
  22.     c.body.SetLinearVelocity(cVel);
  23. }
  24.  
  25. var box:QuickObject = sim.addBox({x:3, y:16, width:2, height: 2});
  26. var boxVel:b2Vec2 = new b2Vec2(15, -25);
  27.  
  28. sim.start();
  29.  
  30. // shoot the box from left to right
  31. setTimeout(positionBox, 200);
  32. function positionBox():void{
  33.     // reset box position
  34.     box.x = 3, box.y = 16;
  35.     setTimeout(shootBox, 500);
  36. }
  37. function shootBox():void{
  38.     box.body.SetLinearVelocity(boxVel);
  39.     box.body.SetAngularVelocity(10);
  40.     // shoot the box again
  41.     setTimeout(positionBox, 4000);
  42. }

You'll need to download the latest version of QuickBox2D in order to run this snippet (alpha 107 as of this post). I recommend checking out the demo below first though...

The most notable feature of this release is FRIM (frame rate independent motion). FRIM is used to attempt to keep the speed of the Box2D simulation constant no matter what frame rate the swf is running at. This is good because frame rates will vary from browser to browser and from computer to computer.

Mr.Doob suggested that I implement this feature in QuickBox2D. If you haven't seen his site I highly recommend checking it out - really great stuff there.

I created a demo to illustrate the FRIM feature... this demo is basically the same as the above snippet with a few extra buttons added to control frame rate and toggle FRIM.


Take a look at the demo here...

While this new feature seems to be working, I believe there still may be room for improvement. Please let me know if the demo doesn't work correctly on your machine. As of now, I've only tested it on my 2.5 Ghz Intel Core 2 Duo.

Also, any suggestions to improve the clarity of this demo are appreciated... I found it kind of hard to think of a good demo to show FRIM in action. The original demo really didn't make much sense... so I gave Rich Shupe a call and he helped me come up with the idea for the current demo.

UPDATE:
I just updated the demo so that the simulation restarts when you change the frame rate (as per Mr.doob's suggestion below)

This entry was posted in Box2D, QuickBox2D, motion and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Posted June 16, 2009 at 2:55 am | Permalink

    Yay! This is great! This is becoming a huge time saver lib. Thanks for sharing it Zevan!

  2. Posted June 16, 2009 at 3:00 am | Permalink

    As for suggestions for a better demo. Maybe restarting the whole simulation with the same values every time will make it more clear. Right now you can’t compare low fps with high fps because the simulation is different in every shot. Which, theoretically should be always the same.

  3. Posted June 16, 2009 at 7:30 am | Permalink

    That’s a good idea… and easy enough to do… at some point today I’ll update the demo… with either a “restart simulation” button… or just automatically restart the simulation when you hit an fps button…. thanks for the suggestions :)

  4. Posted June 16, 2009 at 8:35 am | Permalink

    Updated the demo so that the simulation restarts… MUCH better.

  5. Posted June 16, 2009 at 6:59 pm | Permalink

    Sweet action! Frame rate dependence really killed us on our last box2D project, can’t wait to get into FRIM.

  6. Posted January 24, 2010 at 10:40 pm | Permalink

    Seeing as you can check when all of the bodies go to sleep, I’d go ahead and make the simulation restart then too!

  7. Lino
    Posted January 15, 2013 at 11:27 am | Permalink

    the simulation varies because of the “SetTimeout” function that applies the force to the box. I corrected this problem in my game using the QuickBox2D.STEP event and a counter. So steps are counted and not “ms”.

3 Trackbacks

  1. [...] GroupとJointの使い方とかFRIM、任意の形を三角ポリゴンに分けてオブジェクトを作る、表示オブジェクトを切り替えるなどなど、おもしろい機能がたくさんあるので僕の勉強がてら書いていきます。 [...]

  2. [...] good supercomputer to calculate the box2D physics at full speed. With a little searching I found a solution that was implemented in QuickBox2D. If you take a look at the demo, you will see that on every [...]

  3. [...] フレームレートに依存しないシミュレーション – QuickBox2D FRIM [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*