Application performance.

Hello,
I wrote a simple game (with box2d), but it has small lags at my PC and my smartphone. I have background( big sprite ) and ~40 small sprites( png images with alpha-channel ).
In my update function - void update( ccTime dt ) - “dt” has various values. It can be 0.0f and 0.07f(14 fps) sometimes.
How I can stable this time? My game is already created, but these lags looks not fine.

Dmitry.

There is several things you can do. Always the best thing is to search for your common problems first. Many people have gone through it.

What you should look for is using the CCSpriteBatchNode for your sprites and for your background image, split into smaller piecies, example; if your bg image is 1024x768, then its best to use two 512x768 images, it performs better.

Bad coding style usually leads to bad perfomance as well. Look at the examples, they show the better use of the classes!

Cheers and welcome! Hope i managed to help in someway. :slight_smile:

Thanks for reply.

I can’t use tiles becouse of content of my background.

I’m optimizing code now, and have one question:
Can I set callback function for detecting collision between two specific objects (for example it will depend of user data)?

I have similar problems and have seen a few threads in the past asking the same question. It isn’t down to bad coding and as far as I know hasn’t been resolved. Perhaps someone can explain this:-

If you download the latest ccocos2d-1.0.1-x-0.13.0-beta and open the HellowWorld app, then add a “scheduleUpdate();” to the end of the “bool HelloWorld::init()” function, add the following function for the update call:

void HelloWorld::update( cocos2d::ccTime dt )
{
    printf( "ccTime: %f\n",dt );
}

Run it on a iPhone 4, all the program is doing is showing the Cocos2DX logo and it “should” be calling the update function 60 times a second with a steady delta value of 0.166666 (that is 1/60). But instead it gives the following in the console output:

ccTime: 0.017457
ccTime: 0.015649
ccTime: 0.016756
ccTime: 0.016518
ccTime: 0.016742
ccTime: 0.016803
ccTime: 0.018461
ccTime: 0.015265
ccTime: 0.016842
ccTime: 0.016505
ccTime: 0.017283

Some crazy values there, some are less than 0.16666 (that shouldn’t happen), Some are way too high like “0.018461” (remember the app isn’t actually doing anything more than displaying a sprite).

Trying to write code that uses the delta value to cope with speed changes obviously has problems. Anyone have any ideas???

Replying to dmitry:

Well, it all depends. If you want to detect collision using a physics engine or using the CGRect of the sprites. If you want to use the Rect, you need to loop your sprites and check if they intersect. I used box2d for collisions in my game. And it worked out fine…

Both methods have been covered on this forum! Do a search! Cheers! :slight_smile:

Joe,

Thanks for reply, but I know both methods, and using in my game.
Now, we must use callback object for all contacts to find collision.
I asked:
Are there any way to set up callback object/function for box2d body, which will work at each collision with this body.
I think it can be done, when performs collision processing, isn’t it?

Dmitry.

Well, im a little confused since you told me you checked both methods and still is asking for a question how to do a callback on collision.

Here is how i do it; I inherit from b2ContactListener class that has some callbacks.

class GamePhysicsContactListener : public b2ContactListener {
public:
    GameContactListener();

    void* userData;
    void BeginContact(b2Contact* contact);
    void EndContact(b2Contact* contact);
};

After i create my class on my physics manager, i tell my b2World to use that Object and make the callbacks to it, by doing:

gamePhysicsContactListener_ = new GamePhysicsContactListener();
(b2World*)world_->SetContactListener(gameContactListener_);

Is that what you are looking for ? Sorry if it isnt, im not quite sure! :slight_smile:

Joe,

Looks like! I always used another way from cocos2d sources.
Big thanks!

Dmitry.

No problem man, i will post the rest of my class so you know how to get the userData Object from the contact listener class

void GamePhysicsContactListener::BeginContact(b2Contact* contact) {

}

void GamePhysicsContactListener::EndContact(b2Contact* contact) {
    b2Fixture* fixtureA = contact->GetFixtureA();
    Game_PhysicsNode *actorA = (Game_PhysicsNode*)fixtureA->GetBody()->GetUserData();

    b2Fixture* fixtureB = contact->GetFixtureB();
    Game_PhysicsNode *actorB = (Game_PhysicsNode*)fixtureB->GetBody()->GetUserData();
}

Game_PhysicsNode is just a class inherited from CCNode.

One thing to remember is that you cannot remove your node on this method. It is iterating on the list, and you cannot remove it while it is iterating or else it will crash! I set a flag on my object (ex: actorA->collided = true), then in my game loop i check if it has collided then remove it.

Glad i could help! Cheers! :slight_smile:

Joe,

Thanks for help! As I can I check performance changes.

Dmitry.

Joe,

Big thanks again, I have got a 76% reduction of execution time of some imortant function!

Dmitry.

Hey dmitry! Glad i could help out! Any more questions don’t be afraid to ask! Some people here helped me a lot on releasing my game, i would like to pay it back by helping as much as i can! :slight_smile:

Joe,

If you can, write your email, may be I will have some question about game public/other. I will ask you.

Dmitry.

Joe,

If you can, write your email, may be I will have some question about game public/other. I will ask you.

Dmitry.

Its ok! You can find my email under my profile here: http://www.cocos2d-x.org/users/1917

Joe,

No, I can’t. I dont know why, but in my firefox 12 I see empty email field at all profile pages.

Dmitry.

I have sent you an email on your gmail account from this forum! Funny enough, yours too was hidden, only when i placed my mouse over it appeared… guess its a CSS problem from the website. If you do a CTRL+A when my profile page is loaded, you will probably will see mine too! :slight_smile:

Joe, Gav T,

I fouded solution. I searching for all I assumed, but nothing. Then I created default project(v.0.12.0) and created 100 - 200 images(my and default images from test project ) and project work with lags. I simply updated to v.0.13.0 beta. All lags disappeared, and FPS stabilized!

Dmitry.

+previous message.

I still have one problem.

I created “hello world” project( only 1 static small sprite ) and
tested it on HTC Sensation and my Samsung Galaxy S2.
I have 50 fps average( from 45 to 55 - fast changes ), instead my PC
(at PC fps very stable - 60).
I’m confused, game has one small sprite and slows on good devices!

I also used
CCTexture2D::setDefaultAlphaPixelFormat( kCCTexture2DPixelFormat_RGB5A1 );

Very interesting, that, when I added 100 sprites, game still has similar FPS ( not different! ).

P.S. Can You say, how much FPS You have with standard “hello world” project?

Dmitry.