游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6993|回复: 0

使用JavaScript和canvas做精确的像素碰撞检测

[复制链接]

1万

主题

1万

帖子

3万

积分

论坛元老

Rank: 8Rank: 8

积分
36572
发表于 2016-8-25 14:41:17 | 显示全部楼层 |阅读模式
nzbin编译

  我正在开发一个需要再次使用碰撞检测的游戏。我通常会使用简单高效的盒模型碰撞检测。盒子模型的主要原则就是把所有的物体都抽象成正方形,如果两个正方形有重叠,就认为是一次碰撞。这通常是一个简单的游戏所需要的。但是因为这种模型我之前用过多次,我想尝试一些更深刻更准确的方法。

  我选择从像素级层面来看是否发生了碰撞。首先我要了解“像素是什么”。我测试的元素透明度都不为0,换句话说,所有的可见像素都被看做一个碰撞点。为了提高算法效率,我预先创建了一张图片的像素映射图。换句话说,就是一个数组内包含了屏幕上的所有可见像素。

  1.    /* 描述像素图的伪代码 */
  2.     var pixelMap = [];
  3.     for( var y = 0; y < image.width; y++ ) {
  4.         for( var x = 0; x < image.height; x++ ) {
  5.             // 获取当前位置的元素
  6.             var pixel = ctx.getImageData( x, y, 1, 1 );
  7.             // 判断透明度不为0
  8.             if( pixel.data[3] != 0 ) {
  9.                 pixelMap.push( { x:x, y:y } );
  10.             }
  11.         }
  12.     }
  13.     return pixelMap;
复制代码

  用这种方法,一张小图片会变得很大。一张40X40的图片会有1600像素,所以如果我在一个很大的canvas上做碰撞检测将会非常缓慢。测试之前我先将盒子模型重叠起来,如果点击测试返回true,我会进一步测试是否有像素重叠。这意味着我们只需要测试一次。

  1. /* 盒模型测试, 碰撞返回 true */
  2.     function hitBox( source, target ) {
  3.         /* 源物体和目标物体都包含 x, y 以及 width, height */
  4.         return !(
  5.             ( ( source.y + source.height ) < ( target.y ) ) ||
  6.             ( source.y > ( target.y + target.height ) ) ||
  7.             ( ( source.x + source.width ) < target.x ) ||
  8.             ( source.x > ( target.x + target.width ) )
  9.         );
  10.     }
复制代码

  如果hitBox函数返回true,我们需要比较两个物体的预渲染像素图。然后我们需要测试源物体的每一个像素是否与目标物体的像素有重叠。这是一个非常耗时耗能的函数。其实源物体的每个像素与目标物体的每个像素的匹配需要检测n*x次。假如我们匹配两个40*40像素的正方形,最坏的情况就是,经过2560000次的计算而没有得到一次匹配。

1.png

  1. /* 像素碰撞检测的伪代码 */
  2.     function pixelHitTest( source, target ) {
  3.         // 循环源图像的所有像素
  4.         for( var s = 0; s < source.pixelMap.length; s++ ) {
  5.             var sourcePixel = source.pixelMap[s];
  6.             // 添加位置偏移
  7.             var sourceArea = {
  8.                 x: sourcePixel.x + source.x,
  9.                 y: sourcePixel.y + source.y,
  10.                 width: 1,
  11.                 height: 1
  12.             };
  13.      
  14.             // 循环目标图像的所有像素
  15.             for( var t = 0; t < target.pixelMap.length; t++ ) {
  16.                 var targetPixel = target.pixelMap[t];
  17.                 // 添加位置偏移
  18.                 var targetArea = {
  19.                     x: targetPixel.x + target.x,
  20.                     y: targetPixel.y + target.y,
  21.                     width: 1,
  22.                     height: 1
  23.                 };
  24.      
  25.                 /* 使用之前提到的 hitbox 函数 */
  26.                 if( hitBox( sourceArea, targetArea ) ) {
  27.                     return true;
  28.                 }
  29.             }
  30.         }
  31.     }
复制代码

  当我把物体描绘出来,我几乎没有时间测试物体是否发生了碰撞。如果我们想要一个平滑的60帧动画(我相信大多数浏览器倾向于requestAnimationFrame函数),除了浏览器进程和帧渲染的时间,理论上我们测试两帧的时间只有16.6ms(实际的时间更少)。

  为了解决这个问题,我们可以使用更大的分辨率。我们可以测试一组像素而不是单个像素。所以如果我们在像素图渲染器和像素碰撞测试中使用更大的分辨率,我们必须把计算量降到一个合理的数字上。

2.jpg

  1. /* 描绘更大分辨率像素图的伪代码 */
  2.     function generateRenderMap( image, resolution ) {
  3.         var pixelMap = [];
  4.         for( var y = 0; y < image.width; y=y+resolution ) {
  5.             for( var x = 0; x < image.height; x=x+resolution ) {
  6.                 // 获取当前位置的像素群
  7.                 var pixel = ctx.getImageData( x, y, resolution, resolution );
  8.      
  9.                 // 判断像素群的透明度不为0
  10.                 if( pixel.data[3] != 0 ) {
  11.                     pixelMap.push( { x:x, y:y } );
  12.                 }
  13.             }
  14.         }
  15.         return {
  16.             data: pixelMap,
  17.             resolution: resolution
  18.         };
  19.     }
  20.      
  21.     /* 像素碰撞测试伪代码 */
  22.     function pixelHitTest( source, target ) {
  23.      
  24.         // 源对象和目标对象包含两张属性
  25.         // { data: a render-map, resolution: The precision of the render-map}
  26.      
  27.         // 循环源对象的所有像素
  28.         for( var s = 0; s < source.pixelMap.data.length; s++ ) {
  29.             var sourcePixel = source.data.pixelMap[s];
  30.             // 添加位置偏移
  31.             var sourceArea = {
  32.                 x: sourcePixel.x + source.x,
  33.                 y: sourcePixel.y + source.y,
  34.                 width: target.pixelMap.resolution,
  35.                 height: target.pixelMap.resolution
  36.             };
  37.      
  38.             // 循环源对象的所有像素
  39.             for( var t = 0; t < target.pixelMap.data.length; t++ ) {
  40.                 var targetPixel = target.pixelMap.data[t];
  41.                 // 添加位置偏移               
  42.                 var targetArea = {
  43.                     x: targetPixel.x + target.x,
  44.                     y: targetPixel.y + target.y,
  45.                     width: target.pixelMap.resolution,
  46.                     height: target.pixelMap.resolution
  47.                 };
  48.      
  49.                 /*使用之前提到的 hitbox 函数 */
  50.                 if( hitBox( sourceArea, targetArea ) ) {
  51.                     return true;
  52.                 }
  53.             }
  54.         }
  55.     }
复制代码

  同样的40X40的像素块如今只有100组像素点,而之前是有1600像素的图像。我们将2650000次的计算量降低到10000次的计算量,只有原始 计算量的0.39%。如果你有更多不同分辨率的渲染图,你会建立精度更高的系统,从分辨率大的像素群开始依次计算,当然系统的复杂度也会逐渐提高。在两个 40X40像素的圆形物体上使用3的分辨率(13.33X13.33),当前的方案在最差的碰撞测试中会耗时1-2ms。

  相关阅读安全开发之碰撞检测与伤害计算逻辑

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 05:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表