Here is how I would simply this:
First, I would create a reduced method for getting "body" in fixture as such:
contact.getBody("A");
Underneath the covers this could be
contact.getFixtureA().getBody()
But by doing it this way you
- Shorten the code
- Create a type of factory method for these "fixtures". This may not be appropriate for your case but with no other context that is what I would do
And, what's with all the
final
Craziness? You only need to do this if you want to assure yourself (or the compiler) that it won't be reassigned. This is hyper defensive and, in 15 years of writing code I have never found it to be a necessity.
Frankly, 'final' in this case is repetitive and distracting. Unless you have a need for it (which, from this code, you don't) then ditch it.
this:
if (!isPlayer(bodyA) || !isPlayer(bodyB)) { return; }
is good, but this:
if (!isPlayer(bodyA) || !isPlayer(bodyB)) { throw new IllegalArgumentException("Both bodies must be players!"); }
Is way better! Think about it: In the previous case, you 'return' and terminate the function. But, in this condition you have no idea if the job was done or not and if not, why not. This is pre-condition checking and an ideal case for it
final float vx_a = Math.abs(bodyA.getLinearVelocity().x); final float vx_b = Math.abs(bodyB.getLinearVelocity().x);
Your variable names mean zilch to me. You HAVE to give them a better name. Also, create them up in groups of 2.
This code here is also problematic.
if (save(bodyB)) return; markForDestroy(bodyB);
The return statements are pointless here. You could just do:
if (!save(bodyB)) markForDestroy(bodyB);
And now, a line is gone and your code flows naturally and doesn't abruptly exit the function.
Finally, this thing where you put your
return
on the same line is both cluttering and unnatural; They are sequential instructions to the CPU:
mv e248484 EAX cmp 0 JMP ... POPS
Ok, yes that is 80686 assembly but you get the point. It's a branch and function return.
All, of course, my .02 USD
save(bodyB)does, and what its return value means? \$\endgroup\$