It's pretty simple right now.
Collision Listener
Code:
world.addListener(new CollisionListener() {
public void collisionOccured(CollisionEvent event)
{
Entity a, b;
a = ((Entity)event.getBodyA().getUserData());
b = ((Entity)event.getBodyB().getUserData());
if(a != null && b != null) // Entity collision
{
a.touched(b);
b.touched(a);
}
else // Collided with something else, probably the environment
{
if(a != null)
a.touched();
else
if(b != null)
b.touched();
}
}
});
Environment logic loop
Code:
public void update(int delta) {
boolean first = true;
totalDelta += delta;
while (totalDelta > stepSize) {
world.step(stepSize * 0.01f);
totalDelta -= stepSize;
if (first) {
first = false;
for (int i=0;i<entities.size();i++) {
entities.get(i).preUpdate(delta);
}
}
for (int i=0;i<entities.size();i++) {
Entity e = entities.get(i);
if(e.isDestroyed())
{
if(e.getBody() != null)
world.remove(e.getBody());
entities.remove(i);
} else
e.update(stepSize);
}
}
Beyond that, there aren't any really interesting changes. Just a bunch of rather vanilla classes to flesh out objects, and a modified map reader to read maps generated by TileStudio (as well as place entities into the map based on this data). My test entities are all based off AbstractEntity, do I'm not even using anything expensive like onGroundImpl()
What seems to kill it is stacking bodies (such as crates). They don't seem to rest if they're touching other bodies, which means the physics engine is constantly chugging away trying to calculate things that really don't need to be done. All of my entities are 32x32 boxes (to match the 32x32 tile map)
FPS with step() is < 1, to the point where the game pretty much hangs after the fourth frame. Core CPU use is 100%
FPS without step is a solid 60. Core CPU use ~13%
I've tried removing my collisionlistener and it doesn't really affect performance in any positive way. I've also made sure all of my bodies canRest().
The only thing that relieves the CPU use is converting everything to statics.
I can get core CPU use down to around 50 at a step size of 50, but its not viable.