Determine tiles selected under rectangular marquee

Very quick question… presumably for Dyson, but if anybody else knows that would be swell.

I have a rectangular marquee for spawning units. Given that I know the points of the rectangle with contentBounds(xMin,xMax,yMin,yMax); is there anything in MTE that would enable me to work out which tiles this rectangle is covering?

Many thanks…

Hello SegaBoy,

You can pass those content bounds into mte.screenToLoc(xArg, yArg, layer) to get the tiles it covers on a given layer. The layer parameter is only important if the layer in question is scaled.

local minLocX, minLocY = mte.screenToLoc(xMin, yMin, 1) local maxLocX, maxLocY = mte.screenToLoc(xMax, yMax, 1)

Wow - thanks Dyson.

I’ve been coding this really ugly mess that stepped through a while loop, incrementing xMin by the tilesize until it exceeded xMax - and doing the same for yMin and yMax and retrieving the Loc values using a call to convert. 

A really convoluted, horrible bit of a code and you’ve succinctly done it in 2 lines!!!

Thanks you…

Quick follow-up question… if I may.

It’s largely getting my head around how MTE handles scaling.

I have a map that is 30x20 tiles (landscape with each tile being 32x32). My config.lua is based on the ultimate config.lua setup, which uses a base of 480x360 - so when I load the map I have: 

mte.setCamera({ levelPosX = 480, levelPosY = 360, blockScale = 16})

Which ensures the map is shown on screen accurately. If I omit the blockScale then the map is only showing half and the tiles have all been magnified - apologise I’ve never quite got my head around how MTE scaling works with Corona’s…

When I’m creating the rectangle marquee however (outside of MTE) I’ve noticed that it’s twice as large as it should be.

It seems that everything added to MTE (sprites, images, etc…) are being scaled on account of the blockScale setting and the setup in config.lua - which is effectively working to half the resolution of the device resolution.

Would you have any recommendation on working with display objects that aren’t added/contained with the MTE setup?

Many thanks…

I guess a good way of looking at how scaling works is to imagine two sheets of transparent graph paper, one representing the content area created by your config.lua file, the other representing the map. You are holding these sheets in front of your face so they are layered against each other. The grids on the graph paper are the x and y coordinates, as you would expect. When you first load MTE, before calling setCamera- or if you call setCamera without a blockScale or scale- the grids on the two sheets are the same size because the coordinate systems are the same scale. If each tile takes up 32x32 of one grid, it will take 32x32 of the other grid.

The map you’ve described is about twice as wide and tall as the content area; the content area is 480x360, your map is 960x640. The sheet of graph paper representing the map is about twice as wide and tall as the sheet representing the content area- BUT the grids are still the same size. The relative scales are identical. A tile which takes up 32x32 on the sheet representing the map will take 32x32 on the sheet representing the content area. This is the true scale of the map.

If you create a 100x100 marque in the content area- outside of MTE- it will appear to take up 100x100 on the map. 

When you set the blockScale to 16, you’re moving the sheet of graph paper representing the map further away from your face so that it appears smaller to you. You’re looking through the sheet of graph paper representing the content area, which hasn’t changed, and seeing the more distant sheet of graph paper representing the map. The grid lines on the map now appear to be half as far apart, again because of the greater distance from your face. However, the coordinate system within the map hasn’t changed. Within the map, the tile still takes 32x32 on its grid… but that grid now APPEARS half as large to you. The tile appears to take 16x16 of the grid on the sheet of graph paper representing the content area, because that sheet is closer to you.

If you create a 100x100 marque in the content area- outside of MTE- it will appear to take up 200x200 on the map. 

I hope that helps. It’s tricky to explain exactly what is going on. What you should keep in mind is that any group object created in Corona, such as the masterGroup holding MTE’s map, contains its own internal coordinate system. If you scale that group, you also scale its coordinate system. The graph paper analogy isn’t perfectly accurate in that you aren’t truly moving one sheet further away, but the results are visually and functionally accurate.

As for your question, one thing you can do is adjust the scale of an object to match that of the map. 

local mapObj = mte.getMapObj() local myRectangle --your marque myRectangle.xScale = mapObj.xScale myRectangle.yScale = mapObj.yScale

You could also change the size of the object:

local mapObj = mte.getMapObj() local mySize = 100 local myRectangle = display.newRect(xPos, yPos, mySize \* mapObj.xScale, mySize \* mapObj.yScale)

