【发布时间】:2013-10-25 14:58:13
【问题描述】:
http://videobin.org/+70a/8wi.html
您可以在这里查看正在发生的事情,并在此处试用演示:http://student.dei.uc.pt/~drgomes/carry/index.html。
所以,我正在使用 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 开发人员工具的画布检查器,看看发生了什么:html5rocks.com/en/tutorials/canvas/inspection 因为我担心我无法复制这个问题
-
你能把你的演示放到jsfiddle.net吗?
-
你卡在哪里了?伙计,我在这里没有发现任何问题
标签: javascript html game-physics chipmunk physics-engine