Box2d Flash AS3 (1.4.3 released)

Code: Select all

m_sprite = this.myMovieClip as Sprite

t_anjan wrote:Doing that makes the swf unusably slow.


In general don't use MovieClips unless you have to. You can make an animation engine yourself. I think everyone is talking about MovieClips because that's how we are used to doing things. Now we don't need them any more.

I'm recently converting a 6-month AS2 project to AS3. It's almost like PowerPoint, but powered by flash. The point is that I now went from 100+ external swfs to 3. I'm now generating buttons, backgrounds and all kinds of things. Most images are now streamed in as Bitmaps. The total file-size went down to a 25%. I'm just writing this, because I think we need to start thinking in new ways. Getting rid of extensive MovieClips is probably one of the biggest steps.
brainclog wrote:I don't know if this will improve performance to the degree that it did with APE (~30%), but if you use a square root approximation instead of the native sqrt function in Flash it should make a very significant difference in performance with Box2d as well. I will be testing this soon, but I'm still very new to Box2d and just trying to get the hang of things. Here is the link where I found out about the sqrt approximation: http://osflash.org/as3_speed_optimizations (toward the bottom)


Please let me know what you find. I'm a big fan of optimizations. I plan to sit down with the entire code, profile it, and look for optimizations myself. However I don't know how far that version 2.0 of Box2D is. It might be worth the wait.
skatehead wrote:Ah, sorry t_anjan I completely misread your question, I'm a bit tired.

I think it's running slowly because m_sprite isn't being cleared each frame.
Changing this line in the update function should fix it.

graphics.clear();
to
m_sprite.graphics.clear();

Sorry again :S
-Matt


You don't have to apologise at all. 😀

Your solution makes sense. I'll try it out and post my results. Thanks a lot!

Anjan

edit: I didn't read the entire thread before posting this message. There seems to be more relevant stuff in the other posts about not using MovieClips ........Thanks again
brainclog wrote:All you need to do is removeChild() on the display objects and then set their references to null. You can iterate through the objects on the display list by using getChildAt and numChildren.


This is how I do it. I've sub-classed Sprite to add all nifty sprite functions in one place:

package
{
import flash.display.Sprite;
import flash.display.DisplayObject;

public class AdvSprite extends Sprite
{

// remove all children
public function removeChildren():void{
while(this.numChildren){
removeChildAt(0);
}
}

//end
}
}


Then use that in the update function as in the testbed examples like this:

public function update(e:Event):void{
// clear for rendering
m_sprite.graphics.clear()
m_sprite.removeChildren();
// rest of the update code here:
// ...
}


Obviously m_sprite is an instance of AdvSprite.
Ygryggil wrote:In general don't use MovieClips unless you have to. You can make an animation engine yourself.

Say, I have an image that I want to use to "dress up" a box body (rectangle). Currently, what I'm doing is to import that image into a movieClip, setting a Linkage property to export the movieClip as a class, then instantiating the class, and then setting the object to the m_userData of the body.

Using a MovieClip is imperative for my method. Is there some other method by which I can use a sprite instead?
t_anjan wrote:Say, I have an image that I want to use to "dress up" a box body (rectangle). Currently, what I'm doing is to import that image into a movieClip, setting a Linkage property to export the movieClip as a class, then instantiating the class, and then setting the object to the m_userData of the body.

Using a MovieClip is imperative for my method. Is there some other method by which I can use a sprite instead?


Unfortunately I haven't been using CS Flash for my AS3 projects, as I use Flex2. Therefore I haven't been working with the Linkage property any. Maybe you can figure it out.

Anyways this is what I do in Flex2. I made a TestImage example for the testbed.

Code: Select all

package TestBed
{
import Engine.Dynamics.*;
import Engine.Collision.*;
import Engine.Collision.Shapes.*;
import Engine.Dynamics.Joints.*;
import Engine.Dynamics.Contacts.*;
import Engine.Common.*;
import Engine.Common.Math.*;
   
import flash.display.Bitmap;
import flash.display.Sprite;
   
public class TestImage extends Test
{
   [Embed(source="../../assets/avatar.png")]
   private var GfxAvatar:Class;
      
   public function TestImage(){
      super();
      
      // Set Text field
      Main.m_aboutText.text = "Attach Image";
      
      // Create a box
      var bodyDef:b2BodyDef = new b2BodyDef();
      var boxDef:b2BoxDef = new b2BoxDef();
      boxDef.density = 1.0;
      boxDef.friction = 0.3;
      boxDef.extents.Set(10 / m_physScale, 10 / m_physScale);
      bodyDef.position.Set((Math.random() * 400 + 120) / m_physScale, (Math.random() * 150 + 50) / m_physScale);
      bodyDef.rotation = Math.random() * Math.PI;                     
      bodyDef.AddShape(boxDef);
      var bb:b2Body = m_world.CreateBody(bodyDef);
      
      // Create a sprite
      var sprite:Sprite = new Sprite();
      sprite.addChild(new GfxAvatar() as Bitmap);   
      bb.m_userData = sprite;
   }

//end   
}
}
Ygryggil wrote:

Code: Select all

   [Embed(source="../../assets/avatar.png")]
   private var GfxAvatar:Class;


Code: Select all

         
      // Create a sprite
      var sprite:Sprite = new Sprite();
      sprite.addChild(new GfxAvatar() as Bitmap);   
      bb.m_userData = sprite;


That is exactly what I wanted. Thank you.
I'm developing a game with some specific physics requirements and excited that this flash port of Box2D may meet all my needs. The game requires bodies that are enclosed (or almost enclosed) within other bodies. Box2D considers this a penetration and attempts to separate the two bodies (generally a good idea so I don't think this is a glitch). I should clarify that the bodies are not closed polygons but, instead, series of thin box shapes. Can I allow nested bodies through some settings changes or should I get to work making some special modifications? I think I may be able to achieve what I want with a normal vector correction scheme for contacts but I'll have to sift through some more code to be sure.

Pictures are often better than words. Moving pictures are better than stills. Interactive media wins:
http://www.enigmasand.com/games/box2dtest/
Hi xavierenigma,

This is a bug in 1.4.2, it has been fixed in 1.4.3 which has been completed but not yet released.

For now you can download it from here:

http://skatehed.ho8.com/flash9/143/Box2DFlashAS3_1.4.3.rar

All the best,
-Matt
I have a question regarding dynamic creation and destruction of bodies.

Assuming I have 5 circles of radius 1 and 5 circles of radius 2

How do I identify a specific body to destroy? I dont see any Id fields in the definitions.

Unless there is another approach, please advice.

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