Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411276 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 11:03:19 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Box2d As3 question
Pages: [1]
Print
Author Topic: Box2d As3 question  (Read 1670 times)
RamyDergham1995
Level 0
**


View Profile
« on: May 09, 2015, 06:52:10 AM »

I made a game using flash builder 4.7 standard edition with box2d engine on Android and I am wondering wether there is a performance difference between creating box2d objects before the simulation starts or while the simulation is ongoing. To be more specific , suppose that there is a white screen and every 5 seconds a rock falls down from the upper part of the screen like the game *sky burger* on android , what is more efficient in terms of performance :
1)Create an object every 5 seconds
2)use the same object every 5 seconds by changing its x,y coordinates
3)create the object before simulation loop starts and make the object sleep boolean=true and then seting it back to true and so on.
Logged
Allen Chou
Level 0
**



View Profile WWW
« Reply #1 on: May 09, 2015, 04:56:19 PM »

If you only create one new rigid body every 5 seconds, then you shouldn't be worrying about performance difference. The work you do to create new rigid bodies is negligible from the CPU's standpoint, regardless of the creation method. If your game's performance becomes an issue later, use a profiler first. I doubt the performance bottleneck will be the creation of rigid bodies.
Logged

oahda
Level 10
*****



View Profile
« Reply #2 on: May 09, 2015, 11:46:48 PM »

Yeah, one object isn't going to be a problem.
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #3 on: May 10, 2015, 03:21:41 AM »

First of all , thanks for your reply guys :D . here is the part from my code for that object i am creating

//Code for creating the object
trace("This Level1")
brickPower=true;// a boolean to start the level   
_rotatingBrick=ControlGame._main.createSquare("rotatingBrick",ControlGame._main.stage.fullScreenWidth/2,
            0,200,50,false,false,rotatin,rotatingBrick);//a function that returns a box2d object with given parameters
            ControlGame._main.stage.addChild(rotatingBrick);//adding the image of the object
            _rotatingBrick.SetLinearVelocity(new b2Vec2(0,-1));
            _rotatingBrick.SetAngularVelocity(0)
            _rotatingBrick.SetLinearDamping(1)
            _rotatingBrick.GetFixtureList().SetDensity(1)

//code written in the box2d iteration loop
if(brickPower){         
if ((_rotatingBrick.GetPosition().y>ControlGame._main.stage.fullScreenHeight*pxToM)) {
_rotatingBrick.SetPosition(new b2Vec2((ControlGame._main.stage.fullScreenWidth/2)*pxToM,0));
brickPowerCounter++;
_rotatingBrick.SetLinearVelocity(new b2Vec2(0,-1));}
_rotatingBrick.SetAngularVelocity(10*brickPowerCounter)
if(brickPowerCounter>3){
brickPower=false;
ControlGame._main.world.DestroyBody(_rotatingBrick)
ControlGame._main.stage.removeChild(rotatingBrick);
PowerTurn="MovingWall";//just using a string to switch levels :D
brickPowerCounter=0;
      }
         }



Now the problem that if i started at level 1 , the object is created , but if i lose and restart many times at level 1 the game starts to lag , like there are many objects are being created :/



Logged
oahda
Level 10
*****



View Profile
« Reply #4 on: May 10, 2015, 03:57:07 AM »

Sounds like you might not be properly destroying objects when restarting?
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #5 on: May 10, 2015, 04:04:57 AM »

Sounds like you might not be properly destroying objects when restarting?
you are right :D ,thanks :D, i don't know how i missed that . i am not destroying the object when the user loses in that level . I will fix it and hope it works :D , just a little question , after i destroy the object , do I have to set _rotatingBrick to null??
Logged
oahda
Level 10
*****



View Profile
« Reply #6 on: May 10, 2015, 05:41:03 AM »

Not if you're going to set it to a new square right away anyway. c:
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #7 on: May 10, 2015, 06:55:33 AM »

Setting to null fixed the issue of lag . Also i figured out that i was creating the same object twice  Mock Anger
Logged
oahda
Level 10
*****



View Profile
« Reply #8 on: May 10, 2015, 07:51:36 AM »

Oh, I don't really know AS but maybe you need to null stuff to get it cleared from memory..?

