Inverted mouse_position in relation to screen?

(Fredrik Holmqvist) #1

Hello! Recently started working with Amethyst and it’s a blast. Seriously great API, working with it and specs is really rewarding.

With that said, I have a problem. I have a tileset filling the screen, and I want a system that lets me select tiles. I’ve written this placeholder functionality to change the rendering on a clicked tile:

>    if let Some((x, y)) = input.mouse_position() {
>      for (sprite, transform) in (&mut sprites, &transforms).join() {
>        let t = transform.translation();
>        if x > t[0] && x < t[0] && y > t[1] && y < t[1] {
>          sprite.sprite_number = 1;

This works great for the X axis, but the Y axis is inverted. That is, clicking the top tile selects the bottom tile.

From debugging I’ve noticed that the mouse position is measured from the top left corner, whilst I’m rendering from the bottom left corner.

Any suggestions on how to fix this? Perhaps this is a solved problem and I’m just to blind to see it!

Thanks in advance :slight_smile:

(Azriel Hoh) #2

Heya, I’m not sure of “the best way”, but a way to invert the coordinates is:

// screen_dimensions: ReadExpect<'s, ScreenDimensions>,
let y_amended = screen_dimensions.height() - y;

// Note: I haven't tried it myself, but it would be what I'd try

It is tricky to decide whether changing amethyst to return bottom-left origin for mouse coordinates is the correct thing (consistency with world coordinates, vs consistency with existing UI frameworks).

(Fredrik Holmqvist) #3

Thank you! I did the same thing as a placeholder shortly after, and found a similar solution in an amethyst_ui example so I seems to be somewhat of a standard solution.

Consistency is probably best as to not throw off people coming from other frameworks.