Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera related documentation needs improvement regarding touch/screenspace #5853

Open
jusw85 opened this issue Nov 29, 2019 · 12 comments
Open

Camera related documentation needs improvement regarding touch/screenspace #5853

jusw85 opened this issue Nov 29, 2019 · 12 comments

Comments

@jusw85
Copy link

@jusw85 jusw85 commented Nov 29, 2019

Issue details

Camera.unproject does not return original value

Reproduction steps/code

    public void create() {
        Vector2 tmpvec = new Vector2();
        tmpvec.set(0, 0);

        Stage stage = new Stage(new ScreenViewport());
        stage.stageToScreenCoordinates(tmpvec); // has the correct screen coords;
        stage.screenToStageCoordinates(tmpvec);
        System.out.println(tmpvec); // prints (0, -1), should be (0, 0)
    }

Due to the following line in Camera (-1 at the end):

y = Gdx.graphics.getHeight() - y - 1;

Intended?

Version of LibGDX and/or relevant dependencies

1.9.10

@Tom-Ski
Copy link
Member

@Tom-Ski Tom-Ski commented Nov 29, 2019

If you think there is a problem with Camera, your test should only involve camera, and not Stage/Viewports.

That said, this is a problem with documentation and variable names. It should really be touchToStageCoordinates, not screenToStageCoordaintes, since its expected touch space input. There are some other inconsistencies that probably should be cleaned up that are the same.

@jusw85
Copy link
Author

@jusw85 jusw85 commented Nov 29, 2019

Ok, modified.

public void create() {
      Vector3 tmpvec = new Vector3();
      tmpvec.set(0, 0, 0);

      Camera camera = new OrthographicCamera();
      camera.viewportWidth = 100;
      camera.viewportHeight = 100;
      camera.project(tmpvec);
      camera.unproject(tmpvec);
      System.out.println(tmpvec); // prints (0.0,-0.01999998,0.0)
  }
@Tom-Ski
Copy link
Member

@Tom-Ski Tom-Ski commented Nov 29, 2019

#5854 is the same same issue, and as I said its a problem with documentation as they arent all screen space coordinates. We do not need two issues to discuss this same problem.

@Tom-Ski
Copy link
Member

@Tom-Ski Tom-Ski commented Nov 29, 2019

Here is a demo that demonstrates that this is a documentation issue. https://gist.github.com/Tom-Ski/3c71425e6a199ec2677fbac39e457e54

@jusw85
Copy link
Author

@jusw85 jusw85 commented Nov 29, 2019

Why are your touch coordinates offset by 1?

@jusw85
Copy link
Author

@jusw85 jusw85 commented Nov 29, 2019

Regardless, should a sequence of project and unproject return a different value?

      camera.project(tmpvec);
      camera.unproject(tmpvec);
@Tom-Ski
Copy link
Member

@Tom-Ski Tom-Ski commented Nov 29, 2019

As an example, if you have a screen of 10x10, bottom left coordinate is 0,0. In touch space, this coordinate is 0,9. Gdx.graphics.getHeight() would return 10 however, we need the offset because we start at 0, not at 1.

Range of touch/screen is from (0 to width-1) since we start at 0.

@Tom-Ski
Copy link
Member

@Tom-Ski Tom-Ski commented Nov 29, 2019

As per the documentation, yes they should. There are just a few inconsistencies with the language used that makes it confusing. Although the documentation (for Camera at least) does describe the difference in coordinate spaces the functions return and expect, it still calls them both screen space, which is the confusing part. The solution here is to improve the docs.

@jusw85
Copy link
Author

@jusw85 jusw85 commented Nov 29, 2019

Ok.
However, this is still unrelated to #5854 which is about stages with viewports with y offsets. Even if the method name were stage.stageToTouchCoordinates, I believe there is still an issue there. I'll comment further in that issue.

@Tom-Ski Tom-Ski changed the title Camera.unproject does not return original value Camera related documentation needs improvement regarding touch/screenspace Nov 29, 2019
@jusw85
Copy link
Author

@jusw85 jusw85 commented Nov 29, 2019

OpenGL for reference, specifically gluProject and gluUnProject, compiled and run on Arch Linux/Mesa 3D.

gluProject — map object coordinates to window coordinates
gluUnProject — map window coordinates to object coordinates

The window size is 300x300, and (0,0) is the point at the centre of the screen.

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>

void displayFunc(void)
{
    GLdouble posX, posY, posZ;
    GLdouble winX, winY, winZ;
    GLdouble resX, resY, resZ;
    GLdouble modelview[16];
    GLdouble projection[16];
    GLint viewport[4];
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);
    posX = 0.0;
    posY = 0.0;
    posZ = 0.0;

    gluProject(posX, posY, posZ, modelview, projection, viewport, &winX, &winY, &winZ);
    printf("window: %f %f %f\n", winX, winY, winZ);
    // window: 150.000000 150.000000 0.500000

    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &resX, &resY, &resZ);
    printf("pos: %f %f %f\n", resX, resY, resZ); 
    // pos: 0.000000 0.000000 0.000000
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(300, 300);
    glutCreateWindow("Test glProject glUnProject");
    glutDisplayFunc(displayFunc);
    glutMainLoop();
    return 0;
}

@mark6412
Copy link

@mark6412 mark6412 commented Apr 4, 2020

My problem iPhone X can’t get it to open I’ve tried everything,boot up tried everything

@libgdx libgdx deleted a comment Apr 4, 2020
@libgdx libgdx deleted a comment from mark6412 Apr 4, 2020
@libgdx libgdx temporarily blocked mark6412 Apr 4, 2020
@NathanSweet
Copy link
Member

@NathanSweet NathanSweet commented Apr 4, 2020

@mark6412 Don't ask for help on an issue tracker. See: https://github.com/libgdx/libgdx/wiki/Getting-help

Seems a bit unfortunate to need to think about touch space coordinates. Ideally we'd have only screen space and world space.

@libgdx libgdx deleted a comment Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.