All pastes #1930207 Raw Edit

ScreenCollisionDetector

public java v1 · immutable
#1930207 ·published 2010-09-01 01:23 UTC
rendered paste body
package collisions;import graphics.Sprite;import java.awt.Rectangle;import java.util.ArrayList;import physics.PhysicsData;import physics.Vector2;import core.GameObject;import core.ScreenManager;public class ScreenCollisionDetector {	public static final Vector2 LEFT = new Vector2(-1, 0),								BOTTOMLEFT = new Vector2(-1, -1),								BOTTOM = new Vector2(0, -1),								BOTTOMRIGHT = new Vector2(1, -1),								RIGHT = new Vector2(1, 0),								TOPRIGHT = new Vector2(1, 1),								TOP = new Vector2(0, 1),								TOPLEFT = new Vector2(-1, 1),								NONE = new Vector2(0, 0);	private ScreenManager screen;	private ArrayList<GameObject> toCheck = new ArrayList<GameObject>();		public ScreenCollisionDetector(ScreenManager screen) {		this.screen = screen;	}		/*	 * Adds a GameObject to the list of objects	 * to check against screen collision	 */	public void addToCheckList(GameObject go) {		this.toCheck.add(go);	}		/*	 * Returns a bounding rectangle for the given GameObject	 */	private Rectangle getRect(GameObject go) {		Sprite sprite = go.getSprite();		PhysicsData pd = go.getPhysicsData();		Vector2 position = pd.getPosition();		return new Rectangle((int)position.getXComp(), (int)position.getYComp(), sprite.getHeight(), sprite.getWidth());	}		/*	 * Returns a boolean on whether GameObject is within screen	 */	public boolean isVisible(GameObject go) {		if (this.screen.getRect().contains(this.getRect(go)))			return true;		return false;	}		/*	 * Returns the contacts to be passed to the ContactResolver	 */	public ArrayList<Contact> checkScreenCollisions() {		ArrayList<Contact> contacts = new ArrayList<Contact>();		for (GameObject go : this.toCheck) {			Vector2 contactNormal = this.findContactNormal(go);			contacts.add(new Contact(go.getPhysicsData(),										  null,										  1.0f,										  this.findPenetration(go, contactNormal),										  contactNormal));		}		return contacts;	}		private float findPenetration(GameObject go, Vector2 contactNormal) {		Sprite sprite = go.getSprite();		float height = sprite.getHeight(),			  width = sprite.getWidth();		PhysicsData pd = go.getPhysicsData();		Vector2 pos = pd.getPosition();		float x = pos.getXComp(),			  y = pos.getYComp();				if (contactNormal.equals(LEFT))			return x;		else if (contactNormal.equals(BOTTOMLEFT))			return (float) Math.hypot(x, y + height);		else if (contactNormal.equals(BOTTOM))			return y + height;		else if (contactNormal.equals(BOTTOMRIGHT))			return (float) Math.hypot(x + width, y + height);		else if (contactNormal.equals(RIGHT))			return x + width;		else if (contactNormal.equals(TOPRIGHT))			return (float) Math.hypot(x + width, y);		else if (contactNormal.equals(TOP))			return y;		else if (contactNormal.equals(TOPLEFT))			return (float) Math.hypot(x, y);		return 0.0f;	}		/*	 * Finds and returns the direction on which the	 * GameObject is hitting the screen	 */	private Vector2 findContactNormal(GameObject go) {		Sprite sprite = go.getSprite();		PhysicsData pd = go.getPhysicsData();		Vector2 position = pd.getPosition();		int screenWidth = this.screen.getWidth(),			screenHeight = this.screen.getHeight(),			objWidth = sprite.getWidth(),			objHeight = sprite.getHeight();		float objX = position.getXComp(),			  objY = position.getYComp();			// If it collides with the left		if (objX <= 0)			// If it also collides with the top			if (objY <= 0)				return TOPLEFT;			// Or if it also collides with the bottom			else if (objY + objHeight >= screenHeight)				return BOTTOMLEFT;			// If it only collides with the left			else				return LEFT;		// If it collides with the right		else if (objX + objWidth >= screenWidth)			// If it also collides with the top			if (objY <= 0)				return TOPRIGHT;			// Or if it also collides with the bottom			else if (objY + objHeight >= screenHeight)				return BOTTOMRIGHT;			// If it only collides with the right			else				return RIGHT;		// If it collides with the top		else if (objY <= 0)			return TOP;		// If it collides with the bottom		else if (objY + objHeight >= screenHeight)			return BOTTOM;		else			return NONE;	}}