gpt4 book ai didi

javascript - 与 Chipmunk JS 的碰撞失败

转载 作者:搜寻专家 更新时间:2023-10-31 08:56:28 24 4
gpt4 key购买 nike

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 类来帮助我组织事情:

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;
})();

最佳答案

从观察行为来看,我认为这就像 Sprite 和花栗鼠的 body 不围绕同一点旋转一样简单。我相信花栗鼠的旋转是围绕质心的。看起来 Sprite 围绕左上角旋转。事实上,它们也可能是从那个 Angular 绘制的,这解释了为什么它们堆叠起来很有趣,并且与底部平面相交。

我认为您在 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

关于javascript - 与 Chipmunk JS 的碰撞失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com