I’ve posted these on the forum before, but I may as well do so again.
They’re quite simple, but judging from the amount of people who don’t know about them, maybe they’re not so obvious.
1. You can cut AABB collision checks almost in half by changing the internals.
public class AABB
{
public float x, y; // The centre of the AABB.
public float w, h; // The extent (distance from centre to edge) of the AABB. Half of the actual width/height
public boolean collides(AABB aabb)
{
if(Math.abs(x - aabb.x) < w + aabb.w)
{
if(Math.abs(y - aabb.y) < h + aabb.h)
{
return true;
}
}
return false;
}
public boolean inside(float ox, float oy)
{
if(Math.abs(x - ox) < w)
{
if(Math.abs(y - oy) < h)
{
return true;
}
}
return false;
}
}
It’s not actually as well known as it should be.
2. A reliable and cross-platform way to get the location of the executing jar file.
public static String jarDir()
{
try
{
return new File(SomeClass.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
}
catch(URISyntaxException e)
{
e.printStackTrace();
return null;
}
}
And obviously the file of the jar itself can be obtained by removing the getParent() call.