# Libgdx: Am I abusing Vector2? Is there a better way to do my position updates and rendering?

I'm simulating hair in a game. Currently I have a HairField object, which has a position defined by a Vector2. Each HairField has multiple Hair objects in a list, each with a position defined by a Vector2. Each hair has multiple HairParticle objects in a list, each with a Vector2 position. Like so:

HairField class
Vector2 pos;  //  In world coords
List<Hair> hairs;

Hair class
HairField hairField;
Vector2 pos;  //  Relative to parent HairField
List<HairParticle> hairParticles;

public Vector2 getWorldPos() {
}

HairParticle class
Hair hair;
HairParticle parent;
Vector2 pos;  //  Relative to parent HairParticle

public Vector2 getWorldPos() {
}


My hair physics/rendering loop looks like the following:

for each (Hair h : hairField):
HairParticle parent = null;
for each (HairParticle p : h):
if (parent==null) continue;
//  get position info from each HairParticle's getWorldPos()
//  update p, constraining to certain distance from parent
//  render line from parent --> p


You can see that I'm calling a metric buttload of getWorldPos() methods on the particles, all so I can have information for updating/rendering the particle. Each of these calls involves multiple Vector2.cpy() calls and some vector math.

My question is, am I abusing Vector2's? They make for easy math, but I'm wondering if there's some more primitive (but faster) way I should be doing things. I've heard about transformation matrices, but don't know much about them yet (been reading). Does Libgdx expose some quicker way to be doing this math for updating/rendering that I should be using instead?

• The question is, are you experiencing performance problems? If not, proceed with the faster/easier way and save optimizing for when you reach that bridge. – petervaz Jun 6 '13 at 22:04
• Performance is just fine on my desktop, but on the phone it's abysmal. Like 4fps. I understand that this is an inherently (or rather in-HAIR-ently! heh) intensive task, simulating hundreds of particles. But I'm just wondering if there's a better way. As it is, all the Vector2.cpy() calls are all instantiating new objects, which I'm guessing is impacting performance a great deal. Maybe I could save some time by caching the values, but I'm wondering if Libgdx exposes something "closer to the metal" which is much faster. – loneboat Jun 6 '13 at 22:09
• What command are you using to render? – bobobobo Jun 6 '13 at 23:32
• @bobobobo: Right now I'm just using ShapeRenderer to draw raw lines. Later I'm planning to do something like rendering sprite batches or something, but this is my first graphics project, so I'm still just learning. Right now I'm just focusing on the simulation side of things. – loneboat Jun 7 '13 at 15:12

One of the problems that could be impacting performance is the fact that you're copying your position vector whenever you want a world position. It would be better for you to just have a field for the world position as well, and then you just alter both when you call setPosition.

Hair class
HairField hairField;
Vector2 pos;  //  Relative to parent HairField
Vector2 worldPosition;  //  Relative to the world
List<HairParticle> hairParticles;

public Vector2 getWorldPos() {
return worldPosition;
}

public void setPos(Vector2 pos) {
this.pos = pos.cpy();
}


It's not wrong to use Vector2, they're very helpful objects, but if you don't need all the added functionality, it might just be better to have a float x, y instead of a Vector2 object.

Did you find out which part of your game loop is it slowing down on android? Is rendering or logic, or both? You should profile exactly which part is slowing down and narrow it down.

Assuming you profiled every damned part of your code and you can't find what's making it slow. Then, on andorid, one possibility is garbage collector is taking a chunk of time per frame. GC slow downs can be fairly sporadic and hard to catch through only profiling time. If you're generating out tons of garbage every frame then GC might be slowing it down. GC is slow in general on Android (especially on older devices). So trace your code to see if all the Vector2 copies you made are tossed away(not referenced anymore) as fast as they are generated. You can check out how often and how long it took GC to collect garbage through DDMS view (google it up, I forgot specifics).

Further assuming that GC is your problem, then one way I know is to use an object pool. See https://code.google.com/p/libgdx/wiki/MemoryManagment

• I kinda just assumed you're using android here lol. If you're using the hot new iphone port then I don't know much about that. – XiaoChuan Yu Jun 6 '13 at 22:32
• BTW Java GC is usually (not sure about android) optimized for small short-lived objects. A constant drop in FPS is most likely not caused by the actual creating of new objects or collecting them. – MartinTeeVarga Jun 7 '13 at 2:16
• It could also be a problem of too many draw calls, since you have many moving objects. I'm guessing that the problem is how you draw your hair-particles, depending on whether you call a draw-method for every single hair particle or not. Doing hundreds of draw calls can severely cripple performance. – VaTTeRGeR Jul 17 '15 at 19:07