Great solution and a fantastic explanation - the analogy soon clarified the concept in my mind. As always massive appreciation for your help.

Hello SegaBoy,

You can pass those content bounds into mte.screenToLoc(xArg, yArg, layer) to get the tiles it covers on a given layer. The layer parameter is only important if the layer in question is scaled.

local minLocX, minLocY = mte.screenToLoc(xMin, yMin, 1) local maxLocX, maxLocY = mte.screenToLoc(xMax, yMax, 1)

Wow - thanks Dyson.

I’ve been coding this really ugly mess that stepped through a while loop, incrementing xMin by the tilesize until it exceeded xMax - and doing the same for yMin and yMax and retrieving the Loc values using a call to convert. 

A really convoluted, horrible bit of a code and you’ve succinctly done it in 2 lines!!!

Thanks you…

Quick follow-up question… if I may.

It’s largely getting my head around how MTE handles scaling.

I have a map that is 30x20 tiles (landscape with each tile being 32x32). My config.lua is based on the ultimate config.lua setup, which uses a base of 480x360 - so when I load the map I have: 

mte.setCamera({ levelPosX = 480, levelPosY = 360, blockScale = 16})

Which ensures the map is shown on screen accurately. If I omit the blockScale then the map is only showing half and the tiles have all been magnified - apologise I’ve never quite got my head around how MTE scaling works with Corona’s…

When I’m creating the rectangle marquee however (outside of MTE) I’ve noticed that it’s twice as large as it should be.

It seems that everything added to MTE (sprites, images, etc…) are being scaled on account of the blockScale setting and the setup in config.lua - which is effectively working to half the resolution of the device resolution.

Would you have any recommendation on working with display objects that aren’t added/contained with the MTE setup?

Many thanks…

I guess a good way of looking at how scaling works is to imagine two sheets of transparent graph paper, one representing the content area created by your config.lua file, the other representing the map. You are holding these sheets in front of your face so they are layered against each other. The grids on the graph paper are the x and y coordinates, as you would expect. When you first load MTE, before calling setCamera- or if you call setCamera without a blockScale or scale- the grids on the two sheets are the same size because the coordinate systems are the same scale. If each tile takes up 32x32 of one grid, it will take 32x32 of the other grid.

The map you’ve described is about twice as wide and tall as the content area; the content area is 480x360, your map is 960x640. The sheet of graph paper representing the map is about twice as wide and tall as the sheet representing the content area- BUT the grids are still the same size. The relative scales are identical. A tile which takes up 32x32 on the sheet representing the map will take 32x32 on the sheet representing the content area. This is the true scale of the map.

If you create a 100x100 marque in the content area- outside of MTE- it will appear to take up 100x100 on the map. 

When you set the blockScale to 16, you’re moving the sheet of graph paper representing the map further away from your face so that it appears smaller to you. You’re looking through the sheet of graph paper representing the content area, which hasn’t changed, and seeing the more distant sheet of graph paper representing the map. The grid lines on the map now appear to be half as far apart, again because of the greater distance from your face. However, the coordinate system within the map hasn’t changed. Within the map, the tile still takes 32x32 on its grid… but that grid now APPEARS half as large to you. The tile appears to take 16x16 of the grid on the sheet of graph paper representing the content area, because that sheet is closer to you.

If you create a 100x100 marque in the content area- outside of MTE- it will appear to take up 200x200 on the map. 

I hope that helps. It’s tricky to explain exactly what is going on. What you should keep in mind is that any group object created in Corona, such as the masterGroup holding MTE’s map, contains its own internal coordinate system. If you scale that group, you also scale its coordinate system. The graph paper analogy isn’t perfectly accurate in that you aren’t truly moving one sheet further away, but the results are visually and functionally accurate.

As for your question, one thing you can do is adjust the scale of an object to match that of the map. 

local mapObj = mte.getMapObj() local myRectangle --your marque myRectangle.xScale = mapObj.xScale myRectangle.yScale = mapObj.yScale

You could also change the size of the object:

local mapObj = mte.getMapObj() local mySize = 100 local myRectangle = display.newRect(xPos, yPos, mySize \* mapObj.xScale, mySize \* mapObj.yScale)

Great solution and a fantastic explanation - the analogy soon clarified the concept in my mind. As always massive appreciation for your help.