Anybody with actual knowledge of AS who can confirm? Tongue
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #9 on: May 10, 2015, 12:47:03 PM »

Anything MovieClips you add to the scene will pretty much stay there until you explicitly remove them. Setting to null is not sufficient, you need to call remove child. The flash debugging tools usually make this scenario pretty obvious.

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.

Imho it is easier to wrap all the "main" game stuff in a MovieClip rather than using the top level stage. Then you can remove it and replace it with a fresh every level very easily.

Disclaimer: My Flash knowledge is pretty out of date.
Logged
RamyDergham1995
Level 0
**


View Profile
« Reply #10 on: May 10, 2015, 01:12:09 PM »

Anything MovieClips you add to the scene will pretty much stay there until you explicitly remove them. Setting to null is not sufficient, you need to call remove child. The flash debugging tools usually make this scenario pretty obvious.

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.

Imho it is easier to wrap all the "main" game stuff in a MovieClip rather than using the top level stage. Then you can remove it and replace it with a fresh every level very easily.

Disclaimer: My Flash knowledge is pretty out of date.
Here is the sequence I use :
1)Destroy the box2d object itself
2)Remove the object image via remove child
3)setting the box2d object to null
Don't know if I am missing something , maybe i need to set the movieclip of the object to null then force the garbage collector to start ??Anyway until now I see no lag since I made the object to null , will come back here if anything happened , thanks for your reply :D
Logged
Allen Chou
Level 0
**



View Profile WWW
« Reply #11 on: May 10, 2015, 04:57:16 PM »

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.

When adding the event listener, there is a "useWeakReference" boolean parameter defaulted to false. If set to true, you don't have to explicitly remove the listener for the event dispatcher object to become eligible for garbage collection.
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #12 on: May 11, 2015, 11:40:56 AM »

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.


When adding the event listener, there is a "useWeakReference" boolean parameter defaulted to false. If set to true, you don't have to explicitly remove the listener for the event dispatcher object to become eligible for garbage collection.
So making this param to true is better than removing the listener??
Logged
Allen Chou
Level 0
**



View Profile WWW
« Reply #13 on: May 11, 2015, 12:47:32 PM »

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.


When adding the event listener, there is a "useWeakReference" boolean parameter defaulted to false. If set to true, you don't have to explicitly remove the listener for the event dispatcher object to become eligible for garbage collection.
So making this param to true is better than removing the listener??

I'm not sure how the GC is implemented regarding this matter. One might be more efficient than the other; you can only know by profiling a stress test.

I personally always remove the listeners as opposed to relying on the weak reference argument.
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #14 on: May 11, 2015, 03:32:00 PM »

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.


When adding the event listener, there is a "useWeakReference" boolean parameter defaulted to false. If set to true, you don't have to explicitly remove the listener for the event dispatcher object to become eligible for garbage collection.
So making this param to true is better than removing the listener??

I'm not sure how the GC is implemented regarding this matter. One might be more efficient than the other; you can only know by profiling a stress test.

I personally always remove the listeners as opposed to relying on the weak reference argument.
Hmm. Do you know any free profiling tool?? I am using flash builder 4.7 to build my game , unfortunetely the profile tool is only availabe in the premium version
Logged
Allen Chou
Level 0
**



View Profile WWW
« Reply #15 on: May 11, 2015, 07:16:25 PM »

You can also end up with leaks if you don't unset event handlers you made, but afaik, that will only leak memory, it won't really cause lag.


When adding the event listener, there is a "useWeakReference" boolean parameter defaulted to false. If set to true, you don't have to explicitly remove the listener for the event dispatcher object to become eligible for garbage collection.
So making this param to true is better than removing the listener??

I'm not sure how the GC is implemented regarding this matter. One might be more efficient than the other; you can only know by profiling a stress test.

I personally always remove the listeners as opposed to relying on the weak reference argument.
Hmm. Do you know any free profiling tool?? I am using flash builder 4.7 to build my game , unfortunetely the profile tool is only availabe in the premium version


You can perform very rudimentary profiling simply by using the getTimer() function to time your code.
Logged

RamyDergham1995
Level 0
**


View Profile
« Reply #16 on: May 12, 2015, 02:08:41 PM »

Sure will give it a try , Thanks :D
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic