Oh, Java Java. No operator overloading?

During the time when I worked on my project GA Workbench in Java/LibGDX I have found a strange downside of the Java language.

There is no operator overloading.

So you can NOT add together two vectors like this:

result = Vec1 + Vec2;

That is just not possible in Java. You have to do it via method:

result = Vec1.add(Vec2);

But guess what? It does not only return the value, it also adds the value to Vec1. To overcome this issue
one has to use a copy method. So simple addition turns into this mess:

result = Vec1.cpy().add(Vec2);

Here is an example from my project:

//velocity verlet - use this one, store previous acceleration
//v1 = v0 + (a0+a1)/2 . dt

Not so difficult, is it? Just take initial velocity, do an average of initial and current acceleration and make a step forward.
But now the code.

Vector2 accSum = this.oldAcceleration.cpy().add(a);
Vector2 v = (accSum.scl(0.5f)).scl(delta);
v.add(v0);

This is so hard to read. Even when I know what is happening here…


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *