Screen vs. World Coordinates

Hello everyone,

in my project i use the Rendercam of @ross.grams and i set it to “Expand View”. I also use the defold-input of @britzl.
In my game.project my Width is 640 and the Height is 1136.
The GameObject is positioned at 0,0 in my main.collection so it shows up in the middle of the screen.

In the “on_gesture” message i want to check, if my message.swipe.from was inside my GameObject.
I got this inside the on_message function:

if message_id == hash("on_gesture") then
	if message.swipe_right or message.swipe_left or  message.swipe_up or message.swipe_down then
		local spritePos = go.get_position("#playerSprite") 

		mouseFromX =  rendercam.screen_to_world_2d(message.swipe.from.x, message.swipe.from.y).x
		mouseFromY = rendercam.screen_to_world_2d(message.swipe.from.x, message.swipe.from.y).y
		mouseToX =  rendercam.screen_to_world_2d(message.swipe.to.x, message.swipe.to.y).x
		mouseToY = rendercam.screen_to_world_2d(message.swipe.to.x, message.swipe.to.y).y
		
		local rightBoundary = spritePos.x - ((go.get("#playerSprite", "size.x") * go.get_scale("#playerSprite").x)/2)
		local leftBoundary = spritePos.x + ((go.get("#playerSprite", "size.x") * go.get_scale("#playerSprite").x)/2)
		local upperBoundary = spritePos.y + ((go.get("#playerSprite", "size.y") * go.get_scale("#playerSprite").y)/2)
		local downBoundary = spritePos.y - ((go.get("#playerSprite", "size.y") * go.get_scale("#playerSprite").y)/2)

		local playerTouched = false
		
		
		if mouseFromX >= rightBoundary and mouseFromX <= leftBoundary and mouseFromY >= downBoundary and mouseFromY <= upperBoundary then
			playerTouched = true
		end
	end
end

When i start the game in the Debugger it works great, but when i start it on my Galaxy S9 then my
spritePos.x = 0 and my mouseFromX = -200

Can someone help me to fix it?

Thank you!

I guess you need to convert the screen coordinates that the gesture module uses into world coordinates. RenderCam should have a screen to world function to deal with this for you.

I already use it.
In my script you can see i use:

mouseFromX =  rendercam.screen_to_world_2d(message.swipe.from.x, message.swipe.from.y).x

But I get a sprite position x of 0 and from the gesture i get a x of -200

Britzl probably doesn’t know the details of Rendercam and I don’t know the details of his Defold-Input, so it’s a bit hard to solve I guess. :slight_smile:

Are the ‘message.swipe.from.x/y’ actually screen coordinates, or are they action coordinates, which get stretched to fit the screen? Rendercam.screen_to_world_2d needs screen coordinates, or it will give the wrong result after the window changes size.


Oh, hmm. According to the docs you don’t get screen coordinates for touch input… I guess you will have to convert, let me refresh myself on how to do that…

[Edit2] OK, if this is actually the problem, you can use this snippet to convert from “action” coordinates to screen coordinates:

local adjust = rendercam.guiAdjust[rendercam.GUI_ADJUST_STRETCH]
local screenX = action.x * adjust.sx
local screenY = action.y * adjust.sy
1 Like

Hi How can I get the action.x and action.y of mouse position, with rendercam and fixed aspect ratio option?

'action' is a table you get in your on_input function. See the docs. on_input will get called every time the mouse moves, with the new x and y in the table. This is just base Defold, nothing to do with RenderCam.

I was asking for this,

rendercam.screen_to_world_2d(action.screen_x, action.screen_y)

But I found in another thread.