movement and performance

Hi everyone,

I'm farely new to box2D since this is the first project I've worked with it but the project itself is nearly completed, just needs some optimization.

At the moment I'm not very happy with the performance and I'm not sure if its my code, flash itself, the game's complexity or box2D not being used in the correct manner so here's a couple questions I'd love to see answers for if anyone is kind enough.

First off the fla's framerate is set to 60fps for the animations to run smoothly but the game's engine is running on a FRIM 30fps loop and box2d is being used via quickbox2d at a step of 1/30 with 10 iterations.

Apart from the Player entity there are several non-box2d entities with choreographic AI (no more then 5 on state simultaniously, usually 2/3), there's also some other game logic conditions being checked in the game's loop but I don't believe it's the cause of chocking in the performance.

The Player entity which is a 'lunar-lander-like' ship (jets apply impulses up and sideways) and here is the biggest 'doubt' I have about the whole thing, for the movement I'm using ApplyForce as such:

Code:
if (_keyboard.isDown(_keys.UP)) {
        _player.body.ApplyForce(new b2Vec2(_engine._elapsed * _playerProps.jetUp[0], _engine._elapsed * _playerProps.jetUp[1]), _player.body.GetWorldCenter());
}

if (_keyboard.isDown(_keys.DOWN)) {
        _player.body.ApplyForce(new b2Vec2(_engine._elapsed * _playerProps.jetDown[0], _engine._elapsed * _playerProps.jetDown[1]), _player.body.GetWorldCenter());
}
                  
if (_keyboard.isDown(_keys.LEFT)) {
   _player.body.ApplyForce(new b2Vec2(_engine._elapsed * _playerProps.jetLeft[0], _engine._elapsed * _playerProps.jetLeft[1]), _player.body.GetWorldCenter());
}
                  
if (_keyboard.isDown(_keys.RIGHT)) {
   _player.body.ApplyForce(new b2Vec2(_engine._elapsed * _playerProps.jetRight[0], _engine._elapsed * _playerProps.jetRight[1]), _player.body.GetWorldCenter());
}


Any ideas on how to optimize things would be apreciated, thanks in advance.
I would really apreciate any help with this.

I've optimized all my code and even box2d's code and everything is working well on a good computer like my core 2 duo PC, my iMac core 2 duo and a MacPro at the office mainly because nothing could really go wrong when FPS is solid as a rock.

However when I play it on a slightly slower MacBook and the FPS goes from 30 to 23/28 "jumps" are noticable in the player's movement and trying to be precise on where to land or avoiding obstacles is a nightmare something.

In my opinion the game is not complex enough for this kind of problem and my code is not causing it for sure (I've checked and rechecked, writtent and rewritten, optimized so I'm really sure) so its got to be something else. My guess is that I should be using something other then ApplyForce or there's something I'm doing wrong with either box2D or QuickBox2D.

If any kind soul wants to help me keep my sanity in check I'd be forever grateful.
Thanks in advance.
Ok so noone knows the best way to move a box2d object. Thanks.
about the moving, you aleady doing it right.
  • for 60 fps use time step 1/60. use 0.0167 instead of 1/60.
  • imagine each second you run world.step() for 60 times. that is some heroic computation and require a heroic processor. try a slower FPS, like 30 or 24. change the time step also. dont forget to take your calculator to calculate 1/30 = 0.0333, 1/24 = 0.04167
Is this your first flash project, or your first box2d project?
Higher FPS on slower machines is not a good idea.
Most flash games I know (not talking about box2d games) run with 20~30 FPS.
Its not my first time using flash i've been using it for 10 years and made a living out of it so I'm pretty confortable with actionscript.

However, when it comes to games this is my first "serious" project so you can say I'm a complete noob ( cause I am 😛 ) and its my first time using box2d also.

Just to clarify, the only thing running at 60fps is flash itself and only movieclips with animations are affected by it, the game engine however is framerate independente and uses timer instead, its running at 30fps and I'm confident its very solid since I read loads and loads about the subject and saw several implementations. box2d (or rather, quickbox2d) is running at 1 / 30.

Ive changed from ApplyForce to ApplyImpulse and I noticed it is now a "bit" more stable on slow computers, however I'm still open to any tips
Oh, I see 😀

I don't know if you've noticed this thread: viewtopic.php?f=8&t=5581 Might find something useful there.

I think lowering FPS can still help even if you don't have code running every frame, but in that case you'll probably see slight improvement.
Graphics as well as filters can affect performance, a lot. Like the file format of the graphics and their size, or the use of vectors (to outline things) instead of fills.
Flash, apparently, also takes part of the fault, like in this thread.
Box2D itself can be quite slow, there are other faster flash engines if you really need performance improvement. But I doubt they can do everything Box2D does =D
Other than all that, maybe there's something in your code like using one type of joint instead of another.
You said "the FPS goes from 30 to 23/28", but I thought the FPS is 60? If it drops from 60 to 28 you must be doing something very wrong 😆
Do you have a demo we can test?
Sorry I wasnt very clear about it, flash is set to run at a framerate of 60 but the game (engine and game loop) runs on a Timer each 33ms (roughly 30 FPS). I only have the fla at 60 to make the movieclip's animations run smoother, not saying this aint the problem, I'll try at 30 also but i doubt it.

The game doesnt drop from 60 to 28. The game is set and should run at 30 FPS but on slow computers where it drops below that to 25 or 28 or something, those few iterations it misses make the object movement a LOT sloppier even though I'm using the elapsed time and maxing it for the calculations.

The objects are not complex at all, there's just some static platforms and 1 box that moves around nothing more. I'll take a look at those threads, thanks.
woops 😳
sorry ApplyForce and ApplyImpulse looks the same with my sleepy eyes.

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