XYZBLOG

WELCOME
« »
SuMoTuWeThFrSa
1234
567891011
12131415161718
19202122232425
2627282930
新近评论
新近留言
友情链接
  1. 友情链接 (1)
统计信息
  • 日志: 28
  • 评论: 2
  • 留言: 0
  • 链接: 1
  • 引用: 0
  • 访问: 27242
相册 普通 列表 管理
改进了网上流传的像素级碰撞检测,支持旋转后的mc检测,可以用于飞机游戏.
package general{
  import flash.display.*;
  import flash.geom.*;

  public class HitTest {
    //按像素检测
    private static function _hitTest(shape1:DisplayObject, shape2:DisplayObject):Boolean {
      var rect1:Rectangle = shape1.getRect(shape1.stage);
      var rect2:Rectangle = shape2.getRect(shape2.stage);
      var p1x:Number = rect1.x;
      var p1y:Number = rect1.y;
      var p2x:Number = rect2.x;
      var p2y:Number = rect2.y;

      var p1w:Number = rect1.width;
      var p1h:Number = rect1.height;
      var p2w:Number = rect2.width;
      var p2h:Number = rect2.height;
      p1w = p1w<1?1:p1w;
      p1h = p1h<1?1:p1h;
      p2w = p2w<1?1:p2w;
      p2h = p2h<1?1:p2h;
      
      var BmpData1:BitmapData = new BitmapData(p1w, p1h, true, 0x00000000);
      var BmpData2:BitmapData = new BitmapData(p2w, p2h, true, 0x00000000);
      
      var ma1:Matrix = getMatrix(shape1);
      var ma2:Matrix = getMatrix(shape2);
      
      BmpData1.draw(shape1, ma1);
      BmpData2.draw(shape2,ma2);
      
      var gp1:Point = new Point(p1x,p1y);
      var gp2:Point = new Point(p2x,p2y);
      var re:Boolean = BmpData1.hitTest(gp1, 0x05, BmpData2, gp2, 0x05); 
      
      BmpData1.dispose();
      BmpData2.dispose();
      return re;
    }
    
    static private function getMatrix(shape:DisplayObject):Matrix{
      var matrix:Matrix = new Matrix;
      var rect:Rectangle
      if (shape.parent) {
        var rot:Number = shape.rotation;
        matrix.rotate((shape.parent.rotation + rot) * Math.PI / 180) ;
        shape.rotation += shape.parent.rotation;
        rect = shape.getRect(shape.parent);
        matrix.tx = shape.x - rect.x;
        matrix.ty = shape.y - rect.y;
        shape.rotation = rot;
      }else {
        rect = shape.getRect(shape.parent);
        matrix.rotate(shape.rotation*Math.PI/180)
        matrix.tx = shape.x - rect.x;
        matrix.ty = shape.y - rect.y;   
      }
      return matrix;
    }
    //按矩形区域检测
    public static function hitTestRect(shape1:DisplayObject,shape2:DisplayObject):Boolean {
      if (shape1.hitTestObject(shape2))
        return true;
      return false;
    }
    //按点和形状检测
    public static function hitTestPoint(point:Point,shape:DisplayObject):Boolean {
      if (shape.hitTestPoint(point.x,point.y,true))
        return true;
      return false;
    }
    
    //按形状检测,这个方法很实用,先用效率高的hitTestObject判断是否可能碰撞,一旦发生碰撞了再用效率低的也就是网上流传的像素级碰撞检测的方法去判断
    public static function hitTestShape(shape1:DisplayObject,shape2:DisplayObject):Boolean {
      var hit:Boolean = false;
      if (shape1.hitTestObject(shape2)) {
        if (shape2.width<20 && shape2.height<20) {
          hit = true;
        } else {
          if (HitTest._hitTest(shape1,shape2)) {
            hit = true;
          }
        }
      }
      return hit;
    }
  }
}
http://127.0.0.1/blog/trackback.asp?q=197104929
姓名: 密码:
主页: 私密:

验证码:
    1  2  3  4  5  6  7  8  9  10