All Downloads are FREE. Search and download functionalities are using the official Maven repository.

straightedge.geom.path.ObstacleManager Maven / Gradle / Ivy

Go to download

Includes 2 main parts: - Path finding through 2D polygons using the A star algorithm and navigation-mesh generation Field of vision / shadows / line of sight / lighting. The basic polygon and point classes are the KPolygon and KPoint. KPolygon contains a list of KPoints for vertices as well as a center (centroid), area, and radius (circular bound or distance from center to furthest point). KPolygon was born out of the need for a more game-oriented and flexible polygon class than the Path2D class in the standard Java library. KPolygon implements java.awt.geom.Shape so it can be easily drawn and filled by Java2D's Graphics2D object. - This API provides path-finding and field-of-vision. For other complex geometric operations such as buffering (fattening and shrinking) and constructive area geometry (intersections and unions) it is recommended to use the excellent Java Topology Suite (JTS). The standard Java2D library also provides the Area class which can be used for some constructive area geometry operations. Note that there is a utility class PolygonConverter that can quickly convert KPolygons to JTS polygons and vice versa.

The newest version!
/*
 * Copyright (c) 2008, Keith Woodward
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of Keith Woodward nor the names
 *    of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
package straightedge.geom.path;

import straightedge.geom.util.*;
import straightedge.geom.*;
import java.util.*;
/**
 *
 * @author Keith
 */
public class ObstacleManager{
	public TileBag tileBag;
	public NodeConnector nodeConnector;
	public double maxConnectionDistance;

	public ObstacleManager(TileBag tileBag, double maxConnectionDistance){
		this.tileBag = tileBag;
		this.maxConnectionDistance = maxConnectionDistance;
		nodeConnector = new NodeConnector();
	}

	public void addObstacles(Collection newObstacles){
		// This method re-adds the obstacleList one by one which can be faster
		// since nodes are only checked against each other once rather than twice.
		for (T obst : newObstacles){
			this.addObstacle(obst);
		}
	}
	

	public void addObstacle(T obst){
		tileBag.add(obst);
		nodeConnector.addObstacle(obst, tileBag, maxConnectionDistance);
	}

	public void removeObstacle(T obst){
		long startTime = System.nanoTime();
		nodeConnector.clearConnectionsToRemovedObstacleNodes(obst);
		tileBag.remove(obst);
		KPolygon poly = obst.getInnerPolygon();
		ArrayList nearByObstacles = tileBag.getAllWithin(poly.getCenter(), poly.getRadius() + maxConnectionDistance);
		// Any nodes that may have been contained but now aren't need to be marked as so.
		for (T nearByObstacle : nearByObstacles){
			for (KNodeOfObstacle node : nearByObstacle.getNodes()){
				if (poly.getCenter().distance(node.getPoint()) <= poly.getRadius()){
					node.resetContainedToUnknown();
				}
			}
		}

		ArrayList nodesToBeReconnected = new ArrayList();
		for (T nearByObstacle : nearByObstacles){
			for (KNodeOfObstacle node : nearByObstacle.getNodes()){
				if (node.getPoint().distance(poly.getCenter()) < maxConnectionDistance + poly.getRadius()){
					nodesToBeReconnected.add(node);
				}
			}
		}
		nodeConnector.reConnectNodesAroundRemovedObstacle(obst, nodesToBeReconnected, nearByObstacles, maxConnectionDistance);

		long endTime = System.nanoTime();
		System.out.println(this.getClass().getSimpleName()+".removeObstacle running time = "+((endTime - startTime)/1000000000f));
	}

	public void remakeConnectionsBetweenAllObstacles(double maxConnectionDistance){
		this.maxConnectionDistance = maxConnectionDistance;
		// This method re-adds the obstacleList one by one which can be faster
		// since nodes are only checked against each other once rather than twice.
		Bag copyOfObstacles = new Bag(tileBag.size());
		copyOfObstacles.addAll(tileBag);
		tileBag.clear();

		long startTime = System.nanoTime();
		for (T obst : copyOfObstacles){
			this.addObstacle(obst);
		}
		long endTime = System.nanoTime();
		System.out.println(this.getClass().getSimpleName()+".remakeConnectionsBetweenAllObstacles addObstacle running time = "+((endTime - startTime)/1000000000f));
	}

	public TileBag getTileBag() {
		return tileBag;
	}

	public double getMaxConnectionDistance() {
		return maxConnectionDistance;
	}

	public NodeConnector getNodeConnector() {
		return nodeConnector;
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy