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
This commit is contained in:
@@ -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;
|
package com.threerings.media.sprite;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
@@ -221,6 +222,35 @@ public class Sprite implements DirectionCodes
|
|||||||
return _bounds.contains(x, y);
|
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
|
* Returns whether the sprite is inside the given shape in pixel
|
||||||
* coordinates.
|
* coordinates.
|
||||||
|
|||||||
Reference in New Issue
Block a user