Did you know? Hints and tips

Thread created to share small, library independent (except OpenGL bindings) hints and tips. Please do not post obvious ones, let this topic become a valuable source of informations. :slight_smile:

I will start with this one:

Did you know that you can use WeakReference objects to allow garbage collection of objects which are no longer accessible via “classic” references? In case of games, this can be useful for example in case of listeners.
More info/explanation: http://java.dzone.com/articles/reference-types-java-part-1

Some tips and tricks
http://www.alethis.net/reference/java/java.html

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.

I believe these versions are more succinct: ::slight_smile:
P.S.: rx is w/2 & ry is h/2!

public boolean isColliding(Actor a) { // CENTER coords.
  return x >= a.x-rx & x < a.x+rx & y >= a.y-ry & y < a.ry;
}
public boolean isColliding(Actor a) { // CORNER coords.
  return x >= a.x & x < a.x+w & y >= a.y & y < a.y+h
}

Another good 1… Test for even or odd: (value & 1) == 0.
It’s faster than value % 2 == 0! :wink:

Just off the top of my head these are ones I see all the time.


if(randomBoolean == true)

//can be condensed to

if(randomBoolean)

also System.arraycopy exists and is much faster than copying an array using a loop.

Also if you

import static java.lang.System;

you can print by simply doing

out.println

instead of

System.out.println

For circle shaped objects:

public boolean isColliding(Actor a) { // CIRCLE shapes.
  return sq(a.x - x) + sq(a.y - y) < sq(a.rad + rad);
}
public static final double sq(double val) {
  return val*val;
}

It’s quite likely it’s not faster.
At least not noticeably faster.

Those methods you posted are exactly the methods that are beaten ~2x by my code. Short code does not equal fast performance.

If you want the short versions:


public boolean collides(AABB aabb)
   {
      return Math.abs(x - aabb.x) < w + aabb.w && Math.abs(y - aabb.y) < h + aabb.h);
   }

   public boolean inside(float ox, float oy)
   {
      return Math.abs(x - ox) < w && Math.abs(y - oy) < h;
   }

If you’re going for absolute performance, many of the Math arithmetic methods (min, max, abs, etc.) are usually intrinsified, and thus likely unbeatable in terms of speed. (disregarding high level optimizations)
Here’s a list of known intrinsics: http://www.java-gaming.org/topics/hotspot-intrinsics/27010/view.html

However make sure you read the Math source, some methods (sin, cos, etc.) delegate to StrictMath and may not be intrinsified, which you could probably beat if you don’t mind approximation.

You can initialize a List/Collection of any sort in one statement using double bracket initializers, like you can with arrays:


 Set<String> testSet = new HashSet<String>() {{
	add("valueA");
	add("valueB");
	add("valueC");
	add("valueD");
 }};

Useful!

Also, there’s this really neat app that you can monitor your Java apps with called Java Visual VM or something like that. It comes with the JDK, look in jdkdir/bin/(something like jvisualvm).exe.

Reasons this might not be a great idea: http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization

Some alleviation using Arrays: [icode]List s = Arrays.asList(“1”, “2”);[/icode]