-
像素精确碰撞算法优化
- package
-
- {
-
-
-
- import flash.display.BitmapData;
- import flash.display.BlendMode;
- import flash.display.DisplayObject;
- import flash.geom.ColorTransform;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
-
-
-
- public class BitmapHitTestPlus
-
- {
-
- public static var tileSize:int=20
- public static function complexHitTestObject(target1:DisplayObject, target2:DisplayObject):Boolean
- {
-
- var scaleX:Number = (target1.width < target2.width ? target1.width : target2.width) / BitmapHitTestPlus.tileSize
-
- var scaleY:Number = (target1.height < target2.height ? target1.height : target2.height) / BitmapHitTestPlus.tileSize
-
-
- scaleX = scaleX < 1 ? 1 : scaleX
- scaleY = scaleY < 1 ? 1 : scaleY
-
- var pt:Point=new Point()
-
- var ct:ColorTransform=new ColorTransform()
- ct.color=0xFF00000F
-
- var oldHitRectangle:Rectangle=intersectionRectangle(target1, target2)
-
- var hitRectangle:Rectangle= new Rectangle()
- return complexIntersectionRectangle(target1, target2 , scaleX , scaleY , pt , ct , oldHitRectangle,hitRectangle,BitmapHitTestPlus.tileSize,BitmapHitTestPlus.tileSize).width != 0;
-
- }
-
- public static function intersectionRectangle(target1:DisplayObject, target2:DisplayObject):Rectangle
-
- {
-
-
-
-
-
- if (!target1.root || !target2.root || !target1.hitTestObject(target2))
- return new Rectangle();
-
-
-
-
-
- var bounds1:Rectangle=target1.getBounds(target1.root);
-
- var bounds2:Rectangle=target2.getBounds(target2.root);
-
-
-
-
-
- var intersection:Rectangle=new Rectangle();
-
- intersection.x=Math.max(bounds1.x, bounds2.x);
-
- intersection.y=Math.max(bounds1.y, bounds2.y);
-
- intersection.width=Math.min((bounds1.x + bounds1.width) - intersection.x, (bounds2.x + bounds2.width) - intersection.x);
-
- intersection.height=Math.min((bounds1.y + bounds1.height) - intersection.y, (bounds2.y + bounds2.height) - intersection.y);
-
-
-
- return intersection;
-
- }
-
- public static function complexIntersectionRectangle(target1:DisplayObject, target2:DisplayObject, scaleX:Number , scaleY:Number , pt:Point , ct:ColorTransform ,oldhitRectangle:Rectangle,hitRectangle:Rectangle,nowW:int,nowH:int):Rectangle
-
- {
- if (!target1.hitTestObject(target2))
- return new Rectangle();
-
- hitRectangle.x = oldhitRectangle.x
- hitRectangle.y = oldhitRectangle.y
- hitRectangle.width = oldhitRectangle.width / scaleX
- hitRectangle.height = oldhitRectangle.height / scaleY
-
- var bitmapData:BitmapData=new BitmapData(nowW,nowH, true, 0);
-
- bitmapData.draw(target1, BitmapHitTestPlus.getDrawMatrix(target1, hitRectangle , scaleX , scaleY ),ct);
-
- if(scaleX!=1&&scaleY!=1){
- bitmapData.threshold(bitmapData,bitmapData.rect,pt,">",0,0xFF00000F)
- }
-
- bitmapData.draw(target2, BitmapHitTestPlus.getDrawMatrix(target2, hitRectangle , scaleX , scaleY ),ct, BlendMode.ADD);
-
-
- var hits:int=bitmapData.threshold(bitmapData,bitmapData.rect,pt,">",0xFF00000F,0xFFFF0000)
-
- var intersection:Rectangle=bitmapData.getColorBoundsRect(0xFFFFFFFF, 0xFFFF0000);
-
-
- bitmapData = null
-
- if(intersection.width!=0){
-
- if(scaleX>1||scaleY>1){
-
- if(hits<=(scaleX+scaleY)*1.5){
-
-
- var xadd:int=.5
- var yadd:int=.5
-
- var nextW:int=BitmapHitTestPlus.tileSize
- var nextH:int=BitmapHitTestPlus.tileSize
-
- if(intersection.width!=nowW){
- nextW=BitmapHitTestPlus.tileSize
- }else{
- nextW=nowW*2
- }
- if(intersection.height!=nowH){
- nextH=BitmapHitTestPlus.tileSize
- }else{
- nextH=nowH*2
- }
-
- oldhitRectangle.x += (intersection.x - xadd) * scaleX
- oldhitRectangle.y += (intersection.y - yadd) * scaleY
- oldhitRectangle.width = (intersection.width + xadd*2) * scaleX
- oldhitRectangle.height = (intersection.height + yadd*2) * scaleY
-
- scaleX = (oldhitRectangle.width / nextW)
- scaleY = (oldhitRectangle.height / nextH)
-
- scaleX = scaleX < 2 ? 1 : scaleX
- scaleY = scaleY < 2 ? 1 : scaleY
-
- intersection=complexIntersectionRectangle(target1,target2, scaleX , scaleY ,pt,ct,oldhitRectangle,hitRectangle,nextW,nextH)
- }
- }
- }
-
-
- return intersection;
-
- }
-
- protected static function getDrawMatrix(target:DisplayObject, hitRectangle:Rectangle , scaleX:Number , scaleY:Number ):Matrix
-
- {
-
- var localToGlobal:Point;
- var matrix:Matrix;
- var rootConcatenatedMatrix:Matrix=target.root.transform.concatenatedMatrix;
-
-
-
- localToGlobal=target.localToGlobal(new Point());
-
- matrix=target.transform.concatenatedMatrix;
-
- matrix.tx=(localToGlobal.x - hitRectangle.x) / scaleX;
-
- matrix.ty=(localToGlobal.y - hitRectangle.y) / scaleY;
-
- matrix.a=matrix.a / rootConcatenatedMatrix.a / scaleX ;
- matrix.d=matrix.d / rootConcatenatedMatrix.d / scaleY;
-
- return matrix;
-
- }
-
-
- }
-
-
-
- }
-
相关阅读:
2021牛客暑期多校训练营3AGuess and lies【dp】
leetcode 310. Minimum Height Trees 最小高度树(中等)
leetcode 70. Climbing Stairs 爬楼梯(简单)
go 结构体嵌套结构体指针,必须先初始化指针结构体,才能赋值
go 结构体 注意点:go和c一样,所有的运算都应该在函数内进行
go 模仿go自带的结构体,写go方法
go 结构体继承,及其方法
go json序列化与反序列化
go student mgr 增删改查管理 fmt.Scanf运用
C#手写页面数据校验类
-
原文地址:https://www.cnblogs.com/keng333/p/2439136.html
Copyright © 2020-2023
润新知