#Regarding mouse click location on screen, i can't seem to maybe understand how it works or something

1 messages · Page 1 of 1 (latest)

hoary cliff
#

in the game i want to kind of pop the objects that are being spawned, first i tried to do it with straight clicking on the actual fruits, but the coordinates where the click was occuring were very far off for some reason i can't seem to figure out, now i tried to spawn a collision shape where the mouse is clicked and this is happeneing, i also want to add this to a mobile game so the mouse click would need to be replaced by touch input which i'll learn about later i suppose

#

the other way i'm trying to do it is without making those collisions and just checking where mouse click is happening

Mouse Click Position: (144, 505)
Object Position: (42.625, 43.625) Click Position: (144, 505) Distance: 472.380981445313

this was one of the output where i clicked on the object onscreen, clearly mouse click position is very very far from the object position according....again i clicked on the object

silent oriole
#

I'll be watching this thread with great interest, because I've run into very similar problems trying to use the mouse location.

#

Hopefully someone has an answer.

hoary cliff
#

🤞 i'm just stuck on what i'm making due to dis

open topaz
#

Are you using a subviewport?

hoary cliff
#

what is that?

#

i'm gonna assume no since i don't know what that is

valid ivy
#

use global_position instead of position

open topaz
#

So you may be running into an issue where your "in world" position is not aligned with the screen/camera. If you click the top left corner of the screen, the "click" position is going to be (0,0), but in your gameworld, it is probably not

valid ivy
#

mouse position is always in global space, or screen space

#

there's also the thing where your nodes might be dislocated away from the origin, so your screen position does not relate to your global position

hoary cliff
#

will global position be my current display or the camera window?

valid ivy
#

global position of a node is it's actual position in relation to the origin

#

it doesn't have anything to do with camera or window. It is just the position of anode in the game world

hoary cliff
#

but mouse click wouldn't be like a node right, i've removed the collisionshape spawning

hoary cliff
valid ivy
#

mouse position is the position of the mouse cursor in relation to the upper-left corner of the screen, in pixels

hoary cliff
#

so maybe object's coordinates are wrong then?

valid ivy
#

maybe your screen/camera is dislocated, maybe your nodes have wrong scale, anything could be wrong

open topaz
#

The object's coordinates are right, you're just trying to use a point in one coordinate system in another.

#

If you combine your click position with your camera's position, that should get you started in the right direction

#

(although, your camera's position is at the "center" of the screen, so think about how that might need to be taken into account)

hoary cliff
#

omg i'm stupid, the objects are wrong, there's something wrong with that

#

the object position is always the same, sry

hoary cliff
open topaz
#

Without seeing the full node structure structure it's hard to say, but try setting the position of your new objects using global_position and see what happens. Position = position relative to the parent node; Global Position = position relative to the origin of the scene

#

So if the container for the new objects is at 50,50, and you spawn a new collision shape and give it position (100, 100), it will show up at 150,150

#

but if you set it to global position 100,100, that's where it'll be

hoary cliff
#

i shall make changes...

open topaz
#

can you share your node structure, and which node you're putting these new objects under?

hoary cliff
#

oh there are no nodes except a camera2d in the gamewindow

#

a script spawns the objects from the sides

#

it's just this

open topaz
#

gotcha

#

whats camera2d's position/does that move?

valid ivy
#

yeah, position is local position, it's the position in relation to the parent, global_position is in relation to the root, or origin

hoary cliff
#

no it doesn't it's at 360,640 since the window is 720x1280

#

just seemed like the right thing to do

hoary cliff
open topaz
#

In this case that seems like it should do the trick

valid ivy
hoary cliff
#

ok yes that should be the root then

valid ivy
#

if you need to get the mouse position in relation to some node, you can transform it using to_local() on said node

#

but, maybe first you need to make it global if your tree has weird scaling and translations

hoary cliff
#

yes, but i think i should really check the objects positions aswell just to know everything that's happenening since there's definitely something wrong with those in the script

#

i will update as soon as i can

hoary cliff
#

ok i might've been overcomplicating things with adding the spawned objects in an array in the game window which i wasn't fully understanding. so i simple just added _input_event to the object's script itself, seems a much simpler approach...