Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
1  
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. –  Byte56 Jun 23 '13 at 18:07
    
Thanks @Byte56, that's what I thought but just needed it confirming. If you want to put that into an answer, I will accept. –  user22241 Jun 23 '13 at 18:21

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.