使用p2旋转约束连接2个相位精灵,无需施力

时间:2015-11-16 13:46:02

标签: javascript html5-canvas physics phaser-framework p2

所以我有精灵,我想用p2.js连接#39;轮回约束。一旦创建约束,我当前的实现就会对精灵施加力 我该如何避免这种行为?

如果无法避免,还有另一种方法可以水平连接2个精灵吗?

编辑:

var Game = {

preload: function() {
    game.load.image('tree00', './imgs/tree/tree-00.png');
    game.load.image('tree01', './imgs/tree/tree-01.png');
    game.load.image('tree02', './imgs/tree/tree-02.png');
    game.load.image('tree03', './imgs/tree/tree-03.png');
    game.load.image('tree04', './imgs/tree/tree-04.png');
    game.load.image('tree05', './imgs/tree/tree-05.png');

    game.load.spritesheet('present', './imgs/dude.png', 32, 48);
},

create: function() {
    game.physics.startSystem(Phaser.Physics.P2JS);
    game.physics.p2.gravity.y = 300;
    game.physics.startSystem(Phaser.Physics.ARCADE);

    game.stage.backgroundColor = '#aaffee';

    treeCollsionGroup = game.physics.p2.createCollisionGroup();
    presentCollisionGroup = game.physics.p2.createCollisionGroup();

    this.createPresent(game.world.width * 0.21, game.world.height * 0.6);
    this.createTree(6, game.world.width * 0.2, game.world.height * 0.8);

    //presentCollisionGroup.collides(treeCollsionGroup);

    connection[0] = game.physics.p2.createRevoluteConstraint(treeParts[treeParts.length - 1], [(treeParts[treeParts.length - 1].width)/2, treeParts[treeParts.length - 1].height], present, [-present.width/2, treeParts[treeParts.length - 1].height], maxForce);
    connection[1] = game.physics.p2.createRevoluteConstraint(treeParts[treeParts.length - 1], [(treeParts[treeParts.length - 1].width)/2, 0], present, [-present.width/2, 0], maxForce);
}, 

createTree: function(length, xAnchor, yAnchor) {
    var lastSprite;

    for (var i = 0; i < length; i++) {          
        newSprite = game.add.sprite(xAnchor, yAnchor - i*100, 'tree0' + i);
        newSprite.scale.x = game.world.width/1920;
        newSprite.scale.y = game.world.width/1920;

        game.physics.p2.enable(newSprite, true);
        if (i != length-1) {
            newSprite.body.setRectangle(game.world.width * 0.10, newSprite.height * 0.15);
        } else {
            newSprite.body.setRectangle(newSprite.width * 0.8, newSprite.height * 0.8);
        }
        newSprite.body.setCollisionGroup(treeCollsionGroup);

        if(i === 0) {
            newSprite.body.static = true;
        }

        if (lastSprite) {
            switch(i) {
                case 1: constraint = game.physics.p2.createRevoluteConstraint(newSprite, [0, 0], lastSprite, [0, -lastSprite.height * 0.62], maxForce);
                        treeConstraints.push(constraint);
                        break;
                case 2: constraint = game.physics.p2.createRevoluteConstraint(newSprite, [0, 0], lastSprite, [0, -lastSprite.height * 0.285], maxForce);
                        treeConstraints.push(constraint);
                        break;
                case 3: constraint = game.physics.p2.createRevoluteConstraint(newSprite, [0, 0], lastSprite, [0, -lastSprite.height * 0.425], maxForce);
                        treeConstraints.push(constraint);
                        break;
                case 4: constraint = game.physics.p2.createRevoluteConstraint(newSprite, [0, 0], lastSprite, [0, -lastSprite.height * 0.4], maxForce);
                        treeConstraints.push(constraint);
                        break;
                case 5: constraint = game.physics.p2.createRevoluteConstraint(newSprite, [0, 0], lastSprite, [0, -lastSprite.height * 0.55], maxForce);
                        treeConstraints.push(constraint);
                        break;
            }
        }
        lastSprite = newSprite;
        treeParts.push(newSprite);
        newSprite.body.collides(treeCollsionGroup);
    }
},

createPresent: function(xAnchor, yAnchor) {
    present = game.add.sprite(game.world.width * 0.21, game.world.height * 0.6, 'present');
    game.physics.p2.enable(present, true);
    present.scale.x = game.world.width/1920;
    present.scale.y = game.world.width/1920;
    present.body.setRectangle(present.width, present.height);
    present.body.data.gravityScale = 0;
    present.body.setCollisionGroup(presentCollisionGroup);
}
}



我删除了不太重要的代码,因此它不会太多(它已经是)。
基本上我正在做的是:我创建一个树并使用旋转约束连接各部分,因此它们在现实世界中表现得像一棵树(例如在风中)。
比我创建的礼物基本上是一个应该水平连接到树顶的精灵。因此我使用2个旋转约束一个用于精灵之间的最高点和一个用于最底部。 (我知道这是一种肮脏的代码)

在我创建这些约束并且当前连接到树的顶部之后,树开始摇动并折叠(就像它应该的那样)。但我不想要这种行为。
也许我正在寻找锁定约束,我必须对此进行研究。

编辑2:
在看了锁定约束后,我意识到这就是我正在寻找的。但即使锁定约束也会使树崩溃。

1 个答案:

答案 0 :(得分:0)

我无法理解您的问题,但您是否尝试过Prismatic甚至Lock约束?可以给我们一些代码甚至代码示例,以便我们能够更好地帮助您吗?