Advice on getting World Position of a Touch/Click action (SOLVED)

Hi guys,

I’m working on a simple mechanic that involves click on a screen area. Orienting a gameobject to that screen point, and then having it move based on the direction of rotation that results. This is for a game that is in windowed mode on Desktop.

Currently I’m having a lot of trouble getting consistent readings from action.x and action.y. It seems to have something to do with screen space rather than window space, but I’m not really sure what it’s doing.

This is what I’m doing to orient my sprite toward a point at the moment

function orient_toward_point(self, point)	-- this is action.x, action.y
	local x = point.x - go.get_position().x
	local y = point.y - go.get_position().y
	local angle = math.atan2(y,x)
	local r = angle * (180 / math.pi
	if r < 0 then r = 360 - (-r) end
	local q = go.get_rotation(".")
	go.set(".", "euler.z", r)
	print("X: " .. tostring(x) .. " Y: " .. tostring(y) .. " angle: " .. tostring(r))
end

The problem is that X and Y don’t represent the x and y of the screen position or of world space. I’m wondering how I could do this in Defold?

Would appreciate any input. Cheers.

Check out this render script + the render helper script

If you haven’t modified your render script it should be as simple as using action.screen_x and action.screen_y instead of action.x and action.y.

1 Like

I’m using the default render script, and both action.x and action.screen_x give me the same results currently.

I’ll have a look at pkeod’s suggestion.

I’m not sure I understand the problem then. action.x and action.screen_x will be the same as long as the screen width hasn’t changed, but if you do a desktop build and drag to change the size of the window you’ll notice that action.x and action.screen_x will be different.

Well, the problem I’m having is that if I want for example world position 100, 100, what I’m getting is actually the screen position of the cursor. So I’m not sure how to get the actual position I need from that.

Essentially, I’m asking how do I get a world position point from a mouse click, when the game is windowed?

Are you sure you’re not using any custom rendering? No camera? With vanilla rendering it should be 1:1 already. Post more code of what you are doing? Send zip of project to britzl to take a look?

What’s weird (and might explain why I thought I had it working last night) is that here working on the Mac, I’m getting the correct values.

Mouse cursor in the bottom left of the window is reporting that action.x is 0 and action.y is 0

I’ll have to test again when I get home from work, but I was getting really odd values using the same code. It may have something to do either with windows, or with the fact my windows machine is using multiple monitors?

But I’m definitely getting the correct values here on a mac.

Definitely default rendering code. I’ve not looked at using any custom stuff yet as I’m still wrapping my head around lua and basic use of game objects in Defold.

Take screenshots to compare between your Mac and PC too to see if rendering appears any different? Disable second monitor? Post PC specs - video card, version, monitor size, and check your DPI too.

I’ll take a look and report back.

DPI is a good shout actually, too. Because the main monitor actually has some crazy high resolution natively.

I thought it might be the display scaling settings on my laptop but it was actually just my bad math. Whoops! Fixed.

1 Like