package com.easyasrpg.implement.algorithm
{
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
{
/**
*复杂的碰撞检测
* @param target1
* @param target2
* @param accurracy
* @return
*
*/
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;
}
}
}
分享到:
相关推荐
flash AS3.0检测碰撞工具类 超好用 方法都说静态的 直接调用就ok 像素级碰撞选用最后一个方法
hitTestObjects.as ---------------------- 像素级碰撞检测 LoaderSprite.as ---------------------- 处理Loader相关的Sprite Registration.as ---------------------- 变换注册点 SubMovieClip.as ------------...
碰撞检测:当玩家子弹击中敌机后,敌机爆炸,并播放爆炸音效 游戏规则 界面上有两个矩形条,分别表示生命值和得分; 被敌机子弹击中则减少1分并减少1点生命值,若我方子弹击中敌机则增加1分; 当生命值或得分减到0,...
这是个as3的工具类,用于实现检测2个不规则对象,是否有碰撞。 用法:var res:Boolean=BitmapHitTest.test( mc1,mc2); trace("mc1与mc2碰撞情况为:"+res);
Android游戏开发实例之抢滩登陆源码,资源完整,是android... 用于山地地形碰撞检测的工具类 用于绘制炮弹的球类 星空天球类 图形绘制类 代表陆地的类等 各代码文件中均有注释,很利于学习Android游戏开发。
day14_05在坦克碰撞检测方法里面底层用的是工具类方法当这个方法返回他true的时候我就暴露一个撞上方向出去这个撞上方向的值来自坦克成员方向的赋值在坦克move方法根据撞上方向有值就return不走下面坐标改变的逻辑...
-判断矢量边缘实现不规则物品碰撞检测(非位图) -高效位图碰撞及重叠矢量检测 -A星,深/广度,2D/3D/路点,寻路方式统一处理。 -二次贝尔法曲线,按长度切割和计算法线,光滑曲线拟合(过点或者不过点) -SWF解析器...
本游戏的重点放在屏幕的绘制和碰撞检测技术上,游戏屏幕的绘制关系到整个游戏的显示效果,而碰撞检测的难点是要准确、及时。这两种技术是游戏中的核心技术,其实现的好坏直接影响游戏的运行效果,并给用户不同的游戏...
2、实现了地形静态物体、动态物体的加载渲染,动态物体的更新,静态物体的四叉树、八叉树的生成和动态相机裁剪,主角和地形上物体的快速碰撞检测等功能。 3、两种方法实现阴影:A、实现了CPU构建阴影体的方法渲染...
2、实现了地形静态物体、动态物体的加载渲染,动态物体的更新,静态物体的四叉树、八叉树的生成和动态相机裁剪,主角和地形上物体的快速碰撞检测等功能。 3、顶点着色器阴影体:利用顶点着色器实现模型的阴影体形成...
如何进行碰撞检测。 如何使用键盘控制整个游戏。 ##三. 设计思想 游戏整体应用Java封装特性,创建两个包:com.valueyouth.gameFrame和com.vlaueyouth.util。 前一个包中,分别创建游戏框架类,飞机类和子弹类。在后...
16.1.3 aabb包围盒的碰撞检测 440 16.1.4 一个aabb包围盒的案例 442 16.1.5 旋转后的aabb包围盒 444 16.1.6 aabb包围盒的使用要点 448 16.1.7 obb包围盒 448 16.2 穿透效应 449 16.2.1 案例的...
2.2编译工具 第三章 设计小游戏需求及分析 3.1设计游戏框架 3.2敌人入场 3.3飞行物移动 3.4子弹入场 3.5英雄机随着鼠标动 3.6删除越界的飞行物 3.7子弹与敌人的碰撞 3.8画分和画命 3.9英雄机与敌人的碰撞 3.10检测...
┌第七章 j2me工具类使用 ┃7_1.随机数Random类的使用 ┃7_2.数学Math类的使用 ┃7_3.日期Date类的使用 ┃7_4.系统System类用法 ┃7_5.平台Runtime类的使用 ┃7_6.堆栈类Stack的使用 ┃7_7.枚举类Enumeration的使用 ...
控制器:游戏数据初始化游戏角色初始化游戏逻辑判断:碰撞检测道具拍摄检测根据当前分数,升级游戏难度工具类:用途:提供判断是否为副本,是否为图片,多个随机取值等公共方法提供加载图片和音频的方法提供定时器,...
因此,去除杂草的机具必须检测植物和其他障碍物以避免碰撞。 如下面所示,如果工具前有障碍物,则可以“感觉”到现有技术: 这种物理相互作用会损坏植物的树皮,从而使真菌生长。 非接触式系统,无论是基于传感器...
2.3 使用BSP的基本碰撞检测和反弹 2.3.1 碰撞和BSP遍历 2.3.2 粒子,场景检测和反弹 2.4 特殊的碰撞检测和反弹 2.4.1 AABB的定义 2.4.2 AABB类的定义和静态成员的定义 2.4.3 碰撞检测和碰撞反弹 2.4.4 使用AABB的伪...
2.3 使用BSP的基本碰撞检测和反弹 2.3.1 碰撞和BSP遍历 2.3.2 粒子,场景检测和反弹 2.4 特殊的碰撞检测和反弹 2.4.1 AABB的定义 2.4.2 AABB类的定义和静态成员的定义 2.4.3 碰撞检测和碰撞反弹 2.4.4 使用AABB的伪...
功能打包:可自定义的碰撞检测算法,多个激活器,可拖动的覆盖图,拖动手柄,自动滚动,约束等等。 支持广泛的用例:垂直列表,水平列表,网格,多个容器,嵌套上下文,可变大小的列表和网格,转换后的项目,