javascript 我对相位炮3的碰撞有意见

dtcbnfnu  于 4个月前  发布在  Java
关注(0)|答案(1)|浏览(46)

我创建了两个对象(一个实心圆和一个笔画圆),但是它们并没有像预期的那样碰撞。
下面是我的代码:

const config = {
    type: Phaser.AUTO,
    width: 400,
    height: 400,
    backgroundColor: "#f5bd22",
    scene: {
        preload: preload,
        create: create,
        update: update
    },
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 90 },
            debug: false
        }
    }
}

const game = new Phaser.Game(config);

let circle;
let hollowCircle;

function preload() {

}

function create() {
    circle = this.add.circle(200, 200, 50, '#f5bd22');

    hollowCircle = this.add.graphics();
    hollowCircle.lineStyle(2, 0x000000);
    hollowCircle.strokeCircle(200, 200, 100);

    this.physics.world.enable([circle, hollowCircle]);

    hollowCircle.body.gravity.y = -90;
    this.physics.add.collider(circle, hollowCircle);
}

function update() {

}

字符串
它创建了一个内部有实心圆的笔划圆。重力设置为90到实心圆,0到笔划。当代码运行时,实心圆福尔斯落下,笔划保持静止。但是两个圆接触,什么也没有发生。

czfnxgou

czfnxgou1#

好吧,这是你想做的,不能这样做。街机物理只是在物理体的外部碰撞。另外,如果你看看调试边界框,你可以看到“hollowCircle”的物理框在左上角 注意:physicsgraphics游戏对象,不要像相位器中的其他游戏对象那样React)
x1c 0d1x的数据
也就是说,如果你想要这种效果,一个简单的解决方法就是创建几个小的物理对象,覆盖轮廓的“空心”- Shape,然后再设置与这些对象的碰撞。

这里有一个小演示:

  • (这里我创建了一个有32个小矩形的圆,所以保持形状,我把内圈的物理框做成圆形,这样看起来更好)*
document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    backgroundColor: "#f5bd22",
    physics: {
        default: 'arcade',
        arcade: {            
            gravity:  { y: 90 },
            // turn on to show the created Objects
            // debug: true
        }
    },
    scene: {
        create
    },
}; 

function createCircle(scene, x, y, radius){

  // CREATE VISUALS
  let displayCircle = scene.add.circle(x, y, radius).setStrokeStyle(2,0x000000);
  
  // CREATE PHYSICS PART
  const circle = new Phaser.Geom.Circle(x, y, radius);
  const points = circle.getPoints(32);
  let boxes = [];

  for (let i = 0; i < points.length; i++){
      const p = points[i];

      let box = scene.add.rectangle(p.x - 2, p.y - 2, 4, 4);
      // with the second parameter I'm making the object(s) static, so no need for the -90 gravity
      let boxPhysics = scene.physics.add.existing(box, true);
      boxes.push(box);
  }
    
  return boxes;
}

function create() {
    let circle = this.add.circle(200, 80, 30, '#f5bd22');
    let circlePhysics = this.physics.add.existing(circle);
    circlePhysics.body.setCircle(30);

    let helperBoxes = createCircle(this, 200, 100, 70)
    
    this.physics.add.collider(circle, helperBoxes);
}

const game = new Phaser.Game(config);

个字符
要查看发生了什么,请再次打开config中的调试标志。

  • btw:*作为替代,你可以使用matterjs here are some examples。使用matter引擎,可以实现更复杂的物理对象组合。

相关问题