Dusk Engine

I have a question related to accessing tiles by screen coordinates. I have created a simple map 8 x 8 tiles, with two layers. When you click on a tile in the toplayer, it removes the tile and reveals what’s underneath in the bottom layer. If I load the map without changing the camera position or viewpoint, the touch function works great. I really want the map in the center of the screen though. As soon as I alter the camera position and viewpoint, the touch listener no longer works. Tapping on a tile removes a different tile. I know what’s going on, and I know I can fix it mathematically, but wondered if there was a simpler “dusk” way to handle this. Essentially, when the map is loaded at (0,0), using map.layer.tilesByPixels(x,y) works great because the x/y coordinates of the map match up with the x/y coordinates of the screen. However, as soon as I move the map, they no longer match up, so when I touch the screen at 524,524, it removes the bottom right tile of the map, which is the map pixel coordinate 524,524. Here’s my code:

local dusk = require("Dusk.Dusk") display.setDefault("minTextureFilter", "nearest") display.setDefault("magTextureFilter", "nearest") local centerX = display.contentCenterX local centerY = display.contentCenterY local map = dusk.buildMap("map001.json") local toplayer = map.layer["toplayer"] map.setCameraBounds({xMin = centerX, yMin = centerY, xMax = map.data.width - centerX, yMax = map.data.height - centerY}) map.positionCamera(map.data.width/2, map.data.height/2) map.setViewpoint(map.data.width/2 , map.data.height/2) local function touchTile(event) local t = event print(t.x, t.y) local tile = toplayer.tileByPixels(t.x, t.y) display.remove(tile) tile = nil return true end local function updateMap() map.updateView() end timer.performWithDelay(1,updateMap,-1) toplayer:addEventListener( "touch", touchTile )

There isn’t a built-in Dusk way for this, but it’s really easy to do:

local layerX, layerY = toplayer:contentToLocal(event.x, event.y) local tile = toplayer.tileByPixels(layerX, layerY)
  • Caleb

Even though camera rounding does almost entirely eliminate the problem, I still recommend that you take two more preventative steps to fix it.

First of all, add a spacing of 1-2 pixels between your tiles. This has the dual purpose of making your tilesets easier to see for you and keeping Corona from doing some tile-bleeding stuff with OpenGL (read the first answer from http://gamedev.stackexchange.com/questions/74420/seamless-tilemap-rendering-borderless-adjacent-images)..)

Secondly, make sure these two lines of code are at the top of main.lua:
[lua]
display.setDefault(“minTextureFilter”, “nearest”)
display.setDefault(“magTextureFilter”, “nearest”)
[/lua]

Unfortunately, not having access to Corona’s graphics code, there isn’t a way I (as the developer of Dusk) can completely solve this problem. Apparently (from the StackExchange topic I linked above), to completely destroy the Flickering Tile Lines of Doom, you have to twiddle around with OpenGL.

By the way, is the question you asked just before this one still active? From your update, I couldn’t quite tell if you’d resolved it :).

  • Caleb

Exactly what I needed. Thanks Caleb.

Hello, all who are notified from this topic!

I’d appreciate it if you’d take my Dusk survey I created. The forum topic is http://forums.coronalabs.com/topic/54887-dusk-engine-survey/.

The survey can be found at http://goo.gl/forms/1JjsBp9DB7.

Thank you!

  • Caleb

Is there a build-in save function, to save changes of object layers (or others)  and then override the original map data on a reload?

What do you mean? As in save to file? Or edit original data to load a different map with? Or what?

  • Caleb

resolved! :wink:

Everything works fine for me now. The 1px spacing is not shown when I’m using the bigger tiles, so Dusk is now perfect for the game we want to make. We just finished a first prototype and greenlit the game which will be an adventure-rpg type game. But I think we will work on a smaller project first with Dusk to figure out some more details on how to do things.

Great stuff Caleb! And thx again for your fast feedback!

Regarding torbenratzlaff post I think he means if there is a function to overwrite the json table (map) showing the map tile numbers.

Torbenratzlaff: I think you should look into Coronas Api to learn about such kind of table/file manipulation. A good place to start you can find here: http://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

I wish you all a Merry X-Mas!

Daniela

I mean something like a function that outputs the differences between the current state of an object layer and the original data in the map from Tiled. So I can save those changes easyly.

And on the other hand a loading function, that takes the saved changes as an input, and when a map is built, the loaded data is used instead of the original map.

@d.mach:
Glad everything’s working for you, good luck on your game!

@torbenratzlaff:
There is no built-in function for that in Dusk, but you can first load a map in a separate variable, then build it:
[lua]
local mapData = dusk.loadMap(“map.json”)
local map = dusk.buildMap(mapData)
[/lua]
Now that you have access to both objects, the map and its data, It shouldn’t be to hard to find the differences between them.

Optionally, you can use map.data.mapData - it’s the map’s “constructor data”. You need to be careful, though - the map.data.mapData has two added fields, _dusk and stats, for Dusk’s internal use. If you use a simple difference function, you’ll pick that up as a difference.

Merry Christmas to everyone!

  • Caleb

Thanks alot for your suggestions :slight_smile:

Merry Christmas to you too.

Hi Caleb,

is it possible to animate image sheet frames (tiles) when they are located over the whole image sheet by using their index?

Right now animations are something like “from image (tile) 1 to 4”… but I need something like {1,2,2,16,13,13,10,5}

Is this possible already?

  • Daniela

I asked this in other topic so sorry for double post, but maybe someone here could help 'cause there are more readers in this topic.

Following demo is made with Dusk and Box2D. I’ve read some problems that may occur with physics in tile based games. Please check this 10 sec video:

https://www.youtube.com/watch?v=nOyZUYQF9lQ

When time is about 7 secs you’ll see that the character “hits” the unvisible wall (tile seem?), though the character is on solid ground the whole time. Is this typical problem with tile engine games that are using Box2D physics? And if it is, how can I fix this?

Unfortunately, that is a very common problem with physics engines and tiles. It can’t be completely “got through” (unless you use a custom physics approach), but a number of workarounds exist:

  • Make the player’s body a shape with clipped off corners at the bottom

  • Make the player’s physics body circular

  • Make the player a multi-element physics body with a circle at the bottom

  • Give the player a tiny bit of bounce

  • Give tiles a clipped-off-corner shape and the player a normal rectangular one with a bit of bounce

  • Caleb

Is there a map method to get the position of the edges of the current screen in pixels i.e. top, bottom, left, right in pixels ?

Think i figured it out…

centerX, centerY = map.getViewpoint() top = centerY - (screenH / 2) bottom = centerY + (screenH / 2) left = centerX - (screenW / 2) right = centerX + (screenW / 2)

Beat me to it :slight_smile:

That’s correct. Just remember there’s also the property “display.contentCenterX” which is basically the same as screenW * 0.5 and “display.contentCenterY” which is screenH * 0.5. So maybe:

[lua]
local centerX, centerY = map.getViewpoint()

local top = centerY - display.contentCenterY
local bottom = centerY + display.contentCenterY
local left = centerX - display.contentCenterX
local right = centerX + display.contentCenterX
[/lua]

  • Caleb

Also, @d.mach:

You can do that through the anim:options table. Possibly a more user-friendly way will come sometime, but right now just provide a ‘frames’ table just like in a normal Corona sprite.

anim:options = !!! frames: {1, 2, 3, 4, 5, 4, 3, 1, 9, 2} time: 1000
  • Caleb

Is there any videos on Youtube or such, where you could see Dusk Engine in action or “getting started tutorials”?

Thanks Caleb! I’ll try those.