2
\$\begingroup\$

Background

I'm implementing viewport resizing in order for my game to maintain the same display ratio on all devices.

However, I've found an issue with getting touch events.

Basically, if my viewport, for example, uses the whole of the physical screen's width and height then there is no issue. However, if it is resized say in the vertical, then my touch events are thrown off, because touch events are registered from the top of the physical screen. Hence if I put a sprite at say position 50 in the Y direction then it put's the sprite 50 pixels down from the top of the viewport, not the top of the actual screen.

Do I have to manage this myself by adding my viewport's offset to my touch events or is there a way to tell Android to register 0, as the top of the viewport as opposed to the actual screen.

Here is a graphic to illistrate what I mean, any help would be appreciated

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The touch events are in relation to the hardware screen. The touch events don't care where you're drawing or what you're doing on screen. It should be a simple task to offset your touch events by the viewport offset. \$\endgroup\$
    – House
    Commented Jun 23, 2013 at 18:07
  • \$\begingroup\$ Thanks @Byte56, that's what I thought but just needed it confirming. If you want to put that into an answer, I will accept. \$\endgroup\$ Commented Jun 23, 2013 at 18:21

1 Answer 1

2
\$\begingroup\$

You can see from the documentation related to touch events that touch events are related to the hardware screen, for example related to the x-axis:

For a touch screen, reports the absolute X screen position of the center of the touch contact area. The units are display pixels.

The events are not affected by the viewport.

You can, however, easily get the viewport relative positions by subtracting the offset from the y value returned by the touch event.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .