Distance based functions snippet

From JPCT
Revision as of 00:32, 14 May 2014 by Corey (Talk | contribs)

Jump to: navigation, search

Distance based functions snippet

These snippets of code will help provide a base for a sophisticated distance / region oriented applications, particularly games that use jPCT:

This method for example, would allow you to check the distance between 2 objects:


	public boolean isInRange(double distance, SimpleVector player, SimpleVector target) {
		SimpleVector p = new SimpleVector(player);
		SimpleVector t = new SimpleVector(target);
		
		p.y = 0;
		t.y = 0;
		
		return p.distance(t) <= distance;
	}


If you wanted to use this snippet to render an entity list based on distance from the player you would do something like this:


	public void processInView(final Terrain terrain, final SimpleVector focusPoint) { // focusPoint would be camera pos, player pos, etc..
		for (int i = 0; i < OBJECTS_TO_GENERATE; i++) {
			if (!terrain.isInRange(Settings.RENDER_DISTANCE / 4, focusPoint, generatedObjectList[i].getTransformedCenter())) {
				generatedObjectList[i].setVisibility(false);
			} else {
				generatedObjectList[i].setVisibility(true);
			}
		}
	}