Jump to content

How to work with line of sight on tilemapLayer


marvster
 Share

Recommended Posts

Like the line of sight demo on game mechanicexplorer (http://gamemechanicexplorer.com/#raycasting-1), I'd like use ray casting for enemy detection and movement (chasing), but on a big tilemap, with a specific layer holding the collision information for certain tiles.

 

My questions are:

- How can I get this ray cast demo work with tilemapLayer? I looked up the docs, but could figure it out.

- How can I assure perfomance, as the whole map would consist easily 100-200 objects in general which need to be ray casted, even just a few are in a relevant range where line of sight detection makes sense?

 

Link to comment
Share on other sites

Thanks alot kass,

 

that was exactly what I wanted. Unfortunately my fear come true, as it seems, that all objects will be checked, even those not shown on the screen:

detect_enemies_not_visible.png

 

I've tried to use the visible property of the sprite, but this seems not to be functional.

this.enemyGroup.forEachExists(function (enemy) {            this.game.physics.arcade.collide(this.player, enemy);            var ray = new Phaser.Line(enemy.x, enemy.y, this.player.x, this.player.y);            var tileHits = this.map.layer3.getRayCastTiles(ray, 4, true, false);            if (tileHits.length > 0 && enemy.visible) {                enemy.tint = 0xffffff;                enemy.seePlayer = false;            } else {                enemy.tint = 0xffaaaa;                enemy.seePlayer = true;            }        }, this);

Any Idea?

Link to comment
Share on other sites

Found a solution, even it seems not to work on every Browser:

this.enemyGroup.forEach(function (enemy) {            if (enemy.inCamera) {                this.game.physics.arcade.collide(this.player, enemy);                var ray = new Phaser.Line(enemy.x, enemy.y, this.player.x, this.player.y);                var tileHits = this.map.layer3.getRayCastTiles(ray, 4, true, false);                if (tileHits.length > 0 && enemy.inCamera) {                    enemy.tint = 0xffffff;                    enemy.seePlayer = false;                } else {                    enemy.tint = 0xffaaaa;                    enemy.seePlayer = true;                }            }        }, this);

With a tilemap 1440x1440 and 100 objects, the frame rate was on Chrome between 50-60fps, in Firefox under 20fps (which broke collision detection) and in IE constant 60fps. It seems that Firefox will handle some stuff in a different way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...