# Tilebase movement with tilemap collisions

**URL:** <https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290>\
**Category:** Phaser 3\
**Created:** [February 22, 2020, 7:49am UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290 "2020-02-22T07:49:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_hoskope](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/_hoskope/32/2867_2.png) [@\_hoskope](https://phaser.discourse.group/u/_hoskope)\
**Post date:** [February 22, 2020, 7:49am UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/1 "2020-02-22T07:49:06Z")

</div>

I was browsing old forum and there was some discussion about the topic. However those threads were quite old, so i was thinking to ask again about this topic.

I’d like to create oldschool Ultima style game, but as a freshman i stumbled upon collisions not working when moving character by position. There is one example, which kind of fits to this: [https://phaser.io/examples/v3/view/game-objects/tilemap/static/grid-movement](https://phaser.io/examples/v3/view/game-objects/tilemap/static/grid-movement)

On the example we’re checking against `tile.index`.

```javascript
// Tilebased movement: UP
    this.input.keyboard.on('keydown_W', (event) => {
      let tile = this.elements.getTileAtWorldXY(this.player.x, this.player.y - this.tileSize, true)

      if (tile.index === 3) {
        console.log('Blocked!')
      } else {
        this.player.y -= this.tileSize
      }

    })

```

I don’t know if i have done something wrong with the tilemap layer, but i have multiple indexes there, so of course i could check against them all, but is there a way i could check against whole tilemap layer?

---

<div class="post-metadata">

**Author:** ![\_hoskope](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/_hoskope/32/2867_2.png) [@\_hoskope](https://phaser.discourse.group/u/_hoskope)\
**Post date:** [February 22, 2020, 8:31am UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/2 "2020-02-22T08:31:32Z")

</div>

Okay, seems that `-1` tile index did the trick. Still would love to have discussion around this topic, since my implementation is not probably elegant 🤣

```javascript
// Tilebased movement: LEFT
    this.input.keyboard.on('keydown_A', (event) => {
      let tile = this.blockedLayer.getTileAtWorldXY(this.player.x - this.tileSize, this.player.y, true)

      if (tile.index !== -1) {
        console.log('Blocked!')
      } else {
        this.player.x -= this.tileSize
      }
    })

```

---

<div class="post-metadata">

**Author:** ![Thundros](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/thundros/32/2057_2.png) [@Thundros](https://phaser.discourse.group/u/Thundros)\
**Post date:** [February 22, 2020, 8:36am UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/3 "2020-02-22T08:36:10Z")

</div>

I’m very interested in this topic as well! 🙂

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [February 22, 2020, 10:25am UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/4 "2020-02-22T10:25:36Z")

</div>

> [@\_hoskope](#):
>
> Okay, seems that `-1` tile index did the trick.

Under the default settings, [-1 is indeed](https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tile.html#index) the index used for an empty tile. Your current solution is perfectly fine. Using a separate layer to determine which tiles should be solid can be powerful because solidity doesn’t have to be linked to what the player sees.

> [@\_hoskope](#):
>
> i stumbled upon collisions not working when moving character by position

Assuming you’re talking about Arcade physics, a general rule of thumb is that you should only move bodies by manipulating their velocity. If you directly move a body into a solid tile, there’s no way the physics system can tell which direction it came from, so it won’t behave as expected.

---

<div class="post-metadata">

**Author:** ![\_hoskope](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/_hoskope/32/2867_2.png) [@\_hoskope](https://phaser.discourse.group/u/_hoskope)\
**Post date:** [February 22, 2020, 6:35pm UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/5 "2020-02-22T18:35:01Z")

</div>

I’m not sure if i need physics for this kind of game, but if you were to use physics and moving with velocity how would you go to moving player one tile / key stroke?

I think for camera following character i need physics anyway, so probably need to write again controlling part…

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [February 22, 2020, 8:05pm UTC](https://phaser.discourse.group/t/tilebase-movement-with-tilemap-collisions/5290/6 "2020-02-22T20:05:37Z")

</div>

> [@\_hoskope](#):
>
> if you were to use physics and moving with velocity how would you go to moving player one tile / key stroke

You just wouldn’t rely on the physics engine to handle collisions with the tilemap, only collisions with other objects. However, I personally wouldn’t mix grid-based movement with physics because collisions would end up misaligning the characters.

> [@\_hoskope](#):
>
> I think for camera following character i need physics anyway

A camera can follow any object, no matter if it’s controlled by physics or not. [See the examples.](http://labs.phaser.io/edit.html?src=src/camera/follow%20sprite.js)
