Take the 2-minute tour ×

I am currently attempting to convert my screen coordinates to world coordinates, to be able to interact with objects. I am using glm and unProject to try and achieve this, so far this is my code:

glm::vec4 viewPort = glm::vec4(0.0f, 0.0f, width, height);
glm::mat4 tmpView = sceneCamera->updateView();
glm::mat4 tmpProj = sceneCamera->updateProjection();
glm::vec3 screenPos = glm::vec3(touchPosition.x, height - touchPosition.y - 1.0f, 1.0f);
glm::vec3 worldPos = glm::unProject(screenPos, tmpView, tmpProj, viewport);


I am trying to get a sprite to equal the position where I tap. The issues is that the further I click going down the screen, the further the sprite overshoots, and the same horizontally. So if I click 2/3 the way down the screen, the sprite will overshoot the bottom of the screen.

Image of sprite to be moved

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Your math looks sound, so it's hard to say for sure, but based on the description of your bug I think your touch coordinates are in a different range from your viewport.

If your pixel locations are in a range of x:[0,800), y:[0,600), but your touch coordinates are in x:[0,1024), y:[0,768), you would see a scaling effect where the farther you tap away from the origin, the farther the sprite overshoots its intended target.

share|improve this answer
This sounds exactly like what is going on. Do you have any suggestions on how to fix this? –  dxCUDA Mar 10 '13 at 18:37
I realised that I am using view.drawableWidth and view.drawableHeight which is returning 320x320, not 1024x768... –  dxCUDA Mar 10 '13 at 18:42
Thought so. Did you manage to get it sorted out? All you should need to do is scale your touch coordinates to match your viewport size. –  Daniel Rowe Mar 10 '13 at 19:00
It is sorted now, thanks Daniel. The devil is always in the details as they say. –  dxCUDA Mar 10 '13 at 19:01
add comment

Your Answer


