Difference between revisions of "Distance based functions snippet"

From JPCT
Jump to: navigation, search
(Distance based functions snippet)
(Distance based functions snippet)
 
Line 1: Line 1:
 
=== Distance based functions snippet ===
 
=== 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 snippet will provide some functions that could be used for a sophisticated distance based / region oriented jPCT application.
  
 
This method for example, would allow you to check the distance between 2 objects:
 
This method for example, would allow you to check the distance between 2 objects:

Latest revision as of 00:37, 14 May 2014

Distance based functions snippet

This snippet will provide some functions that could be used for a sophisticated distance based / region oriented jPCT application.

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);
			}
		}
	}