Tiled碰撞编辑器,Phaser中正在使用的对象图层。

huangapple go评论57阅读模式
英文:

Tiled collision editor, object layer in use in Phaser

问题

Tiled碰撞编辑器,Phaser中正在使用的对象图层。

Tiled碰撞编辑器,Phaser中正在使用的对象图层。

我已经在Tiled中以这种方式添加了树碰撞。如何在Phaser中使用这个碰撞与玩家发生碰撞?

英文:

Tiled碰撞编辑器,Phaser中正在使用的对象图层。

Tiled碰撞编辑器,Phaser中正在使用的对象图层。

I have added tree collision this way in Tiled.
How can I use this collision to collide with the player in Phaser?

答案1

得分: 1

这是一个示例代码,用于处理Tiled地图中的碰撞物体。以下是其中的一些部分的翻译:

  1. 获取Tiled地图中指定名称的对象层 链接到文档

    let objectLayer = map.getObjectLayer('Trees');
    
  2. 遍历该层中的所有对象 链接到文档

    for (let obj of objectLayer.objects) {
        // ...
    }
    
  3. 针对每个来自该层的对象:
    根据对象类型(点、矩形、椭圆等)创建碰撞用的物理体(在此示例中使用椭圆):

    let ellipse = this.add.ellipse(obj.x, obj.y, obj.width, obj.height);
    // 你可能需要设置“origin”
    this.physics.add.existing(ellipse, true);
    ellipse.body.setCircle(obj.width / 2);
    
  4. 设置与玩家、AI等的碰撞:

    this.physics.add.collider(player, ellipse);
    

请注意,这段代码中的关键部分用于处理Tiled地图中的对象和碰撞,以及如何为它们创建适当的物理体。希望这有所帮助。

英文:

Well there are many way's to do this, this is one way:

