简体   繁体   English

Jcanvas层碰撞

[英]Jcanvas layer collision

I'm trying to make a function to detect if two layers collide. 我试图做一个功能来检测是否两层碰撞。 Using Jquery, jcanvas and HTML5 canvas 使用Jquery,jcanvas和HTML5 canvas

I'm building a sort of flight simulator where the player can ascend/descend to avoid hot airballons coming towards the player. 我正在构建一种飞行模拟器,玩家可以在其中进行上升/下降操作,以免热气球飞向玩家。

I need to detect if the plane is about to collide with the ballon layer. 我需要检测飞机是否将与气球层碰撞。

I've tried checking the layers x/y position but that doesn't work so well since the hot airballon isn't squere shaped. 我试过检查x / y层的位置,但是由于热气球形状不明显,所以效果不佳。

Imagine this: 想象一下:

      ______________               __
    /                \            / /       _
   /                  \      ___/__/_______/ /\
  |   Air ballon       |    /    Plane 1      /
  |                    |    \___\  \_______\ \
   \                  /          \  \       \/
    \                /            \__\
     \              /
      \            /                __
       \          /                / /       _
        \        /            ___/__/_______/ /\
         \      /            /    Plane 2      /
          \____/             \___\  \_______\ \
           |__|                   \  \       \/
           |__|                    \__\

In my current solution both planes will collide at the same time. 在我当前的解决方案中,两个平面将同时碰撞。 I need plane 2 to collide later, when the plan actually collides with the air ballon basket 当计划实际上与空气气球篮碰撞时,我需要飞机2稍后碰撞

The problem you are having is usually referred to as collision detection . 您遇到的问题通常称为冲突检测

The most common approach to collision detection is to abstract each object as a geometrical shape and then check if these shapes intersect. 碰撞检测的最常见方法是将每个对象抽象为几何形状,然后检查这些形状是否相交。 Common choices are either rectangles or circles, because checking these for intersection with each other is rather trivial. 常见的选择是矩形或圆形,因为检查它们是否相交非常简单。

Another approach is to use pixel-based collision detection. 另一种方法是使用基于像素的碰撞检测。 You draw the two objects you want to check on separate layers, and then you check the color-value of each pixels on both layers. 您在单独的图层上绘制要检查的两个对象,然后在两个图层上检查每个像素的颜色值。 When the alpha-values of both are > 0, you have a collision. 当两个字母的Alpha值都大于0时,就会发生冲突。 The advantage is that it is very accurate, but the disadvantage is that it is quite CPU intense because you need to check so many pixels. 优点是非常准确,但缺点是CPU占用率很高,因为您需要检查很多像素。 To optimize the CPU load, you can combine it with the geometric method to reduce the amounts of pixels you need to check. 为了优化CPU负载,可以将其与几何方法结合使用以减少需要检查的像素数量。

  1. define a rectangle around each object 在每个对象周围定义一个矩形
  2. check if the rectangles intersect 检查矩形是否相交
  3. when they do calculate the intersecting area 当他们计算相交面积时
  4. draw both objects to separate, empty, canvases 将两个对象绘制为单独的空白画布
  5. get the pixel-data of the areas on both canvases with context.getImageData() 使用context.getImageData()两个画布上区域的像素数据。
  6. Compare the alpha-value of the returned arrays. 比较返回数组的alpha值。 When the same pixel on both canvases is greater than 0, you have a collision. 当两个画布上的相同像素都大于0时,就会发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM