watch the video:
http://recordit.co/VRhkPeXc7i
when I switch to fixed_fit_projection mode
clicks do not match the button
take an example from lessons
watch the video:
http://recordit.co/VRhkPeXc7i
when I switch to fixed_fit_projection mode
clicks do not match the button
take an example from lessons
The render script change from stretch to fixed fit will change how the game is rendered but the input system will not know about this. The input coordinates are still in screen space. You need to manually scale the input values to handle the change in projection.
Another option is to use the defold-orthographic or RenderCam extensions from the Asset Portal. These extension takes care of both scaling and input (plus much more).
tried RenderCam extensions
rendercam.screen_to_viewport(x, y,)
y coordinates are working correctly
x- coordinates do not match (if the window stretches wide)
Sorry, my internet is being too slow right now for me to see your video, but I am 99% sure âscreen_to_viewportâ is not the function you want, try ârendercam.screen_to_gui_pickâ.
does not work ;(
function on_input(self, action_id, action)
if action_id == hash("click") and action.pressed then
x,y =rendercam.screen_to_gui_pick(action.x, action.y)
print(action.x, action.y,x,y)
...
DEBUG:SCRIPT: 13.5 | 559.5 | 13.5 | 559.5 |
---|---|---|---|
DEBUG:SCRIPT: 14.4 | 437.5 | 59.9 | 437.5 |
I click on 1 point on the tile x, y 13.5 | 559.5 |
I change the width of the window
I click on the same point on the tile
function returns incorrect coordinates 59.9 | 437.5 | instead of 13.5|559.5|
I set up everything in the instructions
Oh, sorry, if youâre just using mouse clicks/touch you donât need it, just use action.x and action.y directly to with pick_node. (this is in the Rendercam documentation btw)
Action.x and action.y are not screen coordinates if the window size is changed from what you set in your game.project file. They get stretched to fit the new window instead of expanding.
Action.screen_x and action.screen_y will give you real screen coordinates. Most of the Rendercam functions use these, not action.x and action.y.
where to read about action.screen? can not find in the documentation
how gui.pick_node to use with tiles and not with gui?
The on_input
doc lists the available fields but doesnât go into a lot of detail.
The input manual talks about the difference between x/y
and screen_x/screen_y
, but isnât terribly clear about when you need one vs. the other.
The Rendercam documentation for rendercam.screen_to_gui_pick describes when you need that function and when you donât.
This all may seem weird and pointlessly annoyingâhaving separate screen and gui coordinate systemsâbut it makes it so gui nodes keep the same reported position no matter how you change the window, which solves a lot more problems than it creates.
There is no âpick_nodeâ function for tiles (or sprites), that is for gui only. There are multiple different ways to do this. You can find one method in the Colorslide tutorial (in level.script), though that wonât quite work with the fixed_fit_projection. Since the tiles are rendered in âworldâ space, you need to convert your mouse coordinates to âworldâ space for it to work correctly. With Rendercam you can do this with ârendercam.screen_to_world_2dâ. Then use those coordinates to check your tiles for collision (divide by the tile size, ceiling, etc, - like in the Colorslide level.script).
rendercam.screen_to_world_2d(action.screen_x, action.screen_y)
thatâs what i need
thank you
hi! I am getting âattempt to index global ârendercamâ (a nil value)â error.
I have fetched the library and done everything i need to do (created a camera, enabled shared state, added the rendercam to the bootstrap). what step might i be missing?
edit: okay, I was missing local rendercam = require ârendercam.rendercamâ
My next question⌠Iâm trying to create a tool that detects if the user is swiping up, down, left, or right, using a GUI script. Iâve been testing how this works creating a GUI node at the location provided by rendercam.world_to_screen(vmath.vector3(action.x, action.y,0)) when doing a click. But it seems to provide strange results, and results that vary depending on how i resize the screen. Iâd really appreciate someone telling me what i need to do (iâd like to use fixed fit projection) because Iâve been fighting this a long time and i thought it would be something fairly simple!!
âŚOkay. hereâs my solution which doesnât use rendercam, but action.dx and action.dy.
Swipes must be:
in one of four directions (up, down, left, right)
quick (less than 30 frames between action.pressed and action.released)
âŚbut not too quick (more than 5 frames between action.pressed and action.released)
unidirectional (if you swipe diagonally, the swipe isnât counted)
involve a movement of at least 30 pixels (no tiny accidental swipes!)
if self.swiping then
if action_id == hash("click") then
if action.pressed then
self.swx = 0
self.swy = 0
self.time = 0
elseif not action.released then
self.swx = self.swx + action.screen_dx
self.swy = self.swy + action.screen_dy
self.time = self.time+1
else
if self.time < 30 then
if math.abs(self.swx) > math.abs(self.swy)*2 and math.abs(self.swx) > 30 then
if self.swx > 0 then
print("right swipe")
else
print("left swipe")
end
elseif math.abs(self.swy) > math.abs(self.swx)*2 and math.abs(self.swy) > 30 then
if self.swx > 0 then
print("up swipe")
else
print("down swipe")
end
end
end
end
end
end
So here are my question:
THANKS!
The code looks ok. I think i would calculate the delta on release instead of accumulating it over every frame.
I have a gesture detection module here if you want to check another implementation: https://github.com/britzl/defold-input/blob/master/in/gesture.md
thanks britzl!
I do still have one question which boils down to:
how do I get the real-life location of a touch within a GUI script so that I can create a GUI object that appears under the playerâs finger? I am not sure what function of
rendercam I need.
Canât you use the normal on_input() callback to achieve this?
Please can you elaborate?
Youâve just got it backwards. action.x and action.y are already in GUI coordinates. You donât need rendercam for that, just use them directly from on_input.
âWorld Spaceâ means your game objects, sprites, etc. You see those through the camera. GUI is not drawn with a camera, itâs just fit to the window.
The difference between action.x/y and action.screen_x/screen_y is subtle, they are very similar. In fact, they will be exactly the same if your window size matches the resolution you set in your game.project. But if the window size changes, action.x/y will stretch to always give you the same range (the top right corner will always have the same coordinates), whereas action.screen_x/y will be the actual pixel coordinates.
OKAYYYYYY
I am just about ready to punch myself in the nutsack but the problem was the adjust mode of the node i was trying to track. Iâve set it to stretch and everything is now fine. UGH
edit: in case anyone runs into this problem again (creating a GUI node which follows exactly the location of the mouse on all screen sizes and ratios), you need to create a GUI node with another GUI node as a child. Set the parentâs adjust mode to stretch and the alpha to 0. Then, you can use the child GUI node as the one with the image (with the adjust mode as fit, maintaining its ratio) and the parent node will follow the mouse position accurately, just stretching itself if you change the screen size.