<!-- language-all: lang-js -->

  1. get object layer, with the name from Tiled (link to the documentation):

    // let map = this.make.tilemap( {...} );
    let objectLayer = map.getObjectLayer( &#39;Trees&#39; );
    
  2. iterate over all objects from that layer (link to the documentation):

    for( let obj in objectLayer.objects ){
        // ...
    }
    
  3. For each object from the layer:
    Depending on your object type (point, rectangle, ellipse, ...) you create the physics-body for the collision (for this example I will use a ellipse):

    let ellipse = this.add.ellipse( obj.x, obj.y, obj.width, obj.height );
    // you might need to set the &quot;origin&quot; 
    this.physics.add.existing( ellipse, true );
    ellipse.body.setCircle(obj.width / 2);
    

    > Info/Tipp: if you are using arcade physics the "hitbox" will be a rectangle, doesn't matter which gameObject you use. If you want round physics body with arcade you could use the setCircle method on the body (link to documentation). For complex shapes I would recommend using the matter.js engine.

  4. Setup collision with: player, ai, ...

    this.physics.add.collider( player, ellipse );
    

Updated running demo:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

document.body.style = &#39;margin:0;&#39;;

let json_map = {&quot;compressionlevel&quot;:-1,&quot;height&quot;:5,&quot;infinite&quot;:false,&quot;layers&quot;:[{&quot;compression&quot;:&quot;&quot;,&quot;data&quot;:&quot;AQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAA==&quot;,&quot;encoding&quot;:&quot;base64&quot;,&quot;height&quot;:5,&quot;id&quot;:1,&quot;name&quot;:&quot;TileLayer1&quot;,&quot;opacity&quot;:1,&quot;type&quot;:&quot;tilelayer&quot;,&quot;visible&quot;:true,&quot;width&quot;:8,&quot;x&quot;:0,&quot;y&quot;:0},{&quot;draworder&quot;:&quot;topdown&quot;,&quot;id&quot;:2,&quot;name&quot;:&quot;ObjectLayer1&quot;,&quot;objects&quot;:[{&quot;class&quot;:&quot;&quot;,&quot;ellipse&quot;:true,&quot;height&quot;:10,&quot;id&quot;:1,&quot;name&quot;:&quot;&quot;,&quot;rotation&quot;:0,&quot;visible&quot;:true,&quot;width&quot;:10,&quot;x&quot;:8,&quot;y&quot;:8},{&quot;class&quot;:&quot;&quot;,&quot;height&quot;:5,&quot;id&quot;:2,&quot;name&quot;:&quot;&quot;,&quot;rotation&quot;:0,&quot;visible&quot;:true,&quot;width&quot;:15,&quot;x&quot;:19.2,&quot;y&quot;:25.6},{&quot;class&quot;:&quot;&quot;,&quot;height&quot;:0,&quot;id&quot;:3,&quot;name&quot;:&quot;&quot;,&quot;point&quot;:true,&quot;rotation&quot;:0,&quot;visible&quot;:true,&quot;width&quot;:0,&quot;x&quot;:48,&quot;y&quot;:4.8}],&quot;opacity&quot;:1,&quot;type&quot;:&quot;objectgroup&quot;,&quot;visible&quot;:true,&quot;x&quot;:0,&quot;y&quot;:0}],&quot;nextlayerid&quot;:3,&quot;nextobjectid&quot;:4,&quot;orientation&quot;:&quot;orthogonal&quot;,&quot;renderorder&quot;:&quot;right-down&quot;,&quot;tiledversion&quot;:&quot;1.9.2&quot;,&quot;tileheight&quot;:8,&quot;tilesets&quot;:[{&quot;columns&quot;:1,&quot;firstgid&quot;:1,&quot;image&quot;:&quot;tiles.png&quot;,&quot;imageheight&quot;:8,&quot;imagewidth&quot;:8,&quot;margin&quot;:0,&quot;name&quot;:&quot;tiles&quot;,&quot;spacing&quot;:0,&quot;tilecount&quot;:1,&quot;tileheight&quot;:8,&quot;tilewidth&quot;:8}],&quot;tilewidth&quot;:8,&quot;type&quot;:&quot;map&quot;,&quot;version&quot;:&quot;1.9&quot;,&quot;width&quot;:8};

let config = {
    type: Phaser.AUTO,
    width: 8 * 8,
    height: 5 * 8,
    zoom: 4,
    physics: {
        default: &#39;arcade&#39;,
        arcade: { debug: true }
    },
    scene: { preload, create },
}; 

function preload () {
    this.load.tilemapTiledJSON(&#39;map&#39;, json_map);
}

function create () {
    let graphics = this.make.graphics();
    graphics.fillStyle(0x933AFF);
    graphics.fillRect(0, 0, 10, 10);
    graphics.generateTexture(&#39;tiles&#39;, 10, 10);

    let player = this.add.rectangle(50, 10, 5, 5, 0xffffff);

    this.physics.add.existing(player);

    player.setDepth(100);
    player.body.setVelocityX(-10);

    let map = this.make.tilemap({ key: &#39;map&#39;, tileWidth: 8, tileHeight: 8 });
    let tiles = map.addTilesetImage(&#39;tiles&#39;, &#39;tiles&#39;);
    let layer = map.createLayer(0, tiles, 0, 0);
    let objectLayer = map.getObjectLayer( &#39;ObjectLayer1&#39; );

    for( let obj of objectLayer.objects ){
        
        // since you are not displaying the object the shape doesn&#39;t matter, only the collision body
        let gameObject = this.add.rectangle( obj.x, obj.y, obj.width, obj.height )
                .setOrigin(0);
        
        this.physics.add.existing( gameObject, true );
        
        if(obj.ellipse){
           // For the ellipse version you would need to change the body
            gameObject.body.setCircle( obj.width / 2 );
        } else if(obj.point){
            // For the point we need no set an width and height
            gameObject.body.setSize( 4, 4 );
        }
        
        this.physics.add.collider( player, gameObject );
    }
}

new Phaser.Game(config);

<!-- language: lang-html -->

&lt;script src=&quot;//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

> Btw.: In my Tiled version even, when I create the circle-object the width and height, I had to set does properties manualy. So check them if they are set.
>
> Tiled碰撞编辑器,Phaser中正在使用的对象图层。

答案2

得分: 0

最终,我找到了解决方案,这对我有效。

addCollisionFromTiled(layerName: string, group: number) {
    const graphics = this.scene.add.graphics().lineStyle(2, 0x00ff00, 1)
    const objectLayer = this.map.getObjectLayer(layerName)

    objectLayer.objects.forEach((object: Phaser.Types.Tilemaps.TiledObject) => {
      if (object.rectangle) {
        const rect2 = this.scene.add.rectangle(0, 0, object.width, object.height)
        const polygon = new Phaser.Geom.Polygon(rect2.pathData)
        const body2 = this.scene.matter.add.fromVertices(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          polygon.points.slice(0, -1)
        )
        const collision = this.scene.matter.add.gameObject(
          rect2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokeRect(object.x!, object.y!, object.width!, object.height!)
      } else if (object.ellipse) {
        const elps2 = this.scene add ellipse(0, 0, object.width, object.height)
        const polygon = new Phaser.Geom.Polygon(elps2.pathData)
        const body2 = this.scene.matter add fromVertices(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          polygon.points.slice(0, -1)
        )
        const collision = this.scene.matter add gameObject(
          elps2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokeEllipse(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          object.width!,
          object.height!
        )
      } else if (object.polygon || object.polyline) {
        const objPol = object.polygon ? object.polygon : object.polyline
        const polygon = new Phaser.Geom.Polygon(objPol)
        const points: { x: number; y: number }[] = []
        for (let point of polygon.points) {
          points.push({
            x: object.x! + point.x,
            y: object.y! + point.y,
          })
        }
        const sliceCentre = this.scene.matter.vertices.centre(points)
        const body2 = this.scene.matter.add.fromVertices(sliceCentre.x, sliceCentre.y, points)
        const poly2 = this.scene.add.polygon(sliceCentre.x, sliceCentre.y, points)
        const collision = this.scene.matter.add.gameObject(
          poly2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokePoints(points)
      }
    })
  }
英文:

Finally, I got a solution, and this is working for me.


 
  
  addCollisionFromTiled(layerName: string, group: number) {
    const graphics = this.scene.add.graphics().lineStyle(2, 0x00ff00, 1)
    const objectLayer = this.map.getObjectLayer(layerName)

    objectLayer.objects.forEach((object: Phaser.Types.Tilemaps.TiledObject) =&gt; {
      if (object.rectangle) {
        const rect2 = this.scene.add.rectangle(0, 0, object.width, object.height)
        const polygon = new Phaser.Geom.Polygon(rect2.pathData)
        const body2 = this.scene.matter.add.fromVertices(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          polygon.points.slice(0, -1)
        )
        const collision = this.scene.matter.add.gameObject(
          rect2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokeRect(object.x!, object.y!, object.width!, object.height!)
      } else if (object.ellipse) {
        const elps2 = this.scene.add.ellipse(0, 0, object.width, object.height)
        const polygon = new Phaser.Geom.Polygon(elps2.pathData)
        const body2 = this.scene.matter.add.fromVertices(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          polygon.points.slice(0, -1)
        )
        const collision = this.scene.matter.add.gameObject(
          elps2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokeEllipse(
          object.x! + object.width! / 2,
          object.y! + object.height! / 2,
          object.width!,
          object.height!
        )
      } else if (object.polygon || object.polyline) {
        const objPol = object.polygon ? object.polygon : object.polyline
        const polygon = new Phaser.Geom.Polygon(objPol)
        const points: { x: number; y: number }[] = []
        for (let point of polygon.points) {
          points.push({
            x: object.x! + point.x,
            y: object.y! + point.y,
          })
        }
        const sliceCentre = this.scene.matter.vertices.centre(points)
        const body2 = this.scene.matter.add.fromVertices(sliceCentre.x, sliceCentre.y, points)
        const poly2 = this.scene.add.polygon(sliceCentre.x, sliceCentre.y, points)
        const collision = this.scene.matter.add.gameObject(
          poly2,
          body2
        ) as Phaser.Physics.Matter.Sprite
        collision.setStatic(true)
        collision.setCollisionGroup(group)

        graphics.strokePoints(points)
      }
    })
  }

huangapple
  • 本文由 发表于 2023年2月24日 07:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定