Distance based functions snippet
From JPCT
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);
}
}
}