Converting touch point to map coordinates

Hello,

Apologies for such a newbie question…

Whats the easiest way to convert the coordinates in touch events to map coordinates. e.g. the co-ordinates of a player sprite attached to the map is 74,1026 yet the touch event is 221,223 which I think is the screen coordinates?

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 304751[/import]

Hey, there is no such thing as a newbie question, especially with such a young framework :slight_smile:

The easiest way is to get the new update just uploaded from the Beta Updates thread and use the lovely new function I have just added thanks to your question.

To use the function just put it in your touch event like this:

  
local onTouch = function(event)  
  
 local pos = ugni.screenToWorldPosition(map, {x=event.x, y=event.y})  
  
end  
  

You will also need to include the utils module like normal:

local ugni = require("lime-ugni") [import]uid: 5833 topic_id: 4751 reply_id: 15205[/import]

Thanks, that works perfectly. The other functions in the update look good as well.

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 15208[/import]

Yay, glad it worked. I will be putting the rest of the functions into the API later. [import]uid: 5833 topic_id: 4751 reply_id: 15210[/import]

Graham,

Sorry to bother again but the setPosition seems to be not acting as I expect when updating the map position.

I use it successfully in a function to set the initial map position to the player’s spawn point:

local onPlayerSpawnObject = function(object)  
 local layer = map:getLayer("Player")  
 player = display.newImage(layer.group, "player.png")  
 player.x = object.x  
 player.y = object.y  
 map:setPosition(player.x,player.y)  
end  

But it doesn’t set it as I’d expect when moving the player in a touch event. It seems to be moving to the bottom left corner of the map rather than the position I’ve just moved the player to:

local onTouch = function(event)  
 local pos = ugni.screenToWorldPosition(map, {x=event.x, y=event.y})  
 local diffx = player.x - pos.x  
 local diffy = player.y - pos.y  
 local newx = player.x  
 local newy = player.y  
  
 if math.abs(diffx) \> math.abs(diffy) then  
 if diffx \> 0 then  
 print("Move left")  
 newx = player.x - 50  
 else  
 print("Move right")  
 newx = player.x + 50  
 end  
 else  
 if diffy \> 0 then  
 print("Move up")  
 newy = player.y - 50  
 else  
 print("Move down")  
 newy = player.y + 50  
 end  
 end  
 transition.to( player, { time=500, x=newx, y=newy } )  
  
 map:setPosition(newx,newy)  
  
end  

slideToPosition seems to have the same behaviour, it’s actually the slide I want to use (so the map slides as the player moves).

Any advice appreciated,

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 15213[/import]

Hi Rob, could you send me your project to graham AT grahamranson DOT co DOT uk so I could take a look at it?

Thanks [import]uid: 5833 topic_id: 4751 reply_id: 15214[/import]

Thanks, just sent it. [import]uid: 12431 topic_id: 4751 reply_id: 15215[/import]

Graham,

the worldToTilePosition is reporting an error whenever I use it:

“attempt to perform arithmetic on field ‘x’ (a nil value)”

I even did a test in my project to replicate your example code:

local worldPosition = { x = 100, y = 350 }  
local testPos = ugni.worldToTilePosition(map, worldPosition)  

This returned the same error ;-(

I then used the calculation you use in my code and everything works (my tile width is 50):

local pos = {column = (math.ceil(player.x / 50)), row = (math.ceil(player.y / 50))}  

So I can calculate it myself but it would be nice to fix it in your api for other user.

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 15862[/import]

Hmm strange, I must have done something silly. Thank you for bringing this to my attention, should be fixed for the next drop. [import]uid: 5833 topic_id: 4751 reply_id: 15868[/import]

Yup, had done something silly. Fix will be in the next update. [import]uid: 5833 topic_id: 4751 reply_id: 15869[/import]

Thanks Graham,

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 15889[/import]

No problem :slight_smile: [import]uid: 5833 topic_id: 4751 reply_id: 15891[/import]

The new version is up with the fix in as well as some updated camera stuff, also a new tutorial for map movement - http://justaddli.me/tutorial.php?t=0&id=13 [import]uid: 5833 topic_id: 4751 reply_id: 15946[/import]

Thanks Graham, for the fixes and tutorial.

Cheers!

Rob
[import]uid: 12431 topic_id: 4751 reply_id: 15980[/import]

No problem :slight_smile: [import]uid: 5833 topic_id: 4751 reply_id: 15993[/import]