Box2D Forums

It is currently Sun May 19, 2013 3:14 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 118 posts ]  Go to page Previous  1 ... 6, 7, 8, 9, 10, 11, 12  Next
Author Message
 Post subject: Re: Box2d Flash AS3
PostPosted: Sat Dec 15, 2007 6:40 am 
Offline

Joined: Wed Nov 21, 2007 5:08 am
Posts: 19
alg wrote:
Code:
player.ApplyImpulse(new b2Vec2(20000,0), player.GetCenterPosition());



it works, thank you!
for my 64x64 ball I have to use extremely large impulse vectors (with length of 5-6 million!!)


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 3:18 am 
Offline

Joined: Wed Dec 12, 2007 5:07 am
Posts: 29
In the document class (Main.as) there is a line that gives the sprite for the physics objects to be drawn into:

Code:
m_sprite = this as Sprite


Changing 'this' to any other MovieClip name that is already on the stage causes the swf to hang (or work extremely slow). Is this a bug?


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 3:42 am 
Offline

Joined: Tue Sep 25, 2007 1:45 am
Posts: 131
Edit:


Last edited by skatehead on Tue Dec 18, 2007 4:34 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 4:11 am 
Offline

Joined: Wed Dec 12, 2007 5:07 am
Posts: 29
skatehead wrote:
That line is just giving the stage a static reference to itself so other classes can access the stage


I know that. I don't think I made my question clear. That line assumes that I want all my physics objects drawn directly on to the stage. But, what if I don't want the physics objects to appear on the stage, but in a child movieclip of the stage? So then I would change the line to
Code:
m_sprite = this.myMovieClip as Sprite


Doing that makes the swf unusably slow.


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 4:33 am 
Offline

Joined: Tue Sep 25, 2007 1:45 am
Posts: 131
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


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 11:50 am 
Offline

Joined: Wed Nov 21, 2007 9:46 am
Posts: 41
Hi Guys,

Taking TestStack.as as an example.
I have discovered a real major headache that you might eventually encounter with m_sprite.AddChild(DisplayObjects).

Assuming if you did the following in DrawDisplayObject (from previous posts) in Test.as
bb.m_userData = new Actor(h, new DisplayObject());

If you toggle to another Test example
// toggle between tests in Main.as
if (Input.isKeyPressed(39)){ // Right Arrow
m_currId++;
m_currTest = null;
}

Your previous MediaClips will not be disposed (memory leakage)/removed from Stage.

You can see your previous unremoved mediaclip. That is understandble because they share the same m_sprite.

Any suggestions on how to handle these?


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 12:01 pm 
Offline

Joined: Sun Sep 23, 2007 2:35 pm
Posts: 803
I suppose this is more a general AS3 question than one relating to Box2D, but in any case...I was wondering whether in general, the way the Box2D testbed stuff is drawn is the fastest way to do procedural graphics in Flash, or if it's just the easiest. I've seen that there's a bunch of bitmap manipulation available in Flash, but I'm sort of unclear as to the performance differences, i.e. is it worth it to draw your stuff to an offscreen bitmap and then blit the bitmap, or is that ultimately more trouble than it's worth? I remember in the old versions of Flash there were all sorts of nasty tricks that you needed to pull if you wanted to squeeze performance out, but I'm not sure if any of that is still relevant, given how vastly improved AS3 is.

Actually, any sort of AS3 graphical performance advice would be appreciated, as most of my other graphics work has been done in a context where the best performance came from modifying pixel arrays directly, which I imagine is a real bad idea in Flash, so I'm in somewhat new territory here...


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 5:06 pm 
Offline

Joined: Mon Dec 17, 2007 10:11 pm
Posts: 185
@ewjordan:

I'm not an expert on flash display performance, but I have done some tests and extensive reading on the subject. Generally I've found that using copyPixels() is just about the fastest way to do graphics in Flash. I did a test that showed blitting with bitmaps in Flash was quite a bit faster than creating display objects and moving them around. If you use cacheBitmap it's a little better with display objects, but that only counts if they're not rotating. I suspect the blit method would be faster with rotation, but I haven't done any tests yet. I am working on a simple display engine for Flash, but it is delayed slightly because I'm moving from APE to Box2d. I am hoping to allow optional caching of bitmap rotations for objects that are used a lot.

Never use Movieclips for a display object unless you specifically need the features of a movieclip. This is because there is a lot of overhead involved and you should use Sprites instead for display objects if you need a display object.

Other general performance tips include never using uints, always use int or Number instead because apparently even Numbers are faster than uints contrary to what you would think from previous programming experience. And always type your variables and eat your vegetables.

One final tip before I give you a bunch of useful links: 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)
They don't seem to believe their own results, but I tested it with APE and I swear I saw first hand that it makes a huge difference. I even played with the threshold value to make it a rougher approximation than they used and with no visible loss in the accuracy of the simulation. I think I used .002 or something instead of .00001.

And here are all the links I've found useful about optimizing Flash (display or in general) so you can blame the people who originally wrote about them if they're wrong and not me for passing on Flash voodoo magic that turns your teeth green:

http://www.danielhai.com/blog/?p=38
http://www.danielhai.com/blog/?p=55
http://rozengain.com/?postid=35
http://www.boostworthy.com/blog/?p=82
http://www.bytearray.org/?p=67
http://osflash.org/as3_speed_optimizations (same as from above text, this one's really good)

BTW big thanks to Erin and skatehead for making Box2d and Box2d in Flash possible!


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 9:10 pm 
Offline

Joined: Wed Nov 21, 2007 9:46 am
Posts: 41
If you looked at my code snipple earlier. Notice the addChild (DisplayObject)

I am more worried on how to get rid of those references to DisplayObject if we use the current TestBed.
Using Sprites will also bring us back to the same issue. Sometimes, I will that too much Object Orientation leads to more headaches in Flash. :lol:

Of course the demos are drawing using graphics...and no issue.


Top
 Profile  
 
 Post subject: Re: Box2d Flash AS3
PostPosted: Tue Dec 18, 2007 11:42 pm 
Offline

Joined: Mon Dec 17, 2007 10:11 pm
Posts: 185
@armage:

Yeah I wasn't referencing your code with my response, I just saw t_anjan talking about movieclip objects and wanted everyone to be aware that they can eat up memory and possibly slow things down. Using MovieClips for all your objects would be something akin to using a rocket launcher when a slingshot would suffice.

I don't think your memory leak problem is too big of a headache. 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. Actually since all spaces on the display object list must be occupied, and they start at zero, I would think you could just loop until container.getChildAt(0) == null and then removeChild on container.getChildAt(0) to remove all display objects from the list. To get rid of the references though you would have to hand-code all the references to null if you had created unique variable names for them, or a better way would be to just have one temp variable for creating objects and push them onto an Array, then just loop the array and set all to null after removing them from the display list.

I think this would work but if there's something wrong with this concept let me know because I'm sure I will be programming something like this soon myself. I'm still somewhat new to AS3 & Flash, and it's just been experimenting so far and a lot of reading.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 118 posts ]  Go to page Previous  1 ... 6, 7, 8, 9, 10, 11, 12  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group