java - Integrating Box2D Body creation with an Entity-Component System -
background
i developing game (using libgdx) , have learned entity-component systems. decided use ecs came libgdx, ashley. however, applies using box2d entity-component system.
problem
creating box2d body , attaching shape relatively complex. here basic example:
bodydef bodydef = new bodydef(); bodydef.type = bodytype.dynamicbody; bodydef.position.set(0, 0); body body = world.createbody(bodydef); circleshape shape = new circleshape(); shape.setradius(1f); fixturedef fixturedef = new fixturedef(); fixturedef.shape = shape; body.createfixture(fixturedef); shape.dispose();
bodydefs , fixturedefs copied when bodies , fixtures created them, reusable. many of game objects have same shape, can share fixturedef:
private static polygonshape shape = new polygonshape(); public static fixturedef platformfixturedef = new fixturedef(); static { shape.setasbox(1f, 0.2f); platformfixturedef.shape = shape; // side question, in situation, how should dispose of shape when game over/closed? }
however, while parts of bodydef remain same type of objects, position different. how should go keeping parts of bodydef same, while changing position? better reuse 1 static bodydef, changing position variables each time?
or better each body want create, create new bodydef? if create new bodydef each time, impact memory usage or performance? (i creating ~10 per second)
the best course of action in situation focus on game logic first , use design pattern conventions on optimization techniques. once happy game can use profiling tools determine how game does. libgdx seems have own set of tools. regards bodydef
objects, construction extremely simple. it's bunch of primitives being set defaults. memorywise, 9 (floats)*4 + (booleans, considering each boolean in jvm maximum byte)5 + 5 (references) * ~8 (on 64bit) = 81 bytes, give or take, per bodydef
object. source of bodydef
here. shouldn't problem @ all. if limit scope of definition objects, on body destruction there shouldn't references tracing definitions, marked cleanup automatically.
regarding ecs, don't see how fits in question. here's info ecs , libgdx ecs.
Comments
Post a Comment