How do I get the specific coordinates of a control...
# compose
n
How do I get the specific coordinates of a control?
k
In its parent? In its root view? On the screen?
n
on the screen
k
This one is a bit tricky. Do you expect to get notified (somehow) when the window is moved by the user (say, a floating window on a Chromebook or just a window in the desktop environment)?
Do you expect to get notified every time something around your view changes, and even though your view has not been recomposed, it may have moved on the screen
These are just some of the reasons why OnGloballyPositionedModifier removed conversion methods for screen coordinates
n
I need to do collision detection for Dino, but I don't know how to get the coordinates
k
This is too vague. Why do you need screen coordinates for the dino
If this is running as a full-screen activity, there is no need for screen coordinates. If this is running in a floating window, what exactly can it collide with? Edges of its window (in which case you can just look at the regular coordinates and canvas size), or edges of the entire screen (in which case it maybe doesn't make sense since the dino can't be drawn there in any case)
n
I guess the point is this I'll create something to move towards the Dino by animating it, and my Dino already has jumping written in, so that's why I want to get the coordinates of it and other things to detect @Kirill Grouchnikov
k
This whole white area is presumably a Canvas object? And you draw the dino at certain X,Y coordinates. And the rest of the "things" are drawn on the same canvas? Each with its own coordinates? Why do you need screen coordinates to do collision testing?
n
And I want to try to divide it into two areas, when the user touches the right area, the jumping action is performed, and when the user touches the coordinate area, the squatting action is performed
In fact, Dino is an Image, i dont know is Canvas object or no?
k
Do you have any implementation that you started on?
n
its my code
k
Where are your other "things" going to be? Children of the same ConstraintLayout?
n
Yes, I think it will be placed in the ConstrainLayout
k
So all of your objects will "live" under the same parent. Which means that you don't need screen coordinates to perform collision detection.
I would suggest going with the Canvas object since it will be much easier to paint things at certain places (with or without animations)
n
ok thank you, but how should I tell when Dino has collided with something else?
k
I guess you can do it with ConstrainLayout modifiers, but IMO Canvas is a better fit for this
What is this something else? Another object that you're placing in the same container? Then you compare the bounding boxes
n
ahhh! ok, compare the bounding boxes
i will check it
sorry, my expression may not be good