since all the math operations in vecmath apply the result to the object they’re called on instead of returning the result, i can’t just replace + with add() and make sure the parentheses are in the right place. How should i convert a mathematical expression to vecmath without using a bunch of temporary variables?
What vector math library are you using?
There should be functions available that are Copy-On-Write.
Javax.vecmath
It doesn’t. For example adding vectors v1 and v2 with v1.add(v2) stores the result in v1
You could always create a quick wrapper class that does it for you. Or some public static methods in a Util class. Many ways to solve this, just depends on how you like to code things.
javax.vecmath creates so much garbage that it is best avoided
That is just so not true.
Am using it and gwt-vecmath for years in highly memory-allocation-profiled applications and javax.vecmath and gwt-vecmath are amongst the very few vecmath libraries out there that actually do no allocation at all other than what you have to do in your own code.
So, create a few cached Vector3f, Vector4f and Matrix4f (simply because we are using Java here and not C/C++ and every vecmath library in Java requires you to have some object representation of a vector and a matrix to work with) and operate on them, and you’re good to go.
Sun has done a decent job on javax.vecmath.
If you want an example of a very bad library, I’d suggest having a look at jglm. It is supposed to be a port of C/C++'s great glm library, but does a horrible job at it, because it is allocating everything everywhere in methods you cannot control.
Unfortunately I need it for jbullet
I’d recommend you to try to use openMaLi. Here is the conclusion of the author of the JBullet. Though, it is rather negative, openMaLi was improved since then and is much more GC-friendly.
Also, its API is similar enough to vecmath, so I think you can easily replace the vecmath code with the openMaLi one’s in the JBullet (maybe, even, at the ‘Find-Replace’ level), IIRC, someone’s performed the similar task. I would find the links If you will be interested in. So, maybe there’s the reason to try.
Btw, you can have a look at the JME’s experience. AFAIK, it has its own math library and uses JBullet.

It doesn’t. For example adding vectors v1 and v2 with v1.add(v2) stores the result in v1
Use:
public Vec2d add(Vec2d v){
....
return this;
}
public Vec2d add_Res(Vec2d v){
....
return v;
}
public Vec2d add_New(Vec2d v){
....
return new Vec2d(...);
}
p.s if you care about performance make all calculation separate by primitives) - without GC affect
v1.x = v2.x + v3.x + 10;
v1.y = v2.y + v3.y + 10;
or
(v1.add(v2).add(v3).add(10)) //but this hard for big math expression