关于tink的碰撞检测类【1】
tink博客上的一个碰撞检测类,思路是:用flash自带的hitTestObject预判断,再基于像素重叠检测碰撞。
原文链接:http://www.tink.ws/blog/as-30-hittest
代码如下:package ws.tink.display
{
importflash.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, thenthey are not in a display list
// or if a simple hitTestObject is false, they cannot beintersecting.
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 beintersecting.
if( !target1.hitTestObject( target2 ) ) return newRectangle();
var hitRectangle:Rectangle = intersectionRectangle( target1,target2 );
// If their boundaries are no interesecting, they cannot beintersecting.
if( hitRectangle.width * accurracy <1 ||hitRectangle.height * accurracy <1 ) return newRectangle();
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;
}
}
}
下一篇记下算法的思路。
关于tink的碰撞检测类【1】相关推荐
- 关于tink的碰撞检测类【2】
分析算法的思路: Step1:假设stage(黑色)上有4个显示对象red_mc,green_mc,blue_mc,yellow_mc,层级关系是stage>root>red_mc,sta ...
- java 像素级碰撞检测,» 像素级碰撞检测类
//像素级碰撞检测 package { import flash.display.BitmapData; import flash.display.BlendMode; import flash.di ...
- HTML5吃豆豆游戏开发实战(三)2d碰撞检测、重构
今天把小球和墙壁的碰撞检测写了一下,主要就是坐标比较大小. 然后整个项目越来越大,代码越来越多,所以把准备把代码重构一下. 把代码拆分,相同功能的在一起,形成一个类,比如Collide.js,碰撞检测 ...
- osg_操作器、碰撞检测、上楼梯篇
一. 操作器 OSG经常用到的自带操作器为TrackBall, osgEarth经常用到的自带操作器为EarthManipulator 自己要写操作器应继承于osgGA::CameraManipul ...
- python打砖块游戏算法设计分析_python实现打砖块游戏
本文实例为大家分享了Python实现打砖块游戏的具体代码,供大家参考,具体内容如下#导入模块 import pygame from pygame.locals import * import sys, ...
- python小游戏代码大全-Python实现打砖块小游戏代码实例
这次用Python实现的是一个接球打砖块的小游戏,需要导入pygame模块,有以下两条经验总结: 1.多父类的继承2.碰撞检测的数学模型 知识点稍后再说,我们先看看游戏的效果和实现: 一.游戏效果 二 ...
- bat小游戏代码大全_Python打砖块小游戏源代码
这次用Python实现的是一个接球打砖块的小游戏,最核心的就是:碰撞检测的数学模型 程序运行截图: 其实,编程问题到最后就是数学问题,这个游戏涉及到2D圆形与矩形的碰撞检测问题: 碰撞检测原理:通过找 ...
- 课程设计小组报告——基于ARM实验箱的捕鱼游戏的设计与实现
课程设计小组报告--基于ARM实验箱的捕鱼游戏的设计与实现 一.任务简介 1.1 任务内容 捕鱼游戏是一个娱乐性的游戏开发,可以给人们带来娱乐的同时还可以给人感官上的享受,所以很受人们的欢迎.本次游戏 ...
- 可扩展java游戏框架实践之java飞机大战
前言 在Java awt frame下利用画笔工具实现一个游戏框架. 该框架支持: 游戏动画实现 动图实现 移动实现 碰撞检测 游戏流程控制 游戏音乐控制 功能设计实现 ①游戏动画实现 JFrame下 ...
最新文章
- 用于EAM的SAP PM移动应用程序
- LeetCode 1346. 检查整数及其两倍数是否存在(哈希)
- itext html 转换 pdf文件,利用itext实现html转pdf文档
- 【微信开发】-- 发送模板消息
- HttpClient 使用证书访问https站点
- mvc路由原理 php_PHP实战002:CodeIgniter安装和入门使用
- 构图之法——9条构图小贴士
- 浏览器加载、渲染过程总结
- 二手房各项税费计算公式
- 机器人bl虐心_【原创】爱你、无悔(双赛,BL,微虐,含H)
- python gps_python解决GPS打卡问题
- 一款基于 Spring Boot 的公众号管理系统,已开源,别再自己写了!
- 微信浏览器apk下载的解决方案
- D - Molar mass
- 提高办公协同效率,Tracup可能是最好的选择
- 实战:战狼2票房数据分析——(1)数据获取及解析
- 【Leetcode刷题Python】55. 跳跃游戏
- 系统管理员的3大黄金法则
- 2020 DASCTFBJD 部分题解
- python王者战斗_Python3 类与对象之王者荣耀对战小游戏