Speeding up BRIK Brick Breaker

Physics engine box2D is an excellent tool that makes writing games much faster since it provides all the physics and collision detection you might need. In case of BRIK Brick Breaker I had some performance problems with it – when a some objects got stuck between bricks the game was quickly slowing down to a crawl. Soon I was forced to act when on one level the physics took 180ms to execute… A thorough investigation allowed me to find the reason for such slow down – when objects are stuck and overlap with other objects they try to get free and the impulses during collision increase to infinity. As a temporary solution that solved it in one line of code I have decided to cut out all such impulses (with very high values) in postSolve – it worked. The game is now fast and fluid even on older hardware.
Permament and better solution would be setting dumpening for linear and angular movements for all objects to a non-zero value because I suspect the impulses wouldn’t rise so high then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Override
public void postSolve(Contact contact, ContactImpulse imp)
{
    float f[];
    f = imp.getNormalImpulses();
    if(f.length>0)
    {
        for(int i=0; i<f.length; i++)
        { 
            if(f[i]>maxImpulse) // the resulting impulse is too high
            {
                f[i] = maxImpulse;
                contact.setEnabled(false); // temporary solution - disable such contact
            }
            else if(f[i]<-maxImpulse) // lower than zero, too low value
            {
                f[i] = -maxImpulse;
                contact.setEnabled(false); // temporary solution - disable such contact
            }
        }
    }
        
    f = imp.getTangentImpulses(); // the same for tangent impulses (...)
    // the same for tangent impulses (...)
    }
}
@Override
public void postSolve(Contact contact, ContactImpulse imp)
{
	float f[];
	f = imp.getNormalImpulses();
	if(f.length>0)
	{
		for(int i=0; i<f.length; i++)
 		{ 
			if(f[i]>maxImpulse) // the resulting impulse is too high
			{
				f[i] = maxImpulse;
				contact.setEnabled(false); // temporary solution - disable such contact
			}
			else if(f[i]<-maxImpulse) // lower than zero, too low value
			{
				f[i] = -maxImpulse;
				contact.setEnabled(false); // temporary solution - disable such contact
			}
		}
	}
		
	f = imp.getTangentImpulses(); // the same for tangent impulses (...)
	// the same for tangent impulses (...)
	}
}

Lesson learned – keep track of speeds your objects have and forces that are generated in box2d, otherwise you might get some unexpected results and slow downs.

Here are some screenshots from a paid version I have just released – BRIK Extreme:

Screenshot_2014-10-07-14-45-33 Screenshot_2014-10-07-14-42-14 Screenshot_2014-10-07-14-36-59 Screenshot_2014-10-07-14-36-17 Screenshot_2014-10-07-14-35-00

You may also like...