|SOLVED] Rendercam + Defold-input => weird cursor position when scrolling

Hi guys!

I’m currently working on an idle game prototype. I’ve used:

1/ Defold-input to be able to interact with game objects @britzl
2/ Rendercam for the camera basics @ross.grams

Both work exactly as intended separately, but when I mix them… something weird happens:

As you can see, at the beginning I move the cursor over the buildings (that are highlighted) and then click them to upgrade. But when I scroll, the buildings move but not their “screen position” (?). I have to click below their displayed position to upgrade them :frowning:

Probably something related to world pos, screen pos, or something… but I don’t know what exactly :see_no_evil:

Can someone help me? :sweat_smile:

1 Like

Can you please enable Physics Debug and check where the collision objects are?

@britzl
The collision boxes seem to follow the objects:

edit: do you need a minimal version of the project to understand what’s wrong?

There must be something wrong with the screen to world translation from render cam and how you pass that on to the mouse cursor. Like I do in this example using Orthographic:

3 Likes

@britzl thank you for this example! I’ve used it yesterday in my project and it works as intended :slight_smile:

For now, the lines in question are the “input” function and the cursor follows the input position as intended, but there are 2 specific cases I’ve been struggling with:

1/ Real-time following… when not moving the mouse
When I don’t move the mouse, the cursor stops following -which is pretty normal since the message is passed from the input function (so when I scroll then stops moving the mouse, this mouse remains at the same position but the cursor follows the camera movement - and then they re-synchronize when I mouse the mouse again)

How to make the cursor follow in real-time the input position? (in a “clean”/bugfree manner)

For now I’ve added the message passing (to the cursor) in the update loop

function update(self, dt)
  local mouse_pos_cur_WORLD = rc.screen_to_world_2d(self.mouse_pos_cur.x, self.mouse_pos_cur.y)
  self.action.x = mouse_pos_cur_WORLD.x
  self.action.y = mouse_pos_cur_WORLD.y

  msg.post("cursor#cursor", "input", { action_id = self.action_id, action = self.action })

end

I seemed to work fine in the basic test scene (see my “about to be deleted” message)… but I realized that in the building game prototype, it seems to … create some sort of “conflicts” with the other cursor’s action_id… For example, the “release” is no longer detected when I actually release but only when I move after having released (I don’t know how to explain it better, so I made another short video). And the buildings are upgraded consequently (only when I move after having released it). For some reasons, I need to move the mouse so the release can be detected…

2/ Cursor instantly repositioned when clicking
Since I want my prototype to become a mobile game, I need the cursor to “teleport” to the mouse position when I click/tap (to be able to interact with my buildings without having to drag the cursor before - since there is no cursor dragging on mobile)

When I pass the position to the cursor directly from the action.pressed “touch”, the click seems to be detected but the cursor doesn’t teleport to the input position… :-/

Again, is there a clean way to “teleport” the cursor when clicking?
(so players (on mobile) can tap buildings anywhere on the screen to upgrade them)


General note : I’m sorry for posting long messages, and I try to avoid asking questions/for help too frequently… but when I do it’s because I’m really stuck with something important for my project. I don’t know if I should keep it super short (a few words describing the problem) or detailed (in my previous messages I tried to provide everything I can, quick descriptions of my tries/mistakes, screenshots, videos, sometimes minimal project etc. to make it easier for people who can help but maybe I’m wrong => cf the “about to be deleted” message) => should I keep providing as many info as I can in that kind of situations? (not a problem for me, but maybe a bit too “heavy” for readers… I don’t know - my only objective is to find solutions to my problems)

1 Like

The solution you have where your position the cursor every frame is probably the only way I can think of myself.

Sounds strange. I need to look into this to better understand the problem. Can you create an issue on GitHub please?

This is something the cursor script should support. Please create a ticket in GitHub!

1 Like

Just to be sure, I have to create 2 different tickets on GitHub?

(same minimal project + same extensions, but maybe 2 different problems)

In general, it’s recommended to create one ticket per issue.

There are exceptions ofc. E.g. if the issues are very intimately related, so they can be considered the “same” issue.

3 Likes

Ok I think I’ll create 2 different issues, because one is more like a feature request/completion (cursor teleport when clicking, for mobile games), and the other a possible bug (real-time following creating “conflicts” between action_ids).

Just finished preparing the minimal version of the project so it’ll be easier to investigate.

2 Likes

For first case,
You can move the cursor sprite to the opposite scroll direction while cam is moving.
Or you can make it a child of the cam and update it’s pos when cursor moves.

@britzl / @Ragetto Sorry to resurrect an old topic, but I ran into this same issue and this post helped immensely, thank you!

My cursor position is now fixed with this code (at startup of my app):

function on_input(self, action_id, action)
	local pos = rendercam.screen_to_world_2d(action.x, action.y)
	go.set_position(pos)

Launching the app at my configured width/height of 960x540 translates the coordinates correctly, however once I go fullscreen the cursor position is off again.

I’m using the default rendercam settings:

go.property("active", true)
go.property("orthographic", true)
go.property("nearZ", -1)
go.property("farZ", 1)
go.property("viewDistance", 0)
go.property("fov", -1)
go.property("orthoScale", 1)

go.property("fixedAspectRatio", false)
-- Using a vector property to set aspectRatio so it can be more accurate.
-- This way you don't end up with 800 x 599.9994812093 px view areas.
go.property("aspectRatio", vmath.vector3(16, 9, 0)) -- only used with a fixed aspect ratio

go.property("useViewArea", false)
go.property("viewArea", vmath.vector3(800, 600, 0))

go.property("expandView", false)
go.property("fixedArea", true)
go.property("fixedWidth", false)
go.property("fixedHeight", false)

Any idea how I could fix this issue?

Thanks again!

EDIT: Adding screenshots

Window (not resized, works well):

Window (after resizing, cursors are off):

1 Like

I don’t know enough about RenderCam to say for sure, but please try with local pos = rendercam.screen_to_world_2d(action.screen_x, action.screen_y)

3 Likes

That fixed it, thanks so much!

2 Likes