Optimizing physics calculations

Hi, I'm writing a game where some objects are falling from top, and they have polygon shapes, each shape consists about 10-20 points.
Those object should have some constant position at start so I have to create all of them in game load. I did some optimizations and
I am setting active false to objects which are not visible on the screen, it helped a lot in some old devices(like iPhone 3gs or 3g).
but still has some jerky movement.. if physics are not enabled it's working perfectly..
Falling objects are kinematic and I'm calculating their positions manually(cos they can;t be done easily with only physic forces)
so this is what I have done:
Code:
if (object->isVisible())
    object->getBody()->setActive(true);
else
    object->getBody()->setActive(false);


Is there any better solution for optimizing physics calculations?
Thanks, George.
Is this caused by the number of bodies and the number of points their fixtures have? Does it improve if you have less bodies and less points, or make them circles etc?
You are moving the bodies by using SetLinearVelocity right?
irresistible force wrote:
Is this caused by the number of bodies and the number of points their fixtures have? Does it improve if you have less bodies and less points, or make them circles etc?
You are moving the bodies by using SetLinearVelocity right?

Yes, I think so..
no I am changing transform of each object like this:
Code:
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL)
        {
            LHSprite *sprite = (LHSprite*)b->GetUserData();
            if(sprite != NULL)
            {
                if (sprite->isVisible() && isCollisionEnaled) {
                    b->SetActive(true);
                    CCPoint spritePosition = mainLayer->convertToWorldSpaceRecursively(sprite);
                    float spriteRotation = sprite->getRotation();
                    b->SetTransform(LevelHelperLoader::pointsToMeters(spritePosition),
                                    -1 * CC_DEGREES_TO_RADIANS(spriteRotation));
                }
                else {
                    b->SetActive(false);
                }
            }
        }
    }
Quote:
Yes, I think so..

Well if you try using less bodies or less points for each body, then you would know, and we can figure out the problem with less stabbing in the dark 🙂
Do the shapes really need 20 points?

It seems kinda backwards to be setting the physics body positions from sprites. Usually the opposite would be done, otherwise what is the point of having a physics engine...
I guess you are using some tweening features of the sprites, and you want to have collision detection only from Box2D? In that case, maybe what you are doing is ok. Looks like you are using cocos2d - is it setting the sprite positions from the physics bodies maybe?
irresistible force wrote:
Quote:
Yes, I think so..

Well if you try using less bodies or less points for each body, then you would know, and we can figure out the problem with less stabbing in the dark 🙂
Do the shapes really need 20 points?

It seems kinda backwards to be setting the physics body positions from sprites. Usually the opposite would be done, otherwise what is the point of having a physics engine...
I guess you are using some tweening features of the sprites, and you want to have collision detection only from Box2D? In that case, maybe what you are doing is ok. Looks like you are using cocos2d - is it setting the sprite positions from the physics bodies maybe?

I tried to use less points per shape and it did improve game performance..
And yes, I'm writing a game with cocos2d-x framework. I have written some custom animations so I'm handling sprite movements, and I'm using box2d only for detecting collisions. I thought it would be the best in this case... but still has some jerk movement when shape has many points in iPhone 3gs and 3g
To properly implement animated motion for b2_dynamicBody type objects with collision, I recommend instead of SetTransform (teleports) something like this:

Code:
TSBody* gameObject = (TSBody*)body->GetUserData();
if(gameObject)
{
    gameObject->update(time);
    const b2Transform& transform = body->GetTransform();
    b2Vec2 currentPosition = transform.p;
    b2Vec2 futurePosition = gameObject->position;
           
    body->SetLinearVelocity(60.0 * (futurePosition - currentPosition));
           
    if(!body->IsFixedRotation())
    {
        float32 currentAngle = transform.q.GetAngle();
        float32 futureAngle = gameObject->angle;
        body->SetAngularVelocity(60.0 * (normalizeAngle(futureAngle, currentAngle) - currentAngle));
    }
}
pTymN wrote:
To properly implement animated motion for b2_dynamicBody type objects with collision, I recommend instead of SetTransform (teleports) something like this:

Code:
TSBody* gameObject = (TSBody*)body->GetUserData();
if(gameObject)
{
    gameObject->update(time);
    const b2Transform& transform = body->GetTransform();
    b2Vec2 currentPosition = transform.p;
    b2Vec2 futurePosition = gameObject->position;
           
    body->SetLinearVelocity(60.0 * (futurePosition - currentPosition));
           
    if(!body->IsFixedRotation())
    {
        float32 currentAngle = transform.q.GetAngle();
        float32 futureAngle = gameObject->angle;
        body->SetAngularVelocity(60.0 * (normalizeAngle(futureAngle, currentAngle) - currentAngle));
    }
}


Thanks..
In this case I only one dynamic body object and all the remaining ones are kinematic (appx. 200 objects)
cos I don't want any movement when collision occurs..
200 objects? I think I see your problem now...
I would not expect good performance on iPhone 3g.
irresistible force wrote:
200 objects? I think I see your problem now...
I would not expect good performance on iPhone 3g.

Yes, but I enable and draw only items which are visible on the screen, and at most I have 10 objects visible on the screen..
Does the speed improve if you get rid of those offscreen objects completely?
If they are inactive and invisible, do you still need them?

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