From c80ddcee22991df372c3d3574880b1c54ddd4c35 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 31 Jan 2002 01:03:45 +0000 Subject: [PATCH] Added hit test which checks first to see if a pixel is in a sprite's bounding box and then checks to see that the pixel in question is occupied by a non-transparent pixel in the sprite's image. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@897 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/sprite/Sprite.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/media/sprite/Sprite.java b/src/java/com/threerings/media/sprite/Sprite.java index e9c935304..5b8e3a621 100644 --- a/src/java/com/threerings/media/sprite/Sprite.java +++ b/src/java/com/threerings/media/sprite/Sprite.java @@ -1,9 +1,10 @@ // -// $Id: Sprite.java,v 1.35 2002/01/22 18:58:52 shaper Exp $ +// $Id: Sprite.java,v 1.36 2002/01/31 01:03:45 mdb Exp $ package com.threerings.media.sprite; import java.awt.*; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Iterator; @@ -221,6 +222,35 @@ public class Sprite implements DirectionCodes return _bounds.contains(x, y); } + /** + * Returns true if the sprite's bounds contain the specified point, + * and if there is a non-transparent pixel in the sprite's image at + * the specified point, false if not. + */ + public boolean hitTest (int x, int y) + { + // first check to see that we're in the sprite's bounds and that + // we've got a frame image (if we've got no image, there's nothing + // to be hit) + if (!_bounds.contains(x, y) || _frame == null) { + return false; + } + + if (_frame instanceof BufferedImage) { + BufferedImage bimage = (BufferedImage)_frame; + int argb = bimage.getRGB(x - _bounds.x, y - _bounds.y); + int alpha = argb >> 24; + // Log.info("Checking [x=" + x + ", y=" + y + ", bounds=" + _bounds + ", " + alpha); + // it's only a hit if the pixel is non-transparent + return (argb >> 24) != 0; + + } else { + Log.warning("Can't check for transparent pixel " + + "[image=" + _frame + "]."); + return true; + } + } + /** * Returns whether the sprite is inside the given shape in pixel * coordinates.