一个超高效的不规则物体碰撞检测的类
2008-06-18 10:58
441 查看
在坛里看到过有朋友贴过不规则物体碰撞检测的类,这里,我再贴个一位牛老外写的一个类,经自己测试,用一个1500*1500的矢量图和一个10*10的不短移动的小球进行碰状检测,CPU占用仅4%左右,超级高效!!!
这里是代码:
Actionscript:
CODE:
算法值得学习。
这里是原文地址:http://www.tink.ws/blog/as-30-hittest
这里是代码:
Actionscript:
CODE:
package ws.tink.display { import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.DisplayObject; import flash.display.Sprite; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; public class HitTest { public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Boolean { return complexIntersectionRectangle( target1, target2, accurracy ).width != 0; } public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle { // If either of the items don't have a reference to stage, then they are not in a display list // or if a simple hitTestObject is false, they cannot be intersecting. if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle(); // Get the bounds of each DisplayObject. var bounds1:Rectangle = target1.getBounds( target1.root ); var bounds2:Rectangle = target2.getBounds( target2.root ); // Determine test area boundaries. 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, accurracy:Number = 1 ):Rectangle { if( accurracy <= 0 ) throw new Error( "ArgumentError: Error #5001: Invalid value for accurracy", 5001 ); // If a simple hitTestObject is false, they cannot be intersecting. if( !target1.hitTestObject( target2 ) ) return new Rectangle(); var hitRectangle:Rectangle = intersectionRectangle( target1, target2 ); // If their boundaries are no interesecting, they cannot be intersecting. if( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 ) return new Rectangle(); var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 ); // Draw the first target. bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) ); // Overlay the second target. bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE ); // Find the intersection. var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF ); bitmapData.dispose(); // Alter width and positions to compensate for accurracy if( accurracy != 1 ) { intersection.x /= accurracy; intersection.y /= accurracy; intersection.width /= accurracy; intersection.height /= accurracy; } intersection.x += hitRectangle.x; intersection.y += hitRectangle.y; return intersection; } protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy: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; matrix.ty = localToGlobal.y - hitRectangle.y; matrix.a = matrix.a / rootConcatenatedMatrix.a; matrix.d = matrix.d / rootConcatenatedMatrix.d; if( accurracy != 1 ) matrix.scale( accurracy, accurracy ); return matrix; } } }封装了3个静态方法,直接用传参调用即可!
算法值得学习。
这里是原文地址:http://www.tink.ws/blog/as-30-hittest
相关文章推荐
- 一个超高效的不规则物体碰撞检测的类
- 分享一个超高效的不规则物体碰撞检测的类~~
- 超高效的不规则物体碰撞检测
- actionscript不规则物体碰撞检测。
- 物理引擎中不规则物体的碰撞检测
- 分享一位国外大牛写的不规则物体像素级碰撞检测算法及详解
- 简单检测物体碰撞
- [源码解读]Silverlight 4 中对不规则对象进行碰撞检测(在游戏中常使用的是否碰撞怪物边界等原理)
- 【AS3代码】精确到点的不规则图形碰撞检测
- 粗略的物体碰撞预测及检测
- libgdx游戏引擎开发笔记(十三)SuperJumper游戏例子的讲解(篇七)----各个物体的创建及其碰撞检测
- 射线检测,单击鼠标是否单击到一个物体上
- 不规则形状的碰撞检测
- 笔记:基于距离的多物体碰撞检测
- 十六.碰撞检测,外接圆检测,外接矩形检测,多个物体两两比较
- 【canvas】基于坐标的碰撞检测 / 基本的动画 / 多物体动画
- Unity3d 物体碰撞检测前提条件(OnTrigger触发条件)
- Unity3D三维坐标(X,Z轴)下物体拖动移动(贴地面移动)实现并实现物体碰撞检测
- 不规则碰撞检测原理
- 基于投影方法的碰撞检测以及一个测试DEMO【C + SDL】