与Chipmunk JS发生碰撞失败

4

这里可以看到正在发生的事情,这里有一个试用的演示。

因此,我使用Chipmunk JS演示来了解它的工作原理(请参见https://github.com/josephg/Chipmunk-js)。简单的演示一开始很好,但后来东西开始疯狂跳动,我一直在努力解决这个问题,但迄今为止没有成功。

var radToDeg = 180 / Math.PI;

function PlayState() {
  this.blocks = [];

  this.setup = function() {
    space.iterations = 100;
    space.gravity = new cp.Vect(0, 150);
    space.game = this;

    this.ground = space.addShape(new cp.SegmentShape(space.staticBody, new cp.v(0, 480), new cp.v(640, 480), 0));
    this.ground.setElasticity(0);
    this.ground.setFriction(1);
  };

  this.update = function() {
    space.step(this.dt);

    for (var i = 0; i < this.blocks.length; i++) {
      var block = this.blocks[i];
      block.sprite.x = block.body.p.x;
      block.sprite.y = block.body.p.y;
      block.sprite.angle = block.body.a * radToDeg;
    }

    if (isMouseDown("left")) {
      if (this.canAddBlock) {
        this.canAddBlock = false;
        this.addBlock(mouseX, mouseY);
      }
    } else {
      this.canAddBlock = true;
    }
  };

  this.draw = function() {
    clearCanvas();

    for (var i = 0; i < this.blocks.length; i++) {
      this.blocks[i].sprite.draw();
    }

    // this.ground.sprite.draw();
  };

  this.addBlock = function(x, y) {
    width = 64;
    height = 64;

    var newBlock = new Block(x, y, width, height);

    newBlock.body = space.addBody(new cp.Body(1, cp.momentForBox(1, width, height)));
    newBlock.body.setPos(new cp.v(x, y));
    newBlock.shape = space.addShape(new cp.BoxShape(newBlock.body, width, height));
    newBlock.shape.setElasticity(0);
    newBlock.shape.setFriction(1);
    this.blocks.push(newBlock);
  };
}

desiredFPS = 60;
switchState(new PlayState());

源代码很简单明了,但我对创建地面的方式有些怀疑,因为我无法确定它实际上处于什么位置。不过方块似乎能够找到它并与其碰撞。

另一个源文件是一个小的块类,帮助我组织事物:

Block = (function() {
  function constructor(x, y, width, height) {
    this.sprite = new Sprite("res/block.png", x, y);

    this.width = width;
    this.height = height;

  }

  constructor.prototype = {
    update: function() {

    }
  };

  return constructor;
})();

我会启用Chrome Web开发者工具的画布检查器,查看发生了什么:http://www.html5rocks.com/en/tutorials/canvas/inspection/,因为我担心我无法复制该问题。 - silicakes
你能把你的演示放到 http://jsfiddle.net 吗? - net.uk.sweet
你卡在哪里了?我这里没有发现任何问题。 - Just code
2个回答

1
从观察行为来看,我认为问题很简单,即精灵和花栗鼠身体不围绕同一点旋转。我相信花栗鼠的旋转是围绕质心进行的。看起来精灵正在围绕左上角旋转。事实上,它们可能也是从那个角落绘制的,这就解释了它们为什么会堆叠在一起,并与底部平面相交。
我认为您需要在“update”函数中添加类似于以下的内容(伪代码):
offset = Vector(-width/2,-height/2) 
offset.rotate_by(block.body.a)

block.sprite.x = block.body.p.x + offset.x
block.sprite.y = block.body.p.y + offset.y

其实,那是一个很好的理论,我得尝试一下,我相信可能就是这样。 - David Gomes

0
我对花栗鼠一无所知,但在玩弄你的演示时,物理似乎完全不正确(对我来说从一开始就是这样)。从你的代码中看,我有一种直觉,认为你应该在Block类中设置Sprite实例的尺寸,而不是在Block实例本身上设置。
Block = (function() {
  function constructor(x, y, width, height) {
    this.sprite = new Sprite("res/block.png", x, y);

    // Do you mean to set the width and height of the sprite?
    this.sprite.width = width;
    this.sprite.height = height;

  }

  constructor.prototype = {
    update: function() {

    }
  };

  return constructor;
})(); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接