Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random tile of tilemap not rendering with canvas/WebGL when rotated #1200

Closed
r3ndd opened this issue Dec 3, 2018 · 4 comments
Closed

Random tile of tilemap not rendering with canvas/WebGL when rotated #1200

r3ndd opened this issue Dec 3, 2018 · 4 comments

Comments

@r3ndd
Copy link

r3ndd commented Dec 3, 2018

I have the following code for my scene:

var tiles;

var addTile = function (type, x, y, rot, tile_size) {
    var tile = Crafty.e("Tile").attr({x: x * tile_size, y: y * tile_size, rotation: rot || 0});
    tile.type(type);
    tile.origin("center");
    tiles.push(tile);
    if (type == "dirt_water")
        console.log(tile);
};

var addTileRows = function (type, startY, endY, width, rot, tile_size) {
    for (var y = startY; y < endY; y++) {
        for (var x = 0; x < width; x++) {
            addTile(type, x, y, rot, tile_size);
        }
    }
}

Crafty.scene("Main", function (data) {
    var width = 30;
    tiles = [];

    addTileRows("water", 0, 2, width, 0, data.tile_size);
    addTileRows("grass_water", 2, 3, width, -90, data.tile_size);
    addTileRows("grass", 3, 5, width, 0, data.tile_size);
    addTileRows("dirt_grass", 5, 6, width, -90, data.tile_size);
    addTileRows("dirt", 6, 10, width, 0, data.tile_size);
    addTileRows("dirt_water", 10, 11, width, 90, data.tile_size);
    addTileRows("water", 11, 12, width, 0, data.tile_size);
    addTileRows("sand_water", 12, 13, width, -90, data.tile_size);
    addTileRows("sand", 13, 16, width, 0, data.tile_size);
});

The tile component:

Crafty.c("Tile", {
    required: "2D, WebGL",

    init: function () {
        this.size = window.tile_size;
    },

    type: function (tile_name) {
        this.addComponent("spr_" + tile_name);
    }
});

However, I am getting an issue where one of the rotated tiles is not rendered: https://i.gyazo.com/9a526fd6febb991011f32043ca73fdce.png

Only happens on canvas and WebGL, DOM works fine. Adding 1 pixel to the x position of the tile makes the issue disappear, but that obviously isn't a viable fix for a game.

@r3ndd
Copy link
Author

r3ndd commented Dec 3, 2018

I should add that this is version 0.9.0, but after checking it appears 0.8.0 has the same issue.

@starwed
Copy link
Member

starwed commented Dec 4, 2018

Hmm, definitely seems like a bug. If it occurs in WebGL and Canvas layers, but not DOM, and only for rotated entities, I bet it's an error determining whether the entity falls within the viewport.

I could imagine it being some sort of rounding error due to the rotation by -90, but in that case I'm not sure why it would only happen in a single row, instead of all of them.

Do you have a fiddle/etc that reproduces the behavior? What environment (browser+OS) are you seeing it on?

@starwed
Copy link
Member

starwed commented Dec 8, 2018

It's possible that this will be fixed by the changes in #1202 -- changing the origin of an already-rotated entity was not updating some calculated properties correctly.

In particular, the position of the entity in the broad phase map was not updating correctly, and that's used for selecting which entities to render. (For Canvas and WebGL, but not DOM layers.)

@starwed
Copy link
Member

starwed commented Dec 9, 2018

I've reproduced the issue in the release branch, and confirmed it is now fixed in the develop branch. So it does seem like setting the origin after the rotation was the source of the problem -- not sure why that specific row was affected, but likely related to some quirk of how the rotated coordinates are rounded.

You can grab the most recent develop build of Crafty here. A workaround when using 0.9 would be to specify the origin before setting the rotation property.

Thanks for the report!

@starwed starwed closed this as completed Dec 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants