【问题标题】:Revolute constraint in Phaser 3Phaser 3 中的旋转约束
【发布时间】:2019-06-26 16:47:51
【问题描述】:

我是 Phaser 的新手,正在尝试学习 Phaser 3。 我需要一个项目的彻底约束。

有使用 box2d 和 p2physics 的 Phaser 2 的示例,但它们在 Phaser 3 中不起作用。

我尝试在文档中查看它,但我认为它还不完整。

谁能告诉我如何在 Phaser 3 中创建旋转约束(或销接头)。

【问题讨论】:

    标签: javascript phaser-framework game-development revolute-joints


    【解决方案1】:

    查看link 的示例并点击运行代码按钮:

    var config = {
        type: Phaser.AUTO,
        width: 800,
        height: 600,
        backgroundColor: '#1b1464',
        parent: 'phaser-example',
        physics: {
            default: 'matter'
        },
        scene: {
            preload: preload,
            create: create
        }
    };
    
    var game = new Phaser.Game(config);
    
    function preload ()
    {
        this.load.image('ball', 'assets/sprites/shinyball.png');
    }
    
    function create ()
    {
        this.matter.world.setBounds();
    
        //  Our two bodies which will be connected by a constraint (aka a Joint or a Spring)
    
        var ballA = this.matter.add.image(420, 100, 'ball', null, { shape: 'circle', friction: 0.005, restitution: 0.6 });
        var ballB = this.matter.add.image(400, 200, 'ball', null, { shape: 'circle', friction: 0.005, restitution: 0.6 });
    
        //  You can create a constraint between the two bodies using a Factory function.
        //  The value 100 is the resting length and 0.2 is the stiffness of the constraint.
    
        this.matter.add.constraint(ballA, ballB, 100, 0.2);
    
        //  To help those of you more used to the Box2D syntax you can use
        //  add.joint or add.spring instead (with the exact same parameters)
    
        // this.matter.add.spring(ballA, ballB, 100, 0.2);
        // this.matter.add.joint(ballA, ballB, 100, 0.2);
    
        //  Or you can create a native Matter constraint:
    
        // var constraint = Phaser.Physics.Matter.Matter.Constraint.create({
        //     bodyA: ballA.body,
        //     bodyB: ballB.body,
        //     length: 100,
        //     stiffness: 0.2
        // });
    
        //  Which you then have to add to the world yourself:
    
        // this.matter.world.add(constraint);
    
        this.matter.add.mouseSpring();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多