简体   繁体   English

如何检测多边形上的碰撞

[英]How to detect collision on a polygon

I'm trying to detect collisions between my player (just a square) and a polygon with CraftyJS : 我正在尝试使用CraftyJS检测我的播放器(仅一个正方形)和一个多边形之间的碰撞

// Init Crafty:
Crafty.init();
Crafty.canvas.init();

var AttackPoly = new Crafty.polygon([
    [-8, 6],
    [0, -8],
    [8, -14],
    [16, -8],
    [24, 6]
]);

attackEnt = Crafty.e("AttackRange, 2D, Canvas, Collision, SolidHitBox, Color").attr({x:50, y:50}).collision(AttackPoly);

Crafty.e("2D, Canvas, Color, Fourway, Collision, Tester").attr({
    x: 100,
    y: 60,
    w: 4,
    h: 4
}).color("orange");

Crafty.e("2D, Canvas, Color, Fourway, Collision, Tester").attr({
    x: 100,
    y: 50,
    w: 4,
    h: 4
}).color("orange");

Crafty.e("2D, Canvas, Color, Fourway, Collision, Tester").attr({
    x: 100,
    y: 40,
    w: 4,
    h: 4
}).color("orange");

Crafty("Tester").bind("EnterFrame", function () {
    this.x -= .2
    if (this.hit("AttackRange")) this.color("aqua")

});

The squares that hit the polygon doesn't change their color. 命中多边形的正方形不会改变其颜色。 What am I missing? 我想念什么?

Fiddle: http://jsfiddle.net/c3597/32/ 小提琴: http : //jsfiddle.net/c3597/32/

I figured it out: the Polygon has to be inside a bounding rect: 我知道了:多边形必须在边界矩形内:

Crafty
  .e('AttackRange, 2D, Canvas, Collision, SolidHitBox, Color')
  .attr({x:50, y:50, w: 100, h: 100})
  .collision(new Crafty.polygon([
    [0, 0],
    [100, 50],
    [0, 100]
  ]))
  .color('blue'); 

Fiddle: http://jsfiddle.net/c3597/33/ 小提琴: http : //jsfiddle.net/c3597/33/

